aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 17:51:33 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-05 18:16:19 +0100
commit7cbfe2fa7507296e01b442df382c926f88e4d9a0 (patch)
treedb50e0cf54c4526c663f1d77fe56d6ba3f69fdd2
parentImprove templating macros. (diff)
downloadcalp-7cbfe2fa7507296e01b442df382c926f88e4d9a0.tar.gz
calp-7cbfe2fa7507296e01b442df382c926f88e4d9a0.tar.xz
Fix up GC a tiny bit.
-rw-r--r--main.c11
-rw-r--r--vcal.c56
-rw-r--r--vcal.h7
3 files changed, 43 insertions, 31 deletions
diff --git a/main.c b/main.c
index a669f290..87be26ff 100644
--- a/main.c
+++ b/main.c
@@ -62,12 +62,15 @@ int main (int argc, char* argv[argc]) {
}
free_vcalendar(&cal);
- for (int i = 0; i < cline_ptr; i++) {
- if (clines[i] != NULL) {
+ for (int i = 0; i < gc_ptr; i++) {
+ if (gc_vect[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->after->value->mem);
+ // printf("gc_vect[%i] : [%s] := [%s]\n", i, gc_vect[i]->key.mem, gc_vect[i]->vals.head->after->value->mem);
+ printf("gc_vect[%i] : [%s] := [%s]\n", i,
+ ((content_line*) gc_vect[i])->key.mem,
+ ((content_line*) gc_vect[i])->vals.head->after->value->mem);
}
}
- free(clines);
+ free(gc_vect);
}
diff --git a/vcal.c b/vcal.c
index 448a0a34..ba17338a 100644
--- a/vcal.c
+++ b/vcal.c
@@ -11,9 +11,6 @@
#include "linked_list.inc.h"
#undef TYPE
-content_line** clines;
-int cline_ptr;
-
INIT_F(vevent, char* filename) {
INIT(TRIE(content_line), &this->clines);
@@ -36,14 +33,11 @@ content_line* RESOLVE(content_line)
return NULL;
}
- /* TODO actuall iterators could be fun */
- for (LINK(strbuf)* s = new->vals.head->after;
- s->after != NULL;
- s = s->after) {
- PUSH(LLIST(strbuf)) (&dest->vals, s->value);
- }
+ APPEND(LLIST(strbuf)) (&dest->vals, &new->vals);
- FREE(content_line)(new);
+ FREE(strbuf)(&new->key);
+ gc_free(new);
+ free(new);
return dest;
}
@@ -52,13 +46,9 @@ content_line* get_property (vevent* ev, char* key) {
return GET(TRIE(content_line))(&ev->clines, key);
}
-int add_content_line (vevent* ev, content_line* c) {
- // TODO memmory safety on strbuf?
- return PUSH(TRIE(content_line))(&ev->clines, c->key.mem, c);
-}
-
INIT_F(content_line) {
- clines[cline_ptr++] = this;
+ gc_register(this);
+
INIT(strbuf, &this->key);
// INIT(strbuf, &this->val);
INIT( LLIST(strbuf), &this->vals );
@@ -67,7 +57,7 @@ INIT_F(content_line) {
}
INIT_F(content_line, int keylen, int vallen) {
- clines[cline_ptr++] = this;
+ gc_register(this);
INIT(strbuf, &this->key, keylen);
// INIT(strbuf, &this->val, vallen);
INIT( LLIST(strbuf), &this->vals );
@@ -93,12 +83,8 @@ FREE_F(content_line) {
// 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;
- }
- }
+ gc_free(this);
+
// TODO remaining fields
return 0;
}
@@ -131,8 +117,10 @@ int push_event(vcalendar* cal, vevent* ev) {
}
INIT_F(vcalendar) {
- clines = calloc(sizeof(*clines), 10000);
- cline_ptr = 0;
+ // TODO remove
+ gc_vect = calloc(sizeof(*gc_vect), 10000);
+ gc_ptr = 0;
+
this->alloc = 1;
this->events = calloc(sizeof(*this->events), this->alloc);
this->n_events = 0;
@@ -146,3 +134,21 @@ int free_vcalendar (vcalendar* cal) {
free (cal->events);
return 0;
}
+
+void** gc_vect;
+int gc_ptr;
+
+int gc_register(void* ptr) {
+ gc_vect[gc_ptr++] = ptr;
+ return 0;
+}
+
+int gc_free(void* ptr) {
+ for (int i = 0; i < gc_ptr; i++) {
+ if (gc_vect[i] == ptr) {
+ gc_vect[i] = NULL;
+ }
+ }
+
+ return 0;
+}
diff --git a/vcal.h b/vcal.h
index 74ea819b..d25a77aa 100644
--- a/vcal.h
+++ b/vcal.h
@@ -80,7 +80,10 @@ int push_event(vcalendar* cal, vevent* ev);
* TODO
* remove it.
*/
-extern content_line** clines;
-extern int cline_ptr;
+extern void** gc_vect;
+extern int gc_ptr;
+
+int gc_register(void* ptr);
+int gc_free(void* ptr);
#endif /* VCAL_H */