28 #define PTM_MANAGED_MEMORY // comment-out to use malloc/free => can use valgrind
30 #if defined(PTM_MANAGED_MEMORY)
35 #define PTM_MIN_SIZE (int(sizeof(PT_PNTR))+1) // see .@PTM_MIN_SIZE_RESTRICTED
37 #if defined(PTM_MANAGED_MEMORY)
39 #define PTM_TABLE_SIZE (1024*256)
41 #define PTM_TABLE_COUNT 256
42 #define PTM_ELEMS_PER_BLOCK 256
44 #define PTM_MAX_SIZE (PTM_TABLE_COUNT+PTM_MIN_SIZE-1)
46 #define PTM_magic 0xf4
48 #if defined(PTM_MEM_DUMP_STATS)
83 bool contains(
char *somemem,
int blocksize)
const {
87 (somemem >= data && somemem < (data+blocksize))
89 (next && next->
contains(somemem, blocksize));
92 #if defined(PTM_MEM_DUMP_STATS)
93 size_t count()
const {
111 static int blocksize4size(
int forsize) {
116 #if defined(PTM_MEM_DUMP_STATS)
125 #
if defined(PTM_MEM_DUMP_STATS)
138 int allocsize = blocksize4size(forsize);
142 allsize += allocsize;
147 #if defined(PTM_MEM_CHECKED_FREE)
148 bool block_has_size(
char *vblock,
int size)
const {
151 if (block[tab]->
contains(vblock, blocksize4size(size))) {
159 #if defined(PTM_MEM_DUMP_STATS)
160 void dump_max_memory_usage(FILE *out)
const {
166 size_t blocks = block[b]->count();
167 size_t allocated4size = blocks * blocksize4size(size);
168 int elemsPerBlock = blocksize4size(size)/size;
169 size_t maxElements = blocks*elemsPerBlock;
170 int percent = double(allocated4size)/double(allsize)*100+0.5;
172 fprintf(out,
"blocksize: %2i allocated: %7s [~%2i%%] ", size,
GBS_readable_size(allocated4size,
"b"), percent);
176 if (desc) fprintf(out,
" [%s]", desc);
180 sum += allocated4size;
191 if (block[b])
return false;
197 #if defined(PTM_MEM_DUMP_STATS)
198 if (dump_stats) dump_max_memory_usage(stderr);
213 void alloc_new_blocks(
int forsize) {
217 char *prevPos =
NULp;
219 char *block = manager.
get_block(forsize);
231 free_data[
tab] = prevPos;
234 void clear_tables() {
244 #if defined(PTM_MEM_DUMP_STATS)
245 void dump_stats(
bool dump) { manager.dump_stats = dump; }
248 void *
get(
int size) {
252 return ARB_calloc<char>(size);
256 char *erg = free_data[
tab];
258 alloc_new_blocks(size);
259 erg = free_data[
tab];
263 free_data[
tab] = PT_read_pointer<char>(free_data[
tab]);
264 memset(erg, 0, size);
268 #if defined(PTM_MEM_CHECKED_FREE)
269 bool block_has_size(
void *vblock,
int size) {
return manager.block_has_size((
char*)vblock, size); }
272 void put(
void *vblock,
int size) {
275 char *block = (
char*)vblock;
280 #if defined(PTM_MEM_CHECKED_FREE)
281 if (!block_has_size(vblock, size)) {
284 if (isize != size && block_has_size(vblock, isize)) {
304 #else // !defined(PTM_MANAGED_MEMORY)
308 bool is_clear()
const {
return true; }
309 void *
get(
int size) {
return ARB_calloc<char>(size); }
310 void put(
void *block,
int) { free(block); }
318 #error PT_mem.h included twice
bool contains(char *somemem, int blocksize) const
static GB_ERROR tab(GBL_command_arguments *args, bool pretab)
MemBlock(int size, MemBlock *&prev)
TYPE * ARB_alloc(size_t nelem)
void GBK_terminate(const char *error) __ATTR__NORETURN
char * get_block(int forsize)
const char * GBS_readable_size(unsigned long long size, const char *unit_suffix)
static int idx2size(int idx)
GB_write_int const char GB_write_autoconv_string WRITE_SKELETON(write_pointer, GBDATA *,"%p", GB_write_pointer) char *AW_awa if)(!gb_var) return strdup("")
#define PTM_ELEMS_PER_BLOCK
const char * get_blocksize_description(int blocksize)
void put(void *vblock, int size)
bool contains(const CONT &container, const KEY &key)
static int size2idx(int forsize)