| 21 | } |
| 22 | |
| 23 | static void Queue_Resize(struct Queue* queue) { |
| 24 | cc_uint8* entries; |
| 25 | int capacity, headToEndSize, byteOffsetToHead; |
| 26 | |
| 27 | if (queue->capacity >= (Int32_MaxValue / 4)) { |
| 28 | Chat_AddRaw("&cToo many generic queue entries, clearing"); |
| 29 | Queue_Clear(queue); |
| 30 | return; |
| 31 | } |
| 32 | capacity = queue->capacity * 2; |
| 33 | if (capacity < 32) capacity = 32; |
| 34 | entries = (cc_uint8*)Mem_Alloc(capacity, queue->structSize, "Generic queue"); |
| 35 | |
| 36 | /* Elements must be readjusted to avoid index wrapping issues */ |
| 37 | headToEndSize = (queue->capacity - queue->head) * queue->structSize; |
| 38 | byteOffsetToHead = queue->head * queue->structSize; |
| 39 | /* Copy from head to end */ |
| 40 | Mem_Copy(entries, queue->entries + byteOffsetToHead, headToEndSize); |
| 41 | if (queue->head != 0) { |
| 42 | /* If there's any leftover before the head, copy that bit too */ |
| 43 | Mem_Copy(entries + headToEndSize, queue->entries, byteOffsetToHead); |
| 44 | } |
| 45 | |
| 46 | Mem_Free(queue->entries); |
| 47 | |
| 48 | queue->entries = entries; |
| 49 | queue->capacity = capacity; |
| 50 | queue->mask = capacity - 1; /* capacity is power of two */ |
| 51 | queue->head = 0; |
| 52 | queue->tail = queue->count; |
| 53 | } |
| 54 | |
| 55 | /* Appends an entry to the end of the queue, resizing if necessary. */ |
| 56 | void Queue_Enqueue(struct Queue* queue, void* item) { |
no test coverage detected