Coverage Report

Created: 2025-07-18 06:32

/src/opensips/mem/rpm_mem.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * restart persistency shared mem stuff
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 */
20
21
22
#include "../error.h"
23
24
#ifndef rpm_mem_h
25
#define rpm_mem_h
26
27
#include <stdlib.h>
28
#include <string.h>
29
#include <errno.h>
30
#include <sys/types.h>
31
#include <sys/ipc.h>
32
33
#include <sys/sem.h>
34
#include <string.h>
35
#include <errno.h>
36
37
#include "../dprint.h"
38
#include "../globals.h"
39
#include "../lock_ops.h" /* we don't include locking.h on purpose */
40
#include "mem_funcs.h"
41
#include "common.h"
42
43
#include "../mi/mi.h"
44
45
/* address where we should first try to map the file */
46
/* on 64 bits architecture, we "afford" a large memory */
47
#if (defined __CPU_x86_64) || (defined __CPU_sparc64) || \
48
  (defined __CPU_ppc64) || (defined __CPU_mips64)
49
0
#define RPM_MAP_ADDRESS     ((void *)0x550532000000)
50
#else
51
#define RPM_MAP_ADDRESS     ((void *)0xea000000)
52
#endif
53
54
/* number of retries if a map fails */
55
0
#define RPM_MAP_RETRIES     5
56
/* 0RpM - OpenSIPS Restart Persistency Memory */
57
0
#define RPM_MAGIC_CODE      0x0052704Du
58
59
/* returns a pointer to a restart persistency zone, identified by the key */
60
void *rpm_key_get(char *key);
61
/* sets the value of a certain key to the specified value
62
 * IMPORTANT: the value has to be within the restart persistency zone! */
63
int rpm_key_set(char *key, void *val);
64
/* deletes a key from the persistent zone */
65
int rpm_key_del(char *key);
66
/* initializes the restart persistency memory */
67
int rpm_init_mem(void);
68
69
extern enum osips_mm mem_allocator_rpm;
70
int set_rpm_mm(const char *mm_name);
71
extern unsigned long rpm_mem_size;
72
extern char *rpm_mem_file;
73
74
75
#ifndef INLINE_ALLOC
76
#ifdef DBG_MALLOC
77
extern void *(*gen_rpm_malloc)(void *blk, unsigned long size,
78
                        const char *file, const char *func, unsigned int line);
79
extern void *(*gen_rpm_malloc_unsafe)(void *blk, unsigned long size,
80
                        const char *file, const char *func, unsigned int line);
81
extern void *(*gen_rpm_realloc)(void *blk, void *p, unsigned long size,
82
                        const char *file, const char *func, unsigned int line);
83
extern void *(*gen_rpm_realloc_unsafe)(void *blk, void *p, unsigned long size,
84
                        const char *file, const char *func, unsigned int line);
85
extern void (*gen_rpm_free)(void *blk, void *p,
86
                        const char *file, const char *func, unsigned int line);
87
extern void (*gen_rpm_free_unsafe)(void *blk, void *p,
88
                        const char *file, const char *func, unsigned int line);
