2 This file is part of the software library CADLIB written by Conrad Ziesler
3 Copyright 2003, Conrad Ziesler, all rights reserved.
5 *************************
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* memory.c, sequential memory allocator
26 these routines are used to allocate char aligned
27 objects (namely strings) in such a way that we
28 1. allocate in big chunks
29 2. keep track of all the chunk allocations
30 3. free a bunch at once without having to
31 remember all the little pointers we once had.
33 this allows any module to maintain private string
34 (or equations too) storage space that is leak-proof
40 static memory_chain_t
*memory_newblock(void)
44 p
=malloc(sizeof(memory_chain_t
));
46 for(i
=0;i
<4;i
++)p
->magic
[i
]=MEMORY_MAGIC
+i
;
49 /*assert(p!=((void *)0x80d8008)); */
53 void memory_init(memory_t
*ma
)
55 memory_t def
=MEMORY_INIT
;
59 void memory_checkblock(memory_chain_t
*p
)
63 if(p
->magic
[i
]!=MEMORY_MAGIC
+i
)
67 void *memory_alloc(memory_t
*ma
, int size
)
69 memory_chain_t
*p
,*pp
;
72 if(size
==0)return NULL
;
74 /* round up to word alignment, thus all allocations will be aligned */
75 size
=(size
+(sizeof(int)-1))&(~(sizeof(int)-1));
77 /* directly allocate big blocks */
78 if(size
> (MEMORY_CHUNKSIZE
/2))
80 op
=malloc(size
+sizeof(memory_other_t
)+4);
86 /* indirectly allocate small blocks */
89 for(pp
=NULL
,p
=ma
->head
;p
!=NULL
;p
=p
->next
)
92 if((MEMORY_CHUNKSIZE
-p
->q
)>=size
)
97 if(p
->q
> (MEMORY_CHUNKSIZE
-MEMORY_THRESHOLD
))
117 if(ma
->head
==NULL
) ma
->head
= memory_newblock();
120 for(pp
=NULL
,p
=ma
->head
;p
!=NULL
;p
=p
->next
)
122 pp
->next
=memory_newblock();
129 void memory_free(memory_t
*ma
, void *vp
)
131 memory_chain_t
*p
,*pp
, **hp
;
133 memory_other_t
*op
,*pop
;
135 for(hp
=&ma
->head
,pp
=NULL
,p
=ma
->head
;p
!=NULL
;p
=p
->next
)
139 if((c
>=ca
)&&(c
<cb
)) { p
->qref
--; break; }
143 for(hp
=&ma
->full
,pp
=NULL
,p
=ma
->full
;p
!=NULL
;p
=p
->next
)
147 if((c
>=ca
)&&(c
<cb
)) { p
->qref
--; break; }
160 for(pop
=NULL
,op
=ma
->others
;op
!=NULL
;op
=op
->next
)
164 if(pop
==NULL
)ma
->others
=op
->next
;
165 else pop
->next
=ma
->others
;
173 void memory_freeall(memory_t
*ma
)
175 memory_chain_t
*p
,*pp
;
176 memory_other_t
*op
,*pop
;
178 for(pp
=NULL
,p
=ma
->head
;p
!=NULL
;p
=p
->next
)
182 memory_checkblock(pp
);
190 memory_checkblock(pp
);
195 for(pp
=NULL
,p
=ma
->full
;p
!=NULL
;p
=p
->next
)
199 memory_checkblock(pp
);
207 memory_checkblock(pp
);
211 for(pop
=NULL
,op
=ma
->others
;op
!=NULL
;op
=op
->next
)