aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-04 18:23:51 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 18:16:17 +0100
commit14b33ae9c00f74a24a00aaece88b22af0955379e (patch)
treecee997634ecb0977e8f63fadfd6d00fdfdf89439
parentNormalize and improve INIT & FREE macros. (diff)
downloadcalp-14b33ae9c00f74a24a00aaece88b22af0955379e.tar.gz
calp-14b33ae9c00f74a24a00aaece88b22af0955379e.tar.xz
Fix most memmory errors.
-rw-r--r--Makefile2
-rwxr-xr-xcode.scm1
-rw-r--r--linked_list.h6
-rw-r--r--linked_list.inc.h68
-rw-r--r--macro.h11
-rw-r--r--main.c2
-rw-r--r--parse.c5
-rw-r--r--trie.inc.h4
-rw-r--r--vcal.c26
-rw-r--r--vcal.h9
10 files changed, 90 insertions, 44 deletions
diff --git a/Makefile b/Makefile
index 57e78210..f8e07e8a 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CC := gcc
OBJDIR = obj
-CPPFLAGS = -DSAFE_STR -DSAFE_HASH
+CPPFLAGS = -DSAFE_STR # -DSAFE_HASH
CFLAGS = $(CPPFLAGS) \
-std=gnu99 -Wall -Wextra -pedantic \
-ggdb -fPIC \
diff --git a/code.scm b/code.scm
index cca7cdb3..66c61dfc 100755
--- a/code.scm
+++ b/code.scm
@@ -14,4 +14,3 @@
((>= i (calendar-size v)))
(format #t "~3d | ~a~%"
i (calendar-get-attr v i "summary")))
-
diff --git a/linked_list.h b/linked_list.h
index d1354e02..90a110bf 100644
--- a/linked_list.h
+++ b/linked_list.h
@@ -25,10 +25,11 @@ typedef struct {
} LLIST(TYPE);
INIT_F ( LLIST(TYPE) );
-FREE_F( LLIST(TYPE) );
+FREE_F ( LLIST(TYPE) );
INIT_F ( LINK(TYPE) );
INIT_F ( LINK(TYPE), TYPE* val );
+FREE_F ( LINK(TYPE) );
int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val);
@@ -36,4 +37,7 @@ int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src );
int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new );
+int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist );
+int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist );
+
#endif /* TYPE */
diff --git a/linked_list.inc.h b/linked_list.inc.h
index b6942a94..be51e582 100644
--- a/linked_list.inc.h
+++ b/linked_list.inc.h
@@ -6,28 +6,35 @@ INIT_F ( LLIST(TYPE) ) {
this->length = 0;
NEW(LINK(TYPE), head);
NEW(LINK(TYPE), tail);
- this->head = head;
- this->tail = tail;
- head->after = tail;
+ this->head = head;
+ this->tail = tail;
+ head->after = tail;
tail->before = head;
- this->cur = head;
+ this->cur = head;
+ return 0;
+}
+
+FREE_F (LINK(TYPE)) {
+ if (this->before != NULL) this->before->after = NULL;
+ if (this->after != NULL) this->after->before = NULL;
+ // TODO how much of value do I really wanna free?
+ // Should I implement some form of shared pointer?
+ if (this->value != NULL) FFREE(TYPE, this->value);
return 0;
}
FREE_F( LLIST(TYPE) ) {
- LINK(TYPE) *node, *next;
- node = this->head->after;
- while (node->after != NULL) {
- next = node->after;
- if (node->value != NULL) {
- FFREE(TYPE, node->value);
- }
- // free(node);
- node = next;
+
+ LINK(TYPE) *n, *next;
+ n = this->head;
+ while ( n != NULL ) {
+ next = n->after;
+ FFREE(LINK(TYPE), n);
+ n = next;
}
- free (this->head);
- free (this->tail);
- this->length = 0;
+
+ this->length = -1;
+
return 0;
}
@@ -46,16 +53,16 @@ INIT_F ( LINK(TYPE), TYPE* val ) {
}
int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val) {
- NEW(LINK(TYPE), l, val);
+ NEW(LINK(TYPE), link, val);
- l->after = lst->head->after;
- lst->head->after = l;
- l->after->before = l;
- l->before = lst->head;
+ link->after = lst->head->after;
+ lst->head->after = link;
+ link->after->before = link;
+ link->before = lst->head;
++lst->length;
// TODO do I want to change that?
- lst->cur = l;
+ lst->cur = link;
return 0;
}
@@ -76,6 +83,8 @@ int DEEP_COPY(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* src ) {
/*
* TODO
* this or cons links wrong, creating infinite loops.
+ * TODO
+ * or maybe not
*/
int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new ) {
@@ -85,7 +94,7 @@ int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new ) {
dest->tail = new->tail;
dest->length += new->length;
- // free(old_tail);
+ free(old_tail);
// free(new->head);
/*
@@ -96,4 +105,17 @@ int APPEND(LLIST(TYPE)) ( LLIST(TYPE)* dest, LLIST(TYPE)* new ) {
return 0;
}
+int SIZE(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
+ int size = 0;
+ LINK(TYPE)* l = llist->head->after;
+ while (l->after != NULL) {
+ ++size;
+ }
+ return size;
+}
+
+int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) {
+ return llist->head->after == llist->tail;
+}
+
#endif /* TYPE */
diff --git a/macro.h b/macro.h
index 82b8e558..67901ebb 100644
--- a/macro.h
+++ b/macro.h
@@ -21,9 +21,6 @@
#define VA_ARGS_NUM_PRIV(P1, P2, P3, P4, P5, P6, Pn, ...) Pn
#define VA_ARGS_NUM(...) VA_ARGS_NUM_PRIV(-1, ## __VA_ARGS__, 5, 4, 3, 2, 1, 0)
-#define NEW_HELPER(T, ARG_COUNT) \
- TP3(T, _init_, ARG_COUNT)
-
/* Constructor type name */
#define __INIT_T(T, C) TP3(T, __init__, C)
@@ -40,6 +37,12 @@
T* N = malloc(sizeof(*N)); \
INIT(T, N, __VA_ARGS__);
+#define RENEW(T, N, ...) do { \
+ N = malloc(sizeof(*N)); \
+ INIT(T, N, __VA_ARGS__); \
+} while (0)
+
+
/* Allocate a new object on the STACK */
#define SNEW(T, N, ...) \
T N; \
@@ -57,5 +60,7 @@
#define DEEP_COPY(T) TP(deep_copy__, T)
#define RESOLVE(T) TP(resolve__, T)
#define APPEND(T) TP(append__, T)
+#define SIZE(T) TP(size__, T)
+#define EMPTY(T) TP(empty__, T)
#endif /* MACRO_H */
diff --git a/main.c b/main.c
index 1d1b20ed..c9fa7fd8 100644
--- a/main.c
+++ b/main.c
@@ -65,7 +65,7 @@ int main (int argc, char* argv[argc]) {
for (int i = 0; i < cline_ptr; i++) {
if (clines[i] != NULL) {
// printf("clines[%i] : [%s] := [%s]\n", i, clines[i]->key.mem, clines[i]->val.mem);
- printf("clines[%i] : [%s] := [%s]\n", i, clines[i]->key.mem, clines[i]->vals.head->value->mem);
+ printf("clines[%i] : [%s] := [%s]\n", i, clines[i]->key.mem, clines[i]->vals.head->after->value->mem);
}
}
diff --git a/parse.c b/parse.c
index 02900e69..68cd4b60 100644
--- a/parse.c
+++ b/parse.c
@@ -75,8 +75,9 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) {
switch (handle_kv(cal, ev, &cline, line, &ctx)) {
case s_event:
- ev = malloc(sizeof(*ev));
- INIT(vevent, ev, fname);
+ RENEW(vevent, ev, fname);
+ // ev = malloc(sizeof(*ev));
+ // INIT(vevent, ev, fname);
break;
}
strbuf_soft_reset(&str);
diff --git a/trie.inc.h b/trie.inc.h
index 14362058..8a817ab6 100644
--- a/trie.inc.h
+++ b/trie.inc.h
@@ -46,7 +46,7 @@ int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
last->child = t;
last = t;
}
- RESOLVE(TYPE)(&last->value, val);
+ last->value = RESOLVE(TYPE)(last->value, val);
// last->value = val;
return 0;
} else if (cur->c == subkey[0]) {
@@ -58,7 +58,7 @@ int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
} else if (subkey[0] == '\0') {
/* Key finished */
// last->value = val;
- RESOLVE(TYPE)(&last->value, val);
+ last->value = RESOLVE(TYPE)(last->value, val);
return 0;
} else if (cur->next != NULL) {
/* This node was not part of the set, but it's sibling might */
diff --git a/vcal.c b/vcal.c
index 64cc1d73..a62c99a4 100644
--- a/vcal.c
+++ b/vcal.c
@@ -26,18 +26,26 @@ INIT_F(vevent, char* filename) {
return 0;
}
-int RESOLVE(content_line)
- (content_line** dest, content_line* new) {
- if (*dest == NULL) *dest = new;
+content_line* RESOLVE(content_line)
+ (content_line* dest, content_line* new) {
- if (strbuf_cmp(&(*dest)->key, &new->key) != 0) {
+ if (dest == NULL) return new;
+
+ if (strbuf_cmp(&dest->key, &new->key) != 0) {
ERR("Can't resolve between these two types");
- return 1;
+ return NULL;
}
- // printf("len = %i\n", dest->vals.length);
- APPEND(LLIST(strbuf))(&(*dest)->vals, &new->vals);
- return 0;
+ /* TODO actuall iterators could be fun */
+ for (LINK(strbuf)* s = new->vals.head->after;
+ s->after != NULL;
+ s = s->after) {
+ LLIST_CONS(strbuf) (&dest->vals, s->value);
+ }
+
+ FREE(content_line)(new);
+
+ return dest;
}
content_line* get_property (vevent* ev, char* key) {
@@ -83,6 +91,8 @@ FREE_F(content_line) {
// FREE(strbuf)(&this->val);
// LLIST_FREE(strbuf)(&this->vals);
FREE(LLIST(strbuf))(&this->vals);
+
+ // TODO this is just for the GC.
for (int i = 0; i < cline_ptr; i++) {
if (clines[i] == this) {
clines[i] = NULL;
diff --git a/vcal.h b/vcal.h
index 03d04805..9123e3fa 100644
--- a/vcal.h
+++ b/vcal.h
@@ -28,8 +28,13 @@ typedef struct {
INIT_F(content_line);
INIT_F(content_line, int keylen, int vallen);
-int RESOLVE(content_line)
- (content_line** orig, content_line* new);
+/*
+ * This takes two content lines, and return a content line that is the
+ * merge of the two.
+ * The function also cleans up the unused memmory between the two.
+ */
+content_line* RESOLVE(content_line)
+ (content_line* dest, content_line* new);
#define TYPE content_line
// #include "hash.h"