diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-11 12:13:09 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-11 12:13:09 +0300 |
commit | 590e208c40797206fd6a93651fc59e0b68eeb545 (patch) | |
tree | d7abd0ca33b3355b1a27760bf9850e445fafd607 | |
parent | db81e378576dcc5510032c72060e48e562f208c9 (diff) | |
download | wydawca-590e208c40797206fd6a93651fc59e0b68eeb545.tar.gz wydawca-590e208c40797206fd6a93651fc59e0b68eeb545.tar.bz2 |
Remove obstack.
* src/txtacc.c: New file.
* gnulib.modules: Remove obstack.
* src/wydawca.h (txtacc_create, txtacc_free)
(txtacc_free_string, txtacc_grow)
(txtacc_finish): New functions.
(txtacc_1grow): New macro.
-rw-r--r-- | gnulib.modules | 1 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/builtin.c | 16 | ||||
-rw-r--r-- | src/cmdline.opt | 24 | ||||
-rw-r--r-- | src/directive.c | 6 | ||||
-rw-r--r-- | src/lock.c | 2 | ||||
-rw-r--r-- | src/meta.c | 26 | ||||
-rw-r--r-- | src/report.c | 17 | ||||
-rw-r--r-- | src/triplet.c | 8 | ||||
-rw-r--r-- | src/txtacc.c | 177 | ||||
-rw-r--r-- | src/verify.c | 6 | ||||
-rw-r--r-- | src/wydawca.c | 1 | ||||
-rw-r--r-- | src/wydawca.h | 18 |
13 files changed, 242 insertions, 61 deletions
diff --git a/gnulib.modules b/gnulib.modules index 4eb77a8..dd5124a 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -1,3 +1,2 @@ xalloc -obstack getline diff --git a/src/Makefile.am b/src/Makefile.am index 8fd7d94..8139849 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,2 +48,3 @@ wydawca_SOURCES=\ timer.c\ + txtacc.c\ report.c diff --git a/src/builtin.c b/src/builtin.c index 412a2e7..0a76738 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -151,3 +151,3 @@ struct builtin_data_storage { - struct obstack stk; + struct txtacc *acc; char **wp; @@ -166,3 +166,3 @@ builtin_lookup (struct dictionary *dict, void *handle, const char *req) size_t count = 0; - struct obstack stk; + struct txtacc *acc; int flags = 0; @@ -178,3 +178,3 @@ builtin_lookup (struct dictionary *dict, void *handle, const char *req) - obstack_init (&stk); + acc = txtacc_create (); @@ -200,3 +200,3 @@ builtin_lookup (struct dictionary *dict, void *handle, const char *req) char *val = dict->parmv[i + j]; - obstack_grow (&stk, val, strlen (val) + 1); + txtacc_grow (acc, val, strlen (val) + 1); } @@ -213,3 +213,3 @@ builtin_lookup (struct dictionary *dict, void *handle, const char *req) { - obstack_free (&stk, NULL); + txtacc_free (acc); bds = NULL; @@ -225,4 +225,4 @@ builtin_lookup (struct dictionary *dict, void *handle, const char *req) bds->wp = xcalloc (count, sizeof (bds->wp[0])); - bds->stk = stk; - p = obstack_finish (&stk); + bds->acc = acc; + p = txtacc_finish (acc); @@ -247,3 +247,3 @@ builtin_free_result (struct dictionary *dict, void *handle) struct builtin_data_storage *bds = dict->storage; - obstack_free (&bds->stk, NULL); + txtacc_free (bds->acc); free (bds->wp); diff --git a/src/cmdline.opt b/src/cmdline.opt index 4c3d22f..afa1949 100644 --- a/src/cmdline.opt +++ b/src/cmdline.opt @@ -16,4 +16,3 @@ -static struct obstack pp_cmd_stack; -static int pp_cmd_stack_init; +static struct txtacc *pp_cmd_acc; @@ -154,8 +153,5 @@ BEGIN - if (!pp_cmd_stack_init) - { - obstack_init (&pp_cmd_stack); - pp_cmd_stack_init = 1; - } - obstack_grow (&pp_cmd_stack, " \"-D", 4); + if (!pp_cmd_acc) + pp_cmd_acc = txtacc_create (); + txtacc_grow (pp_cmd_acc, " \"-D", 4); for (p = optarg; *p; p++) @@ -163,6 +159,6 @@ BEGIN if (*p == '\\' || *p == '"') - obstack_1grow (&pp_cmd_stack, '\\'); - obstack_1grow (&pp_cmd_stack, *p); + txtacc_1grow (pp_cmd_acc, '\\'); + txtacc_1grow (pp_cmd_acc, *p); } - obstack_1grow (&pp_cmd_stack, '"'); + txtacc_1grow (pp_cmd_acc, '"'); END @@ -215,5 +211,5 @@ parse_options(int argc, char *argv[]) GETOPT(argc, argv) - if (pp_cmd_stack_init && grecs_preprocessor) + if (pp_cmd_acc && grecs_preprocessor) { - char *defs = obstack_finish (&pp_cmd_stack); + char *defs = txtacc_finish (pp_cmd_acc); char *cmd = xmalloc (strlen (grecs_preprocessor) + strlen (defs) + 1); @@ -222,3 +218,3 @@ parse_options(int argc, char *argv[]) grecs_preprocessor = cmd; - obstack_free (&pp_cmd_stack, NULL); + txtacc_free (pp_cmd_acc); } diff --git a/src/directive.c b/src/directive.c index 34e5feb..1f11d77 100644 --- a/src/directive.c +++ b/src/directive.c @@ -500,6 +500,6 @@ run_check_script (const char *script, struct file_triplet *trp, logmsg (LOG_DEBUG, _("read: %s"), buf); - obstack_grow (&trp->obstk, buf, len); + txtacc_grow (trp->acc, buf, len); total += size; } - obstack_1grow (&trp->obstk, 0); + txtacc_1grow (trp->acc, 0); if (debug_level > 2) @@ -513,3 +513,3 @@ run_check_script (const char *script, struct file_triplet *trp, if (total) - trp->check_diag = obstack_finish (&trp->obstk); + trp->check_diag = txtacc_finish (trp->acc); @@ -178,3 +178,3 @@ host_name () static char *hostbuf = NULL; - size_t size; + size_t size = 0; int rc; @@ -56,3 +56,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, char *res; - struct obstack stk; + struct txtacc *acc; @@ -61,3 +61,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, - obstack_init (&stk); + acc = txtacc_create (); @@ -68,3 +68,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, - obstack_grow (&stk, p, len); + txtacc_grow (acc, p, len); p += len; @@ -75,3 +75,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, case '$': - obstack_grow (&stk, p, 1); + txtacc_grow (acc, p, 1); p++; @@ -94,3 +94,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, dictionary_quote_string (dict, handle, s, &newval, &len); - obstack_grow (&stk, newval, len); + txtacc_grow (acc, newval, len); free (newval); @@ -98,3 +98,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, else - obstack_grow (&stk, s, strlen (s)); + txtacc_grow (acc, s, strlen (s)); p = e + 1; @@ -103,3 +103,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, { - obstack_grow (&stk, p - 1, 2); + txtacc_grow (acc, p - 1, 2); p++; @@ -123,3 +123,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, dictionary_quote_string (dict, handle, s, &newval, &len); - obstack_grow (&stk, newval, len); + txtacc_grow (acc, newval, len); free (newval); @@ -127,3 +127,3 @@ meta_expand_string (const char *string, struct metadef *def, void *data, else - obstack_grow (&stk, s, len); + txtacc_grow (acc, s, len); p++; @@ -132,7 +132,7 @@ meta_expand_string (const char *string, struct metadef *def, void *data, else - obstack_grow (&stk, p, 1); + txtacc_grow (acc, p, 1); } - obstack_1grow (&stk, 0); - res = xstrdup (obstack_finish (&stk)); - obstack_free (&stk, NULL); + txtacc_1grow (acc, 0); + res = xstrdup (txtacc_finish (acc)); + txtacc_free (acc); return res; diff --git a/src/report.c b/src/report.c index b09c7c7..929bfff 100644 --- a/src/report.c +++ b/src/report.c @@ -18,4 +18,3 @@ -static struct obstack report_stk; -static int report_stk_inited; +static struct txtacc *report_acc; char *report_string; @@ -25,6 +24,6 @@ report_init () { - if (!report_stk_inited) - obstack_init (&report_stk); + if (!report_acc) + report_acc = txtacc_create (); else - obstack_free (&report_stk, report_string); + txtacc_free_string (report_acc, report_string); } @@ -43,4 +42,4 @@ report_add (const char *fmt, ...) { - obstack_grow (&report_stk, str, strlen (str)); - obstack_1grow (&report_stk, '\n'); + txtacc_grow (report_acc, str, strlen (str)); + txtacc_1grow (report_acc, '\n'); } @@ -52,4 +51,4 @@ report_finish () { - obstack_1grow (&report_stk, 0); - report_string = obstack_finish (&report_stk); + txtacc_1grow (report_acc, 0); + report_string = txtacc_finish (report_acc); } diff --git a/src/triplet.c b/src/triplet.c index bfd58e5..c214385 100644 --- a/src/triplet.c +++ b/src/triplet.c @@ -56,3 +56,3 @@ hash_triplet_free (void *data) free (tp->tmp); - obstack_free (&tp->obstk, NULL); + txtacc_free (tp->acc); @@ -73,4 +73,4 @@ triplet_strdup (struct file_triplet *tp, const char *str) size_t len = strlen (str); - obstack_grow (&tp->obstk, str, len + 1); - return obstack_finish (&tp->obstk); + txtacc_grow (tp->acc, str, len + 1); + return txtacc_finish (tp->acc); } @@ -107,3 +107,3 @@ register_file (struct file_info *finfo, const struct spool *spool) ret->spool = spool; - obstack_init (&ret->obstk); + ret->acc = txtacc_create (); } diff --git a/src/txtacc.c b/src/txtacc.c new file mode 100644 index 0000000..c094c22 --- /dev/null +++ b/src/txtacc.c @@ -0,0 +1,177 @@ +/* wydawca - automatic release submission daemon + Copyright (C) 2007, 2009, 2010 Sergey Poznyakoff + + Wydawca is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + Wydawca is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with wydawca. If not, see <http://www.gnu.org/licenses/>. */ + +#include "wydawca.h" + +struct txtacc_entry +{ + char *buf; /* Text buffer */ + size_t size; /* Buffer size */ + size_t len; /* Actual number of bytes in buffer */ +}; +#define TXTACC_BUFSIZE 1024 +#define txtacc_entry_freesize(e) ((e)->size - (e)->len) + +struct txtacc +{ + struct grecs_list *cur; /* Current build list */ + struct grecs_list *mem; /* List of already allocated elements */ +}; + +static struct txtacc_entry * +txtacc_alloc_entry (struct grecs_list *list, size_t size) +{ + struct txtacc_entry *p = xmalloc (sizeof (*p)); + p->buf = xmalloc (size); + p->size = size; + p->len = 0; + grecs_list_append (list, p); + return p; +} + +static struct txtacc_entry * +txtacc_cur_entry (struct txtacc *acc) +{ + struct txtacc_entry *ent; + + if (grecs_list_size (acc->cur) == 0) + return txtacc_alloc_entry (acc->cur, TXTACC_BUFSIZE); + ent = acc->cur->tail->data; + if (txtacc_entry_freesize (ent) == 0) + ent = txtacc_alloc_entry (acc->cur, TXTACC_BUFSIZE); + return ent; +} + +static void +txtacc_entry_append (struct txtacc_entry *ent, const char *p, size_t size) +{ + memcpy (ent->buf + ent->len, p, size); + ent->len += size; +} + +static void +txtacc_entry_tailor (struct txtacc_entry *ent) +{ + if (ent->size > ent->len) + { + char *p = realloc (ent->buf, ent->len); + if (!p) + return; + ent->buf = p; + ent->size = ent->len; + } +} + +static void +txtacc_entry_free (void *p) +{ + if (p) + { + struct txtacc_entry *ent = p; + free (ent->buf); + free (ent); + } +} + +struct txtacc * +txtacc_create () +{ + struct txtacc *acc = xmalloc (sizeof (*acc)); + acc->cur = grecs_list_create (); + acc->cur->free_entry = txtacc_entry_free; + acc->mem = grecs_list_create (); + acc->mem->free_entry = txtacc_entry_free; + return acc; +} + +void +txtacc_free (struct txtacc *acc) +{ + grecs_list_free (acc->cur); + grecs_list_free (acc->mem); + free (acc); +} + +void +txtacc_grow (struct txtacc *acc, const char *buf, size_t size) +{ + while (size) + { + struct txtacc_entry *ent = txtacc_cur_entry (acc); + size_t rest = txtacc_entry_freesize (ent); + if (rest > size) + rest = size; + txtacc_entry_append (ent, buf, rest); + buf += rest; + size -= rest; + } +} + +char * +txtacc_finish (struct txtacc *acc) +{ + struct grecs_list_entry *ep; + struct txtacc_entry *txtent; + size_t size; + + switch (grecs_list_size (acc->cur)) + { + case 0: + return NULL; + + case 1: + txtent = acc->cur->head->data; + acc->cur->head->data = NULL; + grecs_list_append (acc->mem, txtent); + txtacc_entry_tailor (txtent); + break; + + default: + size = 0; + for (ep = acc->cur->head; ep; ep = ep->next) + { + txtent = ep->data; + size += txtent->len; + } + + txtent = txtacc_alloc_entry (acc->mem, size); + for (ep = acc->cur->head; ep; ep = ep->next) + { + struct txtacc_entry *tp = ep->data; + txtacc_entry_append (txtent, tp->buf, tp->len); + } + } + + grecs_list_clear (acc->cur); + return txtent->buf; +} + +void +txtacc_free_string (struct txtacc *acc, char *str) +{ + struct grecs_list_entry *ep; + for (ep = acc->mem->head; ep; ep = ep->next) + { + struct txtacc_entry *tp = ep->data; + if (tp->buf == str) + { + grecs_list_remove_entry(acc->mem, ep); + free (tp->buf); + return; + } + } +} + diff --git a/src/verify.c b/src/verify.c index 93fd28b..b3233e8 100644 --- a/src/verify.c +++ b/src/verify.c @@ -171,5 +171,5 @@ fill_project_name (struct file_triplet *trp) } - obstack_grow (&trp->obstk, trp->relative_dir, len); - obstack_1grow (&trp->obstk, 0); - trp->project = obstack_finish (&trp->obstk); + txtacc_grow (trp->acc, trp->relative_dir, len); + txtacc_1grow (trp->acc, 0); + trp->project = txtacc_finish (trp->acc); } diff --git a/src/wydawca.c b/src/wydawca.c index bda3065..cc1815b 100644 --- a/src/wydawca.c +++ b/src/wydawca.c @@ -18,3 +18,2 @@ #include "mail.h" -#include "argmatch.h" diff --git a/src/wydawca.h b/src/wydawca.h index 8d5dd34..ef1ff86 100644 --- a/src/wydawca.h +++ b/src/wydawca.h @@ -48,5 +48,2 @@ -#define obstack_chunk_alloc malloc -#define obstack_chunk_free free -#include "obstack.h" #include "error.h" @@ -174,3 +171,3 @@ struct file_triplet size_t tmpsize; /* Size of memory allocated in tmp */ - struct obstack obstk; /* Obstack for string allocation */ + struct txtacc *acc; /* Text accumulator for string allocation */ /* User data */ @@ -560 +557,14 @@ int pop_dir (void); char *getcwd_alloc (void); + +struct txtacc *txtacc_create (void); +void txtacc_free (struct txtacc *acc); +void txtacc_free_string (struct txtacc *acc, char *str); +void txtacc_grow (struct txtacc *acc, const char *buf, size_t size); +#define txtacc_1grow(acc, c) \ + do \ + { \ + char __ch = c; \ + txtacc_grow (acc, &__ch, 1); \ + } \ + while (0) +char *txtacc_finish (struct txtacc *acc); |