make: simplify freeing of lists

This commit is contained in:
rillig 2024-04-27 17:33:46 +00:00
parent 01e711ac18
commit 1bf9200945
7 changed files with 37 additions and 34 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: arch.c,v 1.215 2024/02/07 06:43:02 rillig Exp $ */
/* $NetBSD: arch.c,v 1.216 2024/04/27 17:33:46 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -126,7 +126,7 @@
#include "config.h"
/* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
MAKE_RCSID("$NetBSD: arch.c,v 1.215 2024/02/07 06:43:02 rillig Exp $");
MAKE_RCSID("$NetBSD: arch.c,v 1.216 2024/04/27 17:33:46 rillig Exp $");
typedef struct List ArchList;
typedef struct ListNode ArchListNode;
@ -151,9 +151,8 @@ static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
#ifdef CLEANUP
static void
ArchFree(void *ap)
ArchFree(Arch *a)
{
Arch *a = ap;
HashIter hi;
/* Free memory from hash entries */
@ -1070,7 +1069,11 @@ void
Arch_End(void)
{
#ifdef CLEANUP
Lst_DoneCall(&archives, ArchFree);
ArchListNode *ln;
for (ln = archives.first; ln != NULL; ln = ln->next)
ArchFree(ln->datum);
Lst_Done(&archives);
#endif
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lst.c,v 1.107 2023/12/29 20:43:58 rillig Exp $ */
/* $NetBSD: lst.c,v 1.108 2024/04/27 17:33:46 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -34,7 +34,7 @@
#include "make.h"
MAKE_RCSID("$NetBSD: lst.c,v 1.107 2023/12/29 20:43:58 rillig Exp $");
MAKE_RCSID("$NetBSD: lst.c,v 1.108 2024/04/27 17:33:46 rillig Exp $");
static ListNode *
LstNodeNew(ListNode *prev, ListNode *next, void *datum)
@ -60,13 +60,13 @@ Lst_Done(List *list)
}
void
Lst_DoneCall(List *list, LstFreeProc freeProc)
Lst_DoneFree(List *list)
{
ListNode *ln, *next;
for (ln = list->first; ln != NULL; ln = next) {
next = ln->next;
freeProc(ln->datum);
free(ln->datum);
free(ln);
}
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: lst.h,v 1.104 2023/12/29 20:43:58 rillig Exp $ */
/* $NetBSD: lst.h,v 1.105 2024/04/27 17:33:46 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@ -98,13 +98,10 @@ struct List {
ListNode *last;
};
/* Free the datum of a node, called before freeing the node itself. */
typedef void LstFreeProc(void *);
/* Free the list nodes, but not the list itself. */
/* Free the list nodes. */
void Lst_Done(List *);
/* Free the list nodes, freeing the node data using the given function. */
void Lst_DoneCall(List *, LstFreeProc);
/* Free the list nodes, as well as each node's datum. */
void Lst_DoneFree(List *);
#define LST_INIT { NULL, NULL }

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.612 2024/03/10 02:53:37 sjg Exp $ */
/* $NetBSD: main.c,v 1.613 2024/04/27 17:33:46 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -111,7 +111,7 @@
#include "trace.h"
/* "@(#)main.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: main.c,v 1.612 2024/03/10 02:53:37 sjg Exp $");
MAKE_RCSID("$NetBSD: main.c,v 1.613 2024/04/27 17:33:46 rillig Exp $");
#if defined(MAKE_NATIVE)
__COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
"The Regents of the University of California. "
@ -1199,7 +1199,7 @@ ReadBuiltinRules(void)
Fatal("%s: cannot open %s.",
progname, (const char *)sysMkFiles.first->datum);
Lst_DoneCall(&sysMkFiles, free);
Lst_DoneFree(&sysMkFiles);
}
static void
@ -1564,9 +1564,9 @@ static void
main_CleanUp(void)
{
#ifdef CLEANUP
Lst_DoneCall(&opts.variables, free);
Lst_DoneCall(&opts.makefiles, free);
Lst_DoneCall(&opts.create, free);
Lst_DoneFree(&opts.variables);
Lst_DoneFree(&opts.makefiles);
Lst_DoneFree(&opts.create);
#endif
if (DEBUG(GRAPH2))

View File

@ -1,4 +1,4 @@
/* $NetBSD: meta.c,v 1.207 2023/12/17 09:02:26 rillig Exp $ */
/* $NetBSD: meta.c,v 1.208 2024/04/27 17:33:46 rillig Exp $ */
/*
* Implement 'meta' mode.
@ -1595,7 +1595,7 @@ meta_oodate(GNode *gn, bool oodate)
}
}
Lst_DoneCall(&missingFiles, free);
Lst_DoneFree(&missingFiles);
if (oodate && needOODATE) {
/*

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.721 2024/04/23 22:51:28 rillig Exp $ */
/* $NetBSD: parse.c,v 1.722 2024/04/27 17:33:46 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -105,7 +105,7 @@
#include "pathnames.h"
/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: parse.c,v 1.721 2024/04/23 22:51:28 rillig Exp $");
MAKE_RCSID("$NetBSD: parse.c,v 1.722 2024/04/27 17:33:46 rillig Exp $");
/* Detects a multiple-inclusion guard in a makefile. */
typedef enum {
@ -2966,7 +2966,7 @@ Parse_End(void)
#ifdef CLEANUP
HashIter hi;
Lst_DoneCall(&targCmds, free);
Lst_DoneFree(&targCmds);
assert(targets == NULL);
SearchPath_Free(defSysIncPath);
SearchPath_Free(sysIncPath);

View File

@ -1,4 +1,4 @@
/* $NetBSD: targ.c,v 1.180 2024/03/10 02:53:37 sjg Exp $ */
/* $NetBSD: targ.c,v 1.181 2024/04/27 17:33:47 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -107,7 +107,7 @@
#include "dir.h"
/* "@(#)targ.c 8.2 (Berkeley) 3/19/94" */
MAKE_RCSID("$NetBSD: targ.c,v 1.180 2024/03/10 02:53:37 sjg Exp $");
MAKE_RCSID("$NetBSD: targ.c,v 1.181 2024/04/27 17:33:47 rillig Exp $");
/*
* All target nodes that appeared on the left-hand side of one of the
@ -119,7 +119,7 @@ static HashTable allTargetsByName;
#ifdef CLEANUP
static GNodeList allNodes = LST_INIT;
static void GNode_Free(void *);
static void GNode_Free(GNode *);
#endif
void
@ -131,11 +131,16 @@ Targ_Init(void)
void
Targ_End(void)
{
#ifdef CLEANUP
GNodeListNode *ln;
#endif
Targ_Stats();
#ifdef CLEANUP
Lst_Done(&allTargets);
HashTable_Done(&allTargetsByName);
Lst_DoneCall(&allNodes, GNode_Free);
for (ln = allNodes.first; ln != NULL; ln = ln->next)
GNode_Free(ln->datum);
Lst_Done(&allNodes);
#endif
}
@ -212,10 +217,8 @@ GNode_New(const char *name)
#ifdef CLEANUP
static void
GNode_Free(void *gnp)
GNode_Free(GNode *gn)
{
GNode *gn = gnp;
free(gn->name);
free(gn->uname);
free(gn->path);