2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
11 static const char sccsid
[] = "@(#)db_salloc.c 10.13 (Sleepycat) 5/10/98";
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
23 #include "common_ext.h"
26 * Implement shared memory region allocation, using simple first-fit algorithm.
27 * The model is that we take a "chunk" of shared memory store and begin carving
28 * it up into areas, similarly to how malloc works. We do coalescing on free.
30 * The "len" field in the __data struct contains the length of the free region
31 * (less the size_t bytes that holds the length). We use the address provided
32 * by the caller to find this length, which allows us to free a chunk without
33 * requiring that the caller pass in the length of the chunk they're freeing.
42 * __db_shalloc_init --
43 * Initialize the area as one large chunk.
45 * PUBLIC: void __db_shalloc_init __P((void *, size_t));
48 __db_shalloc_init(area
, size
)
58 elp
= (struct __data
*)(hp
+ 1);
59 elp
->len
= size
- sizeof(struct __head
) - sizeof(elp
->len
);
60 SH_LIST_INSERT_HEAD(hp
, elp
, links
, __data
);
65 * Allocate some space from the shared region.
67 * PUBLIC: int __db_shalloc __P((void *, size_t, size_t, void *));
70 __db_shalloc(p
, len
, align
, retp
)
79 * We never allocate less than the size of a struct __data, align
80 * to less than a size_t boundary, or align to something that's not
81 * a multiple of a size_t.
83 if (len
< sizeof(struct __data
))
84 len
= sizeof(struct __data
);
85 align
= align
<= sizeof(size_t) ?
86 sizeof(size_t) : ALIGN(align
, sizeof(size_t));
88 /* Walk the list, looking for a slot. */
89 for (elp
= SH_LIST_FIRST((struct __head
*)p
, __data
);
91 elp
= SH_LIST_NEXT(elp
, links
, __data
)) {
93 * Calculate the value of the returned pointer if we were to
95 * + Find the end of the chunk.
96 * + Subtract the memory the user wants.
97 * + Find the closest previous correctly-aligned address.
99 rp
= (u_int8_t
*)elp
+ sizeof(size_t) + elp
->len
;
100 rp
= (u_int8_t
*)rp
- len
;
101 rp
= (u_int8_t
*)((ALIGNTYPE
)rp
& ~(align
- 1));
104 * Rp may now point before elp->links, in which case the chunk
105 * was too small, and we have to try again.
107 if ((u_int8_t
*)rp
< (u_int8_t
*)&elp
->links
)
112 #define SHALLOC_FRAGMENT 32
114 * If there are at least SHALLOC_FRAGMENT additional bytes of
115 * memory, divide the chunk into two chunks.
117 if ((u_int8_t
*)rp
>=
118 (u_int8_t
*)&elp
->links
+ SHALLOC_FRAGMENT
) {
121 ((u_int8_t
*)rp
- (u_int8_t
*)&elp
->links
);
122 elp
->len
-= *sp
+ sizeof(size_t);
127 * Otherwise, we return the entire chunk, wasting some amount
128 * of space to keep the list compact. However, because the
129 * address we're returning to the user may not be the address
130 * of the start of the region for alignment reasons, set the
131 * size_t length fields back to the "real" length field to a
132 * flag value, so that we can find the real length during free.
134 #define ILLEGAL_SIZE 1
135 SH_LIST_REMOVE(elp
, links
, __data
);
136 for (sp
= rp
; (u_int8_t
*)--sp
>= (u_int8_t
*)&elp
->links
;)
141 /* Nothing found large enough; need to grow the region. */
146 * __db_shalloc_free --
147 * Free a shared memory allocation.
149 * PUBLIC: void __db_shalloc_free __P((void *, void *));
152 __db_shalloc_free(regionp
, ptr
)
155 struct __data
*elp
, *lastp
, *newp
;
157 size_t free_size
, *sp
;
161 * Step back over flagged length fields to find the beginning of
162 * the object and its real size.
164 for (sp
= (size_t *)ptr
; sp
[-1] == ILLEGAL_SIZE
; --sp
)
168 newp
= (struct __data
*)((u_int8_t
*)ptr
- sizeof(size_t));
169 free_size
= newp
->len
;
171 /* Trash the returned memory. */
173 memset(ptr
, 0xff, free_size
);
177 * Walk the list, looking for where this entry goes.
179 * We keep the free list sorted by address so that coalescing is
183 * Probably worth profiling this to see how expensive it is.
185 hp
= (struct __head
*)regionp
;
186 for (elp
= SH_LIST_FIRST(hp
, __data
), lastp
= NULL
;
187 elp
!= NULL
&& (void *)elp
< (void *)ptr
;
188 lastp
= elp
, elp
= SH_LIST_NEXT(elp
, links
, __data
))
192 * Elp is either NULL (we reached the end of the list), or the slot
193 * after the one that's being returned. Lastp is either NULL (we're
194 * returning the first element of the list) or the element before the
195 * one being returned.
197 * Check for coalescing with the next element.
200 if ((u_int8_t
*)ptr
+ free_size
== (u_int8_t
*)elp
) {
201 newp
->len
+= elp
->len
+ sizeof(size_t);
202 SH_LIST_REMOVE(elp
, links
, __data
);
204 SH_LIST_INSERT_AFTER(lastp
, newp
, links
, __data
);
206 SH_LIST_INSERT_HEAD(hp
, newp
, links
, __data
);
210 /* Check for coalescing with the previous element. */
211 if (lastp
!= NULL
&& (u_int8_t
*)lastp
+
212 lastp
->len
+ sizeof(size_t) == (u_int8_t
*)newp
) {
213 lastp
->len
+= newp
->len
+ sizeof(size_t);
216 * If we have already put the new element into the list take
217 * it back off again because it's just been merged with the
221 SH_LIST_REMOVE(newp
, links
, __data
);
227 SH_LIST_INSERT_HEAD(hp
, newp
, links
, __data
);
229 SH_LIST_INSERT_AFTER(lastp
, newp
, links
, __data
);
233 * __db_shalloc_count --
234 * Return the amount of memory on the free list.
236 * PUBLIC: size_t __db_shalloc_count __P((void *));
239 __db_shalloc_count(addr
)
246 for (elp
= SH_LIST_FIRST((struct __head
*)addr
, __data
);
248 elp
= SH_LIST_NEXT(elp
, links
, __data
))
256 * Return the size of a shalloc'd piece of memory.
258 * PUBLIC: size_t __db_shsizeof __P((void *));
268 * Step back over flagged length fields to find the beginning of
269 * the object and its real size.
271 for (sp
= (size_t *)ptr
; sp
[-1] == ILLEGAL_SIZE
; --sp
)
274 elp
= (struct __data
*)((u_int8_t
*)sp
- sizeof(size_t));
279 * __db_shalloc_dump --
281 * PUBLIC: void __db_shalloc_dump __P((void *, FILE *));
284 __db_shalloc_dump(addr
, fp
)
290 /* Make it easy to call from the debugger. */
294 fprintf(fp
, "%s\nMemory free list\n", DB_LINE
);
296 for (elp
= SH_LIST_FIRST((struct __head
*)addr
, __data
);
298 elp
= SH_LIST_NEXT(elp
, links
, __data
))
299 fprintf(fp
, "%#lx: %lu\t", (u_long
)elp
, (u_long
)elp
->len
);