ed

simple ed
git clone git://git.2f30.org/ed.git
Log | Files | Refs

commit f7835588a3b9d7411ecbbdbe133191ccec714e50
parent 7283f7b7e396bbdc8f6ac0e77a41ec2faae67e0d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun Dec 13 20:41:01 +0100

Use a better allocation schema for undo

We were using a realloc by one schema in
undo data, which can be really inefficient.
This commits use a 10 items at time allocation
schema and creates a struct for the undo data.

Diffstat:
ed.c | 49++++++++++++++++++++++++++++++-------------------
1 file changed, 30 insertions(+), 19 deletions(-)
diff --git a/ed.c b/ed.c @@ -24,12 +24,15 @@ struct hline { int next, prev; }; -struct editop { - int to1, from1; - int to2, from2; +struct undo { + int curln; + size_t nr, cap; + struct link { + int to1, from1; + int to2, from2; + } *vec; }; - char *prompt = "*"; regex_t *pattern; regmatch_t matchs[10]; @@ -56,8 +59,7 @@ size_t cmdsiz, cmdcap; int repidx; char *rhs; char *lastmatch; -struct editop *undoptr; -int nr_undo; +struct undo udata; static void error(char *msg) @@ -226,36 +228,45 @@ setglobal(int i, int v) static void relink(int to1, int from1, int from2, int to2) { - struct editop *p; + struct link *p; - if ((p = realloc(undoptr, sizeof(*p) * (nr_undo + 1))) == NULL) - error("out of memory"); - undoptr = p; - p += nr_undo; + if (udata.nr >= udata.cap) { + size_t siz = (udata.cap + 10) * sizeof(struct link); + if ((p = realloc(udata.vec, siz)) == NULL) + error("out of memory"); + udata.vec = p; + udata.cap = udata.cap + 10; + } + p = &udata.vec[udata.nr++]; p->from1 = from1; p->to1 = zero[from1].next; - p->to2 = zero[from2].prev; p->from2 = from2; + p->to2 = zero[from2].prev; zero[from1].next = to1; zero[from2].prev = to2; modflag = 1; - ++nr_undo; + fprintf(stderr, "linking %d <- %d, %d -> %d\n", to2, from2, from1, to1); } static void undo(void) { int i; - struct editop *p; + struct link *p; - for (p = &undoptr[nr_undo]; p-- > undoptr; ) { + if (udata.nr == 0) + error("nothing to undo"); + for (p = &udata.vec[udata.nr-1]; udata.nr--; --p) { zero[p->from1].next = p->to1; - zero[p->from2].next = p->to2; + zero[p->from2].prev = p->to2; + fprintf(stderr, + "linking %d <- %d, %d -> %d\n", + p->to2, p->from2, p->from1, p->to1); } - free(undoptr); - nr_undo = 0; - undoptr = NULL; + free(udata.vec); + udata.vec = NULL; + udata.cap = 0; } static void