summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/vmalloc/vmpool.c
blob: 965712cf9b3fb6f6a96e5a2b4338a51e0f74d7c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
#if defined(_UWIN) && defined(_BLD_ast)

void _STUB_vmpool(){}

#else

#include	"vmhdr.h"

#define POOLFREE	0x55555555L	/* block free indicator	 */

/*	Method for pool allocation.
**	All elements in a pool have the same size.
**	The following fields of Vmdata_t are used as:
**		pool:	size of a block.
**		free:	list of free blocks.
**
**	Written by Kiem-Phong Vo, kpv@research.att.com, 01/16/94.
*/

#if __STD_C
static Void_t* poolalloc(Vmalloc_t* vm, reg size_t size)
#else
static Void_t* poolalloc(vm, size )
Vmalloc_t*	vm;
reg size_t	size;
#endif
{
	reg Vmdata_t*	vd = vm->data;
	reg Block_t	*tp, *next;
	reg size_t	s;
	reg Seg_t*	seg;
	reg int		local, inuse;

	if(size <= 0)
		return NIL(Void_t*);
	if(size != vd->pool)
	{	if(vd->pool <= 0)
			vd->pool = size;
		else	return NIL(Void_t*);
	}

	SETINUSE(vd, inuse);
	if(!(local = vd->mode&VM_TRUST) )
	{	GETLOCAL(vd,local);
		if(ISLOCK(vd, local))
		{	CLRINUSE(vd, inuse);
			return NIL(Void_t*);
		}
		SETLOCK(vd, local);
	}

	if((tp = vd->free) ) /* there is a ready free block */
	{	vd->free = SEGLINK(tp);
		goto done;
	}

	size = ROUND(size,ALIGN);

	/* look thru all segments for a suitable free block */
	for(tp = NIL(Block_t*), seg = vd->seg; seg; seg = seg->next)
	{	if((tp = seg->free) &&
		   (s = (SIZE(tp) & ~BITS) + sizeof(Head_t)) >= size )
			goto has_blk;
	}

	for(;;) /* must extend region */
	{	if((tp = (*_Vmextend)(vm,ROUND(size,vd->incr),NIL(Vmsearch_f))) )
		{	s = (SIZE(tp) & ~BITS) + sizeof(Head_t);
			seg = SEG(tp);
			goto has_blk;
		}
		else if(vd->mode&VM_AGAIN)
			vd->mode &= ~VM_AGAIN;
		else	goto done;
	}

has_blk: /* if get here, (tp, s, seg) must be well-defined */
	next = (Block_t*)((Vmuchar_t*)tp+size);
	if((s -= size) <= (size + sizeof(Head_t)) )
	{	for(; s >= size; s -= size)
		{	SIZE(next) = POOLFREE;
			SEGLINK(next) = vd->free;
			vd->free = next;
			next = (Block_t*)((Vmuchar_t*)next + size);
		}
		seg->free = NIL(Block_t*);
	}
	else
	{	SIZE(next) = s - sizeof(Head_t);
		SEG(next) = seg;
		seg->free = next;
	}

done:
	if(!local && (vd->mode&VM_TRACE) && _Vmtrace && tp)
		(*_Vmtrace)(vm,NIL(Vmuchar_t*),(Vmuchar_t*)tp,vd->pool,0);

	CLRLOCK(vd, local);
	ANNOUNCE(local, vm, VM_ALLOC, (Void_t*)tp, vm->disc);
	CLRINUSE(vd, inuse);
	return (Void_t*)tp;
}

#if __STD_C
static long pooladdr(Vmalloc_t* vm, reg Void_t* addr)
#else
static long pooladdr(vm, addr)
Vmalloc_t*	vm;
reg Void_t*	addr;
#endif
{
	reg Block_t	*bp, *tp;
	reg Vmuchar_t	*laddr, *baddr;
	reg size_t	size;
	reg Seg_t*	seg;
	reg long	offset;
	reg Vmdata_t*	vd = vm->data;
	reg int		local, inuse;

	SETINUSE(vd, inuse);
	if(!(local = vd->mode&VM_TRUST))
	{	GETLOCAL(vd,local);
		if(ISLOCK(vd,local))
		{	CLRINUSE(vd, inuse);
			return -1L;
		}
		SETLOCK(vd,local);
	}

	offset = -1L;
	for(seg = vd->seg; seg; seg = seg->next)
	{	laddr = (Vmuchar_t*)SEGBLOCK(seg);
		baddr = seg->baddr-sizeof(Head_t);
		if((Vmuchar_t*)addr < laddr || (Vmuchar_t*)addr >= baddr)
			continue;

		/* the block that has this address */
		size = ROUND(vd->pool,ALIGN);
		tp = (Block_t*)(laddr + (((Vmuchar_t*)addr-laddr)/size)*size );

		/* see if this block has been freed */
		if(SIZE(tp) == POOLFREE) /* may be a coincidence - make sure */
			for(bp = vd->free; bp; bp = SEGLINK(bp))
				if(bp == tp)
					goto done;

		offset = (Vmuchar_t*)addr - (Vmuchar_t*)tp;
		goto done;
	}

done :
	CLRLOCK(vd,local);
	CLRINUSE(vd, inuse);
	return offset;
}

#if __STD_C
static int poolfree(reg Vmalloc_t* vm, reg Void_t* data )
#else
static int poolfree(vm, data)
reg Vmalloc_t*	vm;
reg Void_t*	data;
#endif
{
	reg Block_t*	bp;
	reg Vmdata_t*	vd = vm->data;
	reg int		local, inuse;

	if(!data)
		return 0;

	SETINUSE(vd, inuse);
	if(!(local = vd->mode&VM_TRUST))
	{	GETLOCAL(vd, local);

		if(ISLOCK(vd, local) || vd->pool <= 0)
		{	CLRINUSE(vd, inuse);
			return -1;
		}

		if(KPVADDR(vm,data,pooladdr) != 0)
		{	if(vm->disc->exceptf)
				(void)(*vm->disc->exceptf)(vm,VM_BADADDR,data,vm->disc);
			CLRINUSE(vd, inuse);
			return -1;
		}

		SETLOCK(vd, local);
	}

	bp = (Block_t*)data;
	SIZE(bp) = POOLFREE;
	SEGLINK(bp) = vd->free;
	vd->free = bp;

	if(!local && (vd->mode&VM_TRACE) && _Vmtrace)
		(*_Vmtrace)(vm, (Vmuchar_t*)data, NIL(Vmuchar_t*), vd->pool, 0);

	CLRLOCK(vd,local);
	ANNOUNCE(local, vm, VM_FREE, data, vm->disc);
	CLRINUSE(vd, inuse);
	return 0;
}

#if __STD_C
static Void_t* poolresize(Vmalloc_t* vm, Void_t* data, size_t size, int type )
#else
static Void_t* poolresize(vm, data, size, type )
Vmalloc_t*	vm;
Void_t*		data;
size_t		size;
int		type;
#endif
{
	int		local, inuse;
	reg Vmdata_t*	vd = vm->data;

	NOTUSED(type);

	SETINUSE(vd, inuse);
	if(!data)
	{	if((data = poolalloc(vm,size)) && (type&VM_RSZERO) )
		{	reg int	*d = (int*)data, *ed = (int*)((char*)data+size);
			do { *d++ = 0;} while(d < ed);
		}
		CLRINUSE(vd, inuse);
		return data;
	}
	if(size == 0)
	{	(void)poolfree(vm,data);
		CLRINUSE(vd, inuse);
		return NIL(Void_t*);
	}

	if(!(local = vd->mode&VM_TRUST) )
	{	GETLOCAL(vd, local);

		if(ISLOCK(vd, local) )
		{	CLRINUSE(vd, inuse);
			return NIL(Void_t*);
		}

		if(size != vd->pool || KPVADDR(vm,data,pooladdr) != 0)
		{	if(vm->disc->exceptf)
				(void)(*vm->disc->exceptf)(vm,VM_BADADDR,data,vm->disc);
			CLRINUSE(vd, inuse);
			return NIL(Void_t*);
		}

		if((vd->mode&VM_TRACE) && _Vmtrace)
			(*_Vmtrace)(vm, (Vmuchar_t*)data, (Vmuchar_t*)data, size, 0);
	}

	ANNOUNCE(local, vm, VM_RESIZE, data, vm->disc);
	CLRINUSE(vd, inuse);
	return data;
}

#if __STD_C
static long poolsize(Vmalloc_t* vm, Void_t* addr)
#else
static long poolsize(vm, addr)
Vmalloc_t*	vm;
Void_t*		addr;
#endif
{
	return pooladdr(vm,addr) == 0 ? (long)vm->data->pool : -1L;
}

#if __STD_C
static int poolcompact(Vmalloc_t* vm)
#else
static int poolcompact(vm)
Vmalloc_t*	vm;
#endif
{
	reg Block_t*	fp;
	reg Seg_t	*seg, *next;
	reg size_t	s;
	reg Vmdata_t*	vd = vm->data;
	reg int		inuse;

	SETINUSE(vd, inuse);
	if(!(vd->mode&VM_TRUST))
	{	if(ISLOCK(vd,0))
		{	CLRINUSE(vd, inuse);
			return -1;
		}
		SETLOCK(vd,0);
	}

	for(seg = vd->seg; seg; seg = next)
	{	next = seg->next;

		if(!(fp = seg->free))
			continue;

		seg->free = NIL(Block_t*);
		if(seg->size == (s = SIZE(fp)&~BITS))
			s = seg->extent;
		else	s += sizeof(Head_t);

		if((*_Vmtruncate)(vm,seg,s,1) == s)
			seg->free = fp;
	}

	if((vd->mode&VM_TRACE) && _Vmtrace)
		(*_Vmtrace)(vm, (Vmuchar_t*)0, (Vmuchar_t*)0, 0, 0);

	CLRLOCK(vd,0);
	CLRINUSE(vd, inuse);
	return 0;
}

#if __STD_C
static Void_t* poolalign(Vmalloc_t* vm, size_t size, size_t align)
#else
static Void_t* poolalign(vm, size, align)
Vmalloc_t*	vm;
size_t		size;
size_t		align;
#endif
{
	NOTUSED(vm);
	NOTUSED(size);
	NOTUSED(align);
	return NIL(Void_t*);
}

/* Public interface */
static Vmethod_t _Vmpool =
{
	poolalloc,
	poolresize,
	poolfree,
	pooladdr,
	poolsize,
	poolcompact,
	poolalign,
	VM_MTPOOL
};

__DEFINE__(Vmethod_t*,Vmpool,&_Vmpool);

#ifdef NoF
NoF(vmpool)
#endif

#endif