MCPcopy Create free account
hub / github.com/ClassiCube/ClassiCube / Queue_Resize

Function Queue_Resize

src/Queue.c:23–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21}
22
23static 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. */
56void Queue_Enqueue(struct Queue* queue, void* item) {

Callers 1

Queue_EnqueueFunction · 0.85

Calls 5

Chat_AddRawFunction · 0.85
Queue_ClearFunction · 0.85
Mem_AllocFunction · 0.85
Mem_CopyFunction · 0.85
Mem_FreeFunction · 0.70

Tested by

no test coverage detected