From 3af9e099f7f8b3a329ea821f5c8d042c7003077e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 26 Mar 2008 16:56:34 +0000 Subject: [PATCH] r19061@catbus: nickm | 2008-03-26 12:53:18 -0400 Now that every thing in routerparse.c is switched over to use memareas, there is no need to keep the heap-allocated token code. svn:r14196 --- src/or/routerparse.c | 93 ++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/src/or/routerparse.c b/src/or/routerparse.c index 6a1db60242..323adbe81c 100644 --- a/src/or/routerparse.c +++ b/src/or/routerparse.c @@ -113,16 +113,21 @@ typedef enum { * of a keyword, a line full of arguments, and a binary object. The * arguments and object are both optional, depending on the keyword * type. + * + * This structure is only allocated in memareas; do not allocate it on + * the heap, or token_free() won't work. */ typedef struct directory_token_t { directory_keyword tp; /**< Type of the token. */ - unsigned int in_area:1;/**entries, rs); } smartlist_sort(ns->entries, _compare_routerstatus_entries); @@ -2661,35 +2666,20 @@ assert_addr_policy_ok(smartlist_t *lst) static void token_free(directory_token_t *tok) { - int i; tor_assert(tok); if (tok->key) crypto_free_pk_env(tok->key); - - if (! tok->in_area) { - if (tok->args) { - for (i = 0; i < tok->n_args; ++i) { - tor_free(tok->args[i]); - } - tor_free(tok->args); - } - tor_free(tok->object_type); - tor_free(tok->object_body); - tor_free(tok->error); - tor_free(tok); - } } -#define ALLOC_ZERO(sz) (area?memarea_alloc_zero(area,sz):tor_malloc_zero(sz)) -#define ALLOC(sz) (area?memarea_alloc(area,sz):tor_malloc(sz)) -#define STRDUP(str) (area?memarea_strdup(area,str):tor_strdup(str)) -#define STRNDUP(str,n) (area?memarea_strndup(area,(str),(n)):tor_strndup((str),(n))) +#define ALLOC_ZERO(sz) memarea_alloc_zero(area,sz) +#define ALLOC(sz) memarea_alloc(area,sz) +#define STRDUP(str) memarea_strdup(area,str) +#define STRNDUP(str,n) memarea_strndup(area,(str),(n)) #define RET_ERR(msg) \ STMT_BEGIN \ if (tok) token_free(tok); \ tok = ALLOC_ZERO(sizeof(directory_token_t)); \ - tok->in_area = area?1:0; \ tok->tp = _ERR; \ tok->error = STRDUP(msg); \ goto done_tokenizing; \ @@ -2746,15 +2736,15 @@ get_next_token(memarea_t *area, const char **s, const char *eos, token_rule_t *table) { const char *next, *eol, *obstart; - int i, j, allocated; size_t obname_len; + int i; directory_token_t *tok; obj_syntax o_syn = NO_OBJ; char ebuf[128]; const char *kwd = ""; + tor_assert(area); tok = ALLOC_ZERO(sizeof(directory_token_t)); - tok->in_area = area?1:0; tok->tp = _ERR; /* Set *s to first token, eol to end-of-line, next to after first token */ @@ -2792,48 +2782,32 @@ get_next_token(memarea_t *area, tok->n_args = 1; } else { /* This keyword takes multiple arguments. */ - if (area) { - char *mem = memarea_strndup(area, *s, eol-*s); - char *cp = mem; + /* XXXX021 this code is still too complicated. */ + char *mem = memarea_strndup(area, *s, eol-*s); + char *cp = mem; + int j = 0; + while (*cp) { + j++; + cp = (char*)find_whitespace(cp); + if (!cp || !*cp) + break; + cp = (char*)eat_whitespace(cp); + } + tok->n_args = j; + if (tok->n_args) { + tok->args = memarea_alloc(area, sizeof(char*)*tok->n_args); + cp = mem; j = 0; while (*cp) { - j++; + tok->args[j++] = cp; cp = (char*)find_whitespace(cp); - if (!cp || !*cp) - break; - cp = (char*)eat_whitespace(cp); - } - tok->n_args = j; - if (tok->n_args) { - tok->args = memarea_alloc(area, sizeof(char*)*tok->n_args); - cp = mem; - j = 0; - while (*cp) { - tok->args[j++] = cp; - cp = (char*)find_whitespace(cp); if (!cp || !*cp) break; *cp++ = '\0'; cp = (char*)eat_whitespace(cp); - } } - *s = eol; - } else { - /*XXXX021 make this needless. */ - j = 0; - allocated = 16; - tok->args = tor_malloc(sizeof(char*)*allocated); - while (*s < eol) { /* While not at eol, store the next token */ - if (j == allocated) { - allocated *= 2; - tok->args = tor_realloc(tok->args,sizeof(char*)*allocated); - } - tok->args[j++] = tor_strndup(*s, next-*s); - *s = eat_whitespace_eos_no_nl(next, eol); /* eat intra-line ws */ - next = find_whitespace_eos(*s, eol); /* find end of token at *s */ - } - tok->n_args = j; } + *s = eol; } if (tok->n_args < table[i].min_args) { tor_snprintf(ebuf, sizeof(ebuf), "Too few arguments to %s", kwd); @@ -2936,6 +2910,7 @@ tokenize_string(memarea_t *area, int i; int first_nonannotation; int prev_len = smartlist_len(out); + tor_assert(area); s = &start; if (!end)