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