source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mandoc.bsd.lv
To: source@mandoc.bsd.lv
Subject: mandoc: Provide a cleanup function for the term_tab module, freeing
Date: Mon, 4 Oct 2021 13:57:01 -0500 (EST)	[thread overview]
Message-ID: <c2aaf771be06b389@mandoc.bsd.lv> (raw)

Log Message:
-----------
Provide a cleanup function for the term_tab module, freeing memory
and resetting the internal state to the initial state.
Call this function from the proper place in term_free().

With the way the module is currently used, this does not imply any 
functional change, but doing proper cleanup is more robust, makes
it easier during code review to understand what is going on, and
makes it explicit that there is no memory leak.

Modified Files:
--------------
    mandoc:
        term.c
        term.h
        term_tab.c

Revision Data
-------------
Index: term_tab.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/term_tab.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -Lterm_tab.c -Lterm_tab.c -u -p -r1.6 -r1.7
--- term_tab.c
+++ term_tab.c
@@ -1,6 +1,6 @@
 /* $Id$ */
 /*
- * Copyright (c) 2017 Ingo Schwarze <schwarze@openbsd.org>
+ * Copyright (c) 2017, 2021 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -19,6 +19,8 @@
 #include <sys/types.h>
 
 #include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include "mandoc_aux.h"
 #include "out.h"
@@ -33,6 +35,7 @@ struct tablist {
 static struct {
 	struct tablist	 a;	/* All tab positions for lookup. */
 	struct tablist	 p;	/* Periodic tab positions to add. */
+	struct tablist	*r;	/* Tablist currently being recorded. */
 	size_t		 d;	/* Default tab width in units of n. */
 } tabs;
 
@@ -40,8 +43,6 @@ static struct {
 void
 term_tab_set(const struct termp *p, const char *arg)
 {
-	static int	 recording_period;
-
 	struct roffsu	 su;
 	struct tablist	*tl;
 	size_t		 pos;
@@ -51,7 +52,7 @@ term_tab_set(const struct termp *p, cons
 
 	if (arg == NULL) {
 		tabs.a.n = tabs.p.n = 0;
-		recording_period = 0;
+		tabs.r = &tabs.a;
 		if (tabs.d == 0) {
 			a2roffsu(".8i", &su, SCALE_IN);
 			tabs.d = term_hen(p, &su);
@@ -59,7 +60,7 @@ term_tab_set(const struct termp *p, cons
 		return;
 	}
 	if (arg[0] == 'T' && arg[1] == '\0') {
-		recording_period = 1;
+		tabs.r = &tabs.p;
 		return;
 	}
 
@@ -75,7 +76,7 @@ term_tab_set(const struct termp *p, cons
 
 	/* Select the list, and extend it if it is full. */
 
-	tl = recording_period ? &tabs.p : &tabs.a;
+	tl = tabs.r;
 	if (tl->n >= tl->s) {
 		tl->s += 8;
 		tl->t = mandoc_reallocarray(tl->t, tl->s, sizeof(*tl->t));
@@ -127,4 +128,13 @@ term_tab_next(size_t prev)
 		if (prev < tabs.a.t[i])
 			return tabs.a.t[i];
 	}
+}
+
+void
+term_tab_free(void)
+{
+	free(tabs.a.t);
+	free(tabs.p.t);
+	memset(&tabs, 0, sizeof(tabs));
+	tabs.r = &tabs.a;
 }
Index: term.h
===================================================================
RCS file: /home/cvs/mandoc/mandoc/term.h,v
retrieving revision 1.131
retrieving revision 1.132
diff -Lterm.h -Lterm.h -u -p -r1.131 -r1.132
--- term.h
+++ term.h
@@ -1,4 +1,4 @@
-/*	$Id$ */
+/* $Id$ */
 /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2011-2015, 2017, 2019 Ingo Schwarze <schwarze@openbsd.org>
@@ -150,6 +150,7 @@ size_t		  term_len(const struct termp *,
 void		  term_tab_set(const struct termp *, const char *);
 void		  term_tab_iset(size_t);
 size_t		  term_tab_next(size_t);
+void		  term_tab_free(void);
 
 void		  term_fontpush(struct termp *, enum termfont);
 void		  term_fontpop(struct termp *);
Index: term.c
===================================================================
RCS file: /home/cvs/mandoc/mandoc/term.c,v
retrieving revision 1.283
retrieving revision 1.284
diff -Lterm.c -Lterm.c -u -p -r1.283 -r1.284
--- term.c
+++ term.c
@@ -1,7 +1,7 @@
 /* $Id$ */
 /*
+ * Copyright (c) 2010-2021 Ingo Schwarze <schwarze@openbsd.org>
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
- * Copyright (c) 2010-2020 Ingo Schwarze <schwarze@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -58,6 +58,7 @@ term_setcol(struct termp *p, size_t maxt
 void
 term_free(struct termp *p)
 {
+	term_tab_free();
 	for (p->tcol = p->tcols; p->tcol < p->tcols + p->maxtcol; p->tcol++)
 		free(p->tcol->buf);
 	free(p->tcols);
--
 To unsubscribe send an email to source+unsubscribe@mandoc.bsd.lv


                 reply	other threads:[~2021-10-04 18:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c2aaf771be06b389@mandoc.bsd.lv \
    --to=schwarze@mandoc.bsd.lv \
    --cc=source@mandoc.bsd.lv \
    --subject='Re: mandoc: Provide a cleanup function for the term_tab module, freeing' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).