From dc8474d9034d9281463bb69f7f7a922e3ea713ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Mon, 4 Feb 2019 11:25:37 +0100 Subject: Normalize and improve INIT & FREE macros. --- code.scm | 1 - hash.h | 9 ++------- hash.inc.h | 4 ++-- linked_list.h | 8 ++++---- linked_list.inc.h | 8 ++++---- macro.h | 44 +++++++++++++------------------------------- parse.c | 2 +- scheme.scm.c | 2 +- strbuf.c | 8 ++++---- strbuf.h | 10 ++++------ trie.h | 18 +++++++----------- trie.inc.h | 24 ++++++++++++------------ vcal.c | 28 ++++++++++++++-------------- vcal.h | 10 +++++----- 14 files changed, 73 insertions(+), 103 deletions(-) diff --git a/code.scm b/code.scm index c3d5d7b5..cca7cdb3 100755 --- a/code.scm +++ b/code.scm @@ -15,4 +15,3 @@ (format #t "~3d | ~a~%" i (calendar-get-attr v i "summary"))) - diff --git a/hash.h b/hash.h index 07c31460..02741642 100644 --- a/hash.h +++ b/hash.h @@ -10,11 +10,6 @@ unsigned long hash(char*); #define TABLE(T) TP(table__, T) #define HASH_PUT(T) TP(hash_put_, T) #define HASH_GET(T) TP(hash_get_, T) -// #define HASH_INIT(T) TP(hash_init_, T) -// #define HASH_INIT(T) CONSTRUCTOR_T(hash_ ## T, 1) -#define HASH_INIT(T) CONSTRUCTOR_GEN(hash, T, 1) - -#define HASH_FREE(T) TP(hash_free_, T) #endif /* HASH_H */ #ifdef TYPE @@ -31,7 +26,7 @@ typedef struct { int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value ); -int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ); +INIT_F(HASH(TYPE), int init_size ); TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ); @@ -39,6 +34,6 @@ TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ); * Free's all item's stored in table. * And finally frees table. */ -int HASH_FREE(TYPE) ( TABLE(TYPE)* table ); +FREE_F(HASH(TYPE)); #endif /* HASH_H */ diff --git a/hash.inc.h b/hash.inc.h index 0b07629f..6ded0628 100644 --- a/hash.inc.h +++ b/hash.inc.h @@ -17,7 +17,7 @@ int HASH_PUT(TYPE) ( TABLE(TYPE)* table, TYPE* value) { return 0; } -int HASH_INIT(TYPE) ( TABLE(TYPE)* table, int init_size ) { +INIT_F(HASH(TYPE), int init_size ) { /* * TODO parts of table might not get properly initialized to 0 */ @@ -43,7 +43,7 @@ TYPE* HASH_GET(TYPE) ( TABLE(TYPE)* table, char* key ) { } } -int HASH_FREE(TYPE) ( TABLE(TYPE)* table ) { +FREE(HASH(TYPE)) { /* * TODO an early return is possible by checking if all items have * been found. table->item_count diff --git a/linked_list.h b/linked_list.h index a76c3443..d1354e02 100644 --- a/linked_list.h +++ b/linked_list.h @@ -24,11 +24,11 @@ typedef struct { int length; } LLIST(TYPE); -int CONSTRUCTOR_DECL ( LLIST(TYPE) ); -int FREE_DECL( LLIST(TYPE) ); +INIT_F ( LLIST(TYPE) ); +FREE_F( LLIST(TYPE) ); -int CONSTRUCTOR_DECL ( LINK(TYPE) ); -int CONSTRUCTOR_DECL ( LINK(TYPE), TYPE* val ); +INIT_F ( LINK(TYPE) ); +INIT_F ( LINK(TYPE), TYPE* val ); int LLIST_CONS(TYPE) ( LLIST(TYPE)* lst, TYPE* val); diff --git a/linked_list.inc.h b/linked_list.inc.h index a878d075..b6942a94 100644 --- a/linked_list.inc.h +++ b/linked_list.inc.h @@ -2,7 +2,7 @@ #error "Set TYPE before including this file" #else -int CONSTRUCTOR_DECL ( LLIST(TYPE) ) { +INIT_F ( LLIST(TYPE) ) { this->length = 0; NEW(LINK(TYPE), head); NEW(LINK(TYPE), tail); @@ -14,7 +14,7 @@ int CONSTRUCTOR_DECL ( LLIST(TYPE) ) { return 0; } -int FREE_DECL( LLIST(TYPE) ) { +FREE_F( LLIST(TYPE) ) { LINK(TYPE) *node, *next; node = this->head->after; while (node->after != NULL) { @@ -31,14 +31,14 @@ int FREE_DECL( LLIST(TYPE) ) { return 0; } -int CONSTRUCTOR_DECL ( LINK(TYPE) ) { +INIT_F ( LINK(TYPE) ) { this->before = NULL; this->after = NULL; this->value = NULL; return 0; } -int CONSTRUCTOR_DECL ( LINK(TYPE), TYPE* val ) { +INIT_F ( LINK(TYPE), TYPE* val ) { this->before = NULL; this->after = NULL; this->value = val; diff --git a/macro.h b/macro.h index a62d12d1..82b8e558 100644 --- a/macro.h +++ b/macro.h @@ -24,44 +24,26 @@ #define NEW_HELPER(T, ARG_COUNT) \ TP3(T, _init_, ARG_COUNT) -/* - * TODO rename all the constructor macros to something clearer and - * shorter. - */ +/* Constructor type name */ +#define __INIT_T(T, C) TP3(T, __init__, C) -/* - * Constructor type name - */ -#define CONSTRUCTOR_T(T, C) TP3(T, __init__, C) - -#define CONSTRUCTOR_GEN(parent, child, C) \ - CONSTRUCTOR_T(parent ## __ ## child, C) - -/* - * Returns full type of constructor - */ -#define CONSTRUCTOR_DECL(T, ...) \ - CONSTRUCTOR_T(T, VA_ARGS_NUM(__VA_ARGS__)) (T* this __VA_OPT__(,) __VA_ARGS__) +/* Returns full type of constructor */ +#define INIT_F(T, ...) \ + int __INIT_T(T, VA_ARGS_NUM(__VA_ARGS__)) (T* this __VA_OPT__(,) __VA_ARGS__) -/* - * Call the constructor of an object - */ -#define CONSTRUCT(T, N, ...) \ - CONSTRUCTOR_T(T, VA_ARGS_NUM(__VA_ARGS__)) (N __VA_OPT__(,) __VA_ARGS__) +/* Call the constructor of an object */ +#define INIT(T, N, ...) \ + __INIT_T(T, VA_ARGS_NUM(__VA_ARGS__)) (N __VA_OPT__(,) __VA_ARGS__) -/* - * Allocate a new object on the HEAP - */ +/* Allocate a new object on the HEAP */ #define NEW(T, N, ...) \ T* N = malloc(sizeof(*N)); \ - CONSTRUCT(T, N, __VA_ARGS__); + INIT(T, N, __VA_ARGS__); -/* - * Allocate a new object on the STACK - */ +/* Allocate a new object on the STACK */ #define SNEW(T, N, ...) \ T N; \ - CONSTRUCT(T, & N, __VA_ARGS__); + INIT(T, & N, __VA_ARGS__); /* Destructor for type */ #define FREE(T) TP(T, __free) @@ -70,7 +52,7 @@ #define FFREE(T, N) do { FREE(T)(N); free(N); } while (0) /* Declare destructor */ -#define FREE_DECL(T) TP(T, __free) (T* this) +#define FREE_F(T) int TP(T, __free) (T* this) #define DEEP_COPY(T) TP(deep_copy__, T) #define RESOLVE(T) TP(resolve__, T) diff --git a/parse.c b/parse.c index 890c37c3..02900e69 100644 --- a/parse.c +++ b/parse.c @@ -76,7 +76,7 @@ int parse_file(char* fname, FILE* f, vcalendar* cal) { switch (handle_kv(cal, ev, &cline, line, &ctx)) { case s_event: ev = malloc(sizeof(*ev)); - CONSTRUCT(vevent, ev, fname); + INIT(vevent, ev, fname); break; } strbuf_soft_reset(&str); diff --git a/scheme.scm.c b/scheme.scm.c index e62610ff..f1fcb349 100644 --- a/scheme.scm.c +++ b/scheme.scm.c @@ -20,7 +20,7 @@ SCM_DEFINE (make_calendar, "make-calendar", 1, 0, 0, vcalendar* cal = (vcalendar*) scm_gc_malloc ( sizeof(*cal), "calendar"); - CONSTRUCT(vcalendar, cal); + INIT(vcalendar, cal); char* p = scm_to_utf8_stringn(path, NULL); read_vcalendar(cal, p); diff --git a/strbuf.c b/strbuf.c index e4dfb803..874aea4f 100644 --- a/strbuf.c +++ b/strbuf.c @@ -7,7 +7,7 @@ #define ERR(s) fprintf(stderr, "\x1B[0;31mERR\x1b[m (strbuf %3i): %s\n", __LINE__, s) #endif -int CONSTRUCTOR_DECL(strbuf) { +INIT_F(strbuf) { this->mem = NULL; this->alloc = 0; this->len = 0; @@ -15,7 +15,7 @@ int CONSTRUCTOR_DECL(strbuf) { return 0; } -int CONSTRUCTOR_DECL(strbuf, size_t len) { +INIT_F(strbuf, size_t len) { this->mem = calloc(sizeof(*this->mem), len); this->alloc = len; this->ptr = 0; @@ -36,7 +36,7 @@ int strbuf_realloc(strbuf* str, size_t len) { } // int strbuf_free(strbuf* str) { -int FREE_DECL(strbuf) { +FREE_F(strbuf) { #ifdef SAFE_STR if (this->mem == NULL) return 1; #endif @@ -121,7 +121,7 @@ int strbuf_init_copy(strbuf* dest, strbuf* src) { } #endif - CONSTRUCT(strbuf, dest, src->len + 1); + INIT(strbuf, dest, src->len + 1); strbuf_copy(dest, src); return 0; diff --git a/strbuf.h b/strbuf.h index 19987dc3..8a4de030 100644 --- a/strbuf.h +++ b/strbuf.h @@ -20,12 +20,10 @@ typedef struct { * Init strbuf to size of 0 * Doesnt't call malloc. */ -int CONSTRUCTOR_DECL(strbuf); +INIT_F(strbuf); -/* - * Constructor - */ -int CONSTRUCTOR_DECL(strbuf, size_t len); +/* Constructor */ +INIT_F(strbuf, size_t len); /* * Like realloc, but for strbuf @@ -36,7 +34,7 @@ int strbuf_realloc(strbuf* str, size_t len); * Free's contents of str, but keeps str. */ // int strbuf_free(strbuf* str); -int FREE_DECL(strbuf); +FREE_F(strbuf); /* * Copy contents from src to dest. diff --git a/trie.h b/trie.h index 20b08ec9..4fe26df5 100644 --- a/trie.h +++ b/trie.h @@ -8,12 +8,6 @@ #define TRIE(T) TP(trie__, T) #define TRIE_NODE(T) TP(trie_node__, T) -#define TRIE_INIT(T) CONSTRUCTOR_GEN(trie, T, 0) -#define TRIE_NODE_INIT(T) CONSTRUCTOR_GEN(trie_node, T, 0) - -#define TRIE_FREE(T) TP(trie_free__, T) -#define TRIE_NODE_FREE(T) TP(trie_node_free__, T) - #define TRIE_PUT(T) TP(trie_put__, T) #define TRIE_GET(T) TP(trie_get__, T) @@ -35,20 +29,22 @@ typedef struct { } TRIE(TYPE); -int CONSTRUCTOR_DECL ( TRIE(TYPE) ); +INIT_F ( TRIE(TYPE) ); -int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), char c); +INIT_F (TRIE_NODE(TYPE), char c); -int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), +INIT_F (TRIE_NODE(TYPE), char c, TRIE_NODE(TYPE)* next, TRIE_NODE(TYPE)* child ); int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ); TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ); -int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node ); +// int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node ); +FREE_F(TRIE_NODE(TYPE)); -int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie ); +// int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie ); +FREE_F(TRIE(TYPE)); int TRIE_DOT(TYPE) ( TRIE(TYPE)*, FILE* ); int TRIE_DOT_HELP(TYPE) ( TRIE_NODE(TYPE)*, FILE* ); diff --git a/trie.inc.h b/trie.inc.h index 61dc3701..14362058 100644 --- a/trie.inc.h +++ b/trie.inc.h @@ -4,13 +4,13 @@ #include "err.h" -int CONSTRUCTOR_DECL ( TRIE(TYPE) ) { +INIT_F ( TRIE(TYPE) ) { NEW(TRIE_NODE(TYPE), t, '\0'); this->root = t; return 0; } -int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), char c) { +INIT_F (TRIE_NODE(TYPE), char c) { this->c = c; this->value = NULL; this->next = NULL; @@ -18,7 +18,7 @@ int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), char c) { return 0; } -int CONSTRUCTOR_DECL (TRIE_NODE(TYPE), +INIT_F (TRIE_NODE(TYPE), char c, TRIE_NODE(TYPE)* next, TRIE_NODE(TYPE)* child ) @@ -99,21 +99,21 @@ TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ) { return 0; } -int TRIE_NODE_FREE(TYPE) ( TRIE_NODE(TYPE)* node ) { - if (node == NULL) return 0; - 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); - free (node); +FREE_F(TRIE_NODE(TYPE)) { + if (this == NULL) return 0; + if (this->value != NULL) FFREE(TYPE, this->value); + if (this->next != NULL) FREE(TRIE_NODE(TYPE))(this->next); + if (this->child != NULL) FREE(TRIE_NODE(TYPE))(this->child); + free (this); return 0; } -int TRIE_FREE(TYPE) ( TRIE(TYPE)* trie ) { - if (trie->root->c != '\0') { +FREE_F(TRIE(TYPE)) { + if (this->root->c != '\0') { // ERR("Invalid trie"); return 1; } - return TRIE_NODE_FREE(TYPE)(trie->root); + return FREE(TRIE_NODE(TYPE))(this->root); } int TRIE_DOT(TYPE) ( TRIE(TYPE)* trie, FILE* f ) { diff --git a/vcal.c b/vcal.c index 73158aeb..64cc1d73 100644 --- a/vcal.c +++ b/vcal.c @@ -14,9 +14,9 @@ content_line** clines; int cline_ptr; -int CONSTRUCTOR_DECL(vevent, char* filename) { +INIT_F(vevent, char* filename) { - CONSTRUCT(TRIE(content_line), &this->clines); + INIT(TRIE(content_line), &this->clines); this->filename = calloc(sizeof(*filename), strlen(filename) + 1); strcpy(this->filename, filename); @@ -49,20 +49,20 @@ int add_content_line (vevent* ev, content_line* c) { return TRIE_PUT(content_line)(&ev->clines, c->key.mem, c); } -int CONSTRUCTOR_DECL(content_line) { +INIT_F(content_line) { clines[cline_ptr++] = this; - CONSTRUCT(strbuf, &this->key); - // CONSTRUCT(strbuf, &this->val); - CONSTRUCT( LLIST(strbuf), &this->vals ); + INIT(strbuf, &this->key); + // INIT(strbuf, &this->val); + INIT( LLIST(strbuf), &this->vals ); // TODO remaining fields return 0; } -int CONSTRUCTOR_DECL(content_line, int keylen, int vallen) { +INIT_F(content_line, int keylen, int vallen) { clines[cline_ptr++] = this; - CONSTRUCT(strbuf, &this->key, keylen); - // CONSTRUCT(strbuf, &this->val, vallen); - CONSTRUCT( LLIST(strbuf), &this->vals ); + INIT(strbuf, &this->key, keylen); + // INIT(strbuf, &this->val, vallen); + INIT( LLIST(strbuf), &this->vals ); NEW(strbuf, s, vallen); LLIST_CONS(strbuf)(&this->vals, s); // TODO remaining fields @@ -78,7 +78,7 @@ int content_line_copy (content_line* dest, content_line* src) { return 0; } -int FREE_DECL(content_line) { +FREE_F(content_line) { FREE(strbuf)(&this->key); // FREE(strbuf)(&this->val); // LLIST_FREE(strbuf)(&this->vals); @@ -92,9 +92,9 @@ int FREE_DECL(content_line) { return 0; } -int FREE_DECL(vevent) { +FREE_F(vevent) { if (this->filename != NULL) free(this->filename); - if (TRIE_FREE(content_line)(&this->clines) != 0) { + if (FREE(TRIE(content_line))(&this->clines) != 0) { fprintf(stderr, "Error freeing vevent belonging to file \n %s \n", this->filename); } @@ -119,7 +119,7 @@ int push_event(vcalendar* cal, vevent* ev) { return 0; } -int CONSTRUCTOR_DECL(vcalendar) { +INIT_F(vcalendar) { clines = calloc(sizeof(*clines), 10000); cline_ptr = 0; this->alloc = 1; diff --git a/vcal.h b/vcal.h index a5144992..03d04805 100644 --- a/vcal.h +++ b/vcal.h @@ -25,8 +25,8 @@ typedef struct { int param_count; } content_line; -int CONSTRUCTOR_DECL(content_line); -int CONSTRUCTOR_DECL(content_line, int keylen, int vallen); +INIT_F(content_line); +INIT_F(content_line, int keylen, int vallen); int RESOLVE(content_line) (content_line** orig, content_line* new); @@ -42,9 +42,9 @@ typedef struct s_vevent { TRIE(content_line) clines; } vevent; -int CONSTRUCTOR_DECL(vevent, char* filename); +INIT_F(vevent, char* filename); -int FREE_DECL(content_line); +FREE_F(content_line); int content_line_copy (content_line* dest, content_line* src); content_line* get_property (vevent* ev, char* key); @@ -59,7 +59,7 @@ typedef struct s_vcalendar { vevent** events; } vcalendar; -int CONSTRUCTOR_DECL(vcalendar); +INIT_F(vcalendar); int free_vcalendar (vcalendar* cal); /* -- cgit v1.2.3