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