]> git.mdlowis.com Git - proto/sclpl.git/commitdiff
Fixed valgrind errors for garbage collector and implemented proper destructor for...
authorMike D. Lowis <mike.lowis@gentex.com>
Tue, 13 Oct 2015 17:05:19 +0000 (13:05 -0400)
committerMike D. Lowis <mike.lowis@gentex.com>
Tue, 13 Oct 2015 17:05:19 +0000 (13:05 -0400)
build.rb
modules/libopts
source/ast.c
source/gc.c
source/main.c
source/parser.c

index 9f4095dea2388f1c737343033665c64c2fb4d0ae..1863c86be4250c63223c0f660069f4b0d00d1f2c 100755 (executable)
--- a/build.rb
+++ b/build.rb
@@ -14,7 +14,7 @@ base_env = BuildEnv.new do |env|
   env.build_dir('source','build/obj/source')
   env.build_dir('modules','build/obj/modules')
   # Compiler options
-  env["CFLAGS"] += ['-DLEAK_DETECT_LEVEL=1', '--std=c99', '-Wall', '-Wextra']#, '-Werror']
+  env["CFLAGS"] += ['-g', '-DLEAK_DETECT_LEVEL=1', '--std=c99', '-Wall', '-Wextra']#, '-Werror']
   env["CPPPATH"] += Dir['modules/libcds/source/**/'] + [
     'modules/libopts/source',
     'source/',
index 57c1fa4035fadc76abcfb6b7cf4eadfae4db2b85..f0ea1b6d1c01ef84a6eb12bd05829c57dd0bceb4 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 57c1fa4035fadc76abcfb6b7cf4eadfae4db2b85
+Subproject commit f0ea1b6d1c01ef84a6eb12bd05829c57dd0bceb4
index 336a1a1eb045deb6dc1ead9894443d793d368e0f..02edaa11e867f30378f36664f8b152135067925b 100644 (file)
@@ -2,6 +2,41 @@
 
 static void ast_free(void* ptr)
 {
+    AST* ast = (AST*)ptr;
+    switch(ast->type) {
+        case AST_IDENT:
+        case AST_STRING:
+            gc_delref(ast->value.text);
+            break;
+
+        case AST_REQ:
+            gc_delref(ast->value.req.name);
+            break;
+
+        case AST_DEF:
+            gc_delref(ast->value.def.name);
+            gc_delref(ast->value.def.value);
+            break;
+
+        case AST_ANN:
+            gc_delref(ast->value.ann.name);
+            gc_delref(ast->value.ann.value);
+            break;
+
+        case AST_IF:
+            gc_delref(ast->value.ifexpr.cond);
+            gc_delref(ast->value.ifexpr.bthen);
+            gc_delref(ast->value.ifexpr.belse);
+            break;
+
+        case AST_FUNC:
+            gc_delref(ast->value.func.args);
+            gc_delref(ast->value.func.body);
+            break;
+
+        default:
+            break;
+    }
 }
 
 static AST* ast(ASTType type)
index 6a8eec2246bacdc6a99daa769a16f1422608c955..a0d79ef3b4b3ef93d97b66c190648ef4e0b32f77 100644 (file)
@@ -151,6 +151,7 @@ static hash_entry_t* hash_del(hash_t* hash, hash_entry_t* entry)
 
 /*****************************************************************************/
 
+static bool Shutdown;
 static void** Stack_Bottom;
 static hash_t Zero_Count_Table;
 static hash_t Multi_Ref_Table;
@@ -191,22 +192,22 @@ static void gc_mark(void) {
     (noinline ? gc_mark_stack : NULL)();
 }
 
-static void gc_sweep(void) {
+static void gc_sweep_table(hash_t* table) {
     unsigned int i;
     /* Delete all the entries in the hash */
-    for (i = 0; i < num_buckets(Working_Table.bkt_count); i++) {
-        hash_entry_t* node = Working_Table.buckets[i];
-        Working_Table.buckets[i] = NULL;
+    for (i = 0; i < num_buckets(table->bkt_count); i++) {
+        hash_entry_t* node = table->buckets[i];
+      table->buckets[i] = NULL;
         while (node != NULL) {
             hash_entry_t* deadite = node;
+            node = node->next;
             obj_t* obj = ((obj_t*)(deadite->object)-1);
             if (obj->destructor != NULL)
                 obj->destructor(deadite->object);
             free(deadite);
-            node = node->next;
         }
     }
-    free(Working_Table.buckets);
+    free(table->buckets);
 }
 
 void gc_init(void** stack_bottom)
@@ -214,17 +215,34 @@ void gc_init(void** stack_bottom)
     Stack_Bottom = stack_bottom;
     hash_init(&Zero_Count_Table);
     hash_init(&Multi_Ref_Table);
+    atexit(gc_deinit);
+    Shutdown = false;
 }
 
 void gc_deinit(void)
 {
+    Shutdown = true;
+    gc_sweep_table(&Zero_Count_Table);
+    gc_sweep_table(&Multi_Ref_Table);
 }
 
 void gc_collect(void) {
+#ifdef GC_DEBUG_MSGS
+    printf("BEFORE - ZCT: %ld MRT: %ld TOT: %ld\n",
+        hash_size(&Zero_Count_Table),
+        hash_size(&Multi_Ref_Table),
+        hash_size(&Zero_Count_Table) + hash_size(&Multi_Ref_Table));
+#endif
     Working_Table = Zero_Count_Table;
     hash_init(&Zero_Count_Table);
     gc_mark();
-    gc_sweep();
+    gc_sweep_table(&Working_Table);
+#ifdef GC_DEBUG_MSGS
+    printf("AFTER - ZCT: %ld MRT: %ld TOT: %ld\n\n",
+        hash_size(&Zero_Count_Table),
+        hash_size(&Multi_Ref_Table),
+        hash_size(&Zero_Count_Table) + hash_size(&Multi_Ref_Table));
+#endif
 }
 
 void* gc_alloc(size_t size, destructor_t destructor)
@@ -246,7 +264,7 @@ void* gc_addref(void* ptr)
     hash_entry_t lookup = {0, 0, 0};
     hash_entry_t* entry;
     obj_t* obj = ((obj_t*)ptr-1);
-    if (ptr != NULL) {
+    if ((ptr != NULL) && !Shutdown) {
         obj->refs++;
         if (obj->refs == 1) {
             lookup.object = ptr;
@@ -263,7 +281,7 @@ void gc_delref(void* ptr)
     hash_entry_t lookup = {0, 0, 0};
     hash_entry_t* entry;
     obj_t* obj = ((obj_t*)ptr-1);
-    if (ptr != NULL) {
+    if ((ptr != NULL) && !Shutdown) {
         obj->refs--;
         if (obj->refs == 0) {
             lookup.object = ptr;
@@ -287,28 +305,6 @@ int main(int argc, char** argv)
 {
     void* stack_bottom = NULL;
     gc_init(&stack_bottom);
-    atexit(gc_deinit);
     return user_main(argc, argv);
 }
 
-///int user_main(int argc, char** argv) {
-///    uint8_t* foo;
-///    (void)argc;
-///    (void)argv;
-///    foo = (uint8_t*)gc_alloc(42,NULL);
-///    printf("refs: %lu\n", hash_size(&Zero_Count_Table));
-///    gc_collect();
-///    *foo = 42;
-///    printf("data: %d\n", (int)*foo);
-///    (void)foo;
-///    printf("refs: %lu\n", hash_size(&Zero_Count_Table));
-///    printf("ref: %p\n", foo);
-///    foo = NULL;
-///    gc_collect();
-///    printf("refs: %lu\n", hash_size(&Zero_Count_Table));
-///    printf("ref: %p\n", foo);
-///    gc_collect();
-///    printf("refs: %lu\n", hash_size(&Zero_Count_Table));
-///    return 0;
-///}
-
index 1f73bd72fb38737a8a069f727e3edda70200dd4a..19e945f5edf6b9f54eeaf7a193d2e91eafebe17f 100644 (file)
@@ -75,7 +75,8 @@ static int emit_program(void) {
 
 */
 int user_main(int argc, char **argv) {
-    opts_parse( Options_Config, argc, argv );
+    opts_parse( Options_Config, NULL, argc, argv );
+    atexit(&opts_reset);
     if (!opts_is_set(NULL,"mode")) {
         print_usage();
     } else if (opts_equal(NULL, "mode", "tokens")) {
@@ -97,6 +98,5 @@ int user_main(int argc, char **argv) {
     } else {
         print_usage();
     }
-    opts_reset();
     return 1;
 }
index 2f4572cfe4ad645306d62f35ad5b17061634ee53..d252131bec9cf5f68002b595edf8407cb551f0bf 100644 (file)
@@ -13,7 +13,8 @@ static void parser_free(void* obj) {
     if ((NULL != parser->tok) && (&tok_eof != parser->tok)) {
         gc_delref(parser->tok);
     }
-    //gc_delref(parser->stack);
+    if (parser->line != NULL)
+        free(parser->line);
 }
 
 Parser* parser_new(char* prompt, FILE* input)
@@ -25,7 +26,6 @@ Parser* parser_new(char* prompt, FILE* input)
     parser->input   = input;
     parser->prompt  = prompt;
     parser->tok     = NULL;
-    //parser->stack = vec_new(0);
     return parser;
 }
 
@@ -52,7 +52,6 @@ void parser_resume(Parser* parser) {
         gc_delref(parser->tok);
         parser->tok = NULL;
     }
-    //vec_clear(parser->stack);
     /* We ignore the rest of the current line and attempt to start parsing
      * again on the next line */
     fetchline(parser);
@@ -60,7 +59,6 @@ void parser_resume(Parser* parser) {
 
 void error(Parser* parser, const char* text)
 {
-    (void)parser;
     Tok* tok = peek(parser);
     fprintf(stderr, "<file>:%zu:%zu:Error: %s\n", tok->line, tok->col, text);
     exit(1);