| 92 | } |
| 93 | |
| 94 | void *ObjectPool_NewItem(ObjectPool *pool) { |
| 95 | // Reuse a deleted item if one is available. |
| 96 | if(array_len(pool->deletedIdx)) return _ObjectPool_ReuseItem(pool); |
| 97 | |
| 98 | // Make sure we have room for a new item. |
| 99 | if(pool->itemCount >= pool->itemCap) { |
| 100 | // Double the capacity of the pool. |
| 101 | _ObjectPool_AddBlocks(pool, ITEM_COUNT_TO_BLOCK_COUNT(pool->itemCap)); |
| 102 | } |
| 103 | |
| 104 | // Get the index of the new allocation. |
| 105 | ObjectID idx = pool->itemCount; |
| 106 | pool->itemCount++; |
| 107 | |
| 108 | Block *block = GET_ITEM_BLOCK(pool, idx); |
| 109 | uint pos = ITEM_POSITION_WITHIN_BLOCK(idx); |
| 110 | |
| 111 | // Retrieve a pointer to the item's header. |
| 112 | unsigned char *item_header = block->data + (pos * block->itemSize); |
| 113 | unsigned char *item = ITEM_FROM_HEADER(item_header); |
| 114 | ITEM_ID(item) = idx; // Set the item ID. |
| 115 | |
| 116 | return item; |
| 117 | } |
| 118 | |
| 119 | void ObjectPool_DeleteItem(ObjectPool *pool, void *item) { |
| 120 | ASSERT(pool != NULL); |