MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / ensure

Function ensure

external/cJSON/cJSON.c:490–573  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

488
489/* realloc printbuffer if necessary to have at least "needed" bytes more */
490static unsigned char* ensure(printbuffer * const p, size_t needed)
491{
492 unsigned char *newbuffer = NULL;
493 size_t newsize = 0;
494
495 if ((p == NULL) || (p->buffer == NULL))
496 {
497 return NULL;
498 }
499
500 if ((p->length > 0) && (p->offset >= p->length))
501 {
502 /* make sure that offset is valid */
503 return NULL;
504 }
505
506 if (needed > INT_MAX)
507 {
508 /* sizes bigger than INT_MAX are currently not supported */
509 return NULL;
510 }
511
512 needed += p->offset + 1;
513 if (needed <= p->length)
514 {
515 return p->buffer + p->offset;
516 }
517
518 if (p->noalloc) {
519 return NULL;
520 }
521
522 /* calculate new buffer size */
523 if (needed > (INT_MAX / 2))
524 {
525 /* overflow of int, use INT_MAX if possible */
526 if (needed <= INT_MAX)
527 {
528 newsize = INT_MAX;
529 }
530 else
531 {
532 return NULL;
533 }
534 }
535 else
536 {
537 newsize = needed * 2;
538 }
539
540 if (p->hooks.reallocate != NULL)
541 {
542 /* reallocate with realloc if available */
543 newbuffer = (unsigned char*)p->hooks.reallocate(p->buffer, newsize);
544 if (newbuffer == NULL)
545 {
546 p->hooks.deallocate(p->buffer);
547 p->length = 0;

Callers 5

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

Calls 2

deallocateMethod · 0.45
allocateMethod · 0.45

Tested by

no test coverage detected