]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/xalloc.cc
2 #include "compat/xalloc.h"
3 #include "profiler/Profiler.h"
6 #define XMS_DBG_MAXSIZE (1024*1024)
7 #define XMS_DBG_SPLIT (256) /* mallocs below this value are tracked with DBG_GRAIN_SM precision instead of DBG_GRAIN */
8 #define XMS_DBG_GRAIN (16)
9 #define XMS_DBG_GRAIN_SM (4)
10 #define XMS_DBG_OFFSET (XMS_DBG_SPLIT/XMS_DBG_GRAIN_SM - XMS_DBG_SPLIT/XMS_DBG_GRAIN )
11 #define XMS_DBG_MAXINDEX (XMS_DBG_MAXSIZE/XMS_DBG_GRAIN + XMS_DBG_OFFSET)
12 static int malloc_sizes
[XMS_DBG_MAXINDEX
+ 1];
13 static int malloc_histo
[XMS_DBG_MAXINDEX
+ 1];
14 static int dbg_stat_init
= 0;
19 if (sz
>= XMS_DBG_MAXSIZE
)
20 return XMS_DBG_MAXINDEX
;
22 if (sz
<= XMS_DBG_SPLIT
)
23 return (sz
+ XMS_DBG_GRAIN_SM
- 1) / XMS_DBG_GRAIN_SM
;
25 return (sz
+ XMS_DBG_GRAIN
- 1) / XMS_DBG_GRAIN
+ XMS_DBG_OFFSET
;
31 for (int i
= 0; i
<= XMS_DBG_MAXINDEX
; ++i
)
32 malloc_sizes
[i
] = malloc_histo
[i
] = 0;
43 return malloc_sizes
[XMS_DBG_INDEX(sz
)] += 1;
47 malloc_statistics(void (*func
) (int, int, int, void *), void *data
)
51 for (; i
<= XMS_DBG_SPLIT
; i
+= XMS_DBG_GRAIN_SM
)
52 func(i
, malloc_sizes
[XMS_DBG_INDEX(i
)], malloc_histo
[XMS_DBG_INDEX(i
)], data
);
54 i
-= XMS_DBG_GRAIN_SM
;
56 for (; i
<= XMS_DBG_MAXSIZE
; i
+= XMS_DBG_GRAIN
)
57 func(i
, malloc_sizes
[XMS_DBG_INDEX(i
)], malloc_histo
[XMS_DBG_INDEX(i
)], data
);
59 memcpy(&malloc_histo
, &malloc_sizes
, sizeof(malloc_sizes
));
61 #endif /* XMALLOC_STATISTICS */
64 xcalloc(size_t n
, size_t sz
)
75 void *p
= calloc(n
, sz
);
81 snprintf(msg
, 128, "xcalloc: Unable to allocate %lu blocks of %lu bytes!\n", (unsigned long)n
, (unsigned long)sz
);
89 #if XMALLOC_STATISTICS
106 void *p
= malloc(sz
);
110 if (failure_notify
) {
111 static char msg
[128];
112 snprintf(msg
, 128, "xmalloc: Unable to allocate %lu bytes!\n", (unsigned long)sz
);
120 #if XMALLOC_STATISTICS
129 xrealloc(void *s
, size_t sz
)
131 PROF_start(xrealloc
);
137 void *p
= realloc(s
, sz
);
141 if (failure_notify
) {
142 static char msg
[128];
143 snprintf(msg
, 128, "xrealloc: Unable to reallocate %lu bytes!\n", (unsigned long)sz
);
152 #if XMALLOC_STATISTICS
161 free_const(const void *s_const
)
163 void *s
= const_cast<void *>(s_const
);
165 PROF_start(free_const
);
169 PROF_stop(free_const
);