00001 /* -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ 00002 /********************************************************************* 00003 * 00004 * Filename : c_allocator.h (debug) 00005 * 00006 * This file is a part of the UPPAAL toolkit. 00007 * Copyright (c) 1995 - 2003, Uppsala University and Aalborg University. 00008 * All right reserved. 00009 * 00010 * $Id: c_allocator.h,v 1.2 2004/06/14 07:36:54 adavid Exp $ 00011 * 00012 *********************************************************************/ 00013 00014 #ifndef INCLUDE_DEBUG_C_ALLOCATOR_H 00015 #define INCLUDE_DEBUG_C_ALLOCATOR_H 00016 00017 #include "base/c_allocator.h" 00018 #include "base/intutils.h" 00019 00020 #ifdef __cplusplus 00021 extern "C" { 00022 #endif 00023 00024 00025 /** 00026 * @file 00027 * Implementation of a particular allocator_t 00028 * to check for memory leaks when testing. 00029 */ 00030 00031 /** Type to keep track of allocations. 00032 */ 00033 typedef struct debug_allocation_s 00034 { 00035 struct debug_allocation_s *next; 00036 void *ptr; 00037 size_t size; 00038 } debug_allocation_t; 00039 00040 00041 /** Debug allocator function based on malloc. 00042 * @param intSize: size to allocate in ints 00043 * @param debugAllocations: a debug_allocation_t** 00044 * @return int32_t[intSize] allocated by malloc 00045 */ 00046 int32_t* debug_malloc(size_t intSize, void *debugAllocations); 00047 00048 00049 /** Debug free function, to free memory allocated by debug_malloc. 00050 * @param ptr: memory to deallocate. 00051 * @param intSize: size to deallocate 00052 * @param debugAllocations: all the allocations. 00053 */ 00054 void debug_free(void *ptr, size_t intSize, void *debugAllocations); 00055 00056 00057 /** Size of the allocation table. 00058 * This MUST be a power of 2. 00059 */ 00060 enum { debug_ALLOCATION_TABLE = 1 << 20 }; 00061 00062 00063 /** Initialize a debug_allocator_t. 00064 * @param alloc: allocator to initialize. 00065 */ 00066 static inline 00067 void debug_initAllocator(allocator_t *alloc) 00068 { 00069 alloc->allocData = malloc(debug_ALLOCATION_TABLE*sizeof(debug_allocation_t*)); 00070 alloc->allocFunction = debug_malloc; 00071 alloc->deallocFunction = debug_free; 00072 base_resetLarge(alloc->allocData, debug_ALLOCATION_TABLE); 00073 } 00074 00075 00076 /** Destroy a debug allocator and check it for leaks. 00077 * @param debugAllocator: an allocator as initialized by debug_initAllocator 00078 */ 00079 void debug_destroyAllocator(allocator_t *debugAllocator); 00080 00081 00082 #ifdef __cplusplus 00083 } 00084 #endif 00085 00086 #endif /* INCLUDE_DEBUG_C_ALLOCATOR_H */