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 #include <config.h>
18*ee67461eSJoseph Mingrone
19*ee67461eSJoseph Mingrone #include <stdlib.h>
20*ee67461eSJoseph Mingrone #include "netdissect-alloc.h"
21*ee67461eSJoseph Mingrone
22*ee67461eSJoseph Mingrone static void nd_add_alloc_list(netdissect_options *, nd_mem_chunk_t *);
23*ee67461eSJoseph Mingrone
24*ee67461eSJoseph Mingrone /*
25*ee67461eSJoseph Mingrone * nd_free_all() is intended to be used after a packet printing
26*ee67461eSJoseph Mingrone */
27*ee67461eSJoseph Mingrone
28*ee67461eSJoseph Mingrone /* Add a memory chunk in allocation linked list */
29*ee67461eSJoseph Mingrone static void
nd_add_alloc_list(netdissect_options * ndo,nd_mem_chunk_t * chunkp)30*ee67461eSJoseph Mingrone nd_add_alloc_list(netdissect_options *ndo, nd_mem_chunk_t *chunkp)
31*ee67461eSJoseph Mingrone {
32*ee67461eSJoseph Mingrone if (ndo->ndo_last_mem_p == NULL) /* first memory allocation */
33*ee67461eSJoseph Mingrone chunkp->prev_mem_p = NULL;
34*ee67461eSJoseph Mingrone else /* previous memory allocation */
35*ee67461eSJoseph Mingrone chunkp->prev_mem_p = ndo->ndo_last_mem_p;
36*ee67461eSJoseph Mingrone ndo->ndo_last_mem_p = chunkp;
37*ee67461eSJoseph Mingrone }
38*ee67461eSJoseph Mingrone
39*ee67461eSJoseph Mingrone /* malloc replacement, with tracking in a linked list */
40*ee67461eSJoseph Mingrone void *
nd_malloc(netdissect_options * ndo,size_t size)41*ee67461eSJoseph Mingrone nd_malloc(netdissect_options *ndo, size_t size)
42*ee67461eSJoseph Mingrone {
43*ee67461eSJoseph Mingrone nd_mem_chunk_t *chunkp = malloc(sizeof(nd_mem_chunk_t) + size);
44*ee67461eSJoseph Mingrone if (chunkp == NULL)
45*ee67461eSJoseph Mingrone return NULL;
46*ee67461eSJoseph Mingrone nd_add_alloc_list(ndo, chunkp);
47*ee67461eSJoseph Mingrone return chunkp + 1;
48*ee67461eSJoseph Mingrone }
49*ee67461eSJoseph Mingrone
50*ee67461eSJoseph Mingrone /* Free chunks in allocation linked list from last to first */
51*ee67461eSJoseph Mingrone void
nd_free_all(netdissect_options * ndo)52*ee67461eSJoseph Mingrone nd_free_all(netdissect_options *ndo)
53*ee67461eSJoseph Mingrone {
54*ee67461eSJoseph Mingrone nd_mem_chunk_t *current, *previous;
55*ee67461eSJoseph Mingrone current = ndo->ndo_last_mem_p;
56*ee67461eSJoseph Mingrone while (current != NULL) {
57*ee67461eSJoseph Mingrone previous = current->prev_mem_p;
58*ee67461eSJoseph Mingrone free(current);
59*ee67461eSJoseph Mingrone current = previous;
60*ee67461eSJoseph Mingrone }
61*ee67461eSJoseph Mingrone ndo->ndo_last_mem_p = NULL;
62*ee67461eSJoseph Mingrone }
63