13#define XMS_DBG_MAXSIZE (1024*1024)
14#define XMS_DBG_SPLIT (256)
15#define XMS_DBG_GRAIN (16)
16#define XMS_DBG_GRAIN_SM (4)
17#define XMS_DBG_OFFSET (XMS_DBG_SPLIT/XMS_DBG_GRAIN_SM - XMS_DBG_SPLIT/XMS_DBG_GRAIN )
18#define XMS_DBG_MAXINDEX (XMS_DBG_MAXSIZE/XMS_DBG_GRAIN + XMS_DBG_OFFSET)
19static int malloc_sizes[XMS_DBG_MAXINDEX + 1];
20static int malloc_histo[XMS_DBG_MAXINDEX + 1];
21static int dbg_stat_init = 0;
26 if (sz >= XMS_DBG_MAXSIZE)
27 return XMS_DBG_MAXINDEX;
29 if (sz <= XMS_DBG_SPLIT)
30 return (sz + XMS_DBG_GRAIN_SM - 1) / XMS_DBG_GRAIN_SM;
32 return (sz + XMS_DBG_GRAIN - 1) / XMS_DBG_GRAIN + XMS_DBG_OFFSET;
38 for (
int i = 0; i <= XMS_DBG_MAXINDEX; ++i)
39 malloc_sizes[i] = malloc_histo[i] = 0;
50 return malloc_sizes[XMS_DBG_INDEX(sz)] += 1;
54malloc_statistics(
void (*func) (
int,
int,
int,
void *),
void *data)
58 for (; i <= XMS_DBG_SPLIT; i += XMS_DBG_GRAIN_SM)
59 func(i, malloc_sizes[XMS_DBG_INDEX(i)], malloc_histo[XMS_DBG_INDEX(i)], data);
61 i -= XMS_DBG_GRAIN_SM;
63 for (; i <= XMS_DBG_MAXSIZE; i += XMS_DBG_GRAIN)
64 func(i, malloc_sizes[XMS_DBG_INDEX(i)], malloc_histo[XMS_DBG_INDEX(i)], data);
66 memcpy(&malloc_histo, &malloc_sizes,
sizeof(malloc_sizes));
79 void *p = calloc(n, sz);
84 snprintf(msg, 128,
"xcalloc: Unable to allocate %zu blocks of %zu bytes!\n", n, sz);
105 void *p = malloc(sz);
109 static char msg[128];
110 snprintf(msg, 128,
"xmalloc: Unable to allocate %zu bytes!\n", sz);
118#if XMALLOC_STATISTICS
131 void *p= realloc(s, sz);
135 static char msg[128];
136 snprintf(msg, 128,
"xrealloc: Unable to reallocate %zu bytes!\n", sz);
145#if XMALLOC_STATISTICS
155 void *s =
const_cast<void *
>(s_const);
void(* failure_notify)(const char *)
void * xmalloc(size_t sz)
void free_const(const void *s_const)
void * xrealloc(void *s, size_t sz)
void * xcalloc(size_t n, size_t sz)