| 45 | #define PTR_SUB(b,o) ( (void*) ((char*)(b) - (o)) ) |
| 46 | |
| 47 | alloc_pool_t |
| 48 | pool_create(size_t size, size_t quantum, void (*bomb)(const char*, const char*, int), int flags) |
| 49 | { |
| 50 | struct alloc_pool *pool; |
| 51 | |
| 52 | if ((MINALIGN & (MINALIGN - 1)) != (0)) { |
| 53 | if (bomb) |
| 54 | (*bomb)("Compiler error: MINALIGN is not a power of 2", __FILE__, __LINE__); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | if (!(pool = new0(struct alloc_pool))) |
| 59 | return NULL; |
| 60 | |
| 61 | if (!size) |
| 62 | size = POOL_DEF_EXTENT; |
| 63 | if (!quantum) |
| 64 | quantum = MINALIGN; |
| 65 | |
| 66 | if (flags & POOL_INTERN) { |
| 67 | if (size <= sizeof (struct pool_extent)) |
| 68 | size = quantum; |
| 69 | else |
| 70 | size -= sizeof (struct pool_extent); |
| 71 | flags |= POOL_PREPEND; |
| 72 | } |
| 73 | |
| 74 | if (quantum <= 1) |
| 75 | flags = (flags | POOL_NO_QALIGN) & ~POOL_QALIGN_P2; |
| 76 | else if (!(flags & POOL_NO_QALIGN)) { |
| 77 | if (size % quantum) |
| 78 | size += quantum - size % quantum; |
| 79 | /* If quantum is a power of 2, we'll avoid using modulus. */ |
| 80 | if (!(quantum & (quantum - 1))) |
| 81 | flags |= POOL_QALIGN_P2; |
| 82 | } |
| 83 | |
| 84 | pool->size = size; |
| 85 | pool->quantum = quantum; |
| 86 | pool->bomb = bomb; |
| 87 | pool->flags = flags; |
| 88 | |
| 89 | return pool; |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | pool_destroy(alloc_pool_t p) |