summaryrefslogtreecommitdiff
path: root/src/core/token.c
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2008-10-14 20:38:27 +0000
committerPaul Buetow <paul@buetow.org>2008-10-14 20:38:27 +0000
commitd527f50159f056dc165fa7eaf7bf80425a1e758d (patch)
tree03e8aa97c25fbc014876e5113dd66d4a44adac4d /src/core/token.c
parent71185ab0ab0b08b4d5bb2e750ff85e11f105a453 (diff)
GC removed, temporaly
Diffstat (limited to 'src/core/token.c')
-rw-r--r--src/core/token.c108
1 files changed, 73 insertions, 35 deletions
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;
@@ -339,6 +342,11 @@ token_delete_cb(void *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;
token_ref_down(p_token);
@@ -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,