Merge pull request #589 from jacereda/realloc-check

Add CARP_REALLOC() so it can be checked with CHECK_ALLOCATIONS
This commit is contained in:
Erik Svedäng 2019-10-15 09:14:29 +02:00 committed by GitHub
commit e703d95063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 6 deletions

View File

@ -38,7 +38,7 @@ size_t getline(char **lineptr, size_t *n, FILE *stream) {
if (new_size < 128) {
new_size = 128;
}
char *new_ptr = realloc(*lineptr, new_size);
char *new_ptr = CARP_REALLOC(*lineptr, new_size);
if (new_ptr == NULL) {
return -1;
}

View File

@ -32,6 +32,7 @@ void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
#define CARP_MALLOC(size) logged_malloc(size)
#define CARP_FREE(ptr) logged_free(ptr)
#define CARP_REALLOC(ptr, size) realloc(ptr, size)
long Debug_memory_MINUS_balance() {
return malloc_balance_counter;
@ -49,8 +50,14 @@ void* CARP_MALLOC(size_t size) {
if (!res) abort();
return res;
}
void* CARP_REALLOC(void * ptr, size_t size) {
void* res = realloc(ptr, size);
if (!res) abort();
return res;
}
#else
#define CARP_MALLOC(size) malloc(size)
#define CARP_REALLOC(ptr, size) realloc(ptr, size)
#endif
#define CARP_FREE(ptr) free(ptr)

View File

@ -442,7 +442,7 @@ void Pattern_internal_update_int_array(Array* a, int value) {
a->len++;
if(a->len > a->capacity) {
a->capacity = a->len * 2;
a->data = realloc(a->data, sizeof(int) * a->capacity);
a->data = CARP_REALLOC(a->data, sizeof(int) * a->capacity);
}
((int*)a->data)[a->len - 1] = value;
}
@ -570,7 +570,7 @@ PatternGMatchRes Pattern_internal_gmatch_aux(PatternGMatchState* gm) {
Array Array_push_back(Array res, Array tmp) {
res.len++;
res.data = realloc(res.data, res.len*sizeof(Array));
res.data = CARP_REALLOC(res.data, res.len*sizeof(Array));
((Array*)res.data)[res.len-1] = tmp;
return res;
}

View File

@ -41,7 +41,7 @@ templateShrinkCheck :: String -> String
templateShrinkCheck var =
unlines [ " if(" ++ var ++ ".len < (" ++ var ++ ".capacity / 4)) {"
," " ++ var ++ ".capacity = " ++ var ++ ".len * 2;"
," " ++ var ++ ".data = realloc(" ++ var ++ ".data, sizeof($a) * " ++ var ++ " .capacity);"
," " ++ var ++ ".data = CARP_REALLOC(" ++ var ++ ".data, sizeof($a) * " ++ var ++ " .capacity);"
, " }"
]
@ -95,7 +95,7 @@ templatePushBack =
," a.len++;"
," if(a.len > a.capacity) {"
," a.capacity = a.len * 2;"
," a.data = realloc(a.data, sizeof($a) * a.capacity);"
," a.data = CARP_REALLOC(a.data, sizeof($a) * a.capacity);"
," }"
," (($a*)a.data)[a.len - 1] = value;"
," return a;"
@ -117,7 +117,7 @@ templatePushBackBang =
," aRef->len++;"
," if(aRef->len > aRef->capacity) {"
," aRef->capacity = aRef->len * 2;"
," aRef->data = realloc(aRef->data, sizeof($a) * aRef->capacity);"
," aRef->data = CARP_REALLOC(aRef->data, sizeof($a) * aRef->capacity);"
," }"
," (($a*)aRef->data)[aRef->len - 1] = value;"
,"}"