1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*-----------------------------------------------------------------------
File : clb_regmem.c
Author: Stephan Schulz (schulz@eprover.org)
Contents
A module supporting dynamic memory for local static variables that
is still freed quasi-automatically (via a call to a cleanup
function) when the program terminates. This is useful if there is a
need for dynamically growing (or shrinking) persistent memory "owned"
by a function. I still want this cleaned up at the end to keep the
usefulness of the memory counters in detecting (and hence avoiding)
memory leaks.
Copyright 2011 by the author.
This code is released under the GNU General Public Licence.
See the file COPYING in the main CLIB directory for details.
Run "eprover -h" for contact information.
Changes
<1> Mon Feb 13 01:48:00 CET 2012
New
-----------------------------------------------------------------------*/
#include "clb_regmem.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
static PTree_p reg_mem_tree = NULL;
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: RegMemAlloc()
//
// Allocate a registered memory area.
//
// Global Variables: -
//
// Side Effects : Memory operations (duh!)
//
/----------------------------------------------------------------------*/
void* RegMemAlloc(size_t size)
{
void *res = SecureMalloc(size);
PTreeStore(®_mem_tree, res);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: RegMemRealloc()
//
// Realloc a registererd memory area.
//
// Global Variables: -
//
// Side Effects : Memory operations (duh!)
//
/----------------------------------------------------------------------*/
void* RegMemRealloc(void* mem, size_t size)
{
void *res;
PTreeDeleteEntry(®_mem_tree, mem);
res = SecureRealloc(mem,size);
PTreeStore(®_mem_tree, res);
return res;
}
/*-----------------------------------------------------------------------
//
// Function: RegMemFree()
//
// Free a registered memory area.
//
// Global Variables: -
//
// Side Effects : Memory operations (duh!)
//
/----------------------------------------------------------------------*/
void RegMemFree(void* mem)
{
PTreeDeleteEntry(®_mem_tree, mem);
FREE(mem);
}
/*-----------------------------------------------------------------------
//
// Function: RegMemProvide()
//
// Return pointer to a memory section that is large enough to store
// newsize bytes, with the first *oldsize bytes being initialized to
// the value at *mem, and the rest being initialized to '0'. If
// newsize <= oldsize, this is a NOP. If new memory is allocated,
// oldsize will be updated to reflect the new size (which most
// likely is larger than newsize).
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void* RegMemProvide(void* mem, size_t *oldsize, size_t newsize)
{
size_t newlimit;
if(*oldsize >= newsize)
{
return mem;
}
newlimit = MAX(*oldsize, (size_t)1);
while(newlimit < newsize)
{
newlimit *= 2;
}
mem = RegMemRealloc(mem, newlimit);
memset(mem+*oldsize, 0, newlimit-*oldsize);
*oldsize = newlimit;
return mem;
}
/*-----------------------------------------------------------------------
//
// Function: RegMemCleanUp()
//
// Free all registered memory areas.
//
// Global Variables: -
//
// Side Effects : Memory operations (duh!)
//
/----------------------------------------------------------------------*/
void RegMemCleanUp(void)
{
void* mem;
while(reg_mem_tree)
{
mem = PTreeExtractRootKey(®_mem_tree);
FREE(mem);
}
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|