89
#else
90
extern void *(*gen_rpm_malloc)(void *blk, unsigned long size);
91
extern void *(*gen_rpm_malloc_unsafe)(void *blk, unsigned long size);
92
extern void *(*gen_rpm_realloc)(void *blk, void *p, unsigned long size);
93
extern void *(*gen_rpm_realloc_unsafe)(void *blk, void *p, unsigned long size);
94
extern void (*gen_rpm_free)(void *blk, void *p);
95
extern void (*gen_rpm_free_unsafe)(void *blk, void *p);
96
#endif
97
extern void (*gen_rpm_info)(void *blk, struct mem_info *info);
98
extern void (*gen_rpm_status)(void *blk);
99
extern unsigned long (*gen_rpm_get_size)(void *blk);
100
extern unsigned long (*gen_rpm_get_used)(void *blk);
101
extern unsigned long (*gen_rpm_get_rused)(void *blk);
102
extern unsigned long (*gen_rpm_get_mused)(void *blk);
103
extern unsigned long (*gen_rpm_get_free)(void *blk);
104
extern unsigned long (*gen_rpm_get_frags)(void *blk);
105
#endif
106
107
#ifdef INLINE_ALLOC
108
#ifdef F_MALLOC
109
#define RPM_MALLOC             fm_malloc
110
#define RPM_MALLOC_UNSAFE      fm_malloc
111
#define RPM_REALLOC            fm_realloc
112
#define RPM_REALLOC_UNSAFE     fm_realloc
113
#define RPM_FREE               fm_free
114
#define RPM_FREE_UNSAFE        fm_free
115
#define RPM_INFO               fm_info
116
#define RPM_STATUS             fm_status
117
#define RPM_GET_SIZE           fm_get_size
118
#define RPM_GET_USED           fm_get_used
119
#define RPM_GET_RUSED          fm_get_real_used
120
#define RPM_GET_MUSED          fm_get_max_real_used
121
#define RPM_GET_FREE           fm_get_free
122
#define RPM_GET_FRAGS          fm_get_frags
123
#elif defined Q_MALLOC
124
#define RPM_MALLOC             qm_malloc
125
#define RPM_MALLOC_UNSAFE      qm_malloc
126
#define RPM_REALLOC            qm_realloc
127
#define RPM_REALLOC_UNSAFE     qm_realloc
128
#define RPM_FREE               qm_free
129
#define RPM_FREE_UNSAFE        qm_free
130
#define RPM_INFO               qm_info
131
#define RPM_STATUS             qm_status
132
#define RPM_GET_SIZE           qm_get_size
133
#define RPM_GET_USED           qm_get_used
134
#define RPM_GET_RUSED          qm_get_real_used
135
#define RPM_GET_MUSED          qm_get_max_real_used
136
#define RPM_GET_FREE           qm_get_free
137
#define RPM_GET_FRAGS          qm_get_frags
138
#elif defined HP_MALLOC
139
#define RPM_MALLOC             hp_rpm_malloc
140
#define RPM_MALLOC_UNSAFE      hp_rpm_malloc_unsafe
141
#define RPM_REALLOC            hp_rpm_realloc
142
#define RPM_REALLOC_UNSAFE     hp_rpm_realloc_unsafe
143
#define RPM_FREE               hp_rpm_free
144
#define RPM_FREE_UNSAFE        hp_rpm_free_unsafe
145
#define RPM_INFO               hp_info
146
#define RPM_STATUS             hp_status
147
#define RPM_GET_SIZE           hp_rpm_get_size
148
#define RPM_GET_USED           hp_rpm_get_used
149
#define RPM_GET_RUSED          hp_rpm_get_real_used
150
#define RPM_GET_MUSED          hp_rpm_get_max_real_used
151
#define RPM_GET_FREE           hp_rpm_get_free
152
#define RPM_GET_FRAGS          hp_rpm_get_frags
153
#endif /* F_MALLOC || Q_MALLOC || HP_MALLOC */
154
#else
155
0
#define RPM_MALLOC             gen_rpm_malloc
156
#define RPM_MALLOC_UNSAFE      gen_rpm_malloc_unsafe
157
#define RPM_REALLOC            gen_rpm_realloc
158
#define RPM_REALLOC_UNSAFE     gen_rpm_realloc_unsafe
159
#define RPM_FREE               gen_rpm_free
160
0
#define RPM_FREE_UNSAFE        gen_rpm_free_unsafe
161
#define RPM_INFO               gen_rpm_info
162
#define RPM_STATUS             gen_rpm_status
163
0
#define RPM_GET_SIZE           gen_rpm_get_size
164
0
#define RPM_GET_USED           gen_rpm_get_used
165
0
#define RPM_GET_RUSED          gen_rpm_get_rused
166
0
#define RPM_GET_MUSED          gen_rpm_get_mused
167
0
#define RPM_GET_FREE           gen_rpm_get_free
168
0
#define RPM_GET_FRAGS          gen_rpm_get_frags
169
#endif /* INLINE_ALLOC */
170
171
#ifdef HP_MALLOC
172
  #ifdef INLINE_ALLOC
173
  #define rpm_lock()
174
  #define rpm_unlock()
175
  #else
176
  extern int rpm_use_global_lock;
177
  #define rpm_lock() \
178
    do { \
179
      if (rpm_use_global_lock) \
180
        lock_get(rpmem_lock); \
181
    } while (0)
182
  #define rpm_unlock() \
183
    do { \
184
      if (rpm_use_global_lock) \
185
        lock_release(rpmem_lock); \
186
    } while (0)
187
  #endif
188
#else
189
0
#define rpm_lock()    lock_get(rpmem_lock)
190
0
#define rpm_unlock()  lock_release(rpmem_lock)
191
#endif
192
193
#ifdef DBG_MALLOC
194
195
  #ifdef __SUNPRO_C
196
      #define __FUNCTION__ ""  /* gcc specific */
197
  #endif
198
199
inline static void* _rpm_malloc(unsigned long size,
200
  const char *file, const char *function, unsigned int line )
