master
h 73 lines 2.7 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #ifndef NETDATA_ND_MALLOCZ_H
4 #define NETDATA_ND_MALLOCZ_H
5
6 #include "../common.h"
7
8 // memory allocation functions that handle failures
9 #ifdef NETDATA_TRACE_ALLOCATIONS
10 struct malloc_trace {
11 avl_t avl;
12
13 const char *function;
14 const char *file;
15 size_t line;
16
17 size_t malloc_calls;
18 size_t calloc_calls;
19 size_t realloc_calls;
20 size_t strdup_calls;
21 size_t free_calls;
22
23 size_t mmap_calls;
24 size_t munmap_calls;
25
26 size_t allocations;
27 size_t bytes;
28
29 struct rrddim *rd_bytes;
30 struct rrddim *rd_allocations;
31 struct rrddim *rd_avg_alloc;
32 struct rrddim *rd_ops;
33 };
34
35 int malloc_trace_walkthrough(int (*callback)(void *item, void *data), void *data);
36
37 #define strdupz(s) strdupz_int(s, __FILE__, __FUNCTION__, __LINE__)
38 #define strndupz(s, len) strndupz_int(s, len, __FILE__, __FUNCTION__, __LINE__)
39 #define callocz(nmemb, size) callocz_int(nmemb, size, __FILE__, __FUNCTION__, __LINE__)
40 #define mallocz(size) mallocz_int(size, __FILE__, __FUNCTION__, __LINE__)
41 #define reallocz(ptr, size) reallocz_int(ptr, size, __FILE__, __FUNCTION__, __LINE__)
42 #define freez(ptr) freez_int(ptr, __FILE__, __FUNCTION__, __LINE__)
43 #define mallocz_usable_size(ptr) mallocz_usable_size_int(ptr, __FILE__, __FUNCTION__, __LINE__)
44
45 char *strdupz_int(const char *s, const char *file, const char *function, size_t line);
46 char *strndupz_int(const char *s, size_t len, const char *file, const char *function, size_t line);
47 void *callocz_int(size_t nmemb, size_t size, const char *file, const char *function, size_t line);
48 void *mallocz_int(size_t size, const char *file, const char *function, size_t line);
49 void *reallocz_int(void *ptr, size_t size, const char *file, const char *function, size_t line);
50 void freez_int(void *ptr, const char *file, const char *function, size_t line);
51 size_t mallocz_usable_size_int(void *ptr, const char *file, const char *function, size_t line);
52
53 #else // NETDATA_TRACE_ALLOCATIONS
54 char *strdupz(const char *s) MALLOCLIKE NEVERNULL WARNUNUSED;
55 char *strndupz(const char *s, size_t len) MALLOCLIKE NEVERNULL WARNUNUSED;
56 void *callocz(size_t nmemb, size_t size) MALLOCLIKE NEVERNULL WARNUNUSED;
57 void *mallocz(size_t size) MALLOCLIKE NEVERNULL WARNUNUSED;
58 void *reallocz(void *ptr, size_t size) MALLOCLIKE NEVERNULL WARNUNUSED;
59 void freez(void *ptr);
60 #endif // NETDATA_TRACE_ALLOCATIONS
61
62 void mallocz_release_as_much_memory_to_the_system(void);
63
64 int posix_memalignz(void **memptr, size_t alignment, size_t size);
65 void posix_memalign_freez(void *ptr);
66
67 typedef void (*out_of_memory_cb)(void);
68 void mallocz_register_out_of_memory_cb(out_of_memory_cb cb);
69
70 NORETURN
71 void out_of_memory(const char *call, size_t size, const char *details);
72
73 #endif //NETDATA_ND_MALLOCZ_H