]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/xalloc.cc
0fa94f9f79915bdd6a92cf08ef82e492ad6dd488
2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "compat/xalloc.h"
11 #include "profiler/Profiler.h"
13 #if XMALLOC_STATISTICS
14 #define XMS_DBG_MAXSIZE (1024*1024)
15 #define XMS_DBG_SPLIT (256) /* mallocs below this value are tracked with DBG_GRAIN_SM precision instead of DBG_GRAIN */
16 #define XMS_DBG_GRAIN (16)
17 #define XMS_DBG_GRAIN_SM (4)
18 #define XMS_DBG_OFFSET (XMS_DBG_SPLIT/XMS_DBG_GRAIN_SM - XMS_DBG_SPLIT/XMS_DBG_GRAIN )
19 #define XMS_DBG_MAXINDEX (XMS_DBG_MAXSIZE/XMS_DBG_GRAIN + XMS_DBG_OFFSET)
20 static int malloc_sizes
[XMS_DBG_MAXINDEX
+ 1];
21 static int malloc_histo
[XMS_DBG_MAXINDEX
+ 1];
22 static int dbg_stat_init
= 0;
27 if (sz
>= XMS_DBG_MAXSIZE
)
28 return XMS_DBG_MAXINDEX
;
30 if (sz
<= XMS_DBG_SPLIT
)
31 return (sz
+ XMS_DBG_GRAIN_SM
- 1) / XMS_DBG_GRAIN_SM
;
33 return (sz
+ XMS_DBG_GRAIN
- 1) / XMS_DBG_GRAIN
+ XMS_DBG_OFFSET
;
39 for (int i
= 0; i
<= XMS_DBG_MAXINDEX
; ++i
)
40 malloc_sizes
[i
] = malloc_histo
[i
] = 0;
51 return malloc_sizes
[XMS_DBG_INDEX(sz
)] += 1;
55 malloc_statistics(void (*func
) (int, int, int, void *), void *data
)
59 for (; i
<= XMS_DBG_SPLIT
; i
+= XMS_DBG_GRAIN_SM
)
60 func(i
, malloc_sizes
[XMS_DBG_INDEX(i
)], malloc_histo
[XMS_DBG_INDEX(i
)], data
);
62 i
-= XMS_DBG_GRAIN_SM
;
64 for (; i
<= XMS_DBG_MAXSIZE
; i
+= XMS_DBG_GRAIN
)
65 func(i
, malloc_sizes
[XMS_DBG_INDEX(i
)], malloc_histo
[XMS_DBG_INDEX(i
)], data
);
67 memcpy(&malloc_histo
, &malloc_sizes
, sizeof(malloc_sizes
));
69 #endif /* XMALLOC_STATISTICS */
72 xcalloc(size_t n
, size_t sz
)
83 void *p
= calloc(n
, sz
);
89 snprintf(msg
, 128, "xcalloc: Unable to allocate %" PRIuSIZE
" blocks of %" PRIuSIZE
" bytes!\n", n
, sz
);
97 #if XMALLOC_STATISTICS
114 void *p
= malloc(sz
);
118 if (failure_notify
) {
119 static char msg
[128];
120 snprintf(msg
, 128, "xmalloc: Unable to allocate %" PRIuSIZE
" bytes!\n", sz
);
128 #if XMALLOC_STATISTICS
137 xrealloc(void *s
, size_t sz
)
139 PROF_start(xrealloc
);
145 void *p
= realloc(s
, sz
);
149 if (failure_notify
) {
150 static char msg
[128];
151 snprintf(msg
, 128, "xrealloc: Unable to reallocate %" PRIuSIZE
" bytes!\n", sz
);
160 #if XMALLOC_STATISTICS
169 free_const(const void *s_const
)
171 void *s
= const_cast<void *>(s_const
);
173 PROF_start(free_const
);
177 PROF_stop(free_const
);