201
{
202
  void *p;
203
204
  rpm_lock();
205
  p = RPM_MALLOC(rpm_block, size, file, function, line);
206
  rpm_unlock();
207
208
  return p;
209
}
210
211
212
inline static void* _rpm_realloc(void *ptr, unsigned long size,
213
    const char* file, const char* function, unsigned int line )
214
{
215
  void *p;
216
217
  rpm_lock();
218
  p = RPM_REALLOC(rpm_block, ptr, size, file, function, line);
219
  rpm_unlock();
220
221
  return p;
222
}
223
224
inline static void _rpm_free(void *ptr,
225
    const char* file, const char* function, unsigned int line)
226
{
227
  rpm_lock();
228
  RPM_FREE(rpm_block, ptr, file, function, line);
229
  rpm_unlock();
230
}
231
232
#define rpm_malloc_func _rpm_malloc
233
#define rpm_malloc( _size) _rpm_malloc((_size), \
234
  __FILE__, __FUNCTION__, __LINE__ )
235
236
#define rpm_malloc_func_unsafe _rpm_malloc_unsafe
237
#define rpm_malloc_unsafe(_size) RPM_MALLOC(rpm_block, (_size), \
238
  __FILE__, __FUNCTION__, __LINE__ )
239
240
#define rpm_realloc_func _rpm_realloc
241
#define rpm_realloc(_ptr, _size) _rpm_realloc((_ptr), (_size), \
242
  __FILE__, __FUNCTION__, __LINE__ )
243
244
#define rpm_realloc_func_unsafe _rpm_realloc_unsafe
245
#define rpm_realloc_unsafe(_ptr, _size) RPM_REALLOC_UNSAFE(rpm_block, (_ptr), (_size), \
246
  __FILE__, __FUNCTION__, __LINE__ )
247
248
#define rpm_free_func _rpm_free
249
#define rpm_free( _ptr ) _rpm_free( (_ptr), \
250
  __FILE__, __FUNCTION__, __LINE__ )
251
252
#define rpm_free_func_unsafe _rpm_free_unsafe
253
#define rpm_free_unsafe( _ptr ) RPM_FREE_UNSAFE(rpm_block, (_ptr), \
254
  __FILE__, __FUNCTION__, __LINE__ )
