@@ -36,13 +36,10 @@ CIRCLE_internal_queue_t* CIRCLE_internal_queue_init(void)
36
36
qp -> str_count = (int32_t ) str_count ;
37
37
38
38
/* Base address of string pool */
39
- qp -> base = (char * ) malloc (sizeof (char ) * \
40
- CIRCLE_MAX_STRING_LEN * \
41
- str_count );
39
+ qp -> bytes = sizeof (char ) * CIRCLE_MAX_STRING_LEN * str_count ;
40
+ qp -> base = (char * ) malloc (qp -> bytes );
42
41
qp -> count = 0 ;
43
- qp -> head = 0 ;
44
- qp -> end = qp -> base + \
45
- (CIRCLE_MAX_STRING_LEN * str_count );
42
+ qp -> head = 0 ;
46
43
47
44
/* String pointer array */
48
45
qp -> strings = (uintptr_t * ) malloc (sizeof (uintptr_t ) * \
@@ -97,8 +94,7 @@ void CIRCLE_internal_queue_dump(CIRCLE_internal_queue_t* qp)
97
94
uint32_t i = 0 ;
98
95
char * p = qp -> base ;
99
96
100
- while (p ++ != (qp -> base + qp -> strings [qp -> count - 1 ] + \
101
- strlen (qp -> base + qp -> strings [qp -> count - 1 ]))) {
97
+ while (p ++ < (qp -> base + qp -> bytes )) {
102
98
if (i ++ % 120 == 0 ) {
103
99
LOG (CIRCLE_LOG_DBG , "%c" , * p );
104
100
}
@@ -155,13 +151,17 @@ int8_t CIRCLE_internal_queue_str_extend(CIRCLE_internal_queue_t* qp, \
155
151
* Extend the circle queue size
156
152
*
157
153
*/
158
- int8_t CIRCLE_internal_queue_extend (CIRCLE_internal_queue_t * qp )
154
+ int8_t CIRCLE_internal_queue_extend (CIRCLE_internal_queue_t * qp , size_t new_size )
159
155
{
160
- size_t current = (size_t )(qp -> end - qp -> base );
161
- current += ((size_t )sysconf (_SC_PAGESIZE )) * 4096 ;
156
+ size_t current = qp -> bytes ;
157
+
158
+ /* TODO: check for overflow */
159
+ while (current < new_size ) {
160
+ current += ((size_t )sysconf (_SC_PAGESIZE )) * 4096 ;
161
+ }
162
162
163
163
LOG (CIRCLE_LOG_DBG , "Reallocing queue from [%zd] to [%zd] [%p] -> [%p]." , \
164
- ( qp -> end - qp -> base ) , current , qp -> base , qp -> base + current );
164
+ qp -> bytes , current , qp -> base , qp -> base + current );
165
165
166
166
qp -> base = (char * ) realloc (qp -> base , current );
167
167
@@ -170,7 +170,7 @@ int8_t CIRCLE_internal_queue_extend(CIRCLE_internal_queue_t* qp)
170
170
return -1 ;
171
171
}
172
172
173
- qp -> end = qp -> base + current ;
173
+ qp -> bytes = current ;
174
174
return 0 ;
175
175
}
176
176
@@ -190,40 +190,38 @@ int8_t CIRCLE_internal_queue_push(CIRCLE_internal_queue_t* qp, char* str)
190
190
}
191
191
192
192
/* TODO: check that real_len fits within uint32_t */
193
- size_t real_len = strlen (str );
193
+ size_t real_len = strlen (str ) + 1 ;
194
194
uint32_t len = (uint32_t ) real_len ;
195
195
196
196
if (len <= 0 ) {
197
197
LOG (CIRCLE_LOG_ERR , "Attempted to push an empty string onto a queue." );
198
198
return -1 ;
199
199
}
200
200
201
- if (qp -> count > qp -> str_count ) {
201
+ if (len > CIRCLE_MAX_STRING_LEN ) {
202
+ LOG (CIRCLE_LOG_ERR , \
203
+ "Attempted to push a value that was larger than expected." );
204
+ return -1 ;
205
+ }
206
+
207
+ if (qp -> count + 1 > qp -> str_count ) {
202
208
LOG (CIRCLE_LOG_DBG , "Extending string array by 4096." );
203
209
204
- if (CIRCLE_internal_queue_str_extend (qp , qp -> count + 4096 ) < 0 ) {
210
+ if (CIRCLE_internal_queue_str_extend (qp , qp -> count + 1 ) < 0 ) {
205
211
return -1 ;
206
212
}
207
213
}
208
214
209
- if (qp -> count > 0 ) {
210
- if (qp -> base + qp -> strings [qp -> count - 1 ] + \
211
- CIRCLE_MAX_STRING_LEN >= qp -> end ) {
212
- LOG (CIRCLE_LOG_DBG , \
213
- "The queue is not large enough to add another value." );
215
+ size_t new_bytes = (size_t )(qp -> head + len ) * sizeof (char );
216
+ if (new_bytes > qp -> bytes ) {
217
+ LOG (CIRCLE_LOG_DBG , \
218
+ "The queue is not large enough to add another value." );
214
219
215
- if (CIRCLE_internal_queue_extend (qp ) < 0 ) {
216
- return -1 ;
217
- }
220
+ if (CIRCLE_internal_queue_extend (qp , new_bytes ) < 0 ) {
221
+ return -1 ;
218
222
}
219
223
}
220
224
221
- if (len > CIRCLE_MAX_STRING_LEN ) {
222
- LOG (CIRCLE_LOG_ERR , \
223
- "Attempted to push a value that was larger than expected." );
224
- return -1 ;
225
- }
226
-
227
225
/* Set our write location to the end of the current strings array. */
228
226
qp -> strings [qp -> count ] = qp -> head ;
229
227
@@ -234,7 +232,7 @@ int8_t CIRCLE_internal_queue_push(CIRCLE_internal_queue_t* qp, char* str)
234
232
* Make head point to the character after the string (strlen doesn't
235
233
* include a trailing null).
236
234
*/
237
- qp -> head = qp -> head + strlen ( qp -> base + qp -> head ) + 1 ;
235
+ qp -> head += len ;
238
236
239
237
/* Make the head point to the next available memory */
240
238
qp -> count ++ ;
@@ -269,8 +267,10 @@ int8_t CIRCLE_internal_queue_pop(CIRCLE_internal_queue_t* qp, char* str)
269
267
}
270
268
271
269
/* Copy last element into str */
272
- strcpy (str , qp -> base + qp -> strings [qp -> count - 1 ]);
273
- qp -> count = qp -> count - 1 ;
270
+ uintptr_t current = qp -> strings [qp -> count - 1 ];
271
+ strcpy (str , qp -> base + current );
272
+ qp -> head = current ;
273
+ qp -> count -- ;
274
274
275
275
return 0 ;
276
276
}
0 commit comments