forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.h
206 lines (169 loc) · 5.51 KB
/
context.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (C) 2014 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2014-10-30 initial version (liviu)
*/
/*
* This header exposes the basic operations with an OpenSIPS context.
*
* A "context" is:
* - a data storage buffer
* - typically allocated next to the intended structure
* e.g. | struct cell | CONTEXT_BUFFER |
*
* !! All data registrations must be done in the pre-forking phases !!
* (see the register functions below)
*/
#ifndef __CONTEXT_H
#define __CONTEXT_H
#include <stdlib.h>
#include "dprint.h"
typedef void * context_p;
enum osips_context {
CONTEXT_GLOBAL,
CONTEXT_TRAN,
CONTEXT_DIALOG,
CONTEXT_B2B_LOGIC,
CONTEXT_COUNT,
};
enum osips_context_val {
CONTEXT_INT_TYPE,
CONTEXT_STR_TYPE,
CONTEXT_PTR_TYPE,
CONTEXT_COUNT_TYPE
};
#define context_of(entity_p) ((context_p)((entity_p) + 1))
#define context_size(enum_ctx) (context_sizes[enum_ctx])
extern context_p current_processing_ctx;
#define set_global_context(__ctx) current_processing_ctx = __ctx
extern unsigned int context_sizes[];
extern unsigned int type_sizes[CONTEXT_COUNT][CONTEXT_COUNT_TYPE];
extern unsigned int type_offsets[CONTEXT_COUNT][CONTEXT_COUNT_TYPE];
/*
* allocate a new context in pkg mem
*
* Note: this will not change the "current_processing_ctx"
*/
context_p context_alloc(enum osips_context type);
#define context_free(context_p) pkg_free(context_p)
/*
* ensure the global context is non-NULL
*
* Return: context pointer on success, NULL on internal error
*/
int ensure_global_context(void);
/* pushes or pops a context level from internal stack
*/
int push_new_global_context(void);
int pop_pushed_global_context(void);
/* free and reset the global context */
void clear_global_context(void);
/*
* destroys a context by calling each callback registered
* Note: @ctx will *not* be freed!
*/
void context_destroy(enum osips_context type, context_p ctx);
/*
* - register a different function for each field you add in the context
* - each function will be called exactly once, every time a context is freed
*
* Note: for int and (str *) types, you must perform the appropriate casting
*/
typedef void (*context_destroy_f)(void *);
/*
* - the register functions should be called before any forks are made
* (mod_init(), function fixups)
*
* - they reserve and return a position in the context buffer of the given type
*/
int context_register_int(enum osips_context type, context_destroy_f f);
int context_register_str(enum osips_context type, context_destroy_f f);
int context_register_ptr(enum osips_context type, context_destroy_f f);
/****************************** SETTERS ********************************/
static inline void context_put_int(enum osips_context type, context_p ctx,
int pos, int data)
{
#ifdef DBG_MALLOC
if (pos < 0 || pos >= type_sizes[type][CONTEXT_INT_TYPE]) {
LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_INT_TYPE]);
abort();
}
#endif
((int *)ctx)[pos] = data;
}
static inline void context_put_str(enum osips_context type, context_p ctx,
int pos, str *data)
{
#ifdef DBG_MALLOC
if (pos < 0 || pos >= type_sizes[type][CONTEXT_STR_TYPE]) {
LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_STR_TYPE]);
abort();
}
#endif
((str *)(void *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos] = *data;
}
static inline void context_put_ptr(enum osips_context type, context_p ctx,
int pos, void *data)
{
#ifdef DBG_MALLOC
if (pos < 0 || pos >= type_sizes[type][CONTEXT_PTR_TYPE]) {
LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_PTR_TYPE]);
abort();
}
#endif
((void **)(void *)((char *)ctx + type_offsets[type][CONTEXT_PTR_TYPE]))[pos] = data;
}
/****************************** GETTERS ********************************/
static inline int context_get_int(enum osips_context type,
context_p ctx, int pos)
{
#ifdef DBG_MALLOC
if (pos < 0 || pos >= type_sizes[type][CONTEXT_INT_TYPE]) {
LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_INT_TYPE]);
abort();
}
#endif
return ((int *)ctx)[pos];
}
static inline str *context_get_str(enum osips_context type,
context_p ctx, int pos)
{
#ifdef DBG_MALLOC
if (pos < 0 || pos >= type_sizes[type][CONTEXT_STR_TYPE]) {
LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_STR_TYPE]);
abort();
}
#endif
return &((str *)(void *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos];
}
static inline void *context_get_ptr(enum osips_context type,
context_p ctx, int pos)
{
#ifdef DBG_MALLOC
if (pos < 0 || pos >= type_sizes[type][CONTEXT_PTR_TYPE]) {
LM_CRIT("Bad pos: %d (%d)\n", pos, type_sizes[type][CONTEXT_PTR_TYPE]);
abort();
}
#endif
return ((void **)(void *)((char *)ctx +
type_offsets[type][CONTEXT_PTR_TYPE]))[pos];
}
#endif /* __CONTEXT_H */