diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-01-22 14:16:30 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2019-01-22 14:18:19 +0100 |
commit | dba60d8b985247be7f0b0eef0bc0cefef651c824 (patch) | |
tree | 853afca0b32884c279e1a949313873f9c0d649d6 | |
parent | Start using trie's instead of hash-maps. (diff) | |
download | calp-dba60d8b985247be7f0b0eef0bc0cefef651c824.tar.gz calp-dba60d8b985247be7f0b0eef0bc0cefef651c824.tar.xz |
Fix crash.
-rw-r--r-- | macro.h | 9 | ||||
-rw-r--r-- | parse.c | 4 | ||||
-rw-r--r-- | strbuf.c | 16 | ||||
-rw-r--r-- | strbuf.h | 3 | ||||
-rw-r--r-- | trie.h | 1 | ||||
-rw-r--r-- | trie.inc | 13 | ||||
-rw-r--r-- | vcal.c | 6 | ||||
-rw-r--r-- | vcal.h | 2 |
8 files changed, 37 insertions, 17 deletions
@@ -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 */ @@ -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; } @@ -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; @@ -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. @@ -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); @@ -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; @@ -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 @@ -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); |