aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2015-07-19 21:54:57 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2015-07-19 21:54:57 -0300
commit3468762d7a830bd68dafdc4c8d489f965fd48b42 (patch)
tree28b34e92f36275f37280f8816dee266275427b80 /src/utils
parent8aaa346994fe87ce0badfd3ac0c2ee302c86716c (diff)
downloadblogc-3468762d7a830bd68dafdc4c8d489f965fd48b42.tar.gz
blogc-3468762d7a830bd68dafdc4c8d489f965fd48b42.tar.bz2
blogc-3468762d7a830bd68dafdc4c8d489f965fd48b42.zip
utils/mem: added safe realloc
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mem.c14
-rw-r--r--src/utils/strings.c8
-rw-r--r--src/utils/utils.h1
3 files changed, 19 insertions, 4 deletions
diff --git a/src/utils/mem.c b/src/utils/mem.c
index 7befc73..7c5e0a2 100644
--- a/src/utils/mem.c
+++ b/src/utils/mem.c
@@ -26,3 +26,17 @@ b_malloc(size_t size)
}
return rv;
}
+
+
+void*
+b_realloc(void *ptr, size_t size)
+{
+ // simple things even simpler :P
+ void *rv = realloc(ptr, size);
+ if (rv == NULL && size != 0) {
+ fprintf(stderr, "fatal error: Failed to reallocate memory!\n");
+ free(ptr);
+ exit(1);
+ }
+ return rv;
+}
diff --git a/src/utils/strings.c b/src/utils/strings.c
index 502cfd4..40174a1 100644
--- a/src/utils/strings.c
+++ b/src/utils/strings.c
@@ -137,14 +137,14 @@ b_str_split(const char *str, char c, unsigned int max_pieces)
if (str[0] == '\0')
break;
if ((str[i] == c && (!max_pieces || count + 1 < max_pieces)) || str[i] == '\0') {
- rv = realloc(rv, (count + 1) * sizeof(char*));
+ rv = b_realloc(rv, (count + 1) * sizeof(char*));
rv[count] = b_malloc(i - start + 1);
memcpy(rv[count], str + start, i - start);
rv[count++][i - start] = '\0';
start = i + 1;
}
}
- rv = realloc(rv, (count + 1) * sizeof(char*));
+ rv = b_realloc(rv, (count + 1) * sizeof(char*));
rv[count] = NULL;
return rv;
}
@@ -238,7 +238,7 @@ b_string_append_len(b_string_t *str, const char *suffix, size_t len)
str->len += len;
if (str->len + 1 > str->allocated_len) {
str->allocated_len = (((str->len + 1) / B_STRING_CHUNK_SIZE) + 1) * B_STRING_CHUNK_SIZE;
- str->str = realloc(str->str, str->allocated_len);
+ str->str = b_realloc(str->str, str->allocated_len);
}
memcpy(str->str + old_len, suffix, len);
str->str[str->len] = '\0';
@@ -262,7 +262,7 @@ b_string_append_c(b_string_t *str, char c)
str->len += 1;
if (str->len + 1 > str->allocated_len) {
str->allocated_len = (((str->len + 1) / B_STRING_CHUNK_SIZE) + 1) * B_STRING_CHUNK_SIZE;
- str->str = realloc(str->str, str->allocated_len);
+ str->str = b_realloc(str->str, str->allocated_len);
}
str->str[old_len] = c;
str->str[str->len] = '\0';
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 8029a6c..5a1505b 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -72,5 +72,6 @@ unsigned int b_trie_size(b_trie_t *trie);
void b_trie_foreach(b_trie_t *trie, void (*func)(const char *key, void *data));
void* b_malloc(size_t size);
+void* b_realloc(void *ptr, size_t size);
#endif /* _UTILS_UTILS_H */