Browse Source

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 16 years ago
parent
commit
e01a458045
6 changed files with 228 additions and 7 deletions
  1. 1 1
      lib/Makefile
  2. 16 6
      lib/README
  3. 14 0
      lib/shm_regex/Makefile
  4. 23 0
      lib/shm_regex/README
  5. 129 0
      lib/shm_regex/shm_regex.c
  6. 45 0
      lib/shm_regex/shm_regex.h

+ 1 - 1
lib/Makefile

@@ -7,7 +7,7 @@
 #
 #
 
 
 
 
-SUBDIRS=binrpc cds xcap presence
+SUBDIRS=binrpc cds xcap presence shm_regex
 
 
 .PHONY: subdirs $(SUBDIRS) 
 .PHONY: subdirs $(SUBDIRS) 
 
 

+ 16 - 6
lib/README

@@ -24,28 +24,32 @@ xcap - Common XCAP operations and structures (XCAP authorization documents
        requires external libraries: libxml2, libcurl3 (nonSER version)
        requires external libraries: libxml2, libcurl3 (nonSER version)
        requires internal libraries: cds
        requires internal libraries: cds
 
 
+shm_regex - Wrapper functions for regcomp, regexec, regfree and regerror
+	    using shared memory. See 'man regex' for details.
+
 Used by modules: pa, rls, dialog, rpa
 Used by modules: pa, rls, dialog, rpa
 
 
 Usage:
 Usage:
 -----
 -----
 
 
-All libraries can be compiled "with ser" or "without ser". Compilation
-without ser may be useful for debugging purposes or for example for 
+All libraries except shm_regex can be compiled "with ser" or "without ser".
+Compilation without ser may be useful for debugging purposes or for example for 
 searching for memory leaks with some tool like valgrind.
 searching for memory leaks with some tool like valgrind.
+shm_regex library can be compiled only with ser.
 
 
 Compilation with ser:
 Compilation with ser:
 --------------------
 --------------------
 
 
 Compilation and installation of these libraries is NOT DONE by running
 Compilation and installation of these libraries is NOT DONE by running
-main ser makefile now - it MUST be done MANUALLY. For this purpose
-is there Makefile.ser. To compile and install libraries simply run
+main ser makefile now - it MUST be done MANUALLY. To compile and install
+libraries simply run
 
 
-   make -f Makefile.ser install
+   make install
 
 
 in lib directory. You can select destination directory like in the case
 in lib directory. You can select destination directory like in the case
 of ser, for example:
 of ser, for example:
 
 
-   make -f Makefile.ser install prefix="/my/ser/directory"
+   make install prefix="/my/ser/directory"
    
    
 AFTER COMPILATION of libraries you can COMPILE MODULES using this 
 AFTER COMPILATION of libraries you can COMPILE MODULES using this 
 libraries like PA, RLS or dialog.
 libraries like PA, RLS or dialog.
@@ -61,6 +65,12 @@ you should set LD_LIBRARY_PATH=/usr/local/lib/ser. In the case of
 nonstandard installation, you can use something like 
 nonstandard installation, you can use something like 
 LD_LIBRARY_PATH=/my/ser/directory/lib/ser.
 LD_LIBRARY_PATH=/my/ser/directory/lib/ser.
 
 
+Compilation without ser:
+--------------------
+Use the alternate makefile, Makefile.noser:
+
+   make -f Makefile.noser install
+
 Documentation
 Documentation
 -------------
 -------------
 Documentation for all libraries is (or will be) in 
 Documentation for all libraries is (or will be) in 

+ 14 - 0
lib/shm_regex/Makefile

@@ -0,0 +1,14 @@
+#
+# makefile for shm_regex
+#
+
+include ../../Makefile.defs
+auto_gen=
+NAME:=ser_shm_regex
+MAJOR_VER=0
+MINOR_VER=1
+BUGFIX_VER=0
+LIBS=
+
+include ../../Makefile.libs
+

+ 23 - 0
lib/shm_regex/README

@@ -0,0 +1,23 @@
+$Id$
+
+Wrapper functions for regcomp, regexec, regfree, and regerror using shared
+memory allocators instead of libc malloc/realloc/free. The original functions
+are prefixed with "shm_".
+
+The regular expression compiled with shm_regcomp() can be used with shm_regexec()
+simultaneously by multiple processes without locking, because regexec has its own
+internal locks. It can be later on freed with shm_regfree() when the expression
+is no longer used. Note however that
+
+1) shm_regexec allocates shared memory for the pattern buffer when it is successfully
+called for the first time.
+
+2) shm_regexec temporary allocates and deallocates memory for storing the registers
+for the pmatch array runtime if nmatch and pmatch parameters are not 0. This is
+done also in shared memory, therefore use the shared memory wrappers for
+regular expression based search-and-replace only if really necessary.
+
+See 'man regex' for the usage and parameters of the functions.
+
+Supported only with GNU C library.
+

