From c76a78c66b58c4efd80462f3b6ad42c5d5ecea7e Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Thu, 28 Aug 2025 21:20:13 -0400 Subject: [PATCH] don't leak memory --- gloss.py | 10 +++++----- markup.c | 29 +++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/gloss.py b/gloss.py index bf534cb39a50a6e96bc31493152c7d53bb9e97af..21bc8a17a44f647bb11072cd7be931311859eea3 100755 --- a/gloss.py +++ b/gloss.py @@ -119,15 +119,15 @@ def first_pass(slug, fp): class Markup: def __init__(self, where, *, cfile='markup.c', bfile='markup'): - self.cfile = str(Path(where, cfile)) - self.bfile = str(Path(where, bfile)) + self.cfile = Path(where, cfile) + self.bfile = Path(where, bfile) - if stat(self.cfile).st_mtime > stat(self.bfile).st_mtime: + if not self.bfile.exists() or self.cfile.stat().st_mtime > self.bfile.stat().st_mtime: print("recompiling markup subsystem...") - runcmd(['cc', self.cfile, '-o', self.bfile, '-Wall'], check=True) + runcmd(['cc', str(self.cfile), '-o', str(self.bfile), '-Wall'], check=True) print("recompiled!") - self.proc = Popen([self.bfile, 'convert'], stdin=PIPE, stdout=PIPE, text=True) + self.proc = Popen([str(self.bfile), 'convert'], stdin=PIPE, stdout=PIPE, text=True) def process(self, text): self.proc.stdin.write(text+"\n") diff --git a/markup.c b/markup.c index 1d5765780b35398ddf2cc23f73f6c08e2fa06d46..663a5c7ed221e26817d1ac3c49396e4f9a0a0714 100644 --- a/markup.c +++ b/markup.c @@ -22,7 +22,7 @@ #define DEBUG 0 #endif -#define PLINE() fprintf(stderr, "on line %d in func %s\n", __LINE__, __FILE__) +#define PLINE() fprintf(stderr, "on line %d\n", __LINE__) #define LEN(x) (sizeof(x) / sizeof(x[0])) @@ -79,6 +79,19 @@ new_segment(int type, size_t length, const char *text) return new; } +static inline void +shift_segment(struct segment **seg) +{ + struct segment *next = (*seg)->next; + free(*seg); + *seg = next; +} +static inline void +free_segments(struct segment *seg) +{ + while(seg != NULL) + shift_segment(&seg); +} static inline void push_segment(struct segment **seg, int type, size_t length, const char *text) @@ -154,7 +167,7 @@ segmentize(size_t length, const char *source) // p is the start of the most recent text segment const char *p = source, *i, *newi, *end = source + length; int in_italic = 0; - struct segment *head = new_segment(SEG_HEAD, 0, NULL), *ret; + struct segment *head = new_segment(SEG_HEAD, 0, NULL); struct segment *tail = head; struct segment *italic_head, *italic_tail; struct segment *link_head, *link_tail; @@ -243,9 +256,8 @@ segmentize(size_t length, const char *source) tail->next = new_segment(SEG_TEXT, end - p, p); } - ret = head->next; - free(head); - return ret; + shift_segment(&head); + return head; } static const char * @@ -284,16 +296,17 @@ convert() { size_t length; const char *s; - struct segment *seg; + struct segment *seg, *p; while((s = nextline()) != NULL){ if(DEBUG) fprintf(stderr, "processing line: %s\n", s); length = strlen(s); seg = segmentize(length, s); - for(; seg != NULL; seg = seg->next) - print_segment(seg); + for(p = seg; p != NULL; p = p->next) + print_segment(p); puts("NEXT"); + free_segments(seg); fflush(stdout); } }