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
|
/*-----------------------------------------------------------------------
File : clb_newmem.h
Author: Stephan Schulz
This module implements a simple general purpose memory management
stystem that is efficient for problems with a very regular memory
access pattern (like most theorem provers). In addition to the
groundwork it also implements secure versions of standard functions
making use of memory allocation. This is less elegant, but in most
cases more efficient, than the old system.
Copyright 1998, 1999 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Changes
<1> Wed Aug 13 21:56:20 MET DST 1997
New
-----------------------------------------------------------------------*/
#ifndef CLB_NEWMEM
#define CLB_NEWMEM
#include <string.h>
#include "clb_verbose.h"
#include "clb_os_wrapper.h"
/*---------------------------------------------------------------------*/
/* Data type declarations */
/*---------------------------------------------------------------------*/
/* Administrate deallocated memory blocks */
typedef struct memcell
{
struct memcell* next;
#ifndef NDEBUG
unsigned long test;
#endif
}MemCell, *Mem_p;
/*---------------------------------------------------------------------*/
/* Exported Functions and Variables */
/*---------------------------------------------------------------------*/
#define MEM_ARR_SIZE 8192
#define MEM_ALIGN 16
#define MEM_CHUNKLIMIT (4096/MEM_ALIGN) /* Objects smaller than this are
allocated in sets of MEM_MULTIPLIER */
#define MEM_MULTIPLIER 1024
#define MEM_FREE_PATTERN 0xFAFBFAFA
#define MEM_RSET_PATTERN 0x00000000
/* For estimating the real memory consumption of a data type - the
default may be way of for some memory managers, but should be
reasonably ok for many situations. If CONSTANT_MEM_ESTIMATE is on,
a very rough but machine-independent estimate is used. */
#ifdef CONSTANT_MEM_ESTIMATE
#define MEMSIZE(type) "There is a bug in the code! Everything has to work with constants."
#else
#define MEMSIZE(type) (sizeof(type)+sizeof(void*))
#endif
extern bool MemIsLow;
extern Mem_p free_mem_list[];
void* SizeMallocReal(int size);
void SizeFreeReal(void* junk, int size);
#ifndef NDEBUG
#define SizeFree(junk, size) SizeFreeReal(junk, size); junk=NULL
#define SizeMalloc(size) SizeMallocReal(size)
#define ENSURE_NULL(junk) junk=NULL
#else
#define SizeFree(junk, size) SizeFreeReal(junk, size);
#define SizeMalloc(size) SizeMallocReal(size)
#define ENSURE_NULL(junk) /* Only defined in debug mode */
#endif
void MemFlushFreeList(void);
void* SecureMalloc(int size);
void* SecureRealloc(void *ptr, int size);
void MemAddNewChunk(int mem_index);
char* SecureStrdup(const char* source);
char* SecureStrndup(const char* source, size_t n);
#define FREE(junk) assert(junk);free(junk)
long* IntArrayAlloc(int size);
#define IntArrayFree(array, size) SizeFree(array, size*sizeof(long))
#ifdef CLB_MEMORY_DEBUG
void MemDebugPrintStats(FILE* out);
extern long size_malloc_mem;
extern long size_malloc_count;
extern long size_free_mem;
extern long size_free_count;
extern long clb_free_count;
extern long secure_malloc_count;
extern long secure_malloc_mem;
extern long secure_realloc_count;
extern long secure_realloc_m_count;
extern long secure_realloc_f_count;
#undef FREE
#define FREE(junk) assert(junk); clb_free_count++; free(junk); junk=NULL
#endif
/*-------------------------------------------------------------------------
If you want to have a special Allocator and Deallocator for a
datatype just copy the following templates to your .h-file and fill
them in... The allocated structures will not be initialized - you
need to write a function build on top of the macros if you want more
functionality in you Allocator.
#define DataCellAlloc() (DataCell*)SizeMalloc(sizeof(DataCell))
#define DataCellFree(junk) SizeFree(junk, sizeof(DataCell))
-------------------------------------------------------------------------*/
#endif
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|