aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-01-22 14:16:30 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-01-22 14:18:19 +0100
commitdba60d8b985247be7f0b0eef0bc0cefef651c824 (patch)
tree853afca0b32884c279e1a949313873f9c0d649d6
parentStart using trie's instead of hash-maps. (diff)
downloadcalp-dba60d8b985247be7f0b0eef0bc0cefef651c824.tar.gz
calp-dba60d8b985247be7f0b0eef0bc0cefef651c824.tar.xz
Fix crash.
-rw-r--r--macro.h9
-rw-r--r--parse.c4
-rw-r--r--strbuf.c16
-rw-r--r--strbuf.h3
-rw-r--r--trie.h1
-rw-r--r--trie.inc13
-rw-r--r--vcal.c6
-rw-r--r--vcal.h2
8 files changed, 37 insertions, 17 deletions
diff --git a/macro.h b/macro.h
index 89178cb3..ce2bfd21 100644
--- a/macro.h
+++ b/macro.h
@@ -63,4 +63,13 @@
T N; \
CONSTRUCT(T, & N, __VA_ARGS__);
+/* Destructor for type */
+#define FREE(T) TP(T, __free)
+
+/* Call destructor for type, and free object */
+#define FFREE(T, N) do { FREE(T)(N); free(N); } while (0)
+
+/* Declare destructor */
+#define FREE_DECL(T) TP(T, __free) (T* this)
+
#endif /* MACRO_H */
diff --git a/parse.c b/parse.c
index 83693e8c..557046b2 100644
--- a/parse.c
+++ b/parse.c
@@ -128,8 +128,8 @@ int parse_file(FILE* f, vcalendar* cal) {
strbuf_copy(&cline.val, &str);
*strbuf_cur(&cline.val) = 0;
}
- strbuf_free(&str);
- content_line_free(&cline);
+ FREE(strbuf)(&str);
+ FREE(content_line)(&cline);
return 0;
}
diff --git a/strbuf.c b/strbuf.c
index f5680a22..b37bb77a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -23,6 +23,9 @@ int CONSTRUCTOR_DECL(strbuf, size_t len) {
return 0;
}
+/*
+ * TODO check if this is the problem.
+ */
int strbuf_realloc(strbuf* str, size_t len) {
#ifdef SAFE_STR
if (str->mem == NULL || str->alloc == 0) {
@@ -35,18 +38,23 @@ int strbuf_realloc(strbuf* str, size_t len) {
return 0;
}
-int strbuf_free(strbuf* str) {
+// int strbuf_free(strbuf* str) {
+int FREE_DECL(strbuf) {
#ifdef SAFE_STR
- if (str->alloc == 0 || str->mem == NULL) {
+ if (this->alloc == 0 || this->mem == NULL) {
ERR("String not allocated");
return 1;
}
#endif
- free (str->mem);
- str->alloc = 0;
+ // fprintf(stderr, "Memmory = %p | %4lu | %s\n", this->mem, this->alloc, this->mem);
+ free (this->mem);
+ this->alloc = 0;
return 0;
}
+/*
+ * TODO this should do bounds check
+ */
int strbuf_append(strbuf* s, char c) {
s->mem[s->len] = c;
s->ptr = ++s->len;
diff --git a/strbuf.h b/strbuf.h
index af673693..86098e21 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -34,7 +34,8 @@ int strbuf_realloc(strbuf* str, size_t len);
/*
* Free's contents of str, but keeps str.
*/
-int strbuf_free(strbuf* str);
+// int strbuf_free(strbuf* str);
+int FREE_DECL(strbuf);
/*
* Copy contents from src to dest.
diff --git a/trie.h b/trie.h
index 9d8b1ec0..52e1072d 100644
--- a/trie.h
+++ b/trie.h
@@ -22,7 +22,6 @@ typedef struct TRIE_NODE(TYPE) {
char c;
TYPE* value;
struct TRIE_NODE(TYPE)* next;
- /* child == NULL means leaf? */
struct TRIE_NODE(TYPE)* child;
} TRIE_NODE(TYPE);
diff --git a/trie.inc b/trie.inc
index 96765c2f..a3207bb1 100644
--- a/trie.inc
+++ b/trie.inc
@@ -4,15 +4,18 @@
#include "err.h"
-int TRIE_INIT(TYPE) ( TRIE(TYPE)* trie ) {
- NEW(TRIE_NODE(TYPE), t, '\0');
- trie->root = t;
+int CONSTRUCTOR_DECL ( TRIE(TYPE) ) {
+ // NEW(TRIE_NODE(TYPE), t, '\0');
+ TRIE_NODE(TYPE)* t = malloc(sizeof(*t));
+ CONSTRUCT(TRIE_NODE(content_line), t, '\0');
+ this->root = t;
return 0;
}
int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), char c) {
this->c = c;
- this->next = NULL;
+ this->value = NULL;
+ this->next = NULL;
this->child = NULL;
return 0;
}
@@ -97,7 +100,7 @@ TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ) {
int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node ) {
if (node == NULL) return 0;
- if (node->value != NULL) free (node->value);
+ if (node->value != NULL) FFREE(TYPE, node->value);
if (node->next != NULL) TRIE_NODE_FREE(TYPE)(node->next);
if (node->child != NULL) TRIE_NODE_FREE(TYPE)(node->child);
return 0;
diff --git a/vcal.c b/vcal.c
index ceb5980b..c751080e 100644
--- a/vcal.c
+++ b/vcal.c
@@ -48,9 +48,9 @@ int content_line_copy (content_line* dest, content_line* src) {
return 0;
}
-int content_line_free (content_line* c) {
- strbuf_free(&c->key);
- strbuf_free(&c->val);
+int FREE_DECL(content_line) {
+ FREE(strbuf)(&this->key);
+ FREE(strbuf)(&this->val);
// TODO remaining fields
diff --git a/vcal.h b/vcal.h
index 0876e0a9..c7bc0cd8 100644
--- a/vcal.h
+++ b/vcal.h
@@ -44,7 +44,7 @@ int CONSTRUCTOR_DECL(vevent, int init_size);
int CONSTRUCTOR_DECL(content_line);
int CONSTRUCTOR_DECL(content_line, int keylen, int vallen);
-int content_line_free (content_line* c);
+int FREE_DECL(content_line);
int content_line_copy (content_line* dest, content_line* src);
content_line* get_property (vevent* ev, char* key);