+ 129 - 0
lib/shm_regex/shm_regex.c

@@ -0,0 +1,129 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2009 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * History
+ * -------
+ *  2009-04-03	Initial version (Miklos)
+ */
+
+#include <malloc.h>	/* hook prototypes */
+
+#include "../../mem/shm_mem.h"
+#include "shm_regex.h"
+
+typedef void *(malloc_hook_t) (size_t, const void *);
+typedef void *(realloc_hook_t) (void *, size_t, const void *);
+typedef void (free_hook_t) (void *, const void *);
+
+/* The memory hooks are overwritten before calling regcomp(), regfree(),
+ * and regexec(), and shared memory function are called
+ * from the hooks instead of libc malloc/realloc/free.
+ */
+
+static void *shm_malloc_hook(size_t size, const void *caller)
+{
+	return shm_malloc (size);
+}
+
+static void *shm_realloc_hook(void *p, size_t size, const void *caller)
+{
+	return shm_realloc (p, size);
+}
+
+static void shm_free_hook(void *ptr, const void *caller)
+{
+	if (ptr) shm_free (ptr);
+}
+
+#define replace_malloc_hooks() \
+	do { \
+		orig_malloc_hook = __malloc_hook; \
+		orig_realloc_hook = __realloc_hook; \
+		orig_free_hook = __free_hook; \
+		__malloc_hook = shm_malloc_hook; \
+		__realloc_hook = shm_realloc_hook; \
+		__free_hook = shm_free_hook; \
+	} while (0)
+
+#define restore_malloc_hooks() \
+	do { \
+		__malloc_hook = orig_malloc_hook; \
+		__realloc_hook = orig_realloc_hook; \
+		__free_hook = orig_free_hook; \
+	} while (0)
+
+int shm_regcomp(regex_t *preg, const char *regex, int cflags)
+{
+	malloc_hook_t	*orig_malloc_hook;
+	realloc_hook_t	*orig_realloc_hook;
+	free_hook_t	*orig_free_hook;
+	int		ret;
+
+	replace_malloc_hooks();
+	ret = regcomp(preg, regex, cflags);
+	restore_malloc_hooks();
+
+	return ret;
+}
+
+void shm_regfree(regex_t *preg)
+{
+	malloc_hook_t	*orig_malloc_hook;
+	realloc_hook_t	*orig_realloc_hook;
+	free_hook_t	*orig_free_hook;
+
+	replace_malloc_hooks();
+	regfree(preg);
+	restore_malloc_hooks();
+}
+
+int shm_regexec(const regex_t *preg, const char *string, size_t nmatch,
+                   regmatch_t pmatch[], int eflags)
+{
+	malloc_hook_t	*orig_malloc_hook;
+	realloc_hook_t	*orig_realloc_hook;
+	free_hook_t	*orig_free_hook;
+	int		ret;
+
+	/* regexec() allocates some memory for the pattern buffer
+	 * when it is successfully called for the first time, therefore
+	 * shared memory is required also here.
+	 * The drawback is that shared memory allocation is also used
+	 * needlessly for allocating the temporary space for
+	 * the elements of pmatch. -- Does not happen if pmatch and
+	 * nmatch are 0.
+	 * It is safe to call regexec() concurrently without locking,
+	 * because regexec() has its own locks.
+	 * (Miklos)
+	 */
+	replace_malloc_hooks();
+	ret = regexec(preg, string, nmatch,
+			pmatch, eflags);
+	restore_malloc_hooks();
+	
+	return ret;
+}
+

+ 45 - 0
lib/shm_regex/shm_regex.h

@@ -0,0 +1,45 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2009 iptelorg GmbH
+ *
+ * This file is part of ser, a free SIP server.
+ *
+ * ser 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
+ *
+ * For a license to use the ser software under conditions
+ * other than those described here, or to purchase support for this
+ * software, please contact iptel.org by e-mail at the following addresses:
+ *    [email protected]
+ *
+ * ser 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * History
+ * -------
+ *  2009-04-03	Initial version (Miklos)
+ */
+
+#ifndef _SHM_REGEX_H
+#define _SHM_REGEX_H
+
+#include <sys/types.h>
+#include <regex.h>
+
+int shm_regcomp(regex_t *preg, const char *regex, int cflags);
+void shm_regfree(regex_t *preg);
+int shm_regexec(const regex_t *preg, const char *string, size_t nmatch,
+                   regmatch_t pmatch[], int eflags);
+
+#define shm_regerror	regerror
+
+#endif /* _SHM_REGEX_H */