aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2015-04-19 01:17:54 -0300
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2015-04-19 01:17:54 -0300
commit86b551fdf2a8bf5c6e3cebcc463ee830d65ced94 (patch)
tree2ea95245b5d1e82d8a7197206e2b640aa54119aa /src/utils
parentfbc22f39090802845c3a046c3e10bae63d6af6cc (diff)
downloadblogc-86b551fdf2a8bf5c6e3cebcc463ee830d65ced94.tar.gz
blogc-86b551fdf2a8bf5c6e3cebcc463ee830d65ced94.tar.bz2
blogc-86b551fdf2a8bf5c6e3cebcc463ee830d65ced94.zip
safe mallocs are better :)
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/mem.c24
-rw-r--r--src/utils/strings.c6
-rw-r--r--src/utils/trie.c6
-rw-r--r--src/utils/utils.h2
4 files changed, 32 insertions, 6 deletions
diff --git a/src/utils/mem.c b/src/utils/mem.c
new file mode 100644
index 0000000..04201ff
--- /dev/null
+++ b/src/utils/mem.c
@@ -0,0 +1,24 @@
+/*
+ * blogc: A blog compiler.
+ * Copyright (C) 2014-2015 Rafael G. Martins <rafael@rafaelmartins.eng.br>
+ *
+ * This program can be distributed under the terms of the BSD License.
+ * See the file COPYING.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "utils.h"
+
+
+void*
+b_malloc(size_t size)
+{
+ // simple things simple!
+ void *rv = malloc(size);
+ if (rv == NULL) {
+ fprintf(stderr, "fatal error: Failed to allocate memory!\n");
+ exit(1);
+ }
+ return rv;
+}
diff --git a/src/utils/strings.c b/src/utils/strings.c
index b3a19cc..04ccc7a 100644
--- a/src/utils/strings.c
+++ b/src/utils/strings.c
@@ -125,14 +125,14 @@ b_str_split(const char *str, char c, unsigned int max_pieces)
{
if (!str)
return NULL;
- char **rv = malloc(sizeof(char*));
+ char **rv = b_malloc(sizeof(char*));
unsigned int i, start = 0, count = 0;
for (i = 0; i < strlen(str) + 1; i++) {
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[count] = malloc(i - start + 1);
+ rv[count] = b_malloc(i - start + 1);
memcpy(rv[count], str + start, i - start);
rv[count++][i - start] = '\0';
start = i + 1;
@@ -198,7 +198,7 @@ b_strv_length(char **strv)
b_string_t*
b_string_new(void)
{
- b_string_t* rv = malloc(sizeof(b_string_t));
+ b_string_t* rv = b_malloc(sizeof(b_string_t));
rv->str = NULL;
rv->len = 0;
rv->allocated_len = 0;
diff --git a/src/utils/trie.c b/src/utils/trie.c
index b92573f..d33c600 100644
--- a/src/utils/trie.c
+++ b/src/utils/trie.c
@@ -15,7 +15,7 @@
b_trie_t*
b_trie_new(void (*free_func)(void *ptr))
{
- b_trie_t *trie = malloc(sizeof(b_trie_t));
+ b_trie_t *trie = b_malloc(sizeof(b_trie_t));
trie->root = NULL;
trie->free_func = free_func;
return trie;
@@ -59,7 +59,7 @@ b_trie_insert(b_trie_t *trie, const char *key, void *data)
while (1) {
if (trie->root == NULL || (parent != NULL && parent->child == NULL)) {
- current = malloc(sizeof(b_trie_node_t));
+ current = b_malloc(sizeof(b_trie_node_t));
current->key = *key;
current->data = NULL;
current->next = NULL;
@@ -85,7 +85,7 @@ b_trie_insert(b_trie_t *trie, const char *key, void *data)
if (previous == NULL || parent != NULL)
goto clean;
- current = malloc(sizeof(b_trie_node_t));
+ current = b_malloc(sizeof(b_trie_node_t));
current->key = *key;
current->data = NULL;
current->next = NULL;
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 55b9c59..7e61a67 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -69,4 +69,6 @@ void* b_trie_lookup(b_trie_t *trie, const char *key);
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);
+
#endif /* _UTILS_UTILS_H */