MCPcopy Create free account
hub / github.com/OpenSIPS/opensips / ensure

Function ensure

lib/cJSON.c:327–386  ·  view source on GitHub ↗

realloc printbuffer if necessary to have at least "needed" bytes more */

Source from the content-addressed store, hash-verified

325
326/* realloc printbuffer if necessary to have at least "needed" bytes more */
327static unsigned char* ensure(printbuffer *p, size_t needed)
328{
329 unsigned char *newbuffer = NULL;
330 size_t newsize = 0;
331 int written;
332 int flushed = 0;
333
334 if (needed > INT_MAX)
335 {
336 /* sizes bigger than INT_MAX are currently not supported */
337 return NULL;
338 }
339
340 if (!p || !p->buffer)
341 {
342 return NULL;
343 }
344
345retry:
346 if (p->offset + needed <= p->length)
347 {
348 return p->buffer + p->offset;
349 }
350
351 if (p->noalloc) {
352 if (!p->flush)
353 return NULL;
354 /* we will try to flush everything, so we can re-use the same buffer */
355 if (flushed)
356 return NULL; /* TODO: cannot flush what's in here - shall we "remember" the error? */
357 written = (p->flush)(p->buffer, p->offset, p->flush_p);
358 if (written <= 0)
359 return NULL;
360 memmove(p->buffer, p->buffer + written, p->offset - written);
361 p->offset -= written;
362 flushed = 1;
363 goto retry;
364 }
365
366 needed += p->offset;
367 newsize = (size_t) pow2gt((int)needed);
368 newbuffer = (unsigned char*)cJSON_malloc(newsize);
369 if (!newbuffer)
370 {
371 cJSON_free(p->buffer);
372 p->length = 0;
373 p->buffer = NULL;
374
375 return NULL;
376 }
377 if (newbuffer)
378 {
379 memcpy(newbuffer, p->buffer, p->length);
380 }
381 cJSON_free(p->buffer);
382 p->length = newsize;
383 p->buffer = newbuffer;
384

Callers 5

print_numberFunction · 0.85
print_string_ptrFunction · 0.85
print_valueFunction · 0.85
print_arrayFunction · 0.85
print_objectFunction · 0.85

Calls 1

pow2gtFunction · 0.85

Tested by

no test coverage detected