aboutsummaryrefslogtreecommitdiffstats
path: root/src
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
parentfbc22f39090802845c3a046c3e10bae63d6af6cc (diff)
downloadblogc-86b551fdf2a8bf5c6e3cebcc463ee830d65ced94.tar.gz
blogc-86b551fdf2a8bf5c6e3cebcc463ee830d65ced94.tar.bz2
blogc-86b551fdf2a8bf5c6e3cebcc463ee830d65ced94.zip
safe mallocs are better :)
Diffstat (limited to 'src')
-rw-r--r--src/error.c2
-rw-r--r--src/source-parser.c2
-rw-r--r--src/template-parser.c6
-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
7 files changed, 37 insertions, 11 deletions
diff --git a/src/error.c b/src/error.c
index 8475d66..8e19e6e 100644
--- a/src/error.c
+++ b/src/error.c
@@ -21,7 +21,7 @@
blogc_error_t*
blogc_error_new(blogc_error_type_t type, const char *msg)
{
- blogc_error_t *err = malloc(sizeof(blogc_error_t));
+ blogc_error_t *err = b_malloc(sizeof(blogc_error_t));
err->type = type;
err->msg = b_strdup(msg);
return err;
diff --git a/src/source-parser.c b/src/source-parser.c
index 7cace0c..68cf9db 100644
--- a/src/source-parser.c
+++ b/src/source-parser.c
@@ -136,7 +136,7 @@ blogc_source_parse(const char *src, size_t src_len, blogc_error_t **err)
return NULL;
}
- blogc_source_t *rv = malloc(sizeof(blogc_source_t));
+ blogc_source_t *rv = b_malloc(sizeof(blogc_source_t));
rv->config = config;
rv->content = content;
diff --git a/src/template-parser.c b/src/template-parser.c
index d176288..99e17d3 100644
--- a/src/template-parser.c
+++ b/src/template-parser.c
@@ -70,7 +70,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err)
case TEMPLATE_START:
if (last) {
- stmt = malloc(sizeof(blogc_template_stmt_t));
+ stmt = b_malloc(sizeof(blogc_template_stmt_t));
stmt->type = type;
stmt->value = b_strndup(src + start, src_len - start);
stmts = b_slist_append(stmts, stmt);
@@ -89,7 +89,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err)
else
state = TEMPLATE_VARIABLE_START;
if (end > start) {
- stmt = malloc(sizeof(blogc_template_stmt_t));
+ stmt = b_malloc(sizeof(blogc_template_stmt_t));
stmt->type = type;
stmt->value = b_strndup(src + start, end - start);
stmts = b_slist_append(stmts, stmt);
@@ -313,7 +313,7 @@ blogc_template_parse(const char *src, size_t src_len, blogc_error_t **err)
case TEMPLATE_CLOSE_BRACKET:
if (c == '}') {
- stmt = malloc(sizeof(blogc_template_stmt_t));
+ stmt = b_malloc(sizeof(blogc_template_stmt_t));
stmt->type = type;
stmt->value = NULL;
if (end > start)
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 */