1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_MEMALLOC_H
21 #define ZABBIX_MEMALLOC_H
22 
23 #include "common.h"
24 #include "mutexs.h"
25 
26 typedef struct
27 {
28 	void		**buckets;
29 	void		*lo_bound;
30 	void		*hi_bound;
31 	zbx_uint64_t	free_size;
32 	zbx_uint64_t	used_size;
33 	zbx_uint64_t	orig_size;
34 	zbx_uint64_t	total_size;
35 	int		shm_id;
36 	char		use_lock;
37 
38 	/* Continue execution in out of memory situation.                         */
39 	/* Normally allocator forces exit when it runs out of allocatable memory. */
40 	/* Set this flag to 1 to allow execution in out of memory situations.     */
41 	char		allow_oom;
42 
43 	ZBX_MUTEX	mem_lock;
44 	const char	*mem_descr;
45 	const char	*mem_param;
46 }
47 zbx_mem_info_t;
48 
49 void	zbx_mem_create(zbx_mem_info_t **info, key_t shm_key, int lock_name, zbx_uint64_t size,
50 		const char *descr, const char *param, int allow_oom);
51 void	zbx_mem_destroy(zbx_mem_info_t *info);
52 
53 #define	zbx_mem_malloc(info, old, size) __zbx_mem_malloc(__FILE__, __LINE__, info, old, size)
54 #define	zbx_mem_realloc(info, old, size) __zbx_mem_realloc(__FILE__, __LINE__, info, old, size)
55 #define	zbx_mem_free(info, ptr)				\
56 							\
57 do							\
58 {							\
59 	__zbx_mem_free(__FILE__, __LINE__, info, ptr);	\
60 	ptr = NULL;					\
61 }							\
62 while (0)
63 
64 void	*__zbx_mem_malloc(const char *file, int line, zbx_mem_info_t *info, const void *old, size_t size);
65 void	*__zbx_mem_realloc(const char *file, int line, zbx_mem_info_t *info, void *old, size_t size);
66 void	__zbx_mem_free(const char *file, int line, zbx_mem_info_t *info, void *ptr);
67 
68 void	zbx_mem_clear(zbx_mem_info_t *info);
69 
70 void	zbx_mem_dump_stats(int level, zbx_mem_info_t *info);
71 
72 size_t	zbx_mem_required_size(int chunks_num, const char *descr, const char *param);
73 
74 #define ZBX_MEM_FUNC1_DECL_MALLOC(__prefix)				\
75 static void	*__prefix ## _mem_malloc_func(void *old, size_t size)
76 #define ZBX_MEM_FUNC1_DECL_REALLOC(__prefix)				\
77 static void	*__prefix ## _mem_realloc_func(void *old, size_t size)
78 #define ZBX_MEM_FUNC1_DECL_FREE(__prefix)				\
79 static void	__prefix ## _mem_free_func(void *ptr)
80 
81 #define ZBX_MEM_FUNC1_IMPL_MALLOC(__prefix, __info)			\
82 									\
83 static void	*__prefix ## _mem_malloc_func(void *old, size_t size)	\
84 {									\
85 	return zbx_mem_malloc(__info, old, size);			\
86 }
87 
88 #define ZBX_MEM_FUNC1_IMPL_REALLOC(__prefix, __info)			\
89 									\
90 static void	*__prefix ## _mem_realloc_func(void *old, size_t size)	\
91 {									\
92 	return zbx_mem_realloc(__info, old, size);			\
93 }
94 
95 #define ZBX_MEM_FUNC1_IMPL_FREE(__prefix, __info)			\
96 									\
97 static void	__prefix ## _mem_free_func(void *ptr)			\
98 {									\
99 	zbx_mem_free(__info, ptr);					\
100 }
101 
102 #define ZBX_MEM_FUNC_DECL(__prefix)					\
103 									\
104 ZBX_MEM_FUNC1_DECL_MALLOC(__prefix);					\
105 ZBX_MEM_FUNC1_DECL_REALLOC(__prefix);					\
106 ZBX_MEM_FUNC1_DECL_FREE(__prefix);
107 
108 #define ZBX_MEM_FUNC_IMPL(__prefix, __info)				\
109 									\
110 ZBX_MEM_FUNC1_IMPL_MALLOC(__prefix, __info);				\
111 ZBX_MEM_FUNC1_IMPL_REALLOC(__prefix, __info);				\
112 ZBX_MEM_FUNC1_IMPL_FREE(__prefix, __info);
113 
114 #endif
115