From d527f50159f056dc165fa7eaf7bf80425a1e758d Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 14 Oct 2008 20:38:27 +0000 Subject: GC removed, temporaly --- src/build.h | 2 +- src/core/convert.c | 2 +- src/core/function.c | 11 ++---- src/core/garbage.c | 15 +++++-- src/core/interpret.c | 13 +++++-- src/core/scanner.c | 12 ++---- src/core/scanner.h | 1 - src/core/scope.c | 2 +- src/core/symbol.c | 16 ++++---- src/core/token.c | 108 ++++++++++++++++++++++++++++++++++----------------- src/core/token.h | 7 +++- src/data/stack.c | 16 ++++++++ src/data/stack.h | 1 + src/fype.c | 4 +- 14 files changed, 135 insertions(+), 75 deletions(-) (limited to 'src') diff --git a/src/build.h b/src/build.h index 80ea007..bd5bc76 100644 --- a/src/build.h +++ b/src/build.h @@ -35,7 +35,7 @@ #ifndef BUILD_H #define BUILD_H -#define BUILDNR 9330 +#define BUILDNR 9596 #define OS_FREEBSD #endif diff --git a/src/core/convert.c b/src/core/convert.c index 081e92e..fa1282d 100644 --- a/src/core/convert.c +++ b/src/core/convert.c @@ -136,7 +136,7 @@ convert_to_string(Token *p_token) { p_token->c_val = realloc(p_token->c_val, sizeof(char) * (i_len + 1)); strcpy(p_token->c_val, c_tmp); p_token->c_val[i_len] = 0; - array_iterate(p_token->p_array, token_delete_cb); + //array_iterate(p_token->p_array, token_delete_cb); break; default: diff --git a/src/core/function.c b/src/core/function.c index 979454d..fdbf308 100644 --- a/src/core/function.c +++ b/src/core/function.c @@ -95,10 +95,8 @@ _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, printf("PROCESS OPERATOR %s %s\n", tt_get_name(tt_op), tt_get_name(tt_op2)); - token_print(p_token_next); - printf("\n"); - token_print(p_token_store); - printf("\n"); + token_print_ln(p_token_next); + token_print_ln(p_token_store); #endif /* DEBUG_FUNCTION_PROCESS */ if (p_token_op2 != NULL) { @@ -527,11 +525,10 @@ _process(Interpret *p_interpret, Token *p_token_store, Token *p_token_op, } #ifdef DEBUG_FUNCTION_PROCESS - token_print(p_token_store); - printf("\n\n"); + token_print_ln(p_token_store); #endif /* DEBUG_FUNCTION_PROCESS */ - token_delete(p_token_next); + //token_delete(p_token_next); } void diff --git a/src/core/garbage.c b/src/core/garbage.c index aba19c4..3820144 100644 --- a/src/core/garbage.c +++ b/src/core/garbage.c @@ -64,6 +64,12 @@ _garbage_print(_Garbage *p_garbage) { } } +void +_garbage_free(_Garbage *p_garbage) { + (*p_garbage->p_func) (p_garbage->p_2free); + free(p_garbage); +} + void garbage_destroy() { garbage_collect(); @@ -87,17 +93,18 @@ garbage_destroy() { int garbage_collect() { + //printf("GARBAGE_COLLECT\n"); ListIterator *p_iter = listiterator_new(LIST_GARBAGE); List *p_list_garbage_new = list_new(); int i_count = 0; + //printf("size %d\n", list_size(LIST_GARBAGE)); while (listiterator_has_next(p_iter)) { _Garbage *p_garbage = listiterator_next(p_iter); if (p_garbage->p_ref_count == NULL || *p_garbage->p_ref_count <= 0) { - // _garbage_print(p_garbage); - (*p_garbage->p_func) (p_garbage->p_2free); - free(p_garbage); + //_garbage_print(p_garbage); + _garbage_free(p_garbage); ++i_count; } else { @@ -106,9 +113,9 @@ garbage_collect() { } listiterator_delete(p_iter); - list_delete(LIST_GARBAGE); LIST_GARBAGE = p_list_garbage_new; + //printf("GARBAGE_COLLECT_END\n"); return (i_count); } diff --git a/src/core/interpret.c b/src/core/interpret.c index c3f781c..a53f193 100644 --- a/src/core/interpret.c +++ b/src/core/interpret.c @@ -165,8 +165,13 @@ int _program(Interpret *p_interpret) { _CHECK TRACK - while (_statement(p_interpret) == 1) - garbage_collect(); + int i_count = 0; + while (_statement(p_interpret) == 1) { + if (++i_count == 50) { + garbage_collect(); + i_count = 0; + } + } return (1); } @@ -299,7 +304,7 @@ _block_get(Interpret *p_interpret, List *p_list_block) { int __expression_get(Interpret *p_interpret, List *p_list_expression) { for (;;) { - if (p_interpret->tt == TT_PARANT_CL) + if (p_interpret->tt == TT_PARANT_CL) break; /* for */ list_add_back(p_list_expression, p_interpret->p_token); @@ -950,7 +955,7 @@ _term(Interpret *p_interpret) { if (__array_get(p_interpret, p_token_arr) != 0) _INTERPRET_ERROR("Array syntax error", p_token); - return (1); + return (1); } break; diff --git a/src/core/scanner.c b/src/core/scanner.c index 49026c2..5190175 100644 --- a/src/core/scanner.c +++ b/src/core/scanner.c @@ -108,8 +108,8 @@ scanner_post_task(Scanner *p_scanner) { if (tt_cur == TT_INTEGER && tt_last[1] == TT_DOT && tt_last[0] == TT_INTEGER) { - token_ref_down(pt_last[0]); - token_ref_down(pt_last[1]); + //token_ref_down(pt_last[0]); + //token_ref_down(pt_last[1]); char *c_2 = token_get_val(p_token); char *c_0 = token_get_val(pt_last[0]); @@ -338,9 +338,9 @@ scanner_add_token(Scanner *p_scanner, char **cc_token, int *p_token_len, List *p_list_token = scanner_get_list_token(p_scanner); Token *p_token = token_new(*cc_token, tt_cur, p_scanner->i_current_line_nr, p_scanner->i_current_pos_nr, p_scanner->c_filename); + p_token->b_source_token = true; list_add_back(p_list_token, p_token); - token_ref_up(p_token); *cc_token = malloc(sizeof(char)); (*cc_token)[0] = 0; @@ -361,9 +361,3 @@ scanner_get_tt_cur(char *c_token) { return tt_cur == TT_NONE ? TT_IDENT : tt_cur; } - -void -scanner_cleanup_list_token_cb(void *p_void) { - Token *p_token = p_void; - token_delete(p_token); -} diff --git a/src/core/scanner.h b/src/core/scanner.h index 4626595..c4e15fa 100644 --- a/src/core/scanner.h +++ b/src/core/scanner.h @@ -65,6 +65,5 @@ void scanner_run(Fype *p_fype); void scanner_add_token(Scanner *p_scanner, char **cc_token, int *p_token_len, TokenType tt_cur); TokenType scanner_get_tt_cur(char *c_token); -void scanner_cleanup_list_token_cb(void *p_void); #endif diff --git a/src/core/scope.c b/src/core/scope.c index 1bf8dd2..0c65c1d 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -58,7 +58,7 @@ scope_delete(Scope *p_scope) { void scope_up(Scope *p_scope) { - stack_push(p_scope->p_stack_scopes, hash_new(1024)); + stack_push(p_scope->p_stack_scopes, hash_new(24)); } void diff --git a/src/core/symbol.c b/src/core/symbol.c index 9a98e63..7c8a8f7 100644 --- a/src/core/symbol.c +++ b/src/core/symbol.c @@ -57,15 +57,15 @@ symbol_delete(Symbol *p_symbol) { list_delete(p_list_token); } break; - case SYM_VARIABLE: + case SYM_VARIABLE: { - Token *p_token = symbol_get_val(p_symbol); - switch (token_get_tt(p_token)) { - case TT_ARRAY: - array_iterate(p_token->p_array, token_delete_cb); - break; - NO_DEFAULT; - } + Token *p_token = symbol_get_val(p_symbol); + switch (token_get_tt(p_token)) { + case TT_ARRAY: + //array_iterate(p_token->p_array, token_delete_cb); + break; + NO_DEFAULT; + } } break; NO_DEFAULT; diff --git a/src/core/token.c b/src/core/token.c index 49a9721..9162c4d 100644 --- a/src/core/token.c +++ b/src/core/token.c @@ -181,7 +181,8 @@ tt_get_name(TokenType tt_cur) { } Token* -token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, char *c_filename) { +token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, + char *c_filename) { Token *p_token = token_new_dummy(); p_token->c_val = c_val; @@ -192,6 +193,7 @@ token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, char *c_fi p_token->i_pos_nr = i_pos_nr; p_token->c_filename = c_filename; p_token->p_array = NULL; + p_token->b_source_token = false; switch (tt_cur) { case TT_INTEGER: @@ -210,7 +212,7 @@ token_new(char *c_val, TokenType tt_cur, int i_line_nr, int i_pos_nr, char *c_fi NO_DEFAULT; } - return p_token; + return (p_token); } Token* @@ -271,7 +273,7 @@ token_new_dummy() { p_token->i_pos_nr = -1; p_token->c_filename = NULL; p_token->u_token_id = TOKEN_ID_COUNTER++; - p_token->i_ref_count = 0; + p_token->i_ref_count = 1; /* Register the token in the garbage collector */ garbage_add_token(p_token); @@ -282,7 +284,7 @@ token_new_dummy() { Token* token_new_copy(Token *p_token) { Token *p_token_copy = malloc(sizeof(Token)); - p_token->u_token_id = TOKEN_ID_COUNTER++; + p_token_copy->u_token_id = TOKEN_ID_COUNTER++; if (p_token_copy == NULL) ERROR("Memory alloc error"); @@ -296,12 +298,13 @@ token_new_copy(Token *p_token) { return (p_token_copy); } -void token_copy_vals(Token *p_token_to, Token *p_token_from) { +void +token_copy_vals(Token *p_token_to, Token *p_token_from) { int i_len; // TODO: Check against mem leak - // if (p_token_to->c_val) - // free(p_token_to->c_val); + if (p_token_to->c_val) + free(p_token_to->c_val); if (p_token_from->c_val) { i_len = strlen(p_token_from->c_val); @@ -313,16 +316,16 @@ void token_copy_vals(Token *p_token_to, Token *p_token_from) { } if (p_token_from->tt_cur == TT_ARRAY) { - p_token_to->p_array = array_new(); - ArrayIterator *p_iter = arrayiterator_new(p_token_from->p_array); - while (arrayiterator_has_next(p_iter)) { - Token *p_token = arrayiterator_next(p_iter); - token_ref_up(p_token); - array_unshift(p_token_to->p_array, p_token); - } - arrayiterator_delete(p_iter); + p_token_to->p_array = array_new(); + ArrayIterator *p_iter = arrayiterator_new(p_token_from->p_array); + while (arrayiterator_has_next(p_iter)) { + Token *p_token = arrayiterator_next(p_iter); + token_ref_up(p_token); + array_unshift(p_token_to->p_array, p_token); + } + arrayiterator_delete(p_iter); } else { - p_token_to->p_array = NULL; + p_token_to->p_array = NULL; } p_token_to->tt_cur = p_token_from->tt_cur; @@ -338,6 +341,11 @@ token_delete_cb(void *p_void) { token_delete(p_void); } +void +token_delete_force_cb(void *p_void) { + token_delete_force(p_void); +} + void token_ref_down_cb(void *p_void) { Token *p_token = p_void; @@ -349,42 +357,72 @@ token_copy_cb(void *p_void) { return (token_new_copy(p_void)); } +int +token_ref_down(Token *p_token) { +// if (p_token->i_ref_count > 0) + p_token->i_ref_count--; + + return (p_token->i_ref_count); +} + +void +_token_free(Token *p_token) { + if (p_token->c_val) + free(p_token->c_val); + p_token->c_val = NULL; + + if (p_token->p_array) { + array_iterate(p_token->p_array, token_delete_cb); + array_delete(p_token->p_array); + } + p_token->p_array = NULL; + + free(p_token); +} + void token_delete(Token *p_token) { - if (token_ref_down(p_token) <= 0) { - if (p_token->i_ref_count == 0) { + if (IS_SOURCE_TOKEN(p_token)) + return; + + token_ref_down(p_token); + + if (p_token->i_ref_count == 0) { #ifdef DEBUG_TOKEN_REFCOUNT - printf("Token refcount debug: Token ref count is 0 == %d\n", - p_token->i_ref_count); + printf("Token refcount debug: Token ref count is 0 == %d\n", + p_token->i_ref_count); + token_print_ln(p_token); #endif /* DEBUG_TOKEN_REFCOUNT */ - if (p_token->c_val) - free(p_token->c_val); - if (p_token->p_array) { - array_iterate(p_token->p_array, token_delete_cb); - array_delete(p_token->p_array); - } + _token_free(p_token); - free(p_token); - } + } #ifdef DEBUG_TOKEN_REFCOUNT - else { - printf("Token refcount debug: Token ref count is 0 > %d\n", - p_token->i_ref_count); - } -#endif /* DEBUG_TOKEN_REFCOUNT */ + else if (p_token->i_ref_count < 0) { + token_print_ln(p_token); + printf("Token ref count is 0 > %d\n", + p_token->i_ref_count); } -#ifdef DEBUG_TOKEN_REFCOUNT else { printf("Token refcount debug: Token ref count is 0 < %d\n", p_token->i_ref_count); + token_print_ln(p_token); } #endif /* DEBUG_TOKEN_REFCOUNT */ } +void +token_delete_force(Token *p_token) { + token_ref_down(p_token); + //printf("FORCE DEL "); + //token_print_ln(p_token); + _token_free(p_token); +} + void token_print(Token *p_token) { - printf("(id=%05u, line=%05d, pos=%04d, type=%s", + printf("(org=%d, id=%05u, line=%05d, pos=%04d, type=%s", + (int) p_token->b_source_token, p_token->u_token_id, p_token->i_line_nr, p_token->i_pos_nr, diff --git a/src/core/token.h b/src/core/token.h index af74e98..e683e01 100644 --- a/src/core/token.h +++ b/src/core/token.h @@ -55,6 +55,8 @@ #define IS_NOT_NUMERICAL(t) !(IS_NUMERICAL(t)) #define IS_ARRAY(t) (t->tt_cur == TT_ARRAY) #define IS_NOT_ARRAY(t) !(IS_ARRAY(t)) +#define IS_SOURCE_TOKEN(t) (t->b_source_token == true) +#define IS_NOT_SOURCE_TOKEN(t) (t->b_source_token == false) #define token_get_filename(t) \ (t->c_filename != NULL ? t->c_filename : "Code string") @@ -72,7 +74,6 @@ #define token_get_posnr(t) t->i_pos_nr #define token_get_linenr(t) t->i_line_nr #define token_ref_up(t) ++t->i_ref_count -#define token_ref_down(t) --t->i_ref_count typedef enum { // Diverse @@ -166,6 +167,7 @@ typedef struct { char *c_filename; unsigned int u_token_id; int i_ref_count; + _Bool b_source_token; Array *p_array; } Token; @@ -179,6 +181,8 @@ Token* token_new_(char *c_val, TokenType tt_cur, char *c_filename); Token* token_new_dummy(); void token_copy_vals(Token *p_token_to, Token *p_token_from); void token_delete(Token *p_token); +void token_delete_force(Token *p_token); +void token_delete_force_cb(void *p_token); void token_delete_cb(void *p_token); void token_ref_down_cb(void *p_token); void* token_copy_cb(void *p_token); @@ -187,6 +191,7 @@ void token_print_cb(void *p_void); void token_print(Token *p_token); void token_print_ln(Token *p_token); void token_print_val(Token *p_token); +int token_ref_down(Token *p_token); TokenType get_tt(char *c_token); #endif diff --git a/src/data/stack.c b/src/data/stack.c index 9afb9b4..778e41e 100644 --- a/src/data/stack.c +++ b/src/data/stack.c @@ -61,6 +61,22 @@ stack_empty(Stack *p_stack) { return (p_stack->i_size == 0); } +void +stack_debug(Stack *p_stack, void *p_val) { + printf("bPUSH %d %d\n", stack_size(p_stack), (int)p_stack); + StackElem *p_elem = stackelem_new(); + + p_elem->p_val = p_val; + p_elem->p_next = p_stack->p_first; + p_stack->p_first = p_elem; + + if (p_stack->p_last == NULL) + p_stack->p_last = p_stack->p_first; + + ++p_stack->i_size; + printf("aPUSH %d %d\n", stack_size(p_stack), (int)p_stack); +} + void stack_push(Stack *p_stack, void *p_val) { StackElem *p_elem = stackelem_new(); diff --git a/src/data/stack.h b/src/data/stack.h index 4774cef..721ed31 100644 --- a/src/data/stack.h +++ b/src/data/stack.h @@ -60,6 +60,7 @@ StackElem *stackelem_new(); _Bool stack_empty(Stack *p_stack); void stack_iterate(Stack *p_stack, void (*func)(void *p_void)); void stack_push(Stack *p_stack, void *p_val); +void stack_debug(Stack *p_stack, void *p_val); void *stack_pop(Stack *p_stack); void stack_clear(Stack *p_stack); void stack_delete(Stack *p_stack); diff --git a/src/fype.c b/src/fype.c index af61c1d..1b6d975 100644 --- a/src/fype.c +++ b/src/fype.c @@ -61,9 +61,7 @@ fype_delete(Fype *p_fype) { hash_iterate(p_fype->p_hash_syms, symbol_cleanup_hash_syms_cb); hash_delete(p_fype->p_hash_syms); - //list_iterate(p_fype->p_list_token, token_print_cb); - list_iterate(p_fype->p_list_token, token_ref_down_cb); - //list_iterate(p_fype->p_list_token, token_print_cb); + list_iterate(p_fype->p_list_token, token_delete_force_cb); list_delete(p_fype->p_list_token); if (p_fype->c_basename) -- cgit v1.2.3