255
256
#else /*DBG_MALLOC*/
257
258
#define rpm_malloc_func_unsafe  RPM_MALLOC_UNSAFE
259
#define rpm_malloc_unsafe(_size) RPM_MALLOC_UNSAFE(rpm_block, (_size))
260
#define rpm_realloc_func_unsafe RPM_REALLOC_UNSAFE
261
#define rpm_realloc_unsafe(_ptr, _size) RPM_REALLOC_UNSAFE(rpm_block, (_ptr), (_size))
262
#define rpm_free_func_unsafe RPM_FREE_UNSAFE
263
0
#define rpm_free_unsafe( _ptr ) RPM_FREE_UNSAFE(rpm_block, (_ptr))
264
265
266
#define rpm_malloc_func     rpm_malloc
267
inline static void* rpm_malloc(unsigned long size)
268
0
{
269
0
  void *p;
270
271
0
  rpm_lock();
272
0
  p = RPM_MALLOC(rpm_block, size);
273
0
  rpm_unlock();
274
275
0
  return p;
276
0
}
Unexecuted instantiation: statistics.c:rpm_malloc
Unexecuted instantiation: rpm_mem.c:rpm_malloc
Unexecuted instantiation: map.c:rpm_malloc
Unexecuted instantiation: cfg.tab.c:rpm_malloc
277
278
#define rpm_realloc_func    rpm_realloc
279
inline static void* rpm_realloc(void *ptr, unsigned long size)
280
0
{
281
0
  void *p;
282
0
283
0
  rpm_lock();
284
0
  p = RPM_REALLOC(rpm_block, ptr, size);
285
0
  rpm_unlock();
286
0
287
0
  return p;
288
0
}
Unexecuted instantiation: statistics.c:rpm_realloc
Unexecuted instantiation: rpm_mem.c:rpm_realloc
Unexecuted instantiation: map.c:rpm_realloc
Unexecuted instantiation: cfg.tab.c:rpm_realloc
289
290
#define rpm_free_func     rpm_free
291
inline static void rpm_free(void *_p)
292
0
{
293
0
  rpm_lock();
294
295
#ifdef HP_MALLOC
296
  RPM_FREE(rpm_block, _p);
297
#else
298
0
  rpm_free_unsafe( (_p));
299
0
#endif
300
301
0
  rpm_unlock();
302
0
}
Unexecuted instantiation: statistics.c:rpm_free
Unexecuted instantiation: rpm_mem.c:rpm_free
Unexecuted instantiation: map.c:rpm_free
Unexecuted instantiation: cfg.tab.c:rpm_free
303
304
#endif
305
306
inline static void rpm_status(void)
307
0
{
308
0
  if (!rpm_block
309
0
#ifndef INLINE_ALLOC
310
0
    || !gen_rpm_status
311
0
#endif
312
0
      )
313
0
    return;
314
0
315
0
  rpm_lock();
316
0
  RPM_STATUS(rpm_block);
317
0
  rpm_unlock();
318
0
}
Unexecuted instantiation: statistics.c:rpm_status
Unexecuted instantiation: rpm_mem.c:rpm_status
Unexecuted instantiation: map.c:rpm_status
Unexecuted instantiation: cfg.tab.c:rpm_status
319
320
321
inline static void rpm_info(struct mem_info* mi)
322
0
{
323
0
  rpm_lock();
324
0
  RPM_INFO(rpm_block, mi);
325
0
  rpm_unlock();
326
0
}
Unexecuted instantiation: statistics.c:rpm_info
Unexecuted instantiation: rpm_mem.c:rpm_info
Unexecuted instantiation: map.c:rpm_info
Unexecuted instantiation: cfg.tab.c:rpm_info
327
328
329
#ifdef STATISTICS
330
struct hp_block;
331
void hp_init_rpm_statistics(struct hp_block *hpb);
332
333
extern stat_export_t rpm_stats[];
334
335
0
inline static unsigned long rpm_get_size(unsigned short foo) {
336
0
  if (!rpm_block)
337
0
    return 0;
338
0
  return RPM_GET_SIZE(rpm_block);
339
0
}
Unexecuted instantiation: statistics.c:rpm_get_size
Unexecuted instantiation: rpm_mem.c:rpm_get_size
Unexecuted instantiation: map.c:rpm_get_size
Unexecuted instantiation: cfg.tab.c:rpm_get_size
340
0
inline static unsigned long rpm_get_used(unsigned short foo) {
341
0
  if (!rpm_block)
342
0
    return 0;
343
0
  return RPM_GET_USED(rpm_block);
344
0
}
Unexecuted instantiation: statistics.c:rpm_get_used
Unexecuted instantiation: rpm_mem.c:rpm_get_used
Unexecuted instantiation: map.c:rpm_get_used
Unexecuted instantiation: cfg.tab.c:rpm_get_used
345
0
inline static unsigned long rpm_get_rused(unsigned short foo) {
346
0
  if (!rpm_block)
347
0
    return 0;
348
0
  return RPM_GET_RUSED(rpm_block);
349
0
}
Unexecuted instantiation: statistics.c:rpm_get_rused
Unexecuted instantiation: rpm_mem.c:rpm_get_rused
Unexecuted instantiation: map.c:rpm_get_rused
Unexecuted instantiation: cfg.tab.c:rpm_get_rused
350
0
inline static unsigned long rpm_get_mused(unsigned short foo) {
351
0
  if (!rpm_block)
352
0
    return 0;
353
0
  return RPM_GET_MUSED(rpm_block);
354
0
}
Unexecuted instantiation: statistics.c:rpm_get_mused
Unexecuted instantiation: rpm_mem.c:rpm_get_mused
Unexecuted instantiation: map.c:rpm_get_mused
Unexecuted instantiation: cfg.tab.c:rpm_get_mused
355
0
inline static unsigned long rpm_get_free(unsigned short foo) {
356
0
  if (!rpm_block)
357
0
    return 0;
358
0
  return RPM_GET_FREE(rpm_block);
359
0
}
Unexecuted instantiation: statistics.c:rpm_get_free
Unexecuted instantiation: rpm_mem.c:rpm_get_free
Unexecuted instantiation: map.c:rpm_get_free
Unexecuted instantiation: cfg.tab.c:rpm_get_free
360
0
inline static unsigned long rpm_get_frags(unsigned short foo) {
361
0
  if (!rpm_block)
362
0
    return 0;
363
0
  return RPM_GET_FRAGS(rpm_block);
364
0
}
Unexecuted instantiation: statistics.c:rpm_get_frags
Unexecuted instantiation: rpm_mem.c:rpm_get_frags
Unexecuted instantiation: map.c:rpm_get_frags
Unexecuted instantiation: cfg.tab.c:rpm_get_frags
365
#endif /*STATISTICS*/
366
367
#endif