diff options
author | Thomas White <taw@physics.org> | 2024-01-16 12:03:15 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2024-02-06 16:59:34 +0100 |
commit | 4ad424f132dc3311502567e58b695fecdeb10106 (patch) | |
tree | 94fc0c1fe085de5f7e8553dced1f1b2a235fdf92 /libcrystfel/src/utils.c | |
parent | 5960cc81e3e35e4d38fdd720680c98bef070d695 (diff) |
Use libcrystfel memory allocation routines everywhere
Diffstat (limited to 'libcrystfel/src/utils.c')
-rw-r--r-- | libcrystfel/src/utils.c | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/libcrystfel/src/utils.c b/libcrystfel/src/utils.c index 04e028e9..7bfac487 100644 --- a/libcrystfel/src/utils.c +++ b/libcrystfel/src/utils.c @@ -436,9 +436,9 @@ char *cfstrndup(const char *s, size_t n) void *srealloc(void *arr, size_t new_size) { - void *new_arr = realloc(arr, new_size); + void *new_arr = cfrealloc(arr, new_size); if ( new_arr == NULL ) { - free(arr); + cffree(arr); return NULL; } else { return new_arr; @@ -448,7 +448,7 @@ void *srealloc(void *arr, size_t new_size) char *safe_strdup(const char *in) { if ( in == NULL ) return NULL; - return strdup(in); + return cfstrdup(in); } @@ -632,9 +632,9 @@ static int assplode_extract(char ***pbits, int n, size_t n_captured, size_t start, const char *a) { char **bits = *pbits; - bits = realloc(bits, sizeof(char *)*(n+1)); + bits = cfrealloc(bits, sizeof(char *)*(n+1)); assert(bits != NULL); - bits[n] = malloc(n_captured+1); + bits[n] = cfmalloc(n_captured+1); assert(bits[n] != NULL); memcpy(bits[n], a+start, n_captured); bits[n][n_captured] = '\0'; @@ -649,8 +649,8 @@ static int assplode_extract(char ***pbits, int n, size_t n_captured, * deliminators. * Store each segment in bits[0...n] where n is the number of segments and is * the return value. pbits = &bits - * Each segment needs to be freed with free() when finished with. - * The array of bits also needs to be freed with free() when finished with, + * Each segment needs to be freed with cffree() when finished with. + * The array of bits also needs to be freed with cffree() when finished with, * unless n=0 in which case bits==NULL */ int assplode(const char *a, const char *delims, char ***pbits, @@ -742,9 +742,9 @@ char *check_prefix(char *prefix) " with a slash. I'm going to add it for you.\n", prefix); STATUS("If this isn't what you want, run with --no-check-prefix.\n"); len = strlen(prefix)+2; - new = malloc(len); + new = cfmalloc(len); snprintf(new, len, "%s/", prefix); - free(prefix); + cffree(prefix); return new; } @@ -755,7 +755,7 @@ char *safe_basename(const char *in) char *cpy; char *res; - cpy = strdup(in); + cpy = cfstrdup(in); /* Get rid of any trailing slashes */ for ( i=strlen(cpy)-1; i>0; i-- ) { @@ -774,10 +774,10 @@ char *safe_basename(const char *in) } } - res = strdup(cpy+i); + res = cfstrdup(cpy+i); /* If we didn't find a previous slash, i==0 so res==cpy */ - free(cpy); + cffree(cpy); return res; } @@ -962,7 +962,7 @@ char *load_entire_file(const char *filename) return NULL; } - contents = malloc(statbuf.st_size+1); + contents = cfmalloc(statbuf.st_size+1); if ( contents == NULL ) { ERROR("Failed to allocate memory for file\n"); return NULL; @@ -971,14 +971,14 @@ char *load_entire_file(const char *filename) fh = fopen(filename, "r"); if ( fh == NULL ) { ERROR("Failed to open file '%s'\n", filename); - free(contents); + cffree(contents); return NULL; } if ( fread(contents, 1, statbuf.st_size, fh) != statbuf.st_size ) { ERROR("Failed to read file '%s'\n", filename); fclose(fh); - free(contents); + cffree(contents); return NULL; } contents[statbuf.st_size] = '\0'; |