]>
git.ipfire.org Git - thirdparty/squid.git/blob - compat/xalloc.cc
2 * Copyright (C) 1996-2022 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"
12 #if XMALLOC_STATISTICS
13 #define XMS_DBG_MAXSIZE (1024*1024)
14 #define XMS_DBG_SPLIT (256) /* mallocs below this value are tracked with DBG_GRAIN_SM precision instead of DBG_GRAIN */
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)
19 static int malloc_sizes
[XMS_DBG_MAXINDEX
+ 1];
20 static int malloc_histo
[XMS_DBG_MAXINDEX
+ 1];
21 static 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;
54 malloc_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
));
68 #endif /* XMALLOC_STATISTICS */
71 xcalloc(size_t n
, size_t sz
)
79 void *p
= calloc(n
, sz
);
84 snprintf(msg
, 128, "xcalloc: Unable to allocate %" PRIuSIZE
" blocks of %" PRIuSIZE
" bytes!\n", n
, sz
);
92 #if XMALLOC_STATISTICS
105 void *p
= malloc(sz
);
108 if (failure_notify
) {
109 static char msg
[128];
110 snprintf(msg
, 128, "xmalloc: Unable to allocate %" PRIuSIZE
" bytes!\n", sz
);
118 #if XMALLOC_STATISTICS
126 xrealloc(void *s
, size_t sz
)
131 void *p
= realloc(s
, sz
);
134 if (failure_notify
) {
135 static char msg
[128];
136 snprintf(msg
, 128, "xrealloc: Unable to reallocate %" PRIuSIZE
" bytes!\n", sz
);
145 #if XMALLOC_STATISTICS
153 free_const(const void *s_const
)
155 void *s
= const_cast<void *>(s_const
);