From: Laurent Mazet Date: Fri, 30 Dec 2022 23:49:28 +0000 (+0100) Subject: free memories X-Git-Tag: v0.8~54 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=031d7bba7849b5b5ec7d1110f830175cc3b65b44;p=calc.git free memories --- diff --git a/calc.c b/calc.c index d24bda3..41cc6e0 100644 --- a/calc.c +++ b/calc.c @@ -98,13 +98,14 @@ int main (int argc, char *argv[]) if (buffer[i] == '\n') { buffer[i] = 0; VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j)); - element_t *element = parser (buffer, NULL, 0); - if (element == (void *)(-1)) { + element_t *element = parser (buffer + j, NULL, 0); + if (element == ERROR_OP) { VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer)); ret = 1; - } else { + } else { VERBOSE (INFO, print_element (element, 0)); PRINTOUT ("=> %f\n", evaluate_element (element, 0)); + delelement (element); ret = 0; } //fsync (stdfdout); @@ -168,5 +169,10 @@ int main (int argc, char *argv[]) // test: echo "-cos (0) + 1" | calc.exe | grep -q '=> 0' // test: echo "quit" | calc.exe | grep -q 'bye' // test: echo "help" | calc.exe | grep -q 'miscellaneous' +// test: echo "1 + 2 *" | calc.exe | grep -q 'error' +// test: echo "* 1 - 2" | calc.exe | grep -q 'error' +// test: echo "2 + * 3" | calc.exe | grep -q 'error' +// test: echo "sqrt 2" | calc.exe | grep -q 'error' +// test: echo "pow (2)" | calc.exe | grep -q 'error' /* vim: set ts=4 sw=4 et: */ diff --git a/makefile b/makefile index 0901675..b10fc9f 100644 --- a/makefile +++ b/makefile @@ -63,6 +63,9 @@ purge: clean rm -f purge $(ALLEXE:%=%.exe) $(call PASS, SUCCESS) +valgrinds: + $(MAKE) $(addprefix valgrind_,$(ALLEXE)) + wipe: purge $(call TITLE, "wiping") touch wipe @@ -102,10 +105,10 @@ test_%: %.test %.exe done; \ test "$$RC" -ne 1 -valgrind_%: % +valgrind_%: %.exe VALGRIND="valgrind -v --leak-check=full --show-reachable=yes --log-fd=2"; \ export VALGRIND; \ - $(MAKE) test_$< + $(MAKE) $(@:valgrind_%=test_%) %.d: %.c $(call TITLE, "Building $@") diff --git a/parser.c b/parser.c index 1d35b4c..2660b43 100644 --- a/parser.c +++ b/parser.c @@ -45,6 +45,21 @@ element_t *newelement (func_t function, int nbops, int prio) return new; } +/* desallocate element */ + +void delelement (element_t *root) +{ + int i; + if ((root != NULL) && (root != ERROR_OP)) { + for (i = 0; i < root->nbops; i++) { + if ((root->ops[i] != NULL) && (root->ops[i] != ERROR_OP)) { + delelement (root->ops[i]); + } + } + free (root); + } +} + /* functions */ #define NB_OPERATORS 6 @@ -82,10 +97,13 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in new->ops[0] = *proot; new->ops[1] = parser (*pstr, pstr, new->prio); if (new->ops[1] == ERROR_OP) { + delelement (new); + *proot = NULL; return ERROR_OP; } *proot = newelement (Val, 1, 5); - if (*proot == ERROR_OP) { + if (*proot == NULL) { + delelement (new); return ERROR_OP; } (*proot)->ops[0] = new; @@ -124,6 +142,7 @@ element_t *parser (char *str, char **next, int prio) found = 0; new = parser (str + 1, &str, 0); if (new == ERROR_OP) { + delelement (root); return ERROR_OP; } for (i = 0; i < root->nbops; i++) { @@ -134,6 +153,8 @@ element_t *parser (char *str, char **next, int prio) } } if (!found) { + delelement (new); + delelement (root); return ERROR_OP; } } while (*str == ','); @@ -144,6 +165,8 @@ element_t *parser (char *str, char **next, int prio) } new = parser (str + 1, &str, 0); if ((new == ERROR_OP) || (*str == ',')) { + delelement (new); + delelement (root); return ERROR_OP; } root->ops[0] = new; @@ -177,6 +200,7 @@ element_t *parser (char *str, char **next, int prio) str += operator->offset; VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func)); if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) { + delelement (root); return ERROR_OP; } } else if (*str == '-') { @@ -210,7 +234,8 @@ element_t *parser (char *str, char **next, int prio) return ERROR_OP; } root = new; - } else { + } else { + delelement (root); return ERROR_OP; } str += function->offset; @@ -248,12 +273,15 @@ element_t *parser (char *str, char **next, int prio) return root; } if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) { + delelement (root); return ERROR_OP; } } else { + delelement (root); return ERROR_OP; } } else { + delelement (root); return ERROR_OP; } found = 1; @@ -264,6 +292,7 @@ element_t *parser (char *str, char **next, int prio) /* error */ if (!found) { + delelement (root); return ERROR_OP; } @@ -272,6 +301,7 @@ element_t *parser (char *str, char **next, int prio) if (next != NULL) { *next = str; } + return root; } diff --git a/parser.h b/parser.h index 26022e5..90dba49 100644 --- a/parser.h +++ b/parser.h @@ -38,6 +38,8 @@ typedef struct _element_t { /* parser function */ +void delelement (element_t *root); + element_t *parser (char *str, char **next, int prio); void print_element (element_t *root, int level);