1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #if defined(_UWIN) && defined(_BLD_ast)
23da2e3ebdSchin
_STUB_vmpool()24da2e3ebdSchin void _STUB_vmpool(){}
25da2e3ebdSchin
26da2e3ebdSchin #else
27da2e3ebdSchin
28da2e3ebdSchin #include "vmhdr.h"
29da2e3ebdSchin
30da2e3ebdSchin #define POOLFREE 0x55555555L /* block free indicator */
31da2e3ebdSchin
32da2e3ebdSchin /* Method for pool allocation.
33da2e3ebdSchin ** All elements in a pool have the same size.
34da2e3ebdSchin ** The following fields of Vmdata_t are used as:
35da2e3ebdSchin ** pool: size of a block.
36da2e3ebdSchin ** free: list of free blocks.
37da2e3ebdSchin **
38da2e3ebdSchin ** Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
39da2e3ebdSchin */
40da2e3ebdSchin
41da2e3ebdSchin #if __STD_C
poolalloc(Vmalloc_t * vm,reg size_t size)42da2e3ebdSchin static Void_t* poolalloc(Vmalloc_t* vm, reg size_t size)
43da2e3ebdSchin #else
44da2e3ebdSchin static Void_t* poolalloc(vm, size )
45da2e3ebdSchin Vmalloc_t* vm;
46da2e3ebdSchin reg size_t size;
47da2e3ebdSchin #endif
48da2e3ebdSchin {
49da2e3ebdSchin reg Vmdata_t* vd = vm->data;
50da2e3ebdSchin reg Block_t *tp, *next;
51da2e3ebdSchin reg size_t s;
52da2e3ebdSchin reg Seg_t* seg;
537c2fbfb3SApril Chin reg int local, inuse;
54da2e3ebdSchin
55da2e3ebdSchin if(size <= 0)
56da2e3ebdSchin return NIL(Void_t*);
57*3e14f97fSRoger A. Faulkner if(size != vd->pool)
58da2e3ebdSchin { if(vd->pool <= 0)
59da2e3ebdSchin vd->pool = size;
60da2e3ebdSchin else return NIL(Void_t*);
61da2e3ebdSchin }
62da2e3ebdSchin
637c2fbfb3SApril Chin SETINUSE(vd, inuse);
64da2e3ebdSchin if(!(local = vd->mode&VM_TRUST) )
65da2e3ebdSchin { GETLOCAL(vd,local);
66da2e3ebdSchin if(ISLOCK(vd, local))
677c2fbfb3SApril Chin { CLRINUSE(vd, inuse);
68da2e3ebdSchin return NIL(Void_t*);
697c2fbfb3SApril Chin }
70da2e3ebdSchin SETLOCK(vd, local);
71da2e3ebdSchin }
72da2e3ebdSchin
73da2e3ebdSchin if((tp = vd->free) ) /* there is a ready free block */
74da2e3ebdSchin { vd->free = SEGLINK(tp);
75da2e3ebdSchin goto done;
76da2e3ebdSchin }
77da2e3ebdSchin
78da2e3ebdSchin size = ROUND(size,ALIGN);
79da2e3ebdSchin
80da2e3ebdSchin /* look thru all segments for a suitable free block */
81da2e3ebdSchin for(tp = NIL(Block_t*), seg = vd->seg; seg; seg = seg->next)
82da2e3ebdSchin { if((tp = seg->free) &&
83da2e3ebdSchin (s = (SIZE(tp) & ~BITS) + sizeof(Head_t)) >= size )
84da2e3ebdSchin goto has_blk;
85da2e3ebdSchin }
86da2e3ebdSchin
87da2e3ebdSchin for(;;) /* must extend region */
88da2e3ebdSchin { if((tp = (*_Vmextend)(vm,ROUND(size,vd->incr),NIL(Vmsearch_f))) )
89da2e3ebdSchin { s = (SIZE(tp) & ~BITS) + sizeof(Head_t);
90da2e3ebdSchin seg = SEG(tp);
91da2e3ebdSchin goto has_blk;
92da2e3ebdSchin }
93da2e3ebdSchin else if(vd->mode&VM_AGAIN)
94da2e3ebdSchin vd->mode &= ~VM_AGAIN;
95da2e3ebdSchin else goto done;
96da2e3ebdSchin }
97da2e3ebdSchin
98da2e3ebdSchin has_blk: /* if get here, (tp, s, seg) must be well-defined */
99da2e3ebdSchin next = (Block_t*)((Vmuchar_t*)tp+size);
100da2e3ebdSchin if((s -= size) <= (size + sizeof(Head_t)) )
101da2e3ebdSchin { for(; s >= size; s -= size)
102da2e3ebdSchin { SIZE(next) = POOLFREE;
103da2e3ebdSchin SEGLINK(next) = vd->free;
104da2e3ebdSchin vd->free = next;
105da2e3ebdSchin next = (Block_t*)((Vmuchar_t*)next + size);
106da2e3ebdSchin }
107da2e3ebdSchin seg->free = NIL(Block_t*);
108da2e3ebdSchin }
109da2e3ebdSchin else
110da2e3ebdSchin { SIZE(next) = s - sizeof(Head_t);
111da2e3ebdSchin SEG(next) = seg;
112da2e3ebdSchin seg->free = next;
113da2e3ebdSchin }
114da2e3ebdSchin
115da2e3ebdSchin done:
116da2e3ebdSchin if(!local && (vd->mode&VM_TRACE) && _Vmtrace && tp)
117da2e3ebdSchin (*_Vmtrace)(vm,NIL(Vmuchar_t*),(Vmuchar_t*)tp,vd->pool,0);
118da2e3ebdSchin
119da2e3ebdSchin CLRLOCK(vd, local);
120da2e3ebdSchin ANNOUNCE(local, vm, VM_ALLOC, (Void_t*)tp, vm->disc);
1217c2fbfb3SApril Chin CLRINUSE(vd, inuse);
122da2e3ebdSchin return (Void_t*)tp;
123da2e3ebdSchin }
124da2e3ebdSchin
125da2e3ebdSchin #if __STD_C
pooladdr(Vmalloc_t * vm,reg Void_t * addr)126da2e3ebdSchin static long pooladdr(Vmalloc_t* vm, reg Void_t* addr)
127da2e3ebdSchin #else
128da2e3ebdSchin static long pooladdr(vm, addr)
129da2e3ebdSchin Vmalloc_t* vm;
130da2e3ebdSchin reg Void_t* addr;
131da2e3ebdSchin #endif
132da2e3ebdSchin {
133da2e3ebdSchin reg Block_t *bp, *tp;
134da2e3ebdSchin reg Vmuchar_t *laddr, *baddr;
135da2e3ebdSchin reg size_t size;
136da2e3ebdSchin reg Seg_t* seg;
137da2e3ebdSchin reg long offset;
138da2e3ebdSchin reg Vmdata_t* vd = vm->data;
1397c2fbfb3SApril Chin reg int local, inuse;
140da2e3ebdSchin
1417c2fbfb3SApril Chin SETINUSE(vd, inuse);
142da2e3ebdSchin if(!(local = vd->mode&VM_TRUST))
143da2e3ebdSchin { GETLOCAL(vd,local);
144da2e3ebdSchin if(ISLOCK(vd,local))
1457c2fbfb3SApril Chin { CLRINUSE(vd, inuse);
146da2e3ebdSchin return -1L;
1477c2fbfb3SApril Chin }
148da2e3ebdSchin SETLOCK(vd,local);
149da2e3ebdSchin }
150da2e3ebdSchin
151da2e3ebdSchin offset = -1L;
152da2e3ebdSchin for(seg = vd->seg; seg; seg = seg->next)
153da2e3ebdSchin { laddr = (Vmuchar_t*)SEGBLOCK(seg);
154da2e3ebdSchin baddr = seg->baddr-sizeof(Head_t);
155da2e3ebdSchin if((Vmuchar_t*)addr < laddr || (Vmuchar_t*)addr >= baddr)
156da2e3ebdSchin continue;
157da2e3ebdSchin
158da2e3ebdSchin /* the block that has this address */
159da2e3ebdSchin size = ROUND(vd->pool,ALIGN);
160da2e3ebdSchin tp = (Block_t*)(laddr + (((Vmuchar_t*)addr-laddr)/size)*size );
161da2e3ebdSchin
162da2e3ebdSchin /* see if this block has been freed */
163da2e3ebdSchin if(SIZE(tp) == POOLFREE) /* may be a coincidence - make sure */
164da2e3ebdSchin for(bp = vd->free; bp; bp = SEGLINK(bp))
165da2e3ebdSchin if(bp == tp)
166da2e3ebdSchin goto done;
167da2e3ebdSchin
168da2e3ebdSchin offset = (Vmuchar_t*)addr - (Vmuchar_t*)tp;
169da2e3ebdSchin goto done;
170da2e3ebdSchin }
171da2e3ebdSchin
172da2e3ebdSchin done :
173da2e3ebdSchin CLRLOCK(vd,local);
1747c2fbfb3SApril Chin CLRINUSE(vd, inuse);
175da2e3ebdSchin return offset;
176da2e3ebdSchin }
177da2e3ebdSchin
178da2e3ebdSchin #if __STD_C
poolfree(reg Vmalloc_t * vm,reg Void_t * data)179da2e3ebdSchin static int poolfree(reg Vmalloc_t* vm, reg Void_t* data )
180da2e3ebdSchin #else
181da2e3ebdSchin static int poolfree(vm, data)
182da2e3ebdSchin reg Vmalloc_t* vm;
183da2e3ebdSchin reg Void_t* data;
184da2e3ebdSchin #endif
185da2e3ebdSchin {
186da2e3ebdSchin reg Block_t* bp;
187da2e3ebdSchin reg Vmdata_t* vd = vm->data;
1887c2fbfb3SApril Chin reg int local, inuse;
189da2e3ebdSchin
190da2e3ebdSchin if(!data)
191da2e3ebdSchin return 0;
192da2e3ebdSchin
1937c2fbfb3SApril Chin SETINUSE(vd, inuse);
194da2e3ebdSchin if(!(local = vd->mode&VM_TRUST))
195da2e3ebdSchin { GETLOCAL(vd, local);
196da2e3ebdSchin
197da2e3ebdSchin if(ISLOCK(vd, local) || vd->pool <= 0)
1987c2fbfb3SApril Chin { CLRINUSE(vd, inuse);
199da2e3ebdSchin return -1;
2007c2fbfb3SApril Chin }
201da2e3ebdSchin
202da2e3ebdSchin if(KPVADDR(vm,data,pooladdr) != 0)
203da2e3ebdSchin { if(vm->disc->exceptf)
204da2e3ebdSchin (void)(*vm->disc->exceptf)(vm,VM_BADADDR,data,vm->disc);
2057c2fbfb3SApril Chin CLRINUSE(vd, inuse);
206da2e3ebdSchin return -1;
207da2e3ebdSchin }
208da2e3ebdSchin
209da2e3ebdSchin SETLOCK(vd, local);
210da2e3ebdSchin }
211da2e3ebdSchin
212da2e3ebdSchin bp = (Block_t*)data;
213da2e3ebdSchin SIZE(bp) = POOLFREE;
214da2e3ebdSchin SEGLINK(bp) = vd->free;
215da2e3ebdSchin vd->free = bp;
216da2e3ebdSchin
217da2e3ebdSchin if(!local && (vd->mode&VM_TRACE) && _Vmtrace)
218da2e3ebdSchin (*_Vmtrace)(vm, (Vmuchar_t*)data, NIL(Vmuchar_t*), vd->pool, 0);
219da2e3ebdSchin
220da2e3ebdSchin CLRLOCK(vd,local);
221da2e3ebdSchin ANNOUNCE(local, vm, VM_FREE, data, vm->disc);
2227c2fbfb3SApril Chin CLRINUSE(vd, inuse);
223da2e3ebdSchin return 0;
224da2e3ebdSchin }
225da2e3ebdSchin
226da2e3ebdSchin #if __STD_C
poolresize(Vmalloc_t * vm,Void_t * data,size_t size,int type)227da2e3ebdSchin static Void_t* poolresize(Vmalloc_t* vm, Void_t* data, size_t size, int type )
228da2e3ebdSchin #else
229da2e3ebdSchin static Void_t* poolresize(vm, data, size, type )
230da2e3ebdSchin Vmalloc_t* vm;
231da2e3ebdSchin Void_t* data;
232da2e3ebdSchin size_t size;
233da2e3ebdSchin int type;
234da2e3ebdSchin #endif
235da2e3ebdSchin {
2367c2fbfb3SApril Chin int local, inuse;
237da2e3ebdSchin reg Vmdata_t* vd = vm->data;
238da2e3ebdSchin
239da2e3ebdSchin NOTUSED(type);
240da2e3ebdSchin
2417c2fbfb3SApril Chin SETINUSE(vd, inuse);
242da2e3ebdSchin if(!data)
243da2e3ebdSchin { if((data = poolalloc(vm,size)) && (type&VM_RSZERO) )
244da2e3ebdSchin { reg int *d = (int*)data, *ed = (int*)((char*)data+size);
245da2e3ebdSchin do { *d++ = 0;} while(d < ed);
246da2e3ebdSchin }
2477c2fbfb3SApril Chin CLRINUSE(vd, inuse);
248da2e3ebdSchin return data;
249da2e3ebdSchin }
250da2e3ebdSchin if(size == 0)
251da2e3ebdSchin { (void)poolfree(vm,data);
2527c2fbfb3SApril Chin CLRINUSE(vd, inuse);
253da2e3ebdSchin return NIL(Void_t*);
254da2e3ebdSchin }
255da2e3ebdSchin
256da2e3ebdSchin if(!(local = vd->mode&VM_TRUST) )
257da2e3ebdSchin { GETLOCAL(vd, local);
258da2e3ebdSchin
259da2e3ebdSchin if(ISLOCK(vd, local) )
2607c2fbfb3SApril Chin { CLRINUSE(vd, inuse);
261da2e3ebdSchin return NIL(Void_t*);
2627c2fbfb3SApril Chin }
263da2e3ebdSchin
264da2e3ebdSchin if(size != vd->pool || KPVADDR(vm,data,pooladdr) != 0)
265da2e3ebdSchin { if(vm->disc->exceptf)
266da2e3ebdSchin (void)(*vm->disc->exceptf)(vm,VM_BADADDR,data,vm->disc);
2677c2fbfb3SApril Chin CLRINUSE(vd, inuse);
268da2e3ebdSchin return NIL(Void_t*);
269da2e3ebdSchin }
270da2e3ebdSchin
271da2e3ebdSchin if((vd->mode&VM_TRACE) && _Vmtrace)
272da2e3ebdSchin (*_Vmtrace)(vm, (Vmuchar_t*)data, (Vmuchar_t*)data, size, 0);
273da2e3ebdSchin }
274da2e3ebdSchin
275da2e3ebdSchin ANNOUNCE(local, vm, VM_RESIZE, data, vm->disc);
2767c2fbfb3SApril Chin CLRINUSE(vd, inuse);
277da2e3ebdSchin return data;
278da2e3ebdSchin }
279da2e3ebdSchin
280da2e3ebdSchin #if __STD_C
poolsize(Vmalloc_t * vm,Void_t * addr)281da2e3ebdSchin static long poolsize(Vmalloc_t* vm, Void_t* addr)
282da2e3ebdSchin #else
283da2e3ebdSchin static long poolsize(vm, addr)
284da2e3ebdSchin Vmalloc_t* vm;
285da2e3ebdSchin Void_t* addr;
286da2e3ebdSchin #endif
287da2e3ebdSchin {
288da2e3ebdSchin return pooladdr(vm,addr) == 0 ? (long)vm->data->pool : -1L;
289da2e3ebdSchin }
290da2e3ebdSchin
291da2e3ebdSchin #if __STD_C
poolcompact(Vmalloc_t * vm)292da2e3ebdSchin static int poolcompact(Vmalloc_t* vm)
293da2e3ebdSchin #else
294da2e3ebdSchin static int poolcompact(vm)
295da2e3ebdSchin Vmalloc_t* vm;
296da2e3ebdSchin #endif
297da2e3ebdSchin {
298da2e3ebdSchin reg Block_t* fp;
299da2e3ebdSchin reg Seg_t *seg, *next;
300da2e3ebdSchin reg size_t s;
301da2e3ebdSchin reg Vmdata_t* vd = vm->data;
3027c2fbfb3SApril Chin reg int inuse;
303da2e3ebdSchin
3047c2fbfb3SApril Chin SETINUSE(vd, inuse);
305da2e3ebdSchin if(!(vd->mode&VM_TRUST))
306da2e3ebdSchin { if(ISLOCK(vd,0))
3077c2fbfb3SApril Chin { CLRINUSE(vd, inuse);
308da2e3ebdSchin return -1;
3097c2fbfb3SApril Chin }
310da2e3ebdSchin SETLOCK(vd,0);
311da2e3ebdSchin }
312da2e3ebdSchin
313da2e3ebdSchin for(seg = vd->seg; seg; seg = next)
314da2e3ebdSchin { next = seg->next;
315da2e3ebdSchin
316da2e3ebdSchin if(!(fp = seg->free))
317da2e3ebdSchin continue;
318da2e3ebdSchin
319da2e3ebdSchin seg->free = NIL(Block_t*);
320da2e3ebdSchin if(seg->size == (s = SIZE(fp)&~BITS))
321da2e3ebdSchin s = seg->extent;
322da2e3ebdSchin else s += sizeof(Head_t);
323da2e3ebdSchin
324da2e3ebdSchin if((*_Vmtruncate)(vm,seg,s,1) == s)
325da2e3ebdSchin seg->free = fp;
326da2e3ebdSchin }
327da2e3ebdSchin
328da2e3ebdSchin if((vd->mode&VM_TRACE) && _Vmtrace)
329da2e3ebdSchin (*_Vmtrace)(vm, (Vmuchar_t*)0, (Vmuchar_t*)0, 0, 0);
330da2e3ebdSchin
331da2e3ebdSchin CLRLOCK(vd,0);
3327c2fbfb3SApril Chin CLRINUSE(vd, inuse);
333da2e3ebdSchin return 0;
334da2e3ebdSchin }
335da2e3ebdSchin
336da2e3ebdSchin #if __STD_C
poolalign(Vmalloc_t * vm,size_t size,size_t align)337da2e3ebdSchin static Void_t* poolalign(Vmalloc_t* vm, size_t size, size_t align)
338da2e3ebdSchin #else
339da2e3ebdSchin static Void_t* poolalign(vm, size, align)
340da2e3ebdSchin Vmalloc_t* vm;
341da2e3ebdSchin size_t size;
342da2e3ebdSchin size_t align;
343da2e3ebdSchin #endif
344da2e3ebdSchin {
345da2e3ebdSchin NOTUSED(vm);
346da2e3ebdSchin NOTUSED(size);
347da2e3ebdSchin NOTUSED(align);
348da2e3ebdSchin return NIL(Void_t*);
349da2e3ebdSchin }
350da2e3ebdSchin
351da2e3ebdSchin /* Public interface */
352da2e3ebdSchin static Vmethod_t _Vmpool =
353da2e3ebdSchin {
354da2e3ebdSchin poolalloc,
355da2e3ebdSchin poolresize,
356da2e3ebdSchin poolfree,
357da2e3ebdSchin pooladdr,
358da2e3ebdSchin poolsize,
359da2e3ebdSchin poolcompact,
360da2e3ebdSchin poolalign,
361da2e3ebdSchin VM_MTPOOL
362da2e3ebdSchin };
363da2e3ebdSchin
364da2e3ebdSchin __DEFINE__(Vmethod_t*,Vmpool,&_Vmpool);
365da2e3ebdSchin
366da2e3ebdSchin #ifdef NoF
367da2e3ebdSchin NoF(vmpool)
368da2e3ebdSchin #endif
369da2e3ebdSchin
370da2e3ebdSchin #endif
371