|
|
| version 1.1, 2012/02/17 15:09:30 | version 1.1.1.2, 2013/10/14 07:51:14 |
|---|---|
| Line 2 | Line 2 |
| #define POOL_DEF_EXTENT (32 * 1024) | #define POOL_DEF_EXTENT (32 * 1024) |
| #define POOL_QALIGN_P2 (1<<16) /* power-of-2 qalign */ | |
| struct alloc_pool | struct alloc_pool |
| { | { |
| size_t size; /* extent size */ | size_t size; /* extent size */ |
| Line 13 struct alloc_pool | Line 15 struct alloc_pool |
| /* statistical data */ | /* statistical data */ |
| unsigned long e_created; /* extents created */ | unsigned long e_created; /* extents created */ |
| unsigned long e_freed; /* extents detroyed */ | unsigned long e_freed; /* extents destroyed */ |
| int64 n_allocated; /* calls to alloc */ | int64 n_allocated; /* calls to alloc */ |
| int64 n_freed; /* calls to free */ | int64 n_freed; /* calls to free */ |
| int64 b_allocated; /* cum. bytes allocated */ | int64 b_allocated; /* cum. bytes allocated */ |
| Line 22 struct alloc_pool | Line 24 struct alloc_pool |
| struct pool_extent | struct pool_extent |
| { | { |
| struct pool_extent *next; | |
| void *start; /* starting address */ | void *start; /* starting address */ |
| size_t free; /* free bytecount */ | size_t free; /* free bytecount */ |
| size_t bound; /* bytes bound by padding, | size_t bound; /* trapped free bytes */ |
| * overhead and freed */ | |
| struct pool_extent *next; | |
| }; | }; |
| struct align_test { | struct align_test { |
| void *foo; | uchar foo; |
| int64 bar; | union { |
| int64 i; | |
| void *p; | |
| } bar; | |
| }; | }; |
| #define MINALIGN offsetof(struct align_test, bar) | #define MINALIGN offsetof(struct align_test, bar) |
| Line 43 struct align_test { | Line 47 struct align_test { |
| alloc_pool_t | alloc_pool_t |
| pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags) | pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags) |
| { | { |
| struct alloc_pool *pool; | struct alloc_pool *pool; |
| if (!(pool = new(struct alloc_pool))) | if (!(pool = new0(struct alloc_pool))) |
| return pool; | return NULL; |
| memset(pool, 0, sizeof (struct alloc_pool)); | |
| pool->size = size /* round extent size to min alignment reqs */ | if ((MINALIGN & (MINALIGN - 1)) != 0) { |
| ? (size + MINALIGN - 1) & ~(MINALIGN - 1) | if (bomb) |
| : POOL_DEF_EXTENT; | (*bomb)("Compiler error: MINALIGN is not a power of 2\n"); |
| return NULL; | |
| } | |
| if (!size) | |
| size = POOL_DEF_EXTENT; | |
| if (!quantum) | |
| quantum = MINALIGN; | |
| if (flags & POOL_INTERN) { | if (flags & POOL_INTERN) { |
| pool->size -= sizeof (struct pool_extent); | if (size <= sizeof (struct pool_extent)) |
| flags |= POOL_APPEND; | size = quantum; |
| else | |
| size -= sizeof (struct pool_extent); | |
| flags |= POOL_PREPEND; | |
| } | } |
| pool->quantum = quantum ? quantum : MINALIGN; | |
| if (quantum <= 1) | |
| flags = (flags | POOL_NO_QALIGN) & ~POOL_QALIGN_P2; | |
| else if (!(flags & POOL_NO_QALIGN)) { | |
| if (size % quantum) | |
| size += quantum - size % quantum; | |
| /* If quantum is a power of 2, we'll avoid using modulus. */ | |
| if (!(quantum & (quantum - 1))) | |
| flags |= POOL_QALIGN_P2; | |
| } | |
| pool->size = size; | |
| pool->quantum = quantum; | |
| pool->bomb = bomb; | pool->bomb = bomb; |
| pool->flags = flags; | pool->flags = flags; |
| Line 67 void | Line 93 void |
| pool_destroy(alloc_pool_t p) | pool_destroy(alloc_pool_t p) |
| { | { |
| struct alloc_pool *pool = (struct alloc_pool *) p; | struct alloc_pool *pool = (struct alloc_pool *) p; |
| struct pool_extent *cur, *next; | struct pool_extent *cur, *next; |
| if (!pool) | if (!pool) |
| return; | return; |
| for (cur = pool->extents; cur; cur = next) { | for (cur = pool->extents; cur; cur = next) { |
| next = cur->next; | next = cur->next; |
| free(cur->start); | if (pool->flags & POOL_PREPEND) |
| if (!(pool->flags & POOL_APPEND)) | free(PTR_ADD(cur->start, -sizeof (struct pool_extent))); |
| else { | |
| free(cur->start); | |
| free(cur); | free(cur); |
| } | |
| } | } |
| free(pool); | free(pool); |
| } | } |
| Line 90 pool_alloc(alloc_pool_t p, size_t len, const char *bom | Line 120 pool_alloc(alloc_pool_t p, size_t len, const char *bom |
| if (!len) | if (!len) |
| len = pool->quantum; | len = pool->quantum; |
| else if (pool->quantum > 1 && len % pool->quantum) | else if (pool->flags & POOL_QALIGN_P2) { |
| len += pool->quantum - len % pool->quantum; | if (len & (pool->quantum - 1)) |
| len += pool->quantum - (len & (pool->quantum - 1)); | |
| } else if (!(pool->flags & POOL_NO_QALIGN)) { | |
| if (len % pool->quantum) | |
| len += pool->quantum - len % pool->quantum; | |
| } | |
| if (len > pool->size) | if (len > pool->size) |
| goto bomb_out; | goto bomb_out; |
| if (!pool->extents || len > pool->extents->free) { | if (!pool->extents || len > pool->extents->free) { |
| void *start; | void *start; |
| size_t free; | size_t asize; |
| size_t bound; | |
| size_t skew; | |
| size_t asize; | |
| struct pool_extent *ext; | struct pool_extent *ext; |
| free = pool->size; | |
| bound = 0; | |
| asize = pool->size; | asize = pool->size; |
| if (pool->flags & POOL_APPEND) | if (pool->flags & POOL_PREPEND) |
| asize += sizeof (struct pool_extent); | asize += sizeof (struct pool_extent); |
| if (!(start = new_array(char, asize))) | if (!(start = new_array(char, asize))) |
| goto bomb_out; | goto bomb_out; |
| if (pool->flags & POOL_CLEAR) | if (pool->flags & POOL_CLEAR) |
| memset(start, 0, free); | memset(start, 0, asize); |
| if (pool->flags & POOL_APPEND) | if (pool->flags & POOL_PREPEND) { |
| ext = PTR_ADD(start, free); | ext = start; |
| else if (!(ext = new(struct pool_extent))) | start = PTR_ADD(start, sizeof (struct pool_extent)); |
| } else if (!(ext = new(struct pool_extent))) | |
| goto bomb_out; | goto bomb_out; |
| if (pool->flags & POOL_QALIGN && pool->quantum > 1 | |
| && (skew = (size_t)PTR_ADD(start, free) % pool->quantum)) { | |
| bound += skew; | |
| free -= skew; | |
| } | |
| ext->start = start; | ext->start = start; |
| ext->free = free; | ext->free = pool->size; |
| ext->bound = bound; | ext->bound = 0; |
| ext->next = pool->extents; | ext->next = pool->extents; |
| pool->extents = ext; | pool->extents = ext; |
| Line 160 pool_free(alloc_pool_t p, size_t len, void *addr) | Line 185 pool_free(alloc_pool_t p, size_t len, void *addr) |
| if (!pool) | if (!pool) |
| return; | return; |
| if (!addr) { | |
| /* A NULL addr starts a fresh extent for new allocations. */ | |
| if ((cur = pool->extents) != NULL && cur->free != pool->size) { | |
| cur->bound += cur->free; | |
| cur->free = 0; | |
| } | |
| return; | |
| } | |
| if (!len) | if (!len) |
| len = pool->quantum; | len = pool->quantum; |
| else if (pool->quantum > 1 && len % pool->quantum) | else if (pool->flags & POOL_QALIGN_P2) { |
| len += pool->quantum - len % pool->quantum; | if (len & (pool->quantum - 1)) |
| len += pool->quantum - (len & (pool->quantum - 1)); | |
| } else if (!(pool->flags & POOL_NO_QALIGN)) { | |
| if (len % pool->quantum) | |
| len += pool->quantum - len % pool->quantum; | |
| } | |
| pool->n_freed++; | pool->n_freed++; |
| pool->b_freed += len; | pool->b_freed += len; |
| Line 179 pool_free(alloc_pool_t p, size_t len, void *addr) | Line 218 pool_free(alloc_pool_t p, size_t len, void *addr) |
| if (!prev) { | if (!prev) { |
| /* The "live" extent is kept ready for more allocations. */ | /* The "live" extent is kept ready for more allocations. */ |
| if (cur->free + cur->bound + len >= pool->size) { | if (cur->free + cur->bound + len >= pool->size) { |
| size_t skew; | |
| if (pool->flags & POOL_CLEAR) { | if (pool->flags & POOL_CLEAR) { |
| memset(PTR_ADD(cur->start, cur->free), 0, | memset(PTR_ADD(cur->start, cur->free), 0, |
| pool->size - cur->free); | pool->size - cur->free); |
| } | } |
| cur->free = pool->size; | cur->free = pool->size; |
| cur->bound = 0; | cur->bound = 0; |
| if (pool->flags & POOL_QALIGN && pool->quantum > 1 | |
| && (skew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum)) { | |
| cur->bound += skew; | |
| cur->free -= skew; | |
| } | |
| } else if (addr == PTR_ADD(cur->start, cur->free)) { | } else if (addr == PTR_ADD(cur->start, cur->free)) { |
| if (pool->flags & POOL_CLEAR) | if (pool->flags & POOL_CLEAR) |
| memset(addr, 0, len); | memset(addr, 0, len); |
| Line 203 pool_free(alloc_pool_t p, size_t len, void *addr) | Line 235 pool_free(alloc_pool_t p, size_t len, void *addr) |
| if (cur->free + cur->bound >= pool->size) { | if (cur->free + cur->bound >= pool->size) { |
| prev->next = cur->next; | prev->next = cur->next; |
| free(cur->start); | if (pool->flags & POOL_PREPEND) |
| if (!(pool->flags & POOL_APPEND)) | free(PTR_ADD(cur->start, -sizeof (struct pool_extent))); |
| else { | |
| free(cur->start); | |
| free(cur); | free(cur); |
| } | |
| pool->e_freed++; | pool->e_freed++; |
| } else if (prev != pool->extents) { | } else if (prev != pool->extents) { |
| /* Move the extent to be the first non-live extent. */ | /* Move the extent to be the first non-live extent. */ |
| Line 242 pool_free_old(alloc_pool_t p, void *addr) | Line 277 pool_free_old(alloc_pool_t p, void *addr) |
| prev->next = NULL; | prev->next = NULL; |
| next = cur; | next = cur; |
| } else { | } else { |
| size_t skew; | |
| /* The most recent live extent can just be reset. */ | /* The most recent live extent can just be reset. */ |
| if (pool->flags & POOL_CLEAR) | if (pool->flags & POOL_CLEAR) |
| memset(addr, 0, pool->size - cur->free); | memset(addr, 0, pool->size - cur->free); |
| cur->free = pool->size; | cur->free = pool->size; |
| cur->bound = 0; | cur->bound = 0; |
| if (pool->flags & POOL_QALIGN && pool->quantum > 1 | |
| && (skew = (size_t)PTR_ADD(cur->start, cur->free) % pool->quantum)) { | |
| cur->bound += skew; | |
| cur->free -= skew; | |
| } | |
| next = cur->next; | next = cur->next; |
| cur->next = NULL; | cur->next = NULL; |
| } | } |
| Line 264 pool_free_old(alloc_pool_t p, void *addr) | Line 292 pool_free_old(alloc_pool_t p, void *addr) |
| while ((cur = next) != NULL) { | while ((cur = next) != NULL) { |
| next = cur->next; | next = cur->next; |
| free(cur->start); | if (pool->flags & POOL_PREPEND) |
| if (!(pool->flags & POOL_APPEND)) | free(PTR_ADD(cur->start, -sizeof (struct pool_extent))); |
| else { | |
| free(cur->start); | |
| free(cur); | free(cur); |
| } | |
| pool->e_freed++; | pool->e_freed++; |
| } | } |
| } | } |
| Line 313 int | Line 344 int |
| pool_stats(alloc_pool_t p, int fd, int summarize) | pool_stats(alloc_pool_t p, int fd, int summarize) |
| { | { |
| struct alloc_pool *pool = (struct alloc_pool *) p; | struct alloc_pool *pool = (struct alloc_pool *) p; |
| struct pool_extent *cur; | struct pool_extent *cur; |
| char buf[BUFSIZ]; | char buf[BUFSIZ]; |
| int ret = 0; | int ret = 0; |