Clear a deleted item and recycle it to the caller.
| 61 | |
| 62 | // Clear a deleted item and recycle it to the caller. |
| 63 | static void *_ObjectPool_ReuseItem(ObjectPool *pool) { |
| 64 | ObjectID idx = array_pop(pool->deletedIdx); |
| 65 | |
| 66 | pool->itemCount++; |
| 67 | |
| 68 | Block *block = GET_ITEM_BLOCK(pool, idx); |
| 69 | uint pos = ITEM_POSITION_WITHIN_BLOCK(idx); |
| 70 | |
| 71 | // Retrieve a pointer to the item's header. |
| 72 | unsigned char *item_header = block->data + (pos * block->itemSize); |
| 73 | unsigned char *item = ITEM_FROM_HEADER(item_header); |
| 74 | ASSERT(ITEM_ID(item) == idx); // The item ID should not change on reuse. |
| 75 | |
| 76 | // Zero-set the item being returned. |
| 77 | memset(item, 0, block->itemSize - HEADER_SIZE); |
| 78 | |
| 79 | return item; |
| 80 | } |
| 81 | |
| 82 | ObjectPool *ObjectPool_New(uint64_t itemCap, uint itemSize, void (*fp)(void *)) { |
| 83 | ObjectPool *pool = rm_malloc(sizeof(ObjectPool)); |
no outgoing calls
no test coverage detected