1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * standalone mini vmalloc implementation
25  * no resize, no free, no disciplines, no methods
26  */
27 
28 #include <ast.h>
29 #include <vmalloc.h>
30 
31 Vmalloc_t*	Vmregion;
32 
33 Vmalloc_t*
_vm_open(void)34 _vm_open(void)
35 {
36 	Vmalloc_t*	vp;
37 
38 	if (vp = newof(0, Vmalloc_t, 1, 0))
39 	{
40 		vp->current = &vp->base;
41 		vp->data = vp->current->data;
42 		vp->size = sizeof(vp->current->data);
43 	}
44 	return vp;
45 }
46 
47 int
_vm_close(register Vmalloc_t * vp)48 _vm_close(register Vmalloc_t* vp)
49 {
50 	register Vmchunk_t*	cp;
51 	register Vmchunk_t*	np;
52 
53 	if (!vp)
54 		return -1;
55 	np = vp->base.next;
56 	while (cp = np)
57 	{
58 		np = cp->next;
59 		free(cp);
60 	}
61 	free(vp);
62 	return 0;
63 }
64 
65 void*
_vm_resize(register Vmalloc_t * vp,void * o,unsigned long size)66 _vm_resize(register Vmalloc_t* vp, void* o, unsigned long size)
67 {
68 	char*		p;
69 	unsigned long	n;
70 	unsigned long	z;
71 
72 	z = vp->last;
73 	vp->last = size;
74 	if (o && size < z)
75 		return o;
76 	if ((o ? (size - z) : size) > vp->size)
77 	{
78 		n = (size > sizeof(vp->current->data)) ? (size - sizeof(vp->current->data)) : 0;
79 		if (!(vp->current->next = newof(0, Vmchunk_t, 1, n)))
80 			return 0;
81 		vp->current = vp->current->next;
82 		vp->data = vp->current->data;
83 		vp->size = n ? 0 : sizeof(vp->current->data);
84 		if (o)
85 		{
86 			memcpy(vp->data, o, z);
87 			o = (void*)vp->data;
88 		}
89 	}
90 	else if (o)
91 		size -= z;
92 	p = vp->data;
93 	size = roundof(size, VM_ALIGN);
94 	if (size >= vp->size)
95 		vp->size = 0;
96 	else
97 	{
98 		vp->size -= size;
99 		vp->data += size;
100 	}
101 	return p;
102 }
103