xref: /freebsd/contrib/tcpdump/netdissect-alloc.c (revision ee67461e56828dd1f8de165947ba83f6d9148a87)
1*ee67461eSJoseph Mingrone /*
2*ee67461eSJoseph Mingrone  * Copyright (c) 2018 The TCPDUMP project
3*ee67461eSJoseph Mingrone  * All rights reserved.
4*ee67461eSJoseph Mingrone  *
5*ee67461eSJoseph Mingrone  * Redistribution and use in source and binary forms, with or without
6*ee67461eSJoseph Mingrone  * modification, are permitted provided that: (1) source code
7*ee67461eSJoseph Mingrone  * distributions retain the above copyright notice and this paragraph
8*ee67461eSJoseph Mingrone  * in its entirety, and (2) distributions including binary code include
9*ee67461eSJoseph Mingrone  * the above copyright notice and this paragraph in its entirety in
10*ee67461eSJoseph Mingrone  * the documentation or other materials provided with the distribution.
11*ee67461eSJoseph Mingrone  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
12*ee67461eSJoseph Mingrone  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
13*ee67461eSJoseph Mingrone  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14*ee67461eSJoseph Mingrone  * FOR A PARTICULAR PURPOSE.
15*ee67461eSJoseph Mingrone  */
16*ee67461eSJoseph Mingrone 
17*ee67461eSJoseph Mingrone #ifdef HAVE_CONFIG_H
18*ee67461eSJoseph Mingrone #include <config.h>
19*ee67461eSJoseph Mingrone #endif
20*ee67461eSJoseph Mingrone 
21*ee67461eSJoseph Mingrone #include <stdlib.h>
22*ee67461eSJoseph Mingrone #include "netdissect-alloc.h"
23*ee67461eSJoseph Mingrone 
24*ee67461eSJoseph Mingrone static void nd_add_alloc_list(netdissect_options *, nd_mem_chunk_t *);
25*ee67461eSJoseph Mingrone 
26*ee67461eSJoseph Mingrone /*
27*ee67461eSJoseph Mingrone  * nd_free_all() is intended to be used after a packet printing
28*ee67461eSJoseph Mingrone  */
29*ee67461eSJoseph Mingrone 
30*ee67461eSJoseph Mingrone /* Add a memory chunk in allocation linked list */
31*ee67461eSJoseph Mingrone static void
32*ee67461eSJoseph Mingrone nd_add_alloc_list(netdissect_options *ndo, nd_mem_chunk_t *chunkp)
33*ee67461eSJoseph Mingrone {
34*ee67461eSJoseph Mingrone 	if (ndo->ndo_last_mem_p == NULL)	/* first memory allocation */
35*ee67461eSJoseph Mingrone 		chunkp->prev_mem_p = NULL;
36*ee67461eSJoseph Mingrone 	else					/* previous memory allocation */
37*ee67461eSJoseph Mingrone 		chunkp->prev_mem_p = ndo->ndo_last_mem_p;
38*ee67461eSJoseph Mingrone 	ndo->ndo_last_mem_p = chunkp;
39*ee67461eSJoseph Mingrone }
40*ee67461eSJoseph Mingrone 
41*ee67461eSJoseph Mingrone /* malloc replacement, with tracking in a linked list */
42*ee67461eSJoseph Mingrone void *
43*ee67461eSJoseph Mingrone nd_malloc(netdissect_options *ndo, size_t size)
44*ee67461eSJoseph Mingrone {
45*ee67461eSJoseph Mingrone 	nd_mem_chunk_t *chunkp = malloc(sizeof(nd_mem_chunk_t) + size);
46*ee67461eSJoseph Mingrone 	if (chunkp == NULL)
47*ee67461eSJoseph Mingrone 		return NULL;
48*ee67461eSJoseph Mingrone 	nd_add_alloc_list(ndo, chunkp);
49*ee67461eSJoseph Mingrone 	return chunkp + 1;
50*ee67461eSJoseph Mingrone }
51*ee67461eSJoseph Mingrone 
52*ee67461eSJoseph Mingrone /* Free chunks in allocation linked list from last to first */
53*ee67461eSJoseph Mingrone void
54*ee67461eSJoseph Mingrone nd_free_all(netdissect_options *ndo)
55*ee67461eSJoseph Mingrone {
56*ee67461eSJoseph Mingrone 	nd_mem_chunk_t *current, *previous;
57*ee67461eSJoseph Mingrone 	current = ndo->ndo_last_mem_p;
58*ee67461eSJoseph Mingrone 	while (current != NULL) {
59*ee67461eSJoseph Mingrone 		previous = current->prev_mem_p;
60*ee67461eSJoseph Mingrone 		free(current);
61*ee67461eSJoseph Mingrone 		current = previous;
62*ee67461eSJoseph Mingrone 	}
63*ee67461eSJoseph Mingrone 	ndo->ndo_last_mem_p = NULL;
64*ee67461eSJoseph Mingrone }
65