3 #define POOL_DEF_EXTENT (32 * 1024)
5 #define POOL_QALIGN_P2 (1<<16)
9 size_t size; /* extent size */
10 size_t quantum; /* allocation quantum */
11 struct pool_extent *extents; /* top extent is "live" */
12 void (*bomb)(); /* function to call if
16 /* statistical data */
17 unsigned long e_created; /* extents created */
18 unsigned long e_freed; /* extents destroyed */
19 int64 n_allocated; /* calls to alloc */
20 int64 n_freed; /* calls to free */
21 int64 b_allocated; /* cum. bytes allocated */
22 int64 b_freed; /* cum. bytes freed */
27 void *start; /* starting address */
28 size_t free; /* free bytecount */
29 size_t bound; /* trapped free bytes */
30 struct pool_extent *next;
41 #define MINALIGN offsetof(struct align_test, bar)
43 /* Temporarily cast a void* var into a char* var when adding an offset (to
44 * keep some compilers from complaining about the pointer arithmetic). */
45 #define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) )
48 pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags)
50 struct alloc_pool *pool;
52 if (!(pool = new0(struct alloc_pool)))
55 if ((MINALIGN & (MINALIGN - 1)) != 0) {
57 (*bomb)("Compiler error: MINALIGN is not a power of 2\n");
62 size = POOL_DEF_EXTENT;
66 if (flags & POOL_INTERN) {
67 if (size <= sizeof (struct pool_extent))
70 size -= sizeof (struct pool_extent);
71 flags |= POOL_PREPEND;
75 flags &= ~(POOL_QALIGN | POOL_QALIGN_P2);
76 else if (flags & POOL_QALIGN) {
78 size += quantum - size % quantum;
79 /* If quantum is a power of 2, we'll avoid using modulus. */
80 if (!(quantum & (quantum - 1)))
81 flags |= POOL_QALIGN_P2;
85 pool->quantum = quantum;
93 pool_destroy(alloc_pool_t p)
95 struct alloc_pool *pool = (struct alloc_pool *) p;
96 struct pool_extent *cur, *next;
101 for (cur = pool->extents; cur; cur = next) {
103 if (pool->flags & POOL_PREPEND)
104 free(cur->start - sizeof (struct pool_extent));
115 pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg)
117 struct alloc_pool *pool = (struct alloc_pool *) p;
123 else if (pool->flags & POOL_QALIGN_P2) {
124 if (len & (pool->quantum - 1))
125 len += pool->quantum - (len & (pool->quantum - 1));
126 } else if (pool->flags & POOL_QALIGN) {
127 if (len % pool->quantum)
128 len += pool->quantum - len % pool->quantum;
131 if (len > pool->size)
134 if (!pool->extents || len > pool->extents->free) {
137 struct pool_extent *ext;
140 if (pool->flags & POOL_PREPEND)
141 asize += sizeof (struct pool_extent);
143 if (!(start = new_array(char, asize)))
146 if (pool->flags & POOL_CLEAR)
147 memset(start, 0, asize);
149 if (pool->flags & POOL_PREPEND) {
151 start += sizeof (struct pool_extent);
152 } else if (!(ext = new(struct pool_extent)))
155 ext->free = pool->size;
157 ext->next = pool->extents;
164 pool->b_allocated += len;
166 pool->extents->free -= len;
168 return PTR_ADD(pool->extents->start, pool->extents->free);
172 (*pool->bomb)(bomb_msg);
176 /* This function allows you to declare memory in the pool that you are done
177 * using. If you free all the memory in a pool's extent, that extent will
180 pool_free(alloc_pool_t p, size_t len, void *addr)
182 struct alloc_pool *pool = (struct alloc_pool *)p;
183 struct pool_extent *cur, *prev;
190 else if (pool->flags & POOL_QALIGN_P2) {
191 if (len & (pool->quantum - 1))
192 len += pool->quantum - (len & (pool->quantum - 1));
193 } else if (pool->flags & POOL_QALIGN) {
194 if (len % pool->quantum)
195 len += pool->quantum - len % pool->quantum;
199 pool->b_freed += len;
201 for (prev = NULL, cur = pool->extents; cur; prev = cur, cur = cur->next) {
202 if (addr >= cur->start
203 && addr < PTR_ADD(cur->start, pool->size))
210 /* The "live" extent is kept ready for more allocations. */
211 if (cur->free + cur->bound + len >= pool->size) {
212 if (pool->flags & POOL_CLEAR) {
213 memset(PTR_ADD(cur->start, cur->free), 0,
214 pool->size - cur->free);
216 cur->free = pool->size;
218 } else if (addr == PTR_ADD(cur->start, cur->free)) {
219 if (pool->flags & POOL_CLEAR)
220 memset(addr, 0, len);
227 if (cur->free + cur->bound >= pool->size) {
228 prev->next = cur->next;
229 if (pool->flags & POOL_PREPEND)
230 free(cur->start - sizeof (struct pool_extent));
236 } else if (prev != pool->extents) {
237 /* Move the extent to be the first non-live extent. */
238 prev->next = cur->next;
239 cur->next = pool->extents->next;
240 pool->extents->next = cur;
245 /* This allows you to declare that the given address marks the edge of some
246 * pool memory that is no longer needed. Any extents that hold only data
247 * older than the boundary address are freed. NOTE: You MUST NOT USE BOTH
248 * pool_free() and pool_free_old() on the same pool!! */
250 pool_free_old(alloc_pool_t p, void *addr)
252 struct alloc_pool *pool = (struct alloc_pool *)p;
253 struct pool_extent *cur, *prev, *next;
258 for (prev = NULL, cur = pool->extents; cur; prev = cur, cur = cur->next) {
259 if (addr >= cur->start
260 && addr < PTR_ADD(cur->start, pool->size))
266 if (addr == PTR_ADD(cur->start, cur->free)) {
271 /* The most recent live extent can just be reset. */
272 if (pool->flags & POOL_CLEAR)
273 memset(addr, 0, pool->size - cur->free);
274 cur->free = pool->size;
284 while ((cur = next) != NULL) {
286 if (pool->flags & POOL_PREPEND)
287 free(cur->start - sizeof (struct pool_extent));
296 /* If the current extent doesn't have "len" free space in it, mark it as full
297 * so that the next alloc will start a new extent. If len is (size_t)-1, this
298 * bump will always occur. The function returns a boundary address that can
299 * be used with pool_free_old(), or a NULL if no memory is allocated. */
301 pool_boundary(alloc_pool_t p, size_t len)
303 struct alloc_pool *pool = (struct alloc_pool *)p;
304 struct pool_extent *cur;
306 if (!pool || !pool->extents)
311 if (cur->free < len) {
312 cur->bound += cur->free;
316 return PTR_ADD(cur->start, cur->free);
319 #define FDPRINT(label, value) \
320 snprintf(buf, sizeof buf, label, value), \
321 write(fd, buf, strlen(buf))
323 #define FDEXTSTAT(ext) \
324 snprintf(buf, sizeof buf, " %12ld %5ld\n", \
326 (long) ext->bound), \
327 write(fd, buf, strlen(buf))
330 pool_stats(alloc_pool_t p, int fd, int summarize)
332 struct alloc_pool *pool = (struct alloc_pool *) p;
333 struct pool_extent *cur;
339 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
340 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
341 FDPRINT(" Extents created: %12ld\n", pool->e_created);
342 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
343 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
344 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
345 FDPRINT(" Bytes allocated: %12.0f\n", (double) pool->b_allocated);
346 FDPRINT(" Bytes freed: %12.0f\n", (double) pool->b_freed);
356 for (cur = pool->extents; cur; cur = cur->next)