Browse code

Merge commit 'origin/ser_core_cvs'

* commit 'origin/ser_core_cvs':
Wrapper functions for regcomp, regexec, regfree, and regerror using shared
make: minor optimizations
make: fail if make version < 3.80
make: workaround for make 3.80
make: workaround for backwards compat. with make 3.80
dns: fix selecting neg. cname entry

Andrei Pelinescu-Onciul authored on 03/04/2009 14:07:04
Showing 11 changed files
... ...
@@ -53,7 +53,7 @@ Requirements:
53 53
 - bison or yacc (Berkley yacc)
54 54
 - flex
55 55
 - GNU make (on Linux this is the standard "make", on *BSD and Solaris is
56
- called "gmake") version >= 3.79.
56
+ called "gmake") version >= 3.80 (recommended 3.81).
57 57
 - sed and tr (used in the makefiles)
58 58
 - GNU tar ("gtar" on Solaris) and gzip if you want "make tar" to work
59 59
 - GNU install, BSD install or Solaris install if you want "make
... ...
@@ -56,8 +56,21 @@
56 56
 #              added cfg-defs, new target that only rebuilds config.mak
57 57
 #  2009-03-10  replaced DEFS with C_DEFS (DEFS are now used only for
58 58
 #              "temporary" defines inside modules or libs) (andrei)
59
+#  2009-04-02  workaround for export not supported in gnu make 3.80
60
+#               target specific variables: use mk_params for each
61
+#               $(MAKE) invocation (andrei)
59 62
 #
60 63
 
64
+# check make version
65
+# required 3.80, recommended 3.81
66
+req_ver=3.80
67
+# the check below works for version number of the type x.yy or x.yy.z*
68
+# (from the GNU Make Cookbook)
69
+ifeq (,$(filter $(req_ver),$(firstword $(sort $(MAKE_VERSION) $(req_ver)))))
70
+$(error make version $(MAKE_VERSION) not supported, use at least $(req_ver))
71
+endif
72
+
73
+
61 74
 auto_gen=lex.yy.c cfg.tab.c #lexx, yacc etc
62 75
 auto_gen_others=cfg.tab.h  # auto generated, non-c
63 76
 
... ...
@@ -197,7 +210,7 @@ endif
197 197
 # status of the modules
198 198
 # the rest is excluded because it depends on external libraries
199 199
 #
200
-static_modules=
200
+static_modules:=
201 201
 
202 202
 ALLDEP=config.mak Makefile Makefile.sources Makefile.rules
203 203
 
... ...
@@ -235,7 +248,7 @@ static_modules_path=$(addprefix modules/, $(static_modules))
235 235
 extra_sources=$(wildcard $(addsuffix /*.c, $(static_modules_path)))
236 236
 extra_objs=$(extra_sources:.c=.o)
237 237
 
238
-static_defs= $(foreach  mod, $(static_modules), \
238
+static_defs:= $(foreach  mod, $(static_modules), \
239 239
 		-DSTATIC_$(shell echo $(mod) | tr [:lower:] [:upper:]) )
240 240
 
241 241
 override extra_defs+=$(static_defs) $(EXTRA_DEFS)
... ...
@@ -262,7 +275,7 @@ endif # ifneq($(group_include),)
262 262
 endif # ifneq($(modules_configured),1)
263 263
 modules_names=$(shell echo $(modules)| \
264 264
 				sed -e 's/modules\/\([^/ ]*\)\/*/\1.so/g' )
265
-modules_basenames=$(shell echo $(modules)| \
265
+modules_basenames:=$(shell echo $(modules)| \
266 266
 				sed -e 's/modules\/\([^/ ]*\)\/*/\1/g' )
267 267
 #modules_names=$(patsubst modules/%, %.so, $(modules))
268 268
 #modules_full_path=$(join  $(modules), $(addprefix /, $(modules_names)))
... ...
@@ -381,7 +394,8 @@ modules: modules.lst
381 381
 		if [ -n "$$r" -a -r "$$r/Makefile" ]; then \
382 382
 			echo  "" ; \
383 383
 			echo  "" ; \
384
-			if  $(MAKE) -C $$r || [ ${err_fail} != 1 ] ; then \
384
+			if  $(MAKE) -C $$r $(mk_params) || [ ${err_fail} != 1 ] ; \
385
+			then\
385 386
 				:; \
386 387
 			else \
387 388
 				exit 1; \
... ...
@@ -395,7 +409,7 @@ $(extra_objs):
395 395
 		if [ -n "$$r" -a -r "$$r/Makefile"  ]; then \
396 396
 			echo  "" ; \
397 397
 			echo  "Making static module $r" ; \
398
-			if $(MAKE) -C $$r static ; then  \
398
+			if $(MAKE) -C $$r static $(mk_params) ; then  \
399 399
 				:; \
400 400
 			else \
401 401
 				exit 1; \
... ...
@@ -409,7 +423,8 @@ utils:
409 409
 		if [ -n "$$r" ]; then \
410 410
 			echo  "" ; \
411 411
 			echo  "" ; \
412
-			if  $(MAKE) -C $$r || [ ${err_fail} != 1 ] ; then \
412
+			if  $(MAKE) -C $$r $(mk_params) || [ ${err_fail} != 1 ] ; \
413
+			then \
413 414
 				:; \
414 415
 			else \
415 416
 				exit 1; \
... ...
@@ -466,7 +481,7 @@ tar:
466 466
 .PHONY: bin
467 467
 bin:
468 468
 	mkdir -p tmp/ser/usr/local
469
-	$(MAKE) install basedir=tmp/ser 
469
+	$(MAKE) install basedir=tmp/ser $(mk_params)
470 470
 	$(TAR) -C tmp/ser/ -zcf ../$(NAME)-$(RELEASE)_$(OS)_$(ARCH).tar.gz .
471 471
 	rm -rf tmp/ser
472 472
 
... ...
@@ -484,7 +499,7 @@ deb:
484 484
 sunpkg:
485 485
 	mkdir -p tmp/ser
486 486
 	mkdir -p tmp/ser_sun_pkg
487
-	$(MAKE) install basedir=tmp/ser prefix=/usr/local
487
+	$(MAKE) install basedir=tmp/ser prefix=/usr/local $(mk_params)
488 488
 	(cd pkg/solaris; \
489 489
 	pkgmk -r ../../tmp/ser/usr/local -o -d ../../tmp/ser_sun_pkg/ -v "$(RELEASE)" ;\
490 490
 	cd ../..)
... ...
@@ -501,7 +516,7 @@ modules-doc: modules.lst
501 501
 		if [ -n "$$r" ]; then \
502 502
 			echo  "" ; \
503 503
 			echo  "" ; \
504
-			$(MAKE) -C $$r/doc $(doc_format) ; \
504
+			$(MAKE) -C $$r/doc $(doc_format)  $(mk_params); \
505 505
 		fi ; \
506 506
 	done 
507 507
 
... ...
@@ -514,7 +529,8 @@ README: modules.lst
514 514
 		if [ -n "$$r" ]; then \
515 515
 			echo  "" ; \
516 516
 			echo  "" ; \
517
-			if  $(MAKE) -C $$r README || [ ${err_fail} != 1 ] ; then \
517
+			if  $(MAKE) -C $$r README $(mk_params) || \
518
+				[ ${err_fail} != 1 ] ; then \
518 519
 				:; \
519 520
 			else \
520 521
 				exit 1; \
... ...
@@ -531,7 +547,8 @@ man: modules.lst
531 531
 		if [ -n "$$r" ]; then \
532 532
 			echo  "" ; \
533 533
 			echo  "" ; \
534
-			if  $(MAKE) -C $$r man || [ ${err_fail} != 1 ] ; then \
534
+			if  $(MAKE) -C $$r man $(mk_params) || [ ${err_fail} != 1 ] ; \
535
+			then \
535 536
 				:; \
536 537
 			else \
537 538
 				exit 1; \
... ...
@@ -540,7 +557,7 @@ man: modules.lst
540 540
 	done; true
541 541
 
542 542
 .PHONY: install
543
-install: export compile_for_install=yes
543
+install: mk_params="compile_for_install=yes"
544 544
 install: install-bin install-modules install-cfg \
545 545
 	install-doc install-man install-utils install-share
546 546
 
... ...
@@ -636,7 +653,8 @@ install-modules: modules.lst $(modules_prefix)/$(modules_dir)
636 636
 		if [ -n "$$r" -a -r "$$r/Makefile" ]; then \
637 637
 			echo  "" ; \
638 638
 			echo  "" ; \
639
-			if  $(MAKE) -C $$r install || [ ${err_fail} != 1 ] ; then \
639
+			if  $(MAKE) -C $$r install $(mk_params) || \
640
+				[ ${err_fail} != 1 ] ; then \
640 641
 				:; \
641 642
 			else \
642 643
 				exit 1; \
... ...
@@ -110,8 +110,8 @@ EXTRAVERSION = -dev23-make
110 110
 
111 111
 SER_VER = $(shell expr $(VERSION) \* 1000000 + $(PATCHLEVEL) \* 1000 + \
112 112
 			$(SUBLEVEL) )
113
-RELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
114
-OS = $(shell uname -s | sed -e s/SunOS/solaris/ -e s/CYGWIN.*/cygwin/ \
113
+RELEASE:=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
114
+OS := $(shell uname -s | sed -e s/SunOS/solaris/ -e s/CYGWIN.*/cygwin/ \
115 115
 		 | tr "[A-Z]" "[a-z]")
116 116
 
117 117
 ifeq ($(OS),solaris)
... ...
@@ -138,9 +138,9 @@ ifeq ($(ARCH),sparc)
138 138
 	endif
139 139
 endif
140 140
 
141
-OSREL = $(shell uname -r)
141
+OSREL := $(shell uname -r)
142 142
 # numerical version (good for comparisons: A.B.C => A*1000000+B*1000+C)
143
-OSREL_N= $(shell echo $(OSREL) | sed -e 's/^[^0-9]*//' \
143
+OSREL_N:= $(shell echo $(OSREL) | sed -e 's/^[^0-9]*//' \
144 144
 		-e 's/^\([0-9][0-9]*\(\.[0-9][0-9]*\)*\).*$$/\1/g' | \
145 145
 		(IFS=. read A B C D; R=0; \
146 146
 		[ -n "$$A" ] && R=`expr $$R \* 1000 + $$A` && \
... ...
@@ -271,7 +271,7 @@ ifeq ($(CC),)
271 271
 	CC=gcc
272 272
 endif
273 273
 LD= $(CC)
274
-CC_LONGVER=$(shell if  $(CC) -v 2>/dev/null; then \
274
+CC_LONGVER:=$(shell if  $(CC) -v 2>/dev/null; then \
275 275
 						$(CC) -v 2>&1 ;\
276 276
 					else \
277 277
 						$(CC) -V 2>&1 ; \
... ...
@@ -282,14 +282,14 @@ MKTAGS=ctags -R .
282 282
 
283 283
 ifneq (,$(findstring gcc, $(CC_LONGVER)))
284 284
 	CC_NAME=gcc
285
-	CC_VER=$(word 1,$(CC)) $(shell $(CC) - --version|head -n 1|cut -d" " -f 3|\
286
-				 sed -e 's/^.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/'\
285
+	CC_VER:=$(word 1,$(CC)) $(shell $(CC) - --version|head -n 1|cut -d" " -f 3\
286
+				|sed -e 's/^.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/'\
287 287
 				 	 -e 's/^[^0-9].*\([0-9][0-9]*\.[0-9][0-9]*\).*/\1/')
288 288
 	# sed with POSIX.1 regex doesn't support |, + or ? 
289 289
         # (darwin, solaris ...) => this complicated expression
290 290
 	MKDEP=$(CC) -MM 
291 291
 	#transform gcc version into 2.9x or 3.0
292
-	CC_SHORTVER=$(shell echo "$(CC_VER)" | cut -d" " -f 2| \
292
+	CC_SHORTVER:=$(shell echo "$(CC_VER)" | cut -d" " -f 2| \
293 293
 				 sed -e 's/[^0-9]*-\(.*\)/\1/'| \
294 294
 				 sed -e 's/2\.9.*/2.9x/' -e 's/3\.[0-3]\..*/3.0/' -e \
295 295
 				 	's/3\.[0-3]/3.0/' -e 's/3\.[4-9]\..*/3.4/' -e \
... ...
@@ -299,7 +299,7 @@ endif
299 299
 
300 300
 ifneq (, $(findstring Sun, $(CC_LONGVER)))
301 301
 	CC_NAME=suncc
302
-	CC_SHORTVER=$(shell echo "$(CC_LONGVER)"|head -n 1| \
302
+	CC_SHORTVER:=$(shell echo "$(CC_LONGVER)"|head -n 1| \
303 303
 					sed -e 's/.*\([0-9]\.[0-9]\).*/\1/g' )
304 304
 	CC_VER=$(CC) $(CC_SHORTVER)
305 305
 	MKDEP=$(CC) -xM1 
... ...
@@ -308,9 +308,9 @@ endif
308 308
 ifneq (, $(findstring Intel(R) C++ Compiler, $(CC_LONGVER)))
309 309
 	# very nice: gcc compatible
310 310
 	CC_NAME=icc
311
-	CC_FULLVER=$(shell echo "$(CC_LONGVER)"|head -n 1| \
311
+	CC_FULLVER:=$(shell echo "$(CC_LONGVER)"|head -n 1| \
312 312
 					sed -e 's/.*Version \([0-9]\.[0-9]\.[0-9]*\).*/\1/g' )
313
-	CC_SHORTVER=$(shell echo "$(CC_FULLVER)" | cut -d. -f1,2 )
313
+	CC_SHORTVER:=$(shell echo "$(CC_FULLVER)" | cut -d. -f1,2 )
314 314
 	CC_VER=$(CC) $(CC_FULLVER)
315 315
 	MKDEP=$(CC) -MM 
316 316
 endif
... ...
@@ -533,18 +533,18 @@ endif
533 533
 # find ld & as name (gnu or solaris)
534 534
 ifeq ($(OS), solaris)
535 535
 ifeq ($(CC_NAME), gcc)
536
-		LDGCC=$(shell $(CC) -v 2>&1 | grep with-ld| \
536
+		LDGCC:=$(shell $(CC) -v 2>&1 | grep with-ld| \
537 537
 				   sed -e 's/.*--with-ld=\([^ ][^ ]*\).*/\1/' )
538
-		ASGCC=$(shell $(CC) -v 2>&1 | grep with-as| \
538
+		ASGCC:=$(shell $(CC) -v 2>&1 | grep with-as| \
539 539
 				   sed -e 's/.*--with-as=\([^ ][^ ]*\).*/\1/' )
540
-		LDPATH=$(shell  if [ -z "$(LDGCC)" ] ; then echo "ld" ;\
540
+		LDPATH:=$(shell  if [ -z "$(LDGCC)" ] ; then echo "ld" ;\
541 541
 						else \
542 542
 						if $(LDGCC) -V 2>/dev/null 1>/dev/null; then \
543 543
 							echo $(LDGCC); \
544 544
 						else echo "ld" ; \
545 545
 						fi\
546 546
 						fi)
547
-		ASPATH=$(shell  if [ -z "$(ASGCC)" ] ; then echo "as" ;\
547
+		ASPATH:=$(shell  if [ -z "$(ASGCC)" ] ; then echo "as" ;\
548 548
 						else \
549 549
 						if $(ASGCC) -V 2>/dev/null 1>/dev/null; then \
550 550
 							echo $(ASGCC); \
... ...
@@ -552,7 +552,7 @@ ifeq ($(CC_NAME), gcc)
552 552
 						fi\
553 553
 						fi)
554 554
 							
555
-		LDTYPE=$(shell if $(LDPATH) -V 1>/dev/null  2>/dev/null; then \
555
+		LDTYPE:=$(shell if $(LDPATH) -V 1>/dev/null  2>/dev/null; then \
556 556
 							if $(LDPATH) -V 2>&1|grep GNU >/dev/null; \
557 557
 							then echo gnu; \
558 558
 							else \
... ...
@@ -563,13 +563,13 @@ ifeq ($(CC_NAME), gcc)
563 563
 							fi \
564 564
 							fi \
565 565
 						fi)
566
-		ASTYPE=$(shell if $(ASPATH) -V 1>/dev/null  2>/dev/null </dev/null; \
566
+		ASTYPE:=$(shell if $(ASPATH) -V 1>/dev/null  2>/dev/null </dev/null; \
567 567
 						then \
568
-							if $(ASPATH) -V 2>&1 </dev/null |grep GNU >/dev/null; \
569
-							then echo gnu; \
568
+							if $(ASPATH) -V 2>&1 </dev/null | \
569
+								grep GNU >/dev/null; then echo gnu; \
570 570
 							else \
571
-							if $(ASPATH) -V 2>&1 </dev/null |grep Sun >/dev/null;\
572
-							then echo solaris; \
571
+							if $(ASPATH) -V 2>&1 </dev/null | \
572
+								grep Sun >/dev/null; then echo solaris; \
573 573
 							else \
574 574
 								echo unknown ; \
575 575
 							fi \
... ...
@@ -1544,7 +1544,7 @@ ifeq ($(OS), openbsd)
1544 1544
 	# no sched_yield on openbsd unless linking with c_r (not recommended)
1545 1545
 	# unfortunately pthread is needed for sigwait
1546 1546
 	LIBS= -lpthread
1547
-	OPENBSD_IS_AOUT= $(shell echo "$(OSREL)" | \
1547
+	OPENBSD_IS_AOUT:= $(shell echo "$(OSREL)" | \
1548 1548
 				sed -e 's/^3\.[0-3][^0-9]*$$/yes/' |sed -e 's/^[0-2]\..*/yes/')
1549 1549
 # exception: on sparc openbsd 3.2 is elf and not aout
1550 1550
 ifeq ($(OSREL), 3.2)
... ...
@@ -65,7 +65,7 @@ ALLDEP+=makecfg.lst
65 65
 # on some ser libs)
66 66
 
67 67
 ifneq	($(SER_LIBS),)
68
-# abspath & realpath don't seem to work on darwin
68
+# abspath & realpath don't work on make <= 3.80
69 69
 SER_LIBS_DIRS:=$(dir $(SER_LIBS))
70 70
 ifneq	(,$(filter install install% %install, $(MAKECMDGOALS)))
71 71
 lib_compile_for_install=yes
... ...
@@ -79,7 +79,14 @@ ifneq	($(LD_RPATH),)
79 79
 ifneq	(,$(filter install install% %install, $(MAKECMDGOALS)))
80 80
 SER_RPATH_LST:=$(lib_target)
81 81
 else
82
-SER_RPATH_LST:=$(realpath $(dir $(SER_LIBS)))
82
+# realpath is not supported in make 3.80 or older
83
+ifeq (,$(filter-out 3.80 3.80.%,$(MAKE_VERSION)))
84
+fullpath=$(shell cd $(1); pwd)
85
+else
86
+fullpath=$(realpath $(1))
87
+endif
88
+
89
+SER_RPATH_LST:=$(call fullpath,$(dir $(SER_LIBS)))
83 90
 endif
84 91
 ifneq	($(strip $(SER_RPATH_LST)),)
85 92
 SER_RPATH:=$(addprefix $(LD_RPATH),$(SER_RPATH_LST))
... ...
@@ -584,8 +584,11 @@ again:
584 584
 #endif
585 585
 #endif
586 586
 			return e;
587
-		}else if ((e->type==T_CNAME) && (e->name_len==name->len) &&
588
-			(strncasecmp(e->name, name->s, e->name_len)==0)){
587
+		}else if ((e->type==T_CNAME) && !((e->rr_lst==0) || e->err_flags) &&
588
+					(e->name_len==name->len) &&
589
+					(strncasecmp(e->name, name->s, e->name_len)==0)){
590
+			/*if CNAME matches and CNAME is entry is not a neg. cache entry
591
+			  (could be produced by a specific CNAME lookup)*/
589 592
 			e->last_used=now;
590 593
 #ifdef DNS_LU_LST
591 594
 			/* add it at the end */
... ...
@@ -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,6 +24,9 @@ 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
 trie - Common digit trie implementation for prefix matching, used by
... ...
@@ -32,23 +35,24 @@ trie - Common digit trie implementation for prefix matching, used by
32 32
 Usage:
33 33
 -----
34 34
 
35
-All libraries can be compiled "with ser" or "without ser". Compilation
36
-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 
37 37
 searching for memory leaks with some tool like valgrind.
38
+shm_regex library can be compiled only with ser.
38 39
 
39 40
 Compilation with ser:
40 41
 --------------------
41 42
 
42 43
 Compilation and installation of these libraries is NOT DONE by running
43
-main ser makefile now - it MUST be done MANUALLY. For this purpose
44
-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
45 46
 
46
-   make -f Makefile.ser install
47
+   make install
47 48
 
48 49
 in lib directory. You can select destination directory like in the case
49 50
 of ser, for example:
50 51
 
51
-   make -f Makefile.ser install prefix="/my/ser/directory"
52
+   make install prefix="/my/ser/directory"
52 53
    
53 54
 AFTER COMPILATION of libraries you can COMPILE MODULES using this 
54 55
 libraries like PA, RLS or dialog.
... ...
@@ -64,6 +68,12 @@ you should set LD_LIBRARY_PATH=/usr/local/lib/ser. In the case of
64 64
 nonstandard installation, you can use something like 
65 65
 LD_LIBRARY_PATH=/my/ser/directory/lib/ser.
66 66
 
67
+Compilation without ser:
68
+--------------------
69
+Use the alternate makefile, Makefile.noser:
70
+
71
+   make -f Makefile.noser install
72
+
67 73
 Documentation
68 74
 -------------
69 75
 Documentation for all libraries is (or will be) in 
70 76
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 */