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