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)
43 pool_create(size_t size, size_t quantum,
44 void (*bomb)(char *), int flags)
46 struct alloc_pool *pool;
48 if (!(pool = (struct alloc_pool*) malloc(sizeof (struct alloc_pool))))
50 memset(pool, 0, sizeof (struct alloc_pool));
52 pool->size = size /* round extent size to min alignment reqs */
53 ? (size + MINALIGN - 1) & ~(MINALIGN - 1)
55 if (pool->flags & POOL_INTERN)
57 pool->size -= sizeof (struct pool_extent);
60 pool->quantum = quantum ? quantum : MINALIGN;
68 pool_destroy(alloc_pool_t p)
70 struct alloc_pool *pool = (struct alloc_pool *) p;
71 struct pool_extent *cur, *next;
80 if (!(pool->flags & POOL_APPEND))
83 for (cur = pool->free; cur; cur = next)
87 if (!(pool->flags & POOL_APPEND))
94 pool_alloc(alloc_pool_t p, size_t len, char *bomb)
96 struct alloc_pool *pool = (struct alloc_pool *) p;
102 else if (pool->quantum > 1 && len % pool->quantum)
103 len += pool->quantum - len % pool->quantum;
105 if (len > pool->size)
108 if (!pool->live || len > pool->live->free)
118 pool->live->next = pool->free;
119 pool->free = pool->live;
126 if (pool->flags & POOL_APPEND)
127 asize += sizeof (struct pool_extent);
129 if (!(start = (void *) malloc(asize)))
132 if (pool->flags & POOL_CLEAR)
133 memset(start, 0, pool->size);
135 if (pool->flags & POOL_APPEND)
137 pool->live = start + free;
139 else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
143 if (pool->flags & POOL_QALIGN && pool->quantum > 1
144 && (sqew = (size_t)(start + free) % pool->quantum))
149 pool->live->start = start;
150 pool->live->free = free;
151 pool->live->bound = bound;
152 pool->live->next = NULL;
158 pool->b_allocated += len;
160 pool->live->free -= len;
162 return pool->live->start + pool->live->free;
171 pool_free(alloc_pool_t p, size_t len, void *addr)
173 struct alloc_pool *pool = (struct alloc_pool *) p;
174 struct pool_extent *cur;
175 struct pool_extent *prev;
182 else if (pool->quantum > 1 && len % pool->quantum)
183 len += pool->quantum - len % pool->quantum;
185 if (!addr && pool->live)
187 pool->live->next = pool->free;
188 pool->free = pool->live;
193 pool->b_freed += len;
197 && addr >= cur->start
198 && addr < cur->start + pool->size)
200 if (addr == cur->start + cur->free)
202 if (pool->flags & POOL_CLEAR)
203 memset(addr, 0, len);
204 pool->b_freed += len;
208 if (cur->free + cur->bound >= pool->size)
212 cur->free = pool->size;
214 if (pool->flags & POOL_QALIGN && pool->quantum > 1
215 && (sqew = (size_t)(cur->start + cur->free) % pool->quantum))
223 for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next)
225 if (addr >= cur->start
226 && addr < cur->start + pool->size)
234 prev->next = cur->next;
235 cur->next = pool->free;
240 if (cur->free + cur->bound >= pool->size)
242 pool->free = cur->next;
245 if (!(pool->flags & POOL_APPEND))
252 #define FDPRINT(label, value) \
253 snprintf(buf, BUFSIZ, label, value), \
254 write(fd, buf, strlen(buf));
256 #define FDEXTSTAT(ext) \
257 snprintf(buf, BUFSIZ, " %12ld %5ld\n", \
259 (long) ext->bound), \
260 write(fd, buf, strlen(buf))
263 pool_stats(alloc_pool_t p, int fd, int summarize)
265 struct alloc_pool *pool = (struct alloc_pool *) p;
266 struct pool_extent *cur;
272 FDPRINT(" Extent size: %12ld\n", (long) pool->size);
273 FDPRINT(" Alloc quantum: %12ld\n", (long) pool->quantum);
274 FDPRINT(" Extents created: %12ld\n", pool->e_created);
275 FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
276 FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
277 FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
278 FDPRINT(" Alloc bytes: %12.0f\n", (double) pool->b_allocated);
279 FDPRINT(" Free bytes: %12.0f\n", (double) pool->b_freed);
284 if (!pool->live && !pool->free)
291 FDEXTSTAT(pool->live);
293 strcpy(buf, " FREE BOUND\n");
294 write(fd, buf, strlen(buf));
296 for (cur = pool->free; cur; cur = cur->next)