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))
89 pool_alloc(alloc_pool_t p, size_t len, char *bomb)
91 struct alloc_pool *pool = (struct alloc_pool *) p;
97 else if (pool->quantum > 1 && len % pool->quantum)
98 len += pool->quantum - len % pool->quantum;
100 if (len > pool->size)
103 if (!pool->live || len > pool->live->free)
113 pool->live->next = pool->free;
114 pool->free = pool->live;
121 if (pool->flags & POOL_APPEND)
122 asize += sizeof (struct pool_extent);
124 if (!(start = (void *) malloc(asize)))
127 if (pool->flags & POOL_CLEAR)
128 memset(start, 0, pool->size);
130 if (pool->flags & POOL_APPEND)
132 pool->live = start + free;
134 else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
138 if (pool->flags & POOL_QALIGN && pool->quantum > 1
139 && (sqew = (size_t)(start + free) % pool->quantum))
144 pool->live->start = start;
145 pool->live->free = free;
146 pool->live->bound = bound;
147 pool->live->next = NULL;
153 pool->b_allocated += len;
155 pool->live->free -= len;
157 return pool->live->start + pool->live->free;
166 pool_free(alloc_pool_t p, size_t len, void *addr)
168 struct alloc_pool *pool = (struct alloc_pool *) p;
169 struct pool_extent *cur;
170 struct pool_extent *prev;
177 else if (pool->quantum > 1 && len % pool->quantum)
178 len += pool->quantum - len % pool->quantum;
180 if (!addr && pool->live)
182 pool->live->next = pool->free;
183 pool->free = pool->live;
188 pool->b_freed += len;
192 && addr >= cur->start
193 && addr < cur->start + pool->size)
195 if (addr == cur->start + cur->free)
197 if (pool->flags & POOL_CLEAR)
198 memset(addr, 0, len);
199 pool->b_freed += len;
203 if (cur->free + cur->bound >= pool->size)
207 cur->free = pool->size;
209 if (pool->flags & POOL_QALIGN && pool->quantum > 1
210 && (sqew = (size_t)(cur->start + cur->free) % pool->quantum))
218 for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next)
220 if (addr >= cur->start
221 && addr < cur->start + pool->size)
229 prev->next = cur->next;
230 cur->next = pool->free;
235 if (cur->free + cur->bound >= pool->size)
237 pool->free = cur->next;
240 if (!(pool->flags & POOL_APPEND))
247 #define FDPRINT(label, value) \
248 snprintf(buf, BUFSIZ, label, value), \
249 write(fd, buf, strlen(buf));
251 #define FDEXTSTAT(ext) \
252 snprintf(buf, BUFSIZ, " %12ld %5ld\n", \
254 (long) ext->bound), \
255 write(fd, buf, strlen(buf))
258 pool_stats(alloc_pool_t p, int fd, int summarize)
260 struct alloc_pool *pool = (struct alloc_pool *) p;
261 struct pool_extent *cur;
267 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
268 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
269 FDPRINT(" Extents created: %12ld\n", pool->e_created);
270 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
271 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
272 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
273 FDPRINT(" Alloc bytes: %12.0f\n", (double) pool->b_allocated);
274 FDPRINT(" Free bytes: %12.0f\n", (double) pool->b_freed);
279 if (!pool->live && !pool->free)
286 FDEXTSTAT(pool->live);
288 strcpy(buf, " FREE BOUND\n");
289 write(fd, buf, strlen(buf));
291 for (cur = pool->free; cur; cur = cur->next)