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