struct pool_extent *live; /* current extent for
* allocations */
struct pool_extent *free; /* unfreed extent list */
- void (*bomb)();
- /* function to call if
+ void (*bomb)(); /* function to call if
* malloc fails */
int flags;
/* statistical data */
unsigned long e_created; /* extents created */
unsigned long e_freed; /* extents detroyed */
- uint64 n_allocated; /* calls to alloc */
- uint64 n_freed; /* calls to free */
- uint64 b_allocated; /* cum. bytes allocated */
- uint64 b_freed; /* cum. bytes freed */
+ int64 n_allocated; /* calls to alloc */
+ int64 n_freed; /* calls to free */
+ int64 b_allocated; /* cum. bytes allocated */
+ int64 b_freed; /* cum. bytes freed */
};
struct pool_extent
};
struct align_test {
- void *foo;
- uint64 bar;
+ void *foo;
+ int64 bar;
};
#define MINALIGN offsetof(struct align_test, bar)
#define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) )
alloc_pool_t
-pool_create(size_t size, size_t quantum,
- void (*bomb)(char *), int flags)
+pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags)
{
struct alloc_pool *pool;
- if (!(pool = (struct alloc_pool*) malloc(sizeof (struct alloc_pool))))
+ if (!(pool = new(struct alloc_pool)))
return pool;
memset(pool, 0, sizeof (struct alloc_pool));
pool->size = size /* round extent size to min alignment reqs */
? (size + MINALIGN - 1) & ~(MINALIGN - 1)
: POOL_DEF_EXTENT;
- if (pool->flags & POOL_INTERN)
- {
+ if (pool->flags & POOL_INTERN) {
pool->size -= sizeof (struct pool_extent);
flags |= POOL_APPEND;
}
if (!pool)
return;
- if (pool->live)
- {
+ if (pool->live) {
cur = pool->live;
free(cur->start);
if (!(pool->flags & POOL_APPEND))
free(cur);
}
- for (cur = pool->free; cur; cur = next)
- {
+ for (cur = pool->free; cur; cur = next) {
next = cur->next;
free(cur->start);
if (!(pool->flags & POOL_APPEND))
}
void *
-pool_alloc(alloc_pool_t p, size_t len, char *bomb)
+pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg)
{
struct alloc_pool *pool = (struct alloc_pool *) p;
if (!pool)
len += pool->quantum - len % pool->quantum;
if (len > pool->size)
- goto bomb;
+ goto bomb_out;
- if (!pool->live || len > pool->live->free)
- {
+ if (!pool->live || len > pool->live->free) {
void *start;
size_t free;
size_t bound;
- size_t sqew;
+ size_t skew;
size_t asize;
+ struct pool_extent *ext;
- if (pool->live)
- {
+ if (pool->live) {
pool->live->next = pool->free;
pool->free = pool->live;
}
if (pool->flags & POOL_APPEND)
asize += sizeof (struct pool_extent);
- if (!(start = (void *) malloc(asize)))
- goto bomb;
+ if (!(start = new_array(char, asize)))
+ goto bomb_out;
if (pool->flags & POOL_CLEAR)
- memset(start, 0, pool->size);
+ memset(start, 0, free);
if (pool->flags & POOL_APPEND)
- {
- pool->live = PTR_ADD(start, free);
- }
- else if (!(pool->live = (struct pool_extent *) malloc(sizeof (struct pool_extent))))
- {
- goto bomb;
- }
+ ext = PTR_ADD(start, free);
+ else if (!(ext = new(struct pool_extent)))
+ goto bomb_out;
if (pool->flags & POOL_QALIGN && pool->quantum > 1
- && (sqew = (size_t)PTR_ADD(start, free) % pool->quantum))
- {
- bound += sqew;
- free -= sqew;
+ && (skew = (size_t)PTR_ADD(start, free) % pool->quantum)) {
+ bound += skew;
+ free -= skew;
}
- pool->live->start = start;
- pool->live->free = free;
- pool->live->bound = bound;
- pool->live->next = NULL;
+ ext->start = start;
+ ext->free = free;
+ ext->bound = bound;
+ ext->next = NULL;
+ pool->live = ext;
pool->e_created++;
}
return PTR_ADD(pool->live->start, pool->live->free);
-bomb:
+ bomb_out:
if (pool->bomb)
- (*pool->bomb)(bomb);
+ (*pool->bomb)(bomb_msg);
return NULL;
}
+/* This function allows you to declare memory in the pool that you are done
+ * using. If you free all the memory in a pool's extent, that extent will
+ * be freed. */
void
pool_free(alloc_pool_t p, size_t len, void *addr)
{
- struct alloc_pool *pool = (struct alloc_pool *) p;
- struct pool_extent *cur;
- struct pool_extent *prev;
+ struct alloc_pool *pool = (struct alloc_pool *)p;
+ struct pool_extent *cur, *prev;
if (!pool)
return;
else if (pool->quantum > 1 && len % pool->quantum)
len += pool->quantum - len % pool->quantum;
- if (!addr && pool->live)
- {
+ if (!addr && pool->live) {
pool->live->next = pool->free;
pool->free = pool->live;
pool->live = NULL;
pool->b_freed += len;
cur = pool->live;
- if (cur
- && addr >= cur->start
- && addr < PTR_ADD(cur->start, pool->size))
- {
- if (addr == PTR_ADD(cur->start, cur->free))
- {
+ if (cur && addr >= cur->start
+ && addr < PTR_ADD(cur->start, pool->size)) {
+ if (addr == PTR_ADD(cur->start, cur->free)) {
if (pool->flags & POOL_CLEAR)
memset(addr, 0, len);
- pool->b_freed += len;
- } else {
+ cur->free += len;
+ } else
cur->bound += len;
- }
- if (cur->free + cur->bound >= pool->size)
- {
- size_t sqew;
+ if (cur->free + cur->bound >= pool->size) {
+ size_t skew;
+ if (pool->flags & POOL_CLEAR) {
+ memset(PTR_ADD(cur->start, cur->free), 0,
+ pool->size - cur->free);
+ }
cur->free = pool->size;
cur->bound = 0;
if (pool->flags & POOL_QALIGN && pool->quantum > 1
- && (sqew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum))
- {
- cur->bound += sqew;
- cur->free -= sqew;
+ && (skew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum)) {
+ cur->bound += skew;
+ cur->free -= skew;
}
}
return;
}
- for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next)
- {
+ for (prev = NULL, cur = pool->free; cur; prev = cur, cur = cur->next) {
if (addr >= cur->start
- && addr < cur->start + pool->size)
+ && addr < PTR_ADD(cur->start, pool->size))
break;
}
if (!cur)
return;
- if (prev)
- {
+ if (prev) {
prev->next = cur->next;
cur->next = pool->free;
pool->free = cur;
}
cur->bound += len;
- if (cur->free + cur->bound >= pool->size)
- {
+ if (cur->free + cur->bound >= pool->size) {
pool->free = cur->next;
free(cur->start);
free(cur);
pool->e_freed++;
}
- return;
}
#define FDPRINT(label, value) \
- snprintf(buf, BUFSIZ, label, value), \
- write(fd, buf, strlen(buf));
+ snprintf(buf, sizeof buf, label, value), \
+ write(fd, buf, strlen(buf))
#define FDEXTSTAT(ext) \
- snprintf(buf, BUFSIZ, " %12ld %5ld\n", \
+ snprintf(buf, sizeof buf, " %12ld %5ld\n", \
(long) ext->free, \
(long) ext->bound), \
write(fd, buf, strlen(buf))
FDPRINT(" Extents freed: %12ld\n", pool->e_freed);
FDPRINT(" Alloc count: %12.0f\n", (double) pool->n_allocated);
FDPRINT(" Free Count: %12.0f\n", (double) pool->n_freed);
- FDPRINT(" Alloc bytes: %12.0f\n", (double) pool->b_allocated);
- FDPRINT(" Free bytes: %12.0f\n", (double) pool->b_freed);
+ FDPRINT(" Bytes allocated: %12.0f\n", (double) pool->b_allocated);
+ FDPRINT(" Bytes freed: %12.0f\n", (double) pool->b_freed);
if (summarize)
return;
write(fd, "\n", 1);
if (pool->live)
- {
FDEXTSTAT(pool->live);
- }
- strcpy(buf, " FREE BOUND\n");
+ strlcpy(buf, " FREE BOUND\n", sizeof buf);
write(fd, buf, strlen(buf));
for (cur = pool->free; cur; cur = cur->next)
- {
FDEXTSTAT(cur);
- }
}