Browse code

Wrapper functions for regcomp, regexec, regfree, and regerror using shared memory allocators instead of libc malloc/realloc/free. Check the README file for more details.

Miklos Tirpak authored on 03/04/2009 13:03:46
Showing 6 changed files
... ...
@@ -7,7 +7,7 @@
7 7
 #
8 8
 
9 9
 
10
-SUBDIRS=binrpc cds xcap presence
10
+SUBDIRS=binrpc cds xcap presence shm_regex
11 11
 
12 12
 .PHONY: subdirs $(SUBDIRS) 
13 13
 
... ...
@@ -24,28 +24,32 @@ xcap - Common XCAP operations and structures (XCAP authorization documents
24 24
        requires external libraries: libxml2, libcurl3 (nonSER version)
25 25
        requires internal libraries: cds
26 26
 
27
+shm_regex - Wrapper functions for regcomp, regexec, regfree and regerror
28
+	    using shared memory. See 'man regex' for details.
29
+
27 30
 Used by modules: pa, rls, dialog, rpa
28 31
 
29 32
 Usage:
30 33
 -----
31 34
 
32
-All libraries can be compiled "with ser" or "without ser". Compilation
33
-without ser may be useful for debugging purposes or for example for 
35
+All libraries except shm_regex can be compiled "with ser" or "without ser".
36
+Compilation without ser may be useful for debugging purposes or for example for 
34 37
 searching for memory leaks with some tool like valgrind.
38
+shm_regex library can be compiled only with ser.
35 39
 
36 40
 Compilation with ser:
37 41
 --------------------
38 42
 
39 43
 Compilation and installation of these libraries is NOT DONE by running
40
-main ser makefile now - it MUST be done MANUALLY. For this purpose
41
-is there Makefile.ser. To compile and install libraries simply run
44
+main ser makefile now - it MUST be done MANUALLY. To compile and install
45
+libraries simply run
42 46
 
43
-   make -f Makefile.ser install
47
+   make install
44 48
 
45 49
 in lib directory. You can select destination directory like in the case
46 50
 of ser, for example:
47 51
 
48
-   make -f Makefile.ser install prefix="/my/ser/directory"
52
+   make install prefix="/my/ser/directory"
49 53
    
50 54
 AFTER COMPILATION of libraries you can COMPILE MODULES using this 
51 55
 libraries like PA, RLS or dialog.
... ...
@@ -61,6 +65,12 @@ you should set LD_LIBRARY_PATH=/usr/local/lib/ser. In the case of
61 61
 nonstandard installation, you can use something like 
62 62
 LD_LIBRARY_PATH=/my/ser/directory/lib/ser.
63 63
 
64
+Compilation without ser:
65
+--------------------
66
+Use the alternate makefile, Makefile.noser:
67
+
68
+   make -f Makefile.noser install
69
+
64 70
 Documentation
65 71
 -------------
66 72
 Documentation for all libraries is (or will be) in 
67 73
new file mode 100644
... ...
@@ -0,0 +1,14 @@
0
+#
1
+# makefile for shm_regex
2
+#
3
+
4
+include ../../Makefile.defs
5
+auto_gen=
6
+NAME:=ser_shm_regex
7
+MAJOR_VER=0
8
+MINOR_VER=1
9
+BUGFIX_VER=0
10
+LIBS=
11
+
12
+include ../../Makefile.libs
13
+
0 14
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+$Id$
1
+
2
+Wrapper functions for regcomp, regexec, regfree, and regerror using shared
3
+memory allocators instead of libc malloc/realloc/free. The original functions
4
+are prefixed with "shm_".
5
+
6
+The regular expression compiled with shm_regcomp() can be used with shm_regexec()
7
+simultaneously by multiple processes without locking, because regexec has its own
8
+internal locks. It can be later on freed with shm_regfree() when the expression
9
+is no longer used. Note however that
10
+
11
+1) shm_regexec allocates shared memory for the pattern buffer when it is successfully
12
+called for the first time.
13
+
14
+2) shm_regexec temporary allocates and deallocates memory for storing the registers
15
+for the pmatch array runtime if nmatch and pmatch parameters are not 0. This is
16
+done also in shared memory, therefore use the shared memory wrappers for
17
+regular expression based search-and-replace only if really necessary.
18
+
19
+See 'man regex' for the usage and parameters of the functions.
20
+
21
+Supported only with GNU C library.
22
+
0 23
new file mode 100644
... ...
@@ -0,0 +1,129 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2009 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License
23
+ * along with this program; if not, write to the Free Software
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ *
26
+ * History
27
+ * -------
28
+ *  2009-04-03	Initial version (Miklos)
29
+ */
30
+
31
+#include <malloc.h>	/* hook prototypes */
32
+
33
+#include "../../mem/shm_mem.h"
34
+#include "shm_regex.h"
35
+
36
+typedef void *(malloc_hook_t) (size_t, const void *);
37
+typedef void *(realloc_hook_t) (void *, size_t, const void *);
38
+typedef void (free_hook_t) (void *, const void *);
39
+
40
+/* The memory hooks are overwritten before calling regcomp(), regfree(),
41
+ * and regexec(), and shared memory function are called
42
+ * from the hooks instead of libc malloc/realloc/free.
43
+ */
44
+
45
+static void *shm_malloc_hook(size_t size, const void *caller)
46
+{
47
+	return shm_malloc (size);
48
+}
49
+
50
+static void *shm_realloc_hook(void *p, size_t size, const void *caller)
51
+{
52
+	return shm_realloc (p, size);
53
+}
54
+
55
+static void shm_free_hook(void *ptr, const void *caller)
56
+{
57
+	if (ptr) shm_free (ptr);
58
+}
59
+
60
+#define replace_malloc_hooks() \
61
+	do { \
62
+		orig_malloc_hook = __malloc_hook; \
63
+		orig_realloc_hook = __realloc_hook; \
64
+		orig_free_hook = __free_hook; \
65
+		__malloc_hook = shm_malloc_hook; \
66
+		__realloc_hook = shm_realloc_hook; \
67
+		__free_hook = shm_free_hook; \
68
+	} while (0)
69
+
70
+#define restore_malloc_hooks() \
71
+	do { \
72
+		__malloc_hook = orig_malloc_hook; \
73
+		__realloc_hook = orig_realloc_hook; \
74
+		__free_hook = orig_free_hook; \
75
+	} while (0)
76
+
77
+int shm_regcomp(regex_t *preg, const char *regex, int cflags)
78
+{
79
+	malloc_hook_t	*orig_malloc_hook;
80
+	realloc_hook_t	*orig_realloc_hook;
81
+	free_hook_t	*orig_free_hook;
82
+	int		ret;
83
+
84
+	replace_malloc_hooks();
85
+	ret = regcomp(preg, regex, cflags);
86
+	restore_malloc_hooks();
87
+
88
+	return ret;
89
+}
90
+
91
+void shm_regfree(regex_t *preg)
92
+{
93
+	malloc_hook_t	*orig_malloc_hook;
94
+	realloc_hook_t	*orig_realloc_hook;
95
+	free_hook_t	*orig_free_hook;
96
+
97
+	replace_malloc_hooks();
98
+	regfree(preg);
99
+	restore_malloc_hooks();
100
+}
101
+
102
+int shm_regexec(const regex_t *preg, const char *string, size_t nmatch,
103
+                   regmatch_t pmatch[], int eflags)
104
+{
105
+	malloc_hook_t	*orig_malloc_hook;
106
+	realloc_hook_t	*orig_realloc_hook;
107
+	free_hook_t	*orig_free_hook;
108
+	int		ret;
109
+
110
+	/* regexec() allocates some memory for the pattern buffer
111
+	 * when it is successfully called for the first time, therefore
112
+	 * shared memory is required also here.
113
+	 * The drawback is that shared memory allocation is also used
114
+	 * needlessly for allocating the temporary space for
115
+	 * the elements of pmatch. -- Does not happen if pmatch and
116
+	 * nmatch are 0.
117
+	 * It is safe to call regexec() concurrently without locking,
118
+	 * because regexec() has its own locks.
119
+	 * (Miklos)
120
+	 */
121
+	replace_malloc_hooks();
122
+	ret = regexec(preg, string, nmatch,
123
+			pmatch, eflags);
124
+	restore_malloc_hooks();
125
+	
126
+	return ret;
127
+}
128
+
0 129
new file mode 100644
... ...
@@ -0,0 +1,45 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2009 iptelorg GmbH
4
+ *
5
+ * This file is part of ser, a free SIP server.
6
+ *
7
+ * ser is free software; you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation; either version 2 of the License, or
10
+ * (at your option) any later version
11
+ *
12
+ * For a license to use the ser software under conditions
13
+ * other than those described here, or to purchase support for this
14
+ * software, please contact iptel.org by e-mail at the following addresses:
15
+ *    info@iptel.org
16
+ *
17
+ * ser is distributed in the hope that it will be useful,
18
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+ * GNU General Public License for more details.
21
+ *
22
+ * You should have received a copy of the GNU General Public License
23
+ * along with this program; if not, write to the Free Software
24
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
+ *
26
+ * History
27
+ * -------
28
+ *  2009-04-03	Initial version (Miklos)
29
+ */
30
+
31
+#ifndef _SHM_REGEX_H
32
+#define _SHM_REGEX_H
33
+
34
+#include <sys/types.h>
35
+#include <regex.h>
36
+
37
+int shm_regcomp(regex_t *preg, const char *regex, int cflags);
38
+void shm_regfree(regex_t *preg);
39
+int shm_regexec(const regex_t *preg, const char *string, size_t nmatch,
40
+                   regmatch_t pmatch[], int eflags);
41
+
42
+#define shm_regerror	regerror
43
+
44
+#endif /* _SHM_REGEX_H */