Browse code

performance and bug fixes

Jiri Kuthan authored on 07/01/2002 04:39:49
Showing 57 changed files
... ...
@@ -11,7 +11,7 @@ auto_gen=lex.yy.c cfg.tab.c   #lexx, yacc etc
11 11
 include Makefile.sources
12 12
 
13 13
 exclude_modules=CVS usrloc mysql auth
14
-static_modules=
14
+static_modules=tm
15 15
 static_modules_path=$(addprefix modules/, $(static_modules))
16 16
 extra_sources=$(wildcard $(addsuffix /*.c, $(static_modules_path)))
17 17
 extra_objs=$(extra_sources:.c=.o)
... ...
@@ -44,8 +44,16 @@ ARCH = $(shell uname -s)
44 44
 #		extra error checking (trying to free the same pointer
45 45
 #		twice, trying to free a pointer alloc'ed with a different
46 46
 #		malloc etc.)
47
+# -DVQ_MALLOC
48
+#		additional option to PKG_MALLOC which utilizes a fater then
49
+#		qm version
50
+#
51
+
47 52
 DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
48 53
 	 -DDNS_IP_HACK  -DPKG_MALLOC -DSHM_MEM  -DSHM_MMAP \
54
+	-DEXTRA_DEBUG \
55
+	-DVQ_MALLOC 
56
+	  #-DDBG_QM_MALLOC #-DVQ_MALLOC #-DNO_DEBUG
49 57
 	  #-DNO_DEBUG #-DDBG_QM_MALLOC
50 58
 #-DEXTRA_DEBUG
51 59
 # -DUSE_SHM_MEM
... ...
@@ -54,9 +62,9 @@ DEFS+= -DNAME='"$(NAME)"' -DVERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"' \
54 54
 #-DNO_DEBUG#-DSTATS -DNO_DEBUG 
55 55
 #-DNO_LOG
56 56
 
57
-PROFILE=  #-pg #set this if you want profiling
58
-#mode = debug
59
-mode = release
57
+PROFILE=  -pg #set this if you want profiling
58
+mode = debug
59
+#mode = release
60 60
 
61 61
 # platform dependent settings
62 62
 
... ...
@@ -11,7 +11,7 @@
11 11
 # defines: sources, objs, depends
12 12
 #
13 13
 
14
-sources=$(filter-out $(auto_gen), $(wildcard *.c)) $(auto_gen)
14
+sources=$(filter-out $(auto_gen), $(wildcard *.c) $(wildcard mem/*.c) ) $(auto_gen)
15 15
 objs=$(sources:.c=.o)
16 16
 extra_objs=
17 17
 depends=$(sources:.c=.d)
... ...
@@ -15,7 +15,7 @@
15 15
 #include "msg_parser.h"
16 16
 #include "ut.h"
17 17
 #include "sr_module.h"
18
-#include "mem.h"
18
+#include "mem/mem.h"
19 19
 
20 20
 #include <sys/types.h>
21 21
 #include <sys/socket.h>
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- *  $Id
2
+ *  $Id $
3 3
  */
4 4
 
5 5
 
... ...
@@ -12,8 +12,6 @@
12 12
 
13 13
 #define CFG_FILE "./ser.cfg"
14 14
 
15
-/* receive buffer size */
16
-#define BUF_SIZE 65507
17 15
 
18 16
 /* maximum number of addresses on which we will listen */
19 17
 #define MAX_LISTEN 16
... ...
@@ -47,11 +45,29 @@
47 47
 /*used only if PKG_MALLOC is defined*/
48 48
 #define PKG_MEM_POOL_SIZE 1024*1024
49 49
 
50
-/*used is SH_MEM is defined*/
50
+/*used if SH_MEM is defined*/
51 51
 #define SHM_MEM_SIZE 128*1024*1024
52 52
 
53 53
 #define TIMER_TICK 1
54 54
 #define LONG_SLEEP	3600
55 55
 
56
+/* dimensioning buckets in q_malloc */
57
+/* size of the size2bucket table; everything beyond that asks for
58
+   a variable-size kilo-bucket
59
+ */
60
+#define MAX_FIXED_BLOCK         3072
61
+/* distance of kilo-buckets */
62
+#define BLOCK_STEP                      512
63
+/* maximum number of possible buckets */
64
+#define MAX_BUCKET		15
65
+
66
+/* receive buffer size -- preferably set low to
67
+   avoid terror of excessively huge messages
68
+*/
69
+#define BUF_SIZE (MAX_FIXED_BLOCK-32)
70
+
71
+/* forwarding */
72
+#define MAX_VIA_LINE_SIZE	240
73
+#define MAX_RECEIVED_SIZE	57
56 74
 
57 75
 #endif
... ...
@@ -1,8 +1,11 @@
1 1
 /*
2
+ * $Id$
3
+ *
2 4
  *  Crc - 32 + 16 BIT ANSI X3.66 + CCITT CRC checksum files
3 5
  */
4 6
 
5 7
 #include <stdio.h>
8
+#include "crc.h"
6 9
 
7 10
 #define OK 0
8 11
 #define ERROR (-1)
... ...
@@ -60,7 +63,7 @@
60 60
 /*     hardware you could probably optimize the shift in assembler by  */
61 61
 /*     using byte-swap instructions.                                   */
62 62
 
63
-static unsigned long int crc_32_tab[] = { /* CRC polynomial 0xedb88320 */
63
+unsigned long int crc_32_tab[] = { /* CRC polynomial 0xedb88320 */
64 64
 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
65 65
 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
66 66
 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
67 67
new file mode 100644
... ...
@@ -0,0 +1,11 @@
0
+/* $Id$*/
1
+
2
+#ifndef _CRC_H_
3
+#define _CRC_H_
4
+
5
+extern unsigned long int crc_32_tab[];
6
+extern unsigned short int ccitt_tab[];
7
+extern unsigned short int crc_16_tab[];
8
+
9
+#endif
10
+
... ...
@@ -4,7 +4,7 @@
4 4
 
5 5
 #include "data_lump.h"
6 6
 #include "dprint.h"
7
-#include "mem.h"
7
+#include "mem/mem.h"
8 8
 
9 9
 #include <stdlib.h>
10 10
 
... ...
@@ -21,21 +21,15 @@
21 21
 #include "globals.h"
22 22
 #include "data_lump.h"
23 23
 #include "ut.h"
24
-#include "mem.h"
24
+#include "mem/mem.h"
25 25
 #include "msg_translator.h"
26 26
 #include "sr_module.h"
27
+#include "stats.h"
27 28
 
28 29
 #ifdef DEBUG_DMALLOC
29 30
 #include <dmalloc.h>
30 31
 #endif
31 32
 
32
-#define MAX_VIA_LINE_SIZE      240
33
-#define MAX_RECEIVED_SIZE  57
34
-
35
-#ifdef STATS
36
-#include "stats.h"
37
-#endif
38
-
39 33
 
40 34
 
41 35
 int forward_request( struct sip_msg* msg, struct proxy_l * p)
... ...
@@ -79,15 +73,11 @@ int forward_request( struct sip_msg* msg, struct proxy_l * p)
79 79
 				sizeof(struct sockaddr_in))==-1){
80 80
 			p->errors++;
81 81
 			p->ok=0;
82
-#ifdef STATS
83
-			update_fail_on_send;
84
-#endif
82
+			STATS_TX_DROPS;
85 83
 			goto error;
86 84
 	}
87
-#ifdef STATS
88 85
 	/* sent requests stats */
89
-	else update_sent_request( msg->first_line.u.request.method_value );
90
-#endif
86
+	else STATS_TX_REQUEST(  msg->first_line.u.request.method_value );
91 87
 	free(buf);
92 88
 	free(to);
93 89
 	/* received_buf & line_buf will be freed in receiv_msg by free_lump_list*/
... ...
@@ -189,15 +179,10 @@ int forward_reply(struct sip_msg* msg)
189 189
 	if (udp_send(new_buf,new_len, (struct sockaddr*) to,
190 190
 					sizeof(struct sockaddr_in))==-1)
191 191
 	{
192
-#ifdef STATS
193
-		update_fail_on_send;
194
-#endif
192
+		STATS_TX_DROPS;
195 193
 		goto error;
196 194
 	}
197
-#ifdef STATS
198
-	else update_sent_response(  msg->first_line.u.reply.statusclass );
199
-#endif
200
-
195
+	else STATS_TX_RESPONSE(  msg->first_line.u.reply.statusclass );
201 196
 	free(new_buf);
202 197
 	free(to);
203 198
 skip:
... ...
@@ -23,9 +23,9 @@
23 23
 #include "route.h"
24 24
 #include "udp_server.h"
25 25
 #include "globals.h"
26
-#include "mem.h"
26
+#include "mem/mem.h"
27 27
 #ifdef SHM_MEM
28
-#include "shm_mem.h"
28
+#include "mem/shm_mem.h"
29 29
 #endif
30 30
 #include "sr_module.h"
31 31
 #include "timer.h"
... ...
@@ -33,9 +33,7 @@
33 33
 
34 34
 #include <signal.h>
35 35
 
36
-#ifdef STATS
37 36
 #include "stats.h"
38
-#endif
39 37
 
40 38
 #ifdef DEBUG_DMALLOC
41 39
 #include <dmalloc.h>
... ...
@@ -72,6 +70,9 @@ static char flags[]=
72 72
 #ifdef PKG_MALLOC
73 73
 ", PKG_MALLOC"
74 74
 #endif
75
+#ifdef VQ_MALLOC
76
+", VQ_MALLOC"
77
+#endif
75 78
 #ifdef USE_SHM_MEM
76 79
 ", USE_SHM_MEM"
77 80
 #endif
... ...
@@ -171,12 +172,6 @@ int process_no = 0;
171 171
 /* cfg parsing */
172 172
 int cfg_errors=0;
173 173
 
174
-#ifdef PKG_MALLOC
175
-char mem_pool[PKG_MEM_POOL_SIZE];
176
-struct qm_block* mem_block;
177
-#endif
178
-
179
-
180 174
 #define MAX_FD 32 /* maximum number of inherited open file descriptors,
181 175
 		    (normally it shouldn't  be bigger  than 3) */
182 176
 
... ...
@@ -332,7 +327,7 @@ int main_loop()
332 332
 static void sig_usr(int signo)
333 333
 {
334 334
 	DPrint("INT received, program terminates\n");
335
-	if (signo==SIGINT) {	/* exit gracefuly */
335
+	if (signo==SIGINT || signo==SIGPIPE) {	/* exit gracefuly */
336 336
 #ifdef STATS
337 337
 		/* print statistics on exit only for the first process */
338 338
 
... ...
@@ -380,6 +375,7 @@ static void sig_usr(int signo)
380 380
 }
381 381
 
382 382
 
383
+void test();
383 384
 
384 385
 int main(int argc, char** argv)
385 386
 {
... ...
@@ -396,12 +392,20 @@ int main(int argc, char** argv)
396 396
 		DPrint("ERROR: no SIGINT signal handler can be installed\n");
397 397
 		goto error;
398 398
 	}
399
+	/* if we debug and write to a pipe, we want to exit nicely too */
400
+	if (signal(SIGPIPE, sig_usr) == SIG_ERR ) {
401
+		DPrint("ERROR: no SIGINT signal handler can be installed\n");
402
+		goto error;
403
+	}
399 404
 
400 405
 	if (signal(SIGUSR1, sig_usr)  == SIG_ERR ) {
401 406
 		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
402 407
 		goto error;
403 408
 	}
404 409
 
410
+	//memtest();
411
+	//hashtest();
412
+
405 413
 
406 414
 	/* process command line (get port no, cfg. file path etc) */
407 415
 	opterr=0;
... ...
@@ -522,21 +526,9 @@ int main(int argc, char** argv)
522 522
 	}
523 523
 
524 524
 	/*init mallocs (before parsing cfg !)*/
525
-#ifdef PKG_MALLOC
526
-	/*init mem*/
527
-	mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
528
-	if (mem_block==0){
529
-		LOG(L_CRIT, "could not initialize memory pool\n");
525
+	if (init_mallocs()==-1)
530 526
 		goto error;
531
-	}
532
-#endif
533 527
 
534
-#ifdef SHM_MEM
535
-	if (shm_mem_init()<0) {
536
-		LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
537
-		goto error;
538
-	}
539
-#endif
540 528
 	/*init timer, before parsing the cfg!*/
541 529
 	if (init_timer()<0){
542 530
 		LOG(L_CRIT, "could not initialize timer, exiting...\n");
... ...
@@ -641,3 +633,4 @@ error:
641 641
 	return -1;
642 642
 
643 643
 }
644
+
... ...
@@ -28,8 +28,8 @@ jku: added support to deal with vectors
28 28
 #include "md5global.h"
29 29
 #include "md5.h"
30 30
 #include "md5utils.h"
31
-
32 31
 #include "dprint.h"
32
+#include "ut.h"
33 33
 
34 34
 
35 35
 static void MDString PROTO_LIST ((char *));
... ...
@@ -45,13 +45,38 @@ static void MDString PROTO_LIST ((char *));
45 45
  */
46 46
 void MDStringArray (char *dst, str src[], int size)
47 47
 {
48
-  MD_CTX context;
49
-  unsigned char digest[16];
50
-  int i;
48
+	MD_CTX context;
49
+	unsigned char digest[16];
50
+ 	int i;
51
+	int len;
52
+	char *s;
53
+
54
+/*
55
+#	ifdef EXTRA_DEBUG
56
+	int j;
57
+	int sum;
58
+#endif
59
+*/
51 60
 
52
-  MDInit (&context);
53
-  for (i=0; i<size; i++) {
54
-  	MDUpdate (&context, src[i].s, src[i].len);
61
+	MDInit (&context);
62
+	for (i=0; i<size; i++) {
63
+		trim_len( len, s, src[i] );
64
+/*
65
+#		ifdef EXTRA_DEBUG
66
+		fprintf(stderr, "EXTRA_DEBUG: %d. (%d) {", i+1, len);
67
+		sum=0;
68
+		for (j=0; j<len; j++) {
69
+			fprintf( stderr, "%c ", *(s+j));
70
+			sum+=*(s+j);
71
+		}
72
+		for (j=0; j<len; j++) {
73
+			fprintf( stderr, "%d ", *(s+j));
74
+			sum+=*(s+j);
75
+		}
76
+		fprintf(stderr, " [%d]\n", sum );	
77
+#		endif
78
+*/
79
+  		MDUpdate (&context, s, len);
55 80
   }
56 81
   MDFinal (digest, &context);
57 82
 
58 83
deleted file mode 100644
... ...
@@ -1,56 +0,0 @@
1
-/* $Id$
2
- *
3
- * memory related stuff (malloc & friends)
4
- * 
5
- */
6
-
7
-
8
-#ifndef mem_h
9
-#define mem_h
10
-#include "dprint.h"
11
-
12
-#ifdef PKG_MALLOC
13
-#include "q_malloc.h"
14
-
15
-extern struct qm_block* mem_block;
16
-
17
-
18
-#ifdef DBG_QM_MALLOC
19
-
20
-#define pkg_malloc(s) qm_malloc(mem_block, (s),__FILE__, __FUNCTION__, \
21
-								__LINE__);
22
-#define pkg_free(p)   qm_free(mem_block, (p), __FILE__,  __FUNCTION__, \
23
-								__LINE__);
24
-
25
-#else
26
-
27
-#define pkg_malloc(s) qm_malloc(mem_block, (s))
28
-#define pkg_free(p)   qm_free(mem_block, (p))
29
-
30
-#endif
31
-
32
-#define pkg_status()  qm_status(mem_block)
33
-
34
-#elif defined(SHM_MEM) && defined(USE_SHM_MEM)
35
-
36
-#include "shm_mem.h"
37
-
38
-#define pkg_malloc(s) shm_malloc((s))
39
-#define pkg_free(p)   shm_free((p))
40
-#define pkg_status()  shm_status()
41
-
42
-#else
43
-
44
-#include <stdlib.h>
45
-
46
-#define pkg_malloc(s) \
47
-	(  { void *v; v=malloc((s)); \
48
-	   DBG("malloc %x size %d end %x\n", v, s, (unsigned int)v+(s));\
49
-	   v; } )
50
-#define pkg_free(p)  do{ DBG("free %x\n", (p)); free((p)); }while(0);
51
-#define pkg_status()
52
-
53
-#endif
54
-
55
-
56
-#endif
57 1
new file mode 100644
... ...
@@ -0,0 +1,51 @@
0
+/*
1
+ * $Id *
2
+ */
3
+
4
+#include "../config.h"
5
+#include "../dprint.h"
6
+#include "mem.h"
7
+
8
+#ifdef PKG_MALLOC
9
+#	ifdef VQ_MALLOC
10
+#		include "vq_malloc.h"
11
+#	else
12
+#		include "q_malloc.h"
13
+#	endif
14
+#endif
15
+
16
+#ifdef PKG_MALLOC
17
+	char mem_pool[PKG_MEM_POOL_SIZE];
18
+	#ifdef VQ_MALLOC
19
+		struct vqm_block* mem_block;
20
+	#else
21
+		struct qm_block* mem_block;
22
+	#endif
23
+#endif
24
+
25
+int init_mallocs()
26
+{
27
+#ifdef PKG_MALLOC
28
+        /*init mem*/
29
+	#ifdef VQ_MALLOC
30
+        	mem_block=vqm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
31
+	#else
32
+        	mem_block=qm_malloc_init(mem_pool, PKG_MEM_POOL_SIZE);
33
+	#endif
34
+        if (mem_block==0){
35
+                LOG(L_CRIT, "could not initialize memory pool\n");
36
+		return -1;
37
+        }
38
+#endif
39
+
40
+#ifdef SHM_MEM
41
+        if (shm_mem_init()<0) {
42
+                LOG(L_CRIT, "could not initialize shared memory pool, exiting...\n");
43
+                return -1;
44
+        }
45
+#endif
46
+	return 0;
47
+
48
+}
49
+
50
+
0 51
new file mode 100644
... ...
@@ -0,0 +1,68 @@
0
+/* $Id$
1
+ *
2
+ * memory related stuff (malloc & friends)
3
+ * 
4
+ */
5
+
6
+
7
+#ifndef mem_h
8
+#define mem_h
9
+#include "../config.h"
10
+#include "../dprint.h"
11
+
12
+#ifdef PKG_MALLOC
13
+#	ifdef VQ_MALLOC
14
+#		include "vq_malloc.h"
15
+		extern struct vqm_block* mem_block;
16
+#	else
17
+#		include "q_malloc.h"
18
+		extern struct qm_block* mem_block;
19
+#	endif
20
+
21
+	extern char mem_pool[PKG_MEM_POOL_SIZE];
22
+
23
+
24
+#	ifdef DBG_QM_MALLOC
25
+#		ifdef VQ_MALLOC
26
+#			define pkg_malloc(s) vqm_malloc(mem_block, (s),__FILE__, \
27
+				__FUNCTION__, __LINE__);
28
+#			define pkg_free(p)   vqm_free(mem_block, (p), __FILE__,  \
29
+				__FUNCTION__, __LINE__);
30
+#		else
31
+#			define pkg_malloc(s) qm_malloc(mem_block, (s),__FILE__, \
32
+				__FUNCTION__, __LINE__);
33
+#			define pkg_free(p)   qm_free(mem_block, (p), __FILE__,  \
34
+				__FUNCTION__, __LINE__);
35
+#		endif
36
+#	else
37
+#		ifdef VQ_MALLOC
38
+#			define pkg_malloc(s) vqm_malloc(mem_block, (s))
39
+#			define pkg_free(p)   vqm_free(mem_block, (p))
40
+#		else
41
+#			define pkg_malloc(s) qm_malloc(mem_block, (s))
42
+#			define pkg_free(p)   qm_free(mem_block, (p))
43
+#		endif
44
+#	endif
45
+#	ifdef VQ_MALLOC
46
+#		define pkg_status()  vqm_status(mem_block)
47
+#	else
48
+#		define pkg_status()  qm_status(mem_block)
49
+#	endif
50
+#elif defined(SHM_MEM) && defined(USE_SHM_MEM)
51
+#	include "shm_mem.h"
52
+#	define pkg_malloc(s) shm_malloc((s))
53
+#	define pkg_free(p)   shm_free((p))
54
+#	define pkg_status()  shm_status()
55
+#else
56
+#	include <stdlib.h>
57
+#	define pkg_malloc(s) \
58
+	(  { void *v; v=malloc((s)); \
59
+	   DBG("malloc %x size %d end %x\n", v, s, (unsigned int)v+(s));\
60
+	   v; } )
61
+#	define pkg_free(p)  do{ DBG("free %x\n", (p)); free((p)); }while(0);
62
+#	define pkg_status()
63
+#endif
64
+
65
+int init_mallocs();
66
+
67
+#endif
0 68
new file mode 100644
... ...
@@ -0,0 +1,133 @@
0
+#ifdef DBG_QM_MALLOC
1
+
2
+#include "../globals.h"
3
+#include "../config.h"
4
+
5
+#ifdef PKG_MALLOC
6
+#       ifdef VQ_MALLOC
7
+#               include "vq_malloc.h"
8
+#		define MY_MALLOC vqm_malloc
9
+#		define MY_FREE vqm_free
10
+#		define MY_INIT vqm_malloc_init
11
+#		define MY_BLOCK vqm_block
12
+#		define MY_STATUS vqm_status
13
+#       else
14
+#               include "q_malloc.h"
15
+#		define MY_MALLOC qm_malloc
16
+#		define MY_FREE qm_free
17
+#		define MY_INIT qm_malloc_init
18
+#		define MY_BLOCK qm_block
19
+#		define MY_STATUS qm_status
20
+#       endif
21
+#endif
22
+
23
+
24
+void memtest()
25
+{
26
+#define	TEST_SIZE 1024*1024
27
+#define	TEST_RUN 1024
28
+#define LONG_RUN 100000
29
+#define ma(s) MY_MALLOC(mem_block, (s),__FILE__, __FUNCTION__, \
30
+                                                                __LINE__);
31
+#define mf(p)   MY_FREE(mem_block, (p), __FILE__,  __FUNCTION__, \
32
+                                                                __LINE__);
33
+	char tst_mem[TEST_SIZE];
34
+	struct MY_BLOCK* mem_block;
35
+	char *p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
36
+	int i, j, f;
37
+	char *p[TEST_RUN];
38
+	int t;
39
+
40
+	debug=7;
41
+	log_stderr=1;
42
+
43
+	printf("entering test\n");
44
+
45
+	mem_block=MY_INIT( tst_mem, TEST_SIZE );
46
+
47
+	/* coalesing test w/big fragments */
48
+	p0=ma(8194);
49
+	p1=ma(8194);
50
+	p2=ma(8194);
51
+	MY_STATUS(mem_block);
52
+	mf(p1);
53
+	mf(p0);
54
+	MY_STATUS(mem_block);
55
+	mf(p2);
56
+	MY_STATUS(mem_block);
57
+
58
+	/* reuse test w/big fragments */
59
+	p0=ma(8194);
60
+	p1=ma(4196);
61
+	mf(p0);
62
+	p0=ma(8190);
63
+	MY_STATUS(mem_block);
64
+	mf(p1);
65
+	mf(p0);
66
+	MY_STATUS(mem_block);
67
+
68
+
69
+	exit(0);
70
+
71
+	p0=ma(8);
72
+	p1=ma(24);
73
+	p2=ma(32);
74
+	p3=ma(32);
75
+	p4=ma(32);
76
+	p5=ma(1024);
77
+	p6=ma(2048);
78
+
79
+//	MY_STATUS(mem_block);
80
+
81
+//	*(p0+9)=0;
82
+	mf(p0);
83
+	mf(p2);
84
+	mf(p5);
85
+	mf(p6);
86
+	
87
+//	MY_STATUS(mem_block);
88
+
89
+	mf(p1);
90
+	mf(p4);
91
+	mf(p3);
92
+//	mf(p3);
93
+
94
+//	MY_STATUS(mem_block);
95
+
96
+	for (i=0;i<TEST_RUN;i++)
97
+		p[i]=ma( random() & 1023 );
98
+//	MY_STATUS(mem_block);
99
+	for (i=0;i<TEST_RUN;i++)
100
+		mf( p[i] );
101
+//	MY_STATUS(mem_block);
102
+
103
+	f = 0;
104
+#define GRANULARITY 100
105
+	for (j=0; j<LONG_RUN; j++) {
106
+		for (i=0;i<TEST_RUN;i++) {
107
+			t=random() & 1023;
108
+			if (! (t%24) ) t=(t+4096)*2;
109
+			p[i]=ma( random() & 1023 );
110
+		}
111
+		for (i=TEST_RUN/3;i<2*TEST_RUN/3;i++)
112
+			mf( p[i] );
113
+		for (i=TEST_RUN/3;i<2*TEST_RUN/3;i++) {
114
+			t=random() & 1023;
115
+			if (! (t%24) ) t=(t+4096)*2;
116
+			p[i]=ma( random() & 1023 );
117
+		}
118
+		for (i=0;i<TEST_RUN;i++)
119
+			mf( p[i] );
120
+		if ( GRANULARITY*j/LONG_RUN > f ) {
121
+			f=GRANULARITY*j/LONG_RUN ;
122
+			printf("%d%% done\n", f);
123
+		}
124
+	}
125
+	printf("now I'm really done\n");
126
+	MY_STATUS(mem_block);
127
+	printf("And I'm done with dumping final report too\n");
128
+	
129
+	exit(0);
130
+}
131
+
132
+#endif
0 133
new file mode 100644
... ...
@@ -0,0 +1,364 @@
0
+/* $Id$
1
+ *
2
+ */
3
+
4
+#if !defined(q_malloc) && !(defined VQ_MALLOC)
5
+#define q_malloc
6
+
7
+#include "q_malloc.h"
8
+#include "../dprint.h"
9
+
10
+
11
+/*useful macros*/
12
+#define FRAG_END(f)  \
13
+	((struct qm_frag_end*)((char*)(f)+sizeof(struct qm_frag)+ \
14
+	   (f)->size))
15
+
16
+#define FRAG_NEXT(f) \
17
+	((struct qm_frag*)((char*)(f)+sizeof(struct qm_frag)+(f)->size+ \
18
+	   sizeof(struct qm_frag_end)))
19
+			
20
+#define FRAG_PREV(f) \
21
+	( (struct qm_frag*) ( ((char*)(f)-sizeof(struct qm_frag_end))- \
22
+	((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))->size- \
23
+	   sizeof(struct qm_frag) ) )
24
+
25
+#define PREV_FRAG_END(f) \
26
+	((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))
27
+
28
+#ifdef DBG_QM_MALLOC
29
+#define ST_CHECK_PATTERN   0xf0f0f0f0
30
+#define END_CHECK_PATTERN1 0xc0c0c0c0
31
+#define END_CHECK_PATTERN2 0xabcdefed
32
+
33
+
34
+static  void qm_debug_frag(struct qm_block* qm, struct qm_frag* f)
35
+{
36
+	if (f->check!=ST_CHECK_PATTERN){
37
+		LOG(L_CRIT, "BUG: qm_*: fragm. %x beginning overwritten(%x)!\n",
38
+				f, f->check);
39
+		qm_status(qm);
40
+		abort();
41
+	};
42
+	if ((FRAG_END(f)->check1!=END_CHECK_PATTERN1)||
43
+		(FRAG_END(f)->check2!=END_CHECK_PATTERN2)){
44
+		LOG(L_CRIT, "BUG: qm_*: fragm. %x end overwritten(%x, %x)!\n",
45
+				f, FRAG_END(f)->check1, FRAG_END(f)->check2);
46
+		qm_status(qm);
47
+		abort();
48
+	}
49
+	if ((f>qm->first_frag)&&
50
+			((PREV_FRAG_END(f)->check1!=END_CHECK_PATTERN1) ||
51
+				(PREV_FRAG_END(f)->check2!=END_CHECK_PATTERN2) ) ){
52
+		LOG(L_CRIT, "BUG: qm_*: prev. fragm. tail overwritten(%x, %x)[%x]!\n",
53
+				PREV_FRAG_END(f)->check1, PREV_FRAG_END(f)->check2, f);
54
+		qm_status(qm);
55
+		abort();
56
+	}
57
+}
58
+#endif
59
+
60
+
61
+
62
+/* init malloc and return a qm_block*/
63
+struct qm_block* qm_malloc_init(char* address, unsigned int size)
64
+{
65
+	char* start;
66
+	char* end;
67
+	struct qm_block* qm;
68
+	unsigned int init_overhead;
69
+	
70
+	/* make address and size multiple of 8*/
71
+	start=(char*)( ((unsigned int)address%8)?((unsigned int)address+8)/8*8:
72
+			(unsigned int)address);
73
+	if (size<start-address) return 0;
74
+	size-=(start-address);
75
+	if (size <8) return 0;
76
+	size=(size%8)?(size-8)/8*8:size;
77
+	
78
+	init_overhead=sizeof(struct qm_block)+sizeof(struct qm_frag)+
79
+		sizeof(struct qm_frag_end);
80
+	if (size < init_overhead)
81
+	{
82
+		/* not enough mem to create our control structures !!!*/
83
+		return 0;
84
+	}
85
+	end=start+size;
86
+	qm=(struct qm_block*)start;
87
+	memset(qm, 0, sizeof(struct qm_block));
88
+	size-=init_overhead;
89
+	qm->size=size;
90
+	qm->real_used=init_overhead;
91
+	qm->max_real_used=qm->real_used;
92
+	
93
+	qm->first_frag=(struct qm_frag*)(start+sizeof(struct qm_block));
94
+	qm->last_frag_end=(struct qm_frag_end*)(end-sizeof(struct qm_frag_end));
95
+	/* init initial fragment*/
96
+	qm->first_frag->size=size;
97
+	qm->first_frag->u.nxt_free=&(qm->free_lst);
98
+	qm->last_frag_end->size=size;
99
+	qm->last_frag_end->prev_free=&(qm->free_lst);
100
+#ifdef DBG_QM_MALLOC
101
+	qm->first_frag->check=ST_CHECK_PATTERN;
102
+	qm->last_frag_end->check1=END_CHECK_PATTERN1;
103
+	qm->last_frag_end->check2=END_CHECK_PATTERN2;
104
+#endif
105
+	/* init free_lst* */
106
+	qm->free_lst.u.nxt_free=qm->first_frag;
107
+	qm->free_lst_end.prev_free=qm->first_frag;
108
+	qm->free_lst.size=0;
109
+	qm->free_lst_end.size=0;
110
+	
111
+	
112
+	return qm;
113
+}
114
+
115
+
116
+static inline void qm_insert_free(struct qm_block* qm, struct qm_frag* frag)
117
+{
118
+	struct qm_frag* f;
119
+	struct qm_frag* prev;
120
+
121
+	for(f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free){
122
+		if (frag->size < f->size) break;
123
+	}
124
+	/*insert it here*/
125
+	prev=FRAG_END(f)->prev_free;
126
+	prev->u.nxt_free=frag;
127
+	FRAG_END(frag)->prev_free=prev;
128
+	frag->u.nxt_free=f;
129
+	FRAG_END(f)->prev_free=frag;
130
+}
131
+
132
+
133
+
134
+static inline void qm_detach_free(struct qm_block* qm, struct qm_frag* frag)
135
+{
136
+	struct qm_frag *prev;
137
+	struct qm_frag *next;
138
+	
139
+	struct qm_frag_end *end;
140
+
141
+	prev=FRAG_END(frag)->prev_free;
142
+	next=frag->u.nxt_free;
143
+	prev->u.nxt_free=next;
144
+	FRAG_END(next)->prev_free=prev;
145
+	
146
+}
147
+
148
+
149
+
150
+#ifdef DBG_QM_MALLOC
151
+void* qm_malloc(struct qm_block* qm, unsigned int size, char* file, char* func,
152
+					unsigned int line)
153
+#else
154
+void* qm_malloc(struct qm_block* qm, unsigned int size)
155
+#endif
156
+{
157
+	struct qm_frag* f;
158
+	struct qm_frag_end* end;
159
+	struct qm_frag* n;
160
+	unsigned int rest;
161
+	unsigned int overhead;
162
+	
163
+#ifdef DBG_QM_MALLOC
164
+	unsigned int list_cntr;
165
+
166
+	list_cntr = 0;
167
+	DBG("qm_malloc(%x, %d) called from %s: %s(%d)\n", qm, size, file, func,
168
+			line);
169
+#endif
170
+	/*size must be a multiple of 8*/
171
+	size=(size%8)?(size+8)/8*8:size;
172
+	if (size>(qm->size-qm->real_used)) return 0;
173
+	if (qm->free_lst.u.nxt_free==&(qm->free_lst)) return 0;
174
+	/*search for a suitable free frag*/
175
+	for (f=qm->free_lst.u.nxt_free; f!=&(qm->free_lst); f=f->u.nxt_free){
176
+#ifdef DBG_QM_MALLOC
177
+		list_cntr++;
178
+#endif
179
+		
180
+		if (f->size>=size){
181
+			/* we found it!*/
182
+			/*detach it from the free list*/
183
+#ifdef DBG_QM_MALLOC
184
+			qm_debug_frag(qm, f);
185
+#endif
186
+			qm_detach_free(qm, f);
187
+			/*mark it as "busy"*/
188
+			f->u.is_free=0;
189
+			
190
+			/*see if we'll use full frag, or we'll split it in 2*/
191
+			rest=f->size-size;
192
+			overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
193
+			if (rest>overhead){
194
+				f->size=size;
195
+				/*split the fragment*/
196
+				end=FRAG_END(f);
197
+				end->size=size;
198
+				n=(struct qm_frag*)((char*)end+sizeof(struct qm_frag_end));
199
+				n->size=rest-overhead;
200
+				FRAG_END(n)->size=n->size;
201
+				qm->real_used+=overhead;
202
+#ifdef DBG_QM_MALLOC
203
+				end->check1=END_CHECK_PATTERN1;
204
+				end->check2=END_CHECK_PATTERN2;
205
+				/* frag created by malloc, mark it*/
206
+				n->file=file;
207
+				n->func="frag. from qm_malloc";
208
+				n->line=line;
209
+				n->check=ST_CHECK_PATTERN;
210
+/*				FRAG_END(n)->check1=END_CHECK_PATTERN1;
211
+				FRAG_END(n)->check2=END_CHECK_PATTERN2; */
212
+#endif
213
+				/* reinsert n in free list*/
214
+				qm_insert_free(qm, n);
215
+			}else{
216
+				/* we cannot split this fragment any more => alloc all of it*/
217
+			}
218
+			qm->real_used+=f->size;
219
+			qm->used+=f->size;
220
+			if (qm->max_real_used<qm->real_used)
221
+				qm->max_real_used=qm->real_used;
222
+#ifdef DBG_QM_MALLOC
223
+			f->file=file;
224
+			f->func=func;
225
+			f->line=line;
226
+			f->check=ST_CHECK_PATTERN;
227
+		/*  FRAG_END(f)->check1=END_CHECK_PATTERN1;
228
+			FRAG_END(f)->check2=END_CHECK_PATTERN2;*/
229
+	DBG("qm_malloc(%x, %d) returns address %x on %d -th hit\n", qm, size,
230
+			(char*)f+sizeof(struct qm_frag), list_cntr );
231
+#endif
232
+			return (char*)f+sizeof(struct qm_frag);
233
+		}
234
+	}
235
+	return 0;
236
+}
237
+
238
+
239
+
240
+#ifdef DBG_QM_MALLOC
241
+void qm_free(struct qm_block* qm, void* p, char* file, char* func, 
242
+				unsigned int line)
243
+#else
244
+void qm_free(struct qm_block* qm, void* p)
245
+#endif
246
+{
247
+	struct qm_frag* f;
248
+	struct qm_frag* prev;
249
+	struct qm_frag* next;
250
+	struct qm_frag_end *end;
251
+	unsigned int overhead;
252
+	unsigned int size;
253
+
254
+#ifdef DBG_QM_MALLOC
255
+	DBG("qm_free(%x, %x), called from %s: %s(%d)\n", qm, p, file, func, line);
256
+	if (p>(void*)qm->last_frag_end || p<(void*)qm->first_frag){
257
+		LOG(L_CRIT, "BUG: qm_free: bad pointer %x (out of memory block!) - "
258
+				"aborting\n", p);
259
+		abort();
260
+	}
261
+#endif
262
+	if (p==0) {
263
+		DBG("WARNING:qm_free: free(0) called\n");
264
+		return;
265
+	}
266
+	prev=next=0;
267
+	f=(struct qm_frag*) ((char*)p-sizeof(struct qm_frag));
268
+#ifdef DBG_QM_MALLOC
269
+	qm_debug_frag(qm, f);
270
+	if (f->u.is_free){
271
+		LOG(L_CRIT, "BUG: qm_free: freeing already freed pointer,"
272
+				" first free: %s: %s(%d) - aborting\n",
273
+				f->file, f->func, f->line);
274
+		abort();
275
+	}
276
+	DBG("qm_free: freeing block alloc'ed from %s: %s(%d)\n", f->file, f->func,
277
+			f->line);
278
+#endif
279
+	overhead=sizeof(struct qm_frag)+sizeof(struct qm_frag_end);
280
+	next=FRAG_NEXT(f);
281
+	size=f->size;
282
+	qm->used-=size;
283
+	qm->real_used-=size;
284
+#ifdef DBG_QM_MALLOC
285
+	qm_debug_frag(qm, f);
286
+#endif
287
+	if (((char*)next < (char*)qm->last_frag_end) &&( next->u.is_free)){
288
+		/* join */
289
+		qm_detach_free(qm, next);
290
+		size+=next->size+overhead;
291
+		qm->real_used-=overhead;
292
+	}
293
+	
294
+	if (f > qm->first_frag){
295
+		prev=FRAG_PREV(f);
296
+		/*	(struct qm_frag*)((char*)f - (struct qm_frag_end*)((char*)f-
297
+								sizeof(struct qm_frag_end))->size);*/
298
+#ifdef DBG_QM_MALLOC
299
+		qm_debug_frag(qm, f);
300
+#endif
301
+		if (prev->u.is_free){
302
+			/*join*/
303
+			qm_detach_free(qm, prev);
304
+			size+=prev->size+overhead;
305
+			qm->real_used-=overhead;
306
+			f=prev;
307
+		}
308
+	}
309
+	f->size=size;
310
+	FRAG_END(f)->size=f->size;
311
+#ifdef DBG_QM_MALLOC
312
+	f->file=file;
313
+	f->func=func;
314
+	f->line=line;
315
+#endif
316
+	qm_insert_free(qm, f);
317
+}
318
+
319
+
320
+
321
+void qm_status(struct qm_block* qm)
322
+{
323
+	struct qm_frag* f;
324
+	int i;
325
+
326
+	LOG(L_INFO, "qm_status (%x):\n", qm);
327
+	if (!qm) return;
328
+
329
+	LOG(L_INFO, " heap size= %d\n", qm->size);
330
+	LOG(L_INFO, " used= %d, used+overhead=%d, free=%d\n",
331
+			qm->used, qm->real_used, qm->size-qm->real_used);
332
+	LOG(L_INFO, " max used (+overhead)= %d\n", qm->max_real_used);
333
+	
334
+	LOG(L_INFO, "dumping all fragments:\n");
335
+	for (f=qm->first_frag, i=0;(char*)f<(char*)qm->last_frag_end;f=FRAG_NEXT(f)
336
+			,i++){
337
+		LOG(L_INFO, "    %3d. %c  address=%x  size=%d\n", i, 
338
+				(f->u.is_free)?'a':'N',
339
+				(char*)f+sizeof(struct qm_frag), f->size);
340
+#ifdef DBG_QM_MALLOC
341
+		LOG(L_INFO, "            %s from %s: %s(%d)\n",
342
+				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
343
+		LOG(L_INFO, "        start check=%x, end check= %x, %x\n",
344
+				f->check, FRAG_END(f)->check1, FRAG_END(f)->check2);
345
+#endif
346
+	}
347
+	DBG("dumping free list:\n");
348
+	for (f=qm->free_lst.u.nxt_free,i=0; f!=&(qm->free_lst); f=f->u.nxt_free,
349
+			i++){
350
+		DBG("    %3d. %c  address=%x  size=%d\n", i, (f->u.is_free)?'a':'N',
351
+				(char*)f+sizeof(struct qm_frag), f->size);
352
+#ifdef DBG_QM_MALLOC
353
+		DBG("            %s from %s: %s(%d)\n", 
354
+				(f->u.is_free)?"freed":"alloc'd", f->file, f->func, f->line);
355
+#endif
356
+	}
357
+	LOG(L_INFO, "-----------------------------\n");
358
+}
359
+
360
+
361
+
362
+
363
+#endif
0 364
new file mode 100644
... ...
@@ -0,0 +1,68 @@
0
+/* $Id$
1
+ *
2
+ * simple & fast malloc library
3
+ */
4
+
5
+#if !defined(q_malloc_h) && !defined(VQ_MALLOC)
6
+#define q_malloc_h
7
+
8
+
9
+struct qm_frag{
10
+	unsigned int size;
11
+	union{
12
+		struct qm_frag* nxt_free;
13
+		int is_free;
14
+	}u;
15
+#ifdef DBG_QM_MALLOC
16
+	char* file;
17
+	char* func;
18
+	unsigned int line;
19
+	unsigned int check;
20
+#endif
21
+};
22
+
23
+struct qm_frag_end{
24
+#ifdef DBG_QM_MALLOC
25
+	unsigned int check1;
26
+	unsigned int check2;
27
+#endif
28
+	unsigned int size;
29
+	struct qm_frag* prev_free;
30
+};
31
+
32
+
33
+struct qm_block{
34
+	unsigned int size; /* total size */
35
+	unsigned int used; /* alloc'ed size*/
36
+	unsigned int real_used; /* used+malloc overhead*/
37
+	unsigned int max_real_used;
38
+	
39
+	struct qm_frag* first_frag;
40
+	struct qm_frag_end* last_frag_end;
41
+	
42
+	struct qm_frag free_lst;
43
+	struct qm_frag_end free_lst_end;
44
+};
45
+
46
+
47
+
48
+struct qm_block* qm_malloc_init(char* address, unsigned int size);
49
+
50
+#ifdef DBG_QM_MALLOC
51
+void* qm_malloc(struct qm_block*, unsigned int size, char* file, char* func, 
52
+					unsigned int line);
53
+#else
54
+void* qm_malloc(struct qm_block*, unsigned int size);
55
+#endif
56
+
57
+#ifdef DBG_QM_MALLOC
58
+void  qm_free(struct qm_block*, void* p, char* file, char* func, 
59
+				unsigned int line);
60
+#else
61
+void  qm_free(struct qm_block*, void* p);
62
+#endif
63
+
64
+void  qm_status(struct qm_block*);
65
+
66
+
67
+#endif
0 68
new file mode 100644
... ...
@@ -0,0 +1,165 @@
0
+/* $Id$
1
+ *
2
+ * Shared memory functions
3
+ */
4
+
5
+#ifdef SHM_MEM
6
+
7
+#include "shm_mem.h"
8
+#include "../config.h"
9
+
10
+#ifdef  SHM_MMAP
11
+
12
+#include <unistd.h>
13
+#include <sys/mman.h>
14
+#include <sys/types.h> /*open*/
15
+#include <sys/stat.h>
16
+#include <fcntl.h>
17
+
18
+#endif
19
+
20
+
21
+/* define semun */
22
+#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
23
+	/* union semun is defined by including <sys/sem.h> */
24
+#else
25
+	/* according to X/OPEN we have to define it ourselves */
26
+	union semun {
27
+		int val;                    /* value for SETVAL */
28
+		struct semid_ds *buf;       /* buffer for IPC_STAT, IPC_SET */
29
+		unsigned short int *array;  /* array for GETALL, SETALL */
30
+		struct seminfo *__buf;      /* buffer for IPC_INFO */
31
+	};
32
+#endif
33
+
34
+
35
+
36
+#ifndef SHM_MMAP
37
+static int shm_shmid=-1; /*shared memory id*/
38
+#endif
39
+
40
+
41
+int shm_semid=-1; /*semaphore id*/
42
+static void* shm_mempool=(void*)-1;
43
+#ifdef VQ_MALLOC
44
+	struct vqm_block* shm_block;
45
+#else
46
+	struct qm_block* shm_block;
47
+#endif
48
+
49
+
50
+
51
+/* ret -1 on erro*/
52
+int shm_mem_init()
53
+{
54
+
55
+	union semun su;
56
+#ifdef SHM_MMAP
57
+	int fd;
58
+#else
59
+	struct shmid_ds shm_info;
60
+#endif
61
+	int ret;
62
+
63
+#ifdef SHM_MMAP
64
+	if (shm_mempool && (shm_mempool!=(void*)-1)){
65
+#else
66
+	if ((shm_shmid!=-1)||(shm_semid!=-1)||(shm_mempool!=(void*)-1)){
67
+#endif
68
+		LOG(L_CRIT, "BUG: shm_mem_init: shm already initialized\n");
69
+		return -1;
70
+	}
71
+	
72
+#ifdef SHM_MMAP
73
+	fd=open("/dev/zero", O_RDWR);
74
+	if (fd==-1){
75
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not open /dev/zero: %s\n",
76
+				strerror(errno));
77
+		return -1;
78
+	}
79
+	shm_mempool=mmap(0, SHM_MEM_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED,
80
+						fd ,0);
81
+	/* close /dev/zero */
82
+	close(fd);
83
+#else
84
+	
85
+	shm_shmid=shmget(IPC_PRIVATE, SHM_MEM_SIZE, 0700);
86
+	if (shm_shmid==-1){
87
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate shared memory"
88
+				" segment: %s\n", strerror(errno));
89
+		return -1;
90
+	}
91
+	shm_mempool=shmat(shm_shmid, 0, 0);
92
+#endif
93
+	if (shm_mempool==(void*)-1){
94
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not attach shared memory"
95
+				" segment: %s\n", strerror(errno));
96
+		/* destroy segment*/
97
+		shm_mem_destroy();
98
+		return -1;
99
+	}
100
+	/* alloc a semaphore (for malloc)*/
101
+	shm_semid=semget(IPC_PRIVATE, 1, 0700);
102
+	if (shm_semid==-1){
103
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not allocate semaphore: %s\n",
104
+				strerror(errno));
105
+		shm_mem_destroy();
106
+		return -1;
107
+	}
108
+	/* set its value to 1 (mutex)*/
109
+	su.val=1;
110
+	ret=semctl(shm_semid, 0, SETVAL, su);
111
+	if (ret==-1){
112
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not set initial semaphore"
113
+				" value: %s\n", strerror(errno));
114
+		shm_mem_destroy();
115
+		return -1;
116
+	}
117
+	/* init it for malloc*/
118
+#	ifdef VQ_MALLOC
119
+		shm_block=vqm_malloc_init(shm_mempool, SHM_MEM_SIZE);
120
+#	else
121
+		shm_block=qm_malloc_init(shm_mempool, SHM_MEM_SIZE);
122
+#	endif
123
+	if (shm_block==0){
124
+		LOG(L_CRIT, "ERROR: shm_mem_init: could not initialize shared"
125
+				" malloc\n");
126
+		shm_mem_destroy();
127
+		return -1;
128
+	}
129
+	DBG("shm_mem_init: success\n");
130
+	
131
+	return 0;
132
+}
133
+
134
+
135
+
136
+void shm_mem_destroy()
137
+{
138
+#ifndef SHM_MMAP
139
+	struct shmid_ds shm_info;
140
+#endif
141
+	
142
+	DBG("shm_mem_destroy\n");
143
+	if (shm_mempool && (shm_mempool!=(void*)-1)) {
144
+#ifdef SHM_MMAP
145
+		munmap(shm_mempool, SHM_MEM_SIZE);
146
+#else
147
+		shmdt(shm_mempool);
148
+#endif
149
+		shm_mempool=(void*)-1;
150
+	}
151
+#ifndef SHM_MMAP
152
+	if (shm_shmid!=-1) {
153
+		shmctl(shm_shmid, IPC_RMID, &shm_info);
154
+		shm_shmid=-1;
155
+	}
156
+#endif
157
+	if (shm_semid!=-1) {
158
+		semctl(shm_semid, 0, IPC_RMID, (union semun)0);
159
+		shm_semid=-1;
160
+	}
161
+}
162
+
163
+
164
+#endif
0 165
new file mode 100644
... ...
@@ -0,0 +1,171 @@
0
+/* $Id$*
1
+ *
2
+ * shared mem stuff
3
+ */
4
+
5
+#ifdef SHM_MEM
6
+
7
+#ifndef shm_mem_h
8
+#define shm_mem_h
9
+
10
+#include <string.h>
11
+#include <errno.h>
12
+#include <sys/types.h>
13
+#include <sys/ipc.h>
14
+
15
+#ifndef SHM_MMAP
16
+
17
+#include <sys/shm.h>
18
+
19
+#endif
20
+
21
+#include <sys/sem.h>
22
+#include <string.h>
23
+#include <errno.h>
24
+
25
+
26
+
27
+#include "../dprint.h"
28
+
29
+#ifdef VQ_MALLOC
30
+#	include "vq_malloc.h"
31
+	extern struct vqm_block* shm_block;
32
+#	define MY_MALLOC vqm_malloc
33
+#	define MY_FREE vqm_free
34
+#	define MY_STATUS vqm_status
35
+#else
36
+#	include "q_malloc.h"
37
+	extern struct qm_block* shm_block;
38
+#	define MY_MALLOC qm_malloc
39
+#	define MY_FREE qm_free
40
+#	define MY_STATUS qm_status
41
+#endif
42
+extern int shm_semid;
43
+
44
+int shm_mem_init();
45
+void shm_mem_destroy();
46
+
47
+
48
+
49
+inline static void shm_lock()
50
+{
51
+	struct sembuf sop;
52
+	
53
+	sop.sem_num=0;
54
+	sop.sem_op=-1; /*down*/
55
+	sop.sem_flg=0 /*SEM_UNDO*/;
56
+again:
57
+	semop(shm_semid, &sop, 1);
58
+#if 0
59
+	switch(ret){
60
+		case 0: /*ok*/
61
+			break;
62
+		case EINTR: /*interrupted by signal, try again*/
63
+			DBG("sh_lock: interrupted by signal, trying again...\n");
64
+			goto again;
65
+		default:
66
+			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
67
+					strerror(errno));
68
+	}
69
+#endif
70
+}
71
+
72
+
73
+
74
+inline static void shm_unlock()
75
+{
76
+	struct sembuf sop;
77
+	
78
+	sop.sem_num=0;
79
+	sop.sem_op=1; /*up*/
80
+	sop.sem_flg=0 /*SEM_UNDO*/;
81
+again:
82
+	semop(shm_semid, &sop, 1);
83
+#if 0
84
+	/*should ret immediately*/
85
+	switch(ret){
86
+		case 0: /*ok*/
87
+			break;
88
+		case EINTR: /*interrupted by signal, try again*/
89
+			DBG("sh_lock: interrupted by signal, trying again...\n");
90
+			goto again;
91
+		default:
92
+			LOG(L_ERR, "ERROR: sh_lock: error waiting on semaphore: %s\n",
93
+					strerror(errno));
94
+	}
95
+#endif
96
+}
97
+
98
+
99
+
100
+#ifdef DBG_QM_MALLOC
101
+#define shm_malloc(size) \
102
+({\
103
+	void *p;\
104
+	\
105
+	/*if (shm_lock()==0){*/\
106
+		shm_lock();\
107
+		p=MY_MALLOC(shm_block, (size), __FILE__, __FUNCTION__, __LINE__);\
108
+		shm_unlock();\
109
+	/* \
110
+	}else{ \
111
+		p=0;\
112
+	}*/ \
113
+	 p; \
114
+})
115
+
116
+
117
+
118
+#define shm_free(p) \
119
+do { \
120
+		shm_lock(); \
121
+		MY_FREE(shm_block, (p), __FILE__, __FUNCTION__, __LINE__); \
122
+		shm_unlock(); \
123
+}while(0)
124
+
125
+
126
+#else
127
+
128
+
129
+#define shm_malloc(size) \
130