-
Notifications
You must be signed in to change notification settings - Fork 37
/
IGKCircularBuffer.m
155 lines (124 loc) · 4.11 KB
/
IGKCircularBuffer.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#import "IGKCircularBuffer.h"
IGKCircularBuffer IGKCircularBufferCreateFromData(const void *data, CFIndex dataLength, CFIndex maximumCount, CFIndex elementSize)
{
IGKCircularBuffer buffer;
buffer.elementCount = 0;
buffer.allocatedCount = dataLength / elementSize;
buffer.maximumCount = maximumCount;
buffer.elementSize = elementSize;
buffer.items = malloc(dataLength * elementSize);
memcpy(buffer.items, data, dataLength);
buffer.oldestElement = -1;
buffer.youngestElement = -1;
return buffer;
}
//Create an empty buffer with a specified size
IGKCircularBuffer IGKCircularBufferCreate(CFIndex maximumCount, CFIndex elementSize, CFIndex initialSize)
{
IGKCircularBuffer buffer;
buffer.elementCount = 0;
buffer.allocatedCount = initialSize;
buffer.maximumCount = maximumCount;
buffer.elementSize = elementSize;
buffer.items = malloc(initialSize * elementSize);
buffer.oldestElement = -1;
buffer.youngestElement = -1;
return buffer;
}
void* IGKCircularBufferElementAt(IGKCircularBuffer buffer, CFIndex index)
{
return buffer.items + index * buffer.elementSize;
}
//Add elementSize bytes of *pointerToData to the buffer.
void IGKCircularBufferAdd(IGKCircularBuffer buffer, void* pointerToData)
{
//If the buffer needs to grow
if (buffer.elementCount + 1 > buffer.allocatedCount && buffer.allocatedCount < buffer.maximumCount)
{
//Work out a new size for the buffer
CFIndex newCount;
if (buffer.allocatedCount * 2 < buffer.maximumCount)
newCount = buffer.allocatedCount * 2;
else
newCount = buffer.maximumCount;
//Allocate some new memory
void *newItems = NULL;
newItems = reallocf(buffer.items, buffer.elementSize * newCount);
if (!newItems)
{
//Do something if reallocf fails
return;
}
//Set our variables
buffer.items = newItems;
buffer.allocatedCount = newCount;
}
//Is there space for another element?
_Bool hasSpace = buffer.allocatedCount >= buffer.elementCount + 1;
//If there is space, simply copy over the data and continue on our way
if (hasSpace)
{
//Get the slot
void* emptySlot = IGKCircularBufferElementAt(buffer, buffer.elementCount);
//Copy the data
memcpy(emptySlot, pointerToData, buffer.elementSize);
//Set variables
buffer.youngestElement = buffer.elementCount;
buffer.elementCount += 1;
//If we haven't set an oldest element yet (ie the buffer was empty), we must do so
if (buffer.oldestElement == -1)
buffer.oldestElement = buffer.youngestElement;
}
//If there's not enough space, we have to make space
else
{
//We insert at oldestElement
void* vacatedSlot = IGKCircularBufferElementAt(buffer, buffer.oldestElement);
//Since we're inserting at oldestElement, youngestElement becomes oldestElement
buffer.youngestElement = buffer.oldestElement;
//oldestElement gets incremented modulo maximumCount
buffer.oldestElement = (buffer.oldestElement + 1) % buffer.maximumCount;
//Copy the memory into the vacated slot
memcpy(vacatedSlot, pointerToData, buffer.elementSize);
}
//We're done!
}
//Get a pointer to the raw data and the length of the data, eg to pass to NSData
void* IGKCircularBufferRawData(IGKCircularBuffer buffer)
{
return buffer.items;
}
CFIndex IGKCircularBufferRawDataLength(IGKCircularBuffer buffer)
{
if (buffer.items)
return buffer.elementCount * buffer.elementSize;
return 0;
}
//Get data in a format suitable for writing out to disk
NSData* IGKCircularBufferOrderedData(IGKCircularBuffer buffer)
{
NSMutableData *data = [[NSMutableData alloc] initWithCapacity:IGKCircularBufferOrderedDataLength(buffer)];
if (buffer.elementCount == 0)
return data;
for (CFIndex i = buffer.oldestElement; ; i = (i + 1) % buffer.elementCount)
{
if (buffer.items + i == NULL)
continue;
[data appendBytes:buffer.items + i * buffer.elementSize length:buffer.elementSize];
if (i == buffer.youngestElement)
break;
}
return data;
}
CFIndex IGKCircularBufferOrderedDataLength(IGKCircularBuffer buffer)
{
if (buffer.items)
return buffer.elementCount * buffer.elementSize;
return 0;
}
//Delete the buffer
void IGKCircularBufferFree(IGKCircularBuffer buffer)
{
if (buffer.items)
free(buffer.items);
}