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;
35 #define MINALIGN (sizeof (void *))
38 pool_create(size_t size, size_t quantum,
39 void (*bomb)(char *), int flags)
41 struct alloc_pool *pool;
43 if (!(pool = (struct alloc_pool*) malloc(sizeof (struct alloc_pool))))
45 memset(pool, 0, sizeof (struct alloc_pool));
47 pool->size = size /* round extent size to min alignment reqs */
48 ? (size + MINALIGN - 1) & ~(MINALIGN - 1)
50 if (pool->flags & POOL_INTERN)
52 pool->size -= sizeof (struct pool_extent);
55 pool->quantum = quantum ? quantum : MINALIGN;
63 pool_destroy(alloc_pool_t p)
65 struct alloc_pool *pool = (struct alloc_pool *) p;
66 struct pool_extent *cur, *next;
75 if (!(pool->flags & POOL_APPEND))
78 for (cur = pool->free; cur; cur = next)
82 if (!(pool->flags & POOL_APPEND))
88 void *pool_alloc(alloc_pool_t p, size_t len, char *bomb)
90 struct alloc_pool *pool = (struct alloc_pool *) p;
96 else if (pool->quantum > 1 && len % pool->quantum)
97 len += pool->quantum - len % pool->quantum;
102 if (!pool->live || len > pool->live->free)
112 pool->live->next = pool->free;
113 pool->free = pool->live;
120 if (pool->flags & POOL_APPEND)
121 asize += sizeof (struct pool_extent);
123 if (!(start = (void *) malloc(asize)))
126 if (pool->flags & POOL_CLEAR)
127 memset(start, 0, pool->size);
129 if (pool->flags & POOL_APPEND)
131 pool->live = start + free;
133 else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
137 if (pool->flags & POOL_QALIGN && pool->quantum > 1
138 && (sqew = (size_t)(start + free) % pool->quantum))
143 pool->live->start = start;
144 pool->live->free = free;
145 pool->live->bound = bound;
146 pool->live->next = NULL;
152 pool->b_allocated += len;
154 pool->live->free -= len;
156 return pool->live->start + pool->live->free;
165 pool_free(alloc_pool_t p, size_t len, void *addr)
167 struct alloc_pool *pool = (struct alloc_pool *) p;
168 struct pool_extent *cur;
169 struct pool_extent *prev;
176 else if (pool->quantum > 1 && len % pool->quantum)
177 len += pool->quantum - len % pool->quantum;
179 if (!addr && pool->live)
181 pool->live->next = pool->free;
182 pool->free = pool->live;
187 pool->b_freed += len;
191 && addr >= cur->start
192 && addr < cur->start + pool->size)
194 if (addr == cur->start + cur->free)
196 if (pool->flags & POOL_CLEAR)
197 memset(addr, 0, len);
198 pool->b_freed += len;
202 if (cur->free + cur->bound >= pool->size)
206 cur->free = pool->size;
208 if (pool->flags & POOL_QALIGN && pool->quantum > 1
209 && (sqew = (size_t)(cur->start + cur->free) % pool->quantum))
217 for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next)
219 if (addr >= cur->start
220 && addr < cur->start + pool->size)
228 prev->next = cur->next;
229 cur->next = pool->free;
234 if (cur->free + cur->bound >= pool->size)
236 pool->free = cur->next;
239 if (!(pool->flags & POOL_APPEND))
246 #define FDPRINT(label, value) \
247 snprintf(buf, BUFSIZ, label, value), \
248 write(fd, buf, strlen(buf));
250 #define FDEXTSTAT(ext) \
251 snprintf(buf, BUFSIZ, " %12ld %5ld\n", \
253 (long) ext->bound), \
254 write(fd, buf, strlen(buf))
257 pool_stats(alloc_pool_t p, int fd, int summarize)
259 struct alloc_pool *pool = (struct alloc_pool *) p;
260 struct pool_extent *cur;
266 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
267 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
268 FDPRINT(" Extents created: %12ld\n", pool->e_created);
269 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
270 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
271 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
272 FDPRINT(" Alloc bytes: %12.0f\n", (double) pool->b_allocated);
273 FDPRINT(" Free bytes: %12.0f\n", (double) pool->b_freed);
278 if (!pool->live && !pool->free)
285 FDEXTSTAT(pool->live);
287 strcpy(buf, " FREE BOUND\n");
288 write(fd, buf, strlen(buf));