3 #define POOL_DEF_EXTENT (32 * 1024)
7 size_t size; /* extent size */
8 size_t quantum; /* allocation quantum */
9 struct pool_extent *live; /* current extent for
11 struct pool_extent *free; /* unfreed extent list */
13 /* function to call if
17 /* statistical data */
18 unsigned long e_created; /* extents created */
19 unsigned long e_freed; /* extents detroyed */
20 uint64 n_allocated; /* calls to alloc */
21 uint64 n_freed; /* calls to free */
22 uint64 b_allocated; /* cum. bytes allocated */
23 uint64 b_freed; /* cum. bytes freed */
28 void *start; /* starting address */
29 size_t free; /* free bytecount */
30 size_t bound; /* bytes bound by padding,
31 * overhead and freed */
32 struct pool_extent *next;
40 #define MINALIGN offsetof(struct align_test, bar)
42 /* Temporarily cast a void* var into a char* var when adding an offset (to
43 * keep some compilers from complaining about the pointer arithmetic). */
44 #define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) )
47 pool_create(size_t size, size_t quantum,
48 void (*bomb)(char *), int flags)
50 struct alloc_pool *pool;
52 if (!(pool = (struct alloc_pool*) malloc(sizeof (struct alloc_pool))))
54 memset(pool, 0, sizeof (struct alloc_pool));
56 pool->size = size /* round extent size to min alignment reqs */
57 ? (size + MINALIGN - 1) & ~(MINALIGN - 1)
59 if (pool->flags & POOL_INTERN)
61 pool->size -= sizeof (struct pool_extent);
64 pool->quantum = quantum ? quantum : MINALIGN;
72 pool_destroy(alloc_pool_t p)
74 struct alloc_pool *pool = (struct alloc_pool *) p;
75 struct pool_extent *cur, *next;
84 if (!(pool->flags & POOL_APPEND))
87 for (cur = pool->free; cur; cur = next)
91 if (!(pool->flags & POOL_APPEND))
98 pool_alloc(alloc_pool_t p, size_t len, char *bomb)
100 struct alloc_pool *pool = (struct alloc_pool *) p;
106 else if (pool->quantum > 1 && len % pool->quantum)
107 len += pool->quantum - len % pool->quantum;
109 if (len > pool->size)
112 if (!pool->live || len > pool->live->free)
122 pool->live->next = pool->free;
123 pool->free = pool->live;
130 if (pool->flags & POOL_APPEND)
131 asize += sizeof (struct pool_extent);
133 if (!(start = (void *) malloc(asize)))
136 if (pool->flags & POOL_CLEAR)
137 memset(start, 0, pool->size);
139 if (pool->flags & POOL_APPEND)
141 pool->live = PTR_ADD(start, free);
143 else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
147 if (pool->flags & POOL_QALIGN && pool->quantum > 1
148 && (sqew = (size_t)PTR_ADD(start, free) % pool->quantum))
153 pool->live->start = start;
154 pool->live->free = free;
155 pool->live->bound = bound;
156 pool->live->next = NULL;
162 pool->b_allocated += len;
164 pool->live->free -= len;
166 return PTR_ADD(pool->live->start, pool->live->free);
175 pool_free(alloc_pool_t p, size_t len, void *addr)
177 struct alloc_pool *pool = (struct alloc_pool *) p;
178 struct pool_extent *cur;
179 struct pool_extent *prev;
186 else if (pool->quantum > 1 && len % pool->quantum)
187 len += pool->quantum - len % pool->quantum;
189 if (!addr && pool->live)
191 pool->live->next = pool->free;
192 pool->free = pool->live;
197 pool->b_freed += len;
201 && addr >= cur->start
202 && addr < PTR_ADD(cur->start, pool->size))
204 if (addr == PTR_ADD(cur->start, cur->free))
206 if (pool->flags & POOL_CLEAR)
207 memset(addr, 0, len);
208 pool->b_freed += len;
212 if (cur->free + cur->bound >= pool->size)
216 cur->free = pool->size;
218 if (pool->flags & POOL_QALIGN && pool->quantum > 1
219 && (sqew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum))
227 for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next)
229 if (addr >= cur->start
230 && addr < PTR_ADD(cur->start, pool->size))
238 prev->next = cur->next;
239 cur->next = pool->free;
244 if (cur->free + cur->bound >= pool->size)
246 pool->free = cur->next;
249 if (!(pool->flags & POOL_APPEND))
256 #define FDPRINT(label, value) \
257 snprintf(buf, BUFSIZ, label, value), \
258 write(fd, buf, strlen(buf));
260 #define FDEXTSTAT(ext) \
261 snprintf(buf, BUFSIZ, " %12ld %5ld\n", \
263 (long) ext->bound), \
264 write(fd, buf, strlen(buf))
267 pool_stats(alloc_pool_t p, int fd, int summarize)
269 struct alloc_pool *pool = (struct alloc_pool *) p;
270 struct pool_extent *cur;
276 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
277 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
278 FDPRINT(" Extents created: %12ld\n", pool->e_created);
279 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
280 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
281 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
282 FDPRINT(" Alloc bytes: %12.0f\n", (double) pool->b_allocated);
283 FDPRINT(" Free bytes: %12.0f\n", (double) pool->b_freed);
288 if (!pool->live && !pool->free)
295 FDEXTSTAT(pool->live);
297 strcpy(buf, " FREE BOUND\n");
298 write(fd, buf, strlen(buf));
300 for (cur = pool->free; cur; cur = cur->next)