1 /*
2 * mpatrol
3 * A library for controlling and tracing dynamic memory allocations.
4 * Copyright (C) 1997-2002 Graeme S. Roy <graeme.roy@analog.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 * MA 02111-1307, USA.
20 */
21
22
23 /*
24 * Slot tables. New free entries for a slot table are initialised by
25 * supplying a block of contiguous memory which is to be used for slot
26 * entries. This memory is divided into slots and all of these slots are
27 * then added to the free chain. Other blocks of memory can be added at a
28 * later time in order to expand the table further, and these memory blocks
29 * need not be adjacent to the existing blocks.
30 */
31
32
33 #include "slots.h"
34 #include "utils.h"
35
36
37 #if MP_IDENT_SUPPORT
38 #ident "$Id: slots.c,v 1.8 2002/01/08 20:13:59 graeme Exp $"
39 #else /* MP_IDENT_SUPPORT */
40 static MP_CONST MP_VOLATILE char *slots_id = "$Id: slots.c,v 1.8 2002/01/08 20:13:59 graeme Exp $";
41 #endif /* MP_IDENT_SUPPORT */
42
43
44 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif /* __cplusplus */
48
49
50 /* Initialise the fields of a slot table so that the table becomes empty
51 * and there are no free slots.
52 */
53
54 MP_GLOBAL
55 void
__mp_newslots(slottable * t,size_t s,size_t a)56 __mp_newslots(slottable *t, size_t s, size_t a)
57 {
58 struct { char x; slotentry y; } z;
59 long n;
60
61 t->free = NULL;
62 /* Determine the minimum alignment for a slot entry on this system.
63 * If the supplied alignment is less than this then force it to be the
64 * minimum. In any case, force the alignment to be a power of two.
65 */
66 n = (char *) &z.y - &z.x;
67 if (a < (unsigned long) n)
68 a = n;
69 t->entalign = __mp_poweroftwo(a);
70 /* If the supplied size is less than the size of a slot entry then
71 * force it to be the size of a slot entry.
72 */
73 if (s < sizeof(slotentry))
74 s = sizeof(slotentry);
75 t->entsize = s;
76 t->size = 0;
77 }
78
79
80 /* Add a new block of memory to be used for free slots to a slot table.
81 */
82
83 MP_GLOBAL
84 size_t
__mp_initslots(slottable * t,void * p,size_t s)85 __mp_initslots(slottable *t, void *p, size_t s)
86 {
87 slotentry *e;
88 char *b;
89 size_t l, n;
90
91 /* Obtain a pointer for the first slot entry, given that the pointer
92 * to the supplied block may not be suitably aligned.
93 */
94 e = (slotentry *) __mp_roundup((unsigned long) p, t->entalign);
95 s -= (char *) e - (char *) p;
96 b = (char *) e + t->entsize;
97 l = __mp_roundup(t->entsize, t->entalign) - t->entsize;
98 for (n = 0, p = (char *) p + s; b <= (char *) p; n++)
99 {
100 e->next = t->free;
101 t->free = e;
102 e = (slotentry *) (b + l);
103 b = (char *) e + t->entsize;
104 }
105 return n;
106 }
107
108
109 /* Return a free slot entry from a slot table, or NULL if the table is full.
110 */
111
112 MP_GLOBAL
113 void *
__mp_getslot(slottable * t)114 __mp_getslot(slottable *t)
115 {
116 slotentry *e;
117
118 if ((e = t->free) != NULL)
119 {
120 t->free = e->next;
121 t->size++;
122 }
123 return (void *) e;
124 }
125
126
127 /* Free up an allocated slot entry and return it to the free chain in the
128 * slot table.
129 */
130
131 MP_GLOBAL
132 void
__mp_freeslot(slottable * t,void * p)133 __mp_freeslot(slottable *t, void *p)
134 {
135 slotentry *e;
136
137 e = (slotentry *) p;
138 e->next = t->free;
139 t->free = e;
140 t->size--;
141 }
142
143
144 #ifdef __cplusplus
145 }
146 #endif /* __cplusplus */
147