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 */
12 void (*bomb)(); /* function to call if
16 /* statistical data */
17 unsigned long e_created; /* extents created */
18 unsigned long e_freed; /* extents detroyed */
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; /* bytes bound by padding,
30 * overhead and freed */
31 struct pool_extent *next;
39 #define MINALIGN offsetof(struct align_test, bar)
41 /* Temporarily cast a void* var into a char* var when adding an offset (to
42 * keep some compilers from complaining about the pointer arithmetic). */
43 #define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) )
46 pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags)
48 struct alloc_pool *pool;
50 if (!(pool = new(struct alloc_pool)))
52 memset(pool, 0, sizeof (struct alloc_pool));
54 pool->size = size /* round extent size to min alignment reqs */
55 ? (size + MINALIGN - 1) & ~(MINALIGN - 1)
57 if (pool->flags & POOL_INTERN) {
58 pool->size -= sizeof (struct pool_extent);
61 pool->quantum = quantum ? quantum : MINALIGN;
69 pool_destroy(alloc_pool_t p)
71 struct alloc_pool *pool = (struct alloc_pool *) p;
72 struct pool_extent *cur, *next;
80 if (!(pool->flags & POOL_APPEND))
83 for (cur = pool->free; cur; cur = next) {
86 if (!(pool->flags & POOL_APPEND))
93 pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg)
95 struct alloc_pool *pool = (struct alloc_pool *) p;
101 else if (pool->quantum > 1 && len % pool->quantum)
102 len += pool->quantum - len % pool->quantum;
104 if (len > pool->size)
107 if (!pool->live || len > pool->live->free) {
113 struct pool_extent *ext;
116 pool->live->next = pool->free;
117 pool->free = pool->live;
124 if (pool->flags & POOL_APPEND)
125 asize += sizeof (struct pool_extent);
127 if (!(start = new_array(char, asize)))
130 if (pool->flags & POOL_CLEAR)
131 memset(start, 0, free);
133 if (pool->flags & POOL_APPEND)
134 ext = PTR_ADD(start, free);
135 else if (!(ext = new(struct pool_extent)))
137 if (pool->flags & POOL_QALIGN && pool->quantum > 1
138 && (skew = (size_t)PTR_ADD(start, free) % pool->quantum)) {
152 pool->b_allocated += len;
154 pool->live->free -= len;
156 return PTR_ADD(pool->live->start, pool->live->free);
160 (*pool->bomb)(bomb_msg);
164 /* This function allows you to declare memory in the pool that you are done
165 * using. If you free all the memory in a pool's extent, that extent will
168 pool_free(alloc_pool_t p, size_t len, void *addr)
170 struct alloc_pool *pool = (struct alloc_pool *)p;
171 struct pool_extent *cur, *prev;
178 else if (pool->quantum > 1 && len % pool->quantum)
179 len += pool->quantum - len % pool->quantum;
181 if (!addr && pool->live) {
182 pool->live->next = pool->free;
183 pool->free = pool->live;
188 pool->b_freed += len;
191 if (cur && addr >= cur->start
192 && addr < PTR_ADD(cur->start, pool->size)) {
193 if (addr == PTR_ADD(cur->start, cur->free)) {
194 if (pool->flags & POOL_CLEAR)
195 memset(addr, 0, len);
199 if (cur->free + cur->bound >= pool->size) {
202 if (pool->flags & POOL_CLEAR) {
203 memset(PTR_ADD(cur->start, cur->free), 0,
204 pool->size - cur->free);
206 cur->free = pool->size;
208 if (pool->flags & POOL_QALIGN && pool->quantum > 1
209 && (skew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum)) {
216 for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next) {
217 if (addr >= cur->start
218 && addr < PTR_ADD(cur->start, pool->size))
225 prev->next = cur->next;
226 cur->next = pool->free;
231 if (cur->free + cur->bound >= pool->size) {
232 pool->free = cur->next;
235 if (!(pool->flags & POOL_APPEND))
241 #define FDPRINT(label, value) \
242 snprintf(buf, sizeof buf, label, value), \
243 write(fd, buf, strlen(buf))
245 #define FDEXTSTAT(ext) \
246 snprintf(buf, sizeof buf, " %12ld %5ld\n", \
248 (long) ext->bound), \
249 write(fd, buf, strlen(buf))
252 pool_stats(alloc_pool_t p, int fd, int summarize)
254 struct alloc_pool *pool = (struct alloc_pool *) p;
255 struct pool_extent *cur;
261 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
262 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
263 FDPRINT(" Extents created: %12ld\n", pool->e_created);
264 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
265 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
266 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
267 FDPRINT(" Bytes allocated: %12.0f\n", (double) pool->b_allocated);
268 FDPRINT(" Bytes freed: %12.0f\n", (double) pool->b_freed);
273 if (!pool->live && !pool->free)
279 FDEXTSTAT(pool->live);
280 strlcpy(buf, " FREE BOUND\n", sizeof buf);
281 write(fd, buf, strlen(buf));
283 for (cur = pool->free; cur; cur = cur->next)