Browse code

Added a set of shared libraries with useful functions and common data structures. Short description is in lib/README, more documentation will be added soon.

Vaclav Kubart authored on 25/10/2005 14:10:16
Showing 65 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,30 @@
1
+# variables to change
2
+
3
+INCLUDES += -I$(CURDIR)
4
+LIBS     += -L$(CURDIR)/cds -L$(CURDIR)/qsa
5
+
6
+####################################
7
+# make rules
8
+
9
+export LIBS
10
+export INCLUDES
11
+
12
+SUBDIRS=cds xcap presence
13
+
14
+.PHONY: subdirs $(SUBDIRS) 
15
+
16
+# clean install tags proper
17
+
18
+subdirs:	$(SUBDIRS)
19
+
20
+$(SUBDIRS):	
21
+			@echo "Making $(MAKECMDGOALS) in $@" ; $(MAKE) $(MAKECMDGOALS) -C $@
22
+
23
+all:	subdirs
24
+
25
+proper:	clean
26
+
27
+clean:	subdirs
28
+
29
+install:	subdirs
30
+
0 31
new file mode 100644
... ...
@@ -0,0 +1,126 @@
1
+# lib-type may be given from commandline to control 
2
+# if resulting library will be static or shared
3
+# possible values: static, shared
4
+lib-type ?= shared
5
+
6
+# install directories
7
+prefix ?= /usr/local
8
+bin-dir ?= ${prefix}/bin
9
+include-dir ?= ${prefix}/include
10
+lib-dir ?= ${prefix}/lib
11
+
12
+# install programs
13
+install          ?= install
14
+install-bin      ?= $(install) -m 755
15
+install-lib      ?= $(install) -m 755
16
+install-includes ?= $(install) -m 644
17
+
18
+# working variables
19
+SRCS := $(wildcard *.c)
20
+HDRS := $(wildcard *.h)
21
+DEP_IN = ${SRCS} ${HDRS}
22
+OBJS := $(patsubst %.c,%.o,$(SRCS))
23
+
24
+####################################
25
+# make rules
26
+
27
+static_lib_name  = lib${NAME}.a
28
+dynamic_lib_name = lib${NAME}.so
29
+prg_name = ${NAME}
30
+
31
+out_name = ${prg_name}
32
+
33
+ifeq ($(TYPE),lib)
34
+INCLUDES += -I$(CURDIR)/..
35
+ifeq ($(lib-type),static)
36
+out_name = ${static_lib_name}
37
+else
38
+out_name = ${dynamic_lib_name}
39
+endif
40
+endif
41
+
42
+default:	${out_name}
43
+
44
+#static library
45
+
46
+${static_lib_name}:	${OBJS}
47
+		ar -r $@ ${OBJS}
48
+
49
+# dynamic library
50
+
51
+${dynamic_lib_name}:	${OBJS}
52
+		${CC} -shared ${DEFS} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}
53
+
54
+# executable
55
+
56
+${NAME}:	${OBJS}
57
+		${CC} ${DEFS} ${CFLAGS} ${INCLUDES} -o $@ ${OBJS} ${LIBS}
58
+		
59
+# common rules
60
+
61
+%.o:	%.c
62
+		${CC} ${DEFS} ${CFLAGS} ${INCLUDES} -c $<
63
+
64
+.PHONY:	install clean proper
65
+
66
+proper: clean
67
+
68
+clean:
69
+		-@rm -f ${prg_name} ${static_lib_name} ${dynamic_lib_name} *.o *.so *.d core core.* *~ tags Makefile.deps
70
+
71
+
72
+ifneq ($(MAKECMDGOALS),proper)
73
+ifneq ($(MAKECMDGOALS),clean)
74
+-include $(SRCS:.c=.d)
75
+endif
76
+endif
77
+
78
+%.d:	%.c
79
+		@$(CC) -M ${DEFS} $(CFLAGS) $(INCLUDES) $< > $@.$$$$; \
80
+		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
81
+		rm -f $@.$$$$
82
+
83
+
84
+# -include Makefile.deps
85
+#
86
+# Makefile.deps: ${DEP_IN}
87
+#		@echo "" > Makefile.deps
88
+#		@makedepend -fMakefile.deps -- ${DEFS} ${CFLAGS} ${INCLUDES} -- ${DEP_IN} 2>/dev/null
89
+#		-@rm -f Makefile.deps.bak
90
+
91
+# instalation rules
92
+
93
+ifeq ($(TYPE),lib)
94
+
95
+# library instalation
96
+
97
+install:	${out_name} install_dirs
98
+			$(install-lib) ${out_name} $(lib-dir)
99
+			@for hdr in ${HDRS} ; do \
100
+				$(install-includes) $$hdr $(include-dir)/$(NAME); \
101
+			done
102
+
103
+install_dirs:	$(lib-dir) $(include-dir)/$(NAME)
104
+
105
+else
106
+
107
+# executable instalation
108
+
109
+install:	${out_name} install_dirs
110
+			$(install-bin) ${out_name} $(bin-dir)
111
+
112
+install_dirs:	$(bin-dir)
113
+				
114
+endif
115
+
116
+# creating install directories
117
+
118
+$(bin-dir):	
119
+			mkdir -p $(bin-dir)
120
+
121
+$(lib-dir):	
122
+			mkdir -p $(lib-dir)
123
+			
124
+$(include-dir)/$(NAME):	
125
+			mkdir -p $(include-dir)/$(NAME)
126
+
0 127
new file mode 100644
... ...
@@ -0,0 +1,44 @@
1
+# variables to change
2
+
3
+CFLAGS   += -g -Wall
4
+DEFS     += -DSER
5
+ser      = $(CURDIR)/..
6
+
7
+INCLUDES += -I$(CURDIR) -I$(ser)
8
+LIBS     += -L$(CURDIR)/cds -L$(CURDIR)/qsa
9
+
10
+####################################
11
+# make rules
12
+
13
+include ../Makefile.defs
14
+
15
+lib-dir     = lib/ser
16
+INSTALL-LIB = $(INSTALL-BIN)
17
+LIBDIR      = $(modules-prefix)/$(lib-dir)
18
+
19
+export INSTALL-LIB
20
+export LIBDIR
21
+export INCLUDES
22
+export LIBS
23
+export CFLAGS
24
+export DEFS
25
+
26
+SUBDIRS=cds xcap presence
27
+
28
+.PHONY: subdirs $(SUBDIRS) 
29
+
30
+# clean install tags proper
31
+
32
+subdirs:	$(SUBDIRS)
33
+
34
+$(SUBDIRS):	
35
+			@echo "Making $(MAKECMDGOALS) in $@" ; $(MAKE) -f Makefile.ser $(MAKECMDGOALS) -C $@
36
+
37
+all:	subdirs
38
+
39
+proper:	clean
40
+
41
+clean:	subdirs
42
+
43
+install:	subdirs
44
+
0 45
new file mode 100644
... ...
@@ -0,0 +1,51 @@
1
+# working variables
2
+
3
+SRCS := $(wildcard *.c)
4
+HDRS := $(wildcard *.h)
5
+DEP_IN = $(SRCS) $(HDRS)
6
+OBJS := $(patsubst %.c,%.o,$(SRCS))
7
+
8
+# static libraries
9
+
10
+ifeq ($(OUT_TYPE),static-lib)
11
+
12
+$(OUT_NAME):	$(OBJS)
13
+		ar -r $@ $(OBJS)
14
+
15
+install:	$(OUT_NAME)
16
+
17
+endif
18
+
19
+# dynamic libraries
20
+
21
+ifeq ($(OUT_TYPE),lib)
22
+$(OUT_NAME):	$(OBJS)
23
+		$(CC) -shared $(DEFS) $(CFLAGS) $(INCLUDES) -o $@ $(OBJS) $(LIBS)
24
+
25
+install:	$(OUT_NAME)
26
+			$(INSTALL-TOUCH) $(LIBDIR)/$(OUT_NAME)
27
+			$(INSTALL-LIB) $(OUT_NAME) $(LIBDIR)
28
+endif
29
+
30
+# common rules 
31
+
32
+%.o:	%.c
33
+		$(CC) $(DEFS) $(CFLAGS) $(INCLUDES) -c $<
34
+
35
+.PHONY:	clean install proper
36
+
37
+proper: clean
38
+
39
+clean:
40
+		-@rm -f $(OUT_NAME) *.o *.so *.d core core.* *~ Makefile.deps
41
+
42
+
43
+ifneq ($(MAKECMDGOALS),clean)
44
+-include $(SRCS:.c=.d)
45
+endif
46
+
47
+%.d:	%.c
48
+		@$(CC) -M $(DEFS) $(CFLAGS) $(INCLUDES) $< > $@.$$$$; \
49
+		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
50
+		rm -f $@.$$$$
51
+
0 52
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+Libraries common to modules:
2
+---------------------------
3
+
4
+cds - Common Data Structures (basic string operations, dynamic string,
5
+      vector, message queue, ...)
6
+
7
+presence - Library holding common structures and functions abaut presence
8
+           (API for internal subscriptions, common presence structures,
9
+           common presence data formats)
10
+           requires internal libraries: cds
11
+
12
+xcap - Common XCAP operations and structures (XCAP authorization documents
13
+       and XCAP resource lists processing)
14
+       requires external libraries: libxml2, libcurl3
15
+       requires internal libraries: cds
16
+
17
+Used by modules: pa, rls, dialog, rpa
18
+
0 19
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+DEFS     += 
2
+INCLUDES +=
3
+LIBS     +=
4
+
5
+# name of result executable or library
6
+NAME = cds
7
+
8
+# TYPE=lib => shared or static library, executable otherwise
9
+TYPE = lib
10
+
11
+include ../Makefile.defs
12
+
0 13
new file mode 100644
... ...
@@ -0,0 +1,6 @@
1
+LIBNAME  = cds
2
+OUT_NAME = libcds.so
3
+OUT_TYPE = lib
4
+
5
+include ../Makefile.ser.defs
6
+
0 7
new file mode 100644
... ...
@@ -0,0 +1,138 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#include <cds/dlink.h>
27
+#include <cds/memory.h>
28
+#include <stdlib.h>
29
+
30
+dlink_element_t *dlink_element_alloc(int _data_length)
31
+{
32
+	dlink_element_t *e = (dlink_element_t*)cds_malloc(_data_length + sizeof(dlink_element_t)); /* shm */
33
+	if (e) {
34
+		e->data_length = _data_length;
35
+		e->next = NULL;
36
+		e->prev = NULL;
37
+	}
38
+	return e;
39
+}
40
+
41
+void dlink_element_free(dlink_element_t *e)
42
+{
43
+	if (e) cds_free(e); /* shm */
44
+}
45
+
46
+/* dlink_element_t *dlink_element_alloc_pkg(int _data_length)
47
+{
48
+	dlink_element_t *e = (dlink_element_t*)DLINK_PKG_ALLOC(_data_length + sizeof(dlink_element_t));
49
+	if (e) {
50
+		e->data_length = _data_length;
51
+		e->next = NULL;
52
+		e->prev = NULL;
53
+	}
54
+	return e;
55
+}
56
+
57
+void dlink_element_free_pkg(dlink_element_t *e) 
58
+{
59
+	if (e) DLINK_PKG_FREE(e);
60
+} */
61
+
62
+char* dlink_element_data(dlink_element_t *e)
63
+{
64
+	if (e) return e->data;
65
+	else return NULL;
66
+}
67
+
68
+
69
+void dlink_init(dlink_t *l) 
70
+{
71
+	if (l) {
72
+		l->first = NULL;
73
+		l->last = NULL;
74
+	}
75
+}
76
+
77
+void dlink_add(dlink_t *l, dlink_element_t *e)
78
+{
79
+	if ( (!l) || (!e) ) return;
80
+	
81
+	e->next = NULL;
82
+	if (!l->last) {
83
+		l->first = e;
84
+		e->prev = NULL;
85
+	}
86
+	else {
87
+		l->last->next = e;
88
+		e->prev = l->last;
89
+	}
90
+	l->last = e;
91
+}
92
+
93
+void dlink_remove(dlink_t *l, dlink_element_t *e)
94
+{
95
+	if ((!l) || (!e)) return;
96
+
97
+	if (e == l->first) l->first = e->next;
98
+	if (e == l->last) l->last = e->prev;
99
+	
100
+	if (e->prev) e->prev->next = e->next;
101
+	if (e->next) e->next->prev = e->prev;
102
+	
103
+	e->next = NULL;
104
+	e->prev = NULL;
105
+}
106
+
107
+dlink_element_t *dlink_start_walk(dlink_t *l) { if (l) return l->first; else return NULL; }
108
+dlink_element_t *dlink_next_element(dlink_element_t *e) { if (e) return e->next; else return NULL; }
109
+dlink_element_t *dlink_prev_element(dlink_element_t *e) { if (e) return e->prev; else return NULL; }
110
+
111
+dlink_element_t *dlink_last_element(dlink_t *l) 
112
+{
113
+	if (l) return l->last;
114
+	else return NULL;
115
+}
116
+
117
+void dlink_destroy(dlink_t *l)
118
+{
119
+	dlink_element_t *e,*n;
120
+	e = dlink_start_walk(l);
121
+	while (e) {
122
+		n = dlink_next_element(e);
123
+		dlink_element_free(e);
124
+		e = n;
125
+	}
126
+}
127
+/*
128
+void dlink_destroy_pkg(dlink_t *l)
129
+{
130
+	dlink_element_t *e,*n;
131
+	e = dlink_start_walk(l);
132
+	while (e) {
133
+		n = dlink_next_element(e);
134
+		dlink_element_free_pkg(e);
135
+		e = n;
136
+	}
137
+}
138
+*/
0 139
new file mode 100644
... ...
@@ -0,0 +1,100 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#ifndef __DLINK_H
27
+#define __DLINK_H
28
+
29
+#include <stdio.h>
30
+
31
+/** 
32
+ * \defgroup dlink Universal double linked lists
33
+ *
34
+ */
35
+
36
+/**
37
+ * One element of dynamic linked list. Each element 
38
+ * can carry an arbitrary data element, which is allocated 
39
+ * together with extra data needed for linking 
40
+ * (pointers to next and previous elements).
41
+ */
42
+typedef struct dlink_element {
43
+	struct dlink_element *next;
44
+	struct dlink_element *prev;
45
+	
46
+	int data_length;
47
+	/** data array of unspecified size */
48
+	char data[1];
49
+} dlink_element_t;
50
+
51
+dlink_element_t *dlink_element_alloc(int _data_length);
52
+void dlink_element_free(dlink_element_t *e);
53
+/* dlink_element_t *dlink_element_alloc_pkg(int _data_length);
54
+void dlink_element_free_pkg(dlink_element_t *e); */
55
+char* dlink_element_data(dlink_element_t *e);
56
+
57
+/**
58
+ * Structure carying information about linked list.
59
+ */
60
+typedef struct dlink {
61
+	/** Pointer to the first element of this list. */
62
+	dlink_element_t *first;
63
+	
64
+	/** Pointer to the last element of this list due 
65
+	 * to more effective adding to the end of the list */	
66
+	dlink_element_t *last;
67
+
68
+	/* TODO: add some members for monitoring, ...
69
+	 * add hash map?
70
+	 */
71
+} dlink_t;
72
+
73
+/**
74
+ * Initializes dlink structure - clears pointers to the
75
+ * start and end of the link and so on.
76
+ */
77
+void dlink_init(dlink_t *l);
78
+
79
+/** destroys all elements in shared memory */
80
+void dlink_destroy(dlink_t *l);
81
+
82
+/* destroys all elements in pkg memory */
83
+/* void dlink_destroy_pkg(dlink_t *l); */
84
+
85
+/** Adds one (only one!) element e to the end of the link l. */
86
+void dlink_add(dlink_t *l, dlink_element_t *e);
87
+
88
+/** Removes an element e from the link. */
89
+void dlink_remove(dlink_t *l, dlink_element_t *e);
90
+
91
+/** Thiss method initiates walking through the list.
92
+ * It returns a pointer to the first element of the link. */
93
+dlink_element_t *dlink_start_walk(dlink_t *l);
94
+
95
+dlink_element_t *dlink_last_element(dlink_t *l);
96
+
97
+dlink_element_t *dlink_next_element(dlink_element_t *e);
98
+dlink_element_t *dlink_prev_element(dlink_element_t *e);
99
+
100
+#endif
0 101
new file mode 100644
... ...
@@ -0,0 +1,157 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#include <stdio.h>
27
+#include <string.h>
28
+#include <cds/dstring.h>
29
+#include <cds/memory.h>
30
+
31
+static dstr_buff_t *get_current_buffer(dstring_t *dstr) 
32
+{
33
+	dstr_buff_t *buff;
34
+	buff = (dstr_buff_t*)dlink_element_data(dlink_last_element(&dstr->buffers));
35
+	return buff;
36
+}
37
+
38
+static dstr_buff_t *add_new_buffer(dstring_t *dstr) 
39
+{
40
+	dstr_buff_t *buff = NULL;
41
+	dlink_element_t *e;
42
+	
43
+	/* e = dlink_element_alloc_pkg(sizeof(dstr_buff_t) + dstr->buff_size); */
44
+	e = dlink_element_alloc(sizeof(dstr_buff_t) + dstr->buff_size);
45
+	if (e) {
46
+		buff = (dstr_buff_t*)dlink_element_data(e);
47
+		buff->len = dstr->buff_size;
48
+		buff->used = 0;
49
+		dlink_add(&dstr->buffers, e);
50
+	}
51
+	return buff;
52
+}
53
+
54
+int dstr_append(dstring_t *dstr, const char *s, int len)
55
+{
56
+	int size;
57
+	dstr_buff_t *buff;
58
+	
59
+	if (len == 0) return 0; /*append empty string*/
60
+	
61
+	buff = get_current_buffer(dstr);
62
+	if (!buff) buff = add_new_buffer(dstr);
63
+	while ((len > 0) && (buff)) {
64
+		size = buff->len - buff->used;
65
+		if (size > len) size = len;
66
+		memcpy(buff->data + buff->used, s, size);
67
+		buff->used += size;
68
+		len -= size;
69
+		s += size;
70
+		dstr->len += size;
71
+		if (len > 0) buff = add_new_buffer(dstr);
72
+	}
73
+	if (!buff) return -1;
74
+	return 0;
75
+}
76
+
77
+int dstr_append_zt(dstring_t *dstr, const char *s)
78
+{
79
+	if (!dstr) return -1;
80
+	if (!s) return 0; /*append empty string*/
81
+	return dstr_append(dstr, s, strlen(s));
82
+}
83
+
84
+int dstr_append_str(dstring_t *dstr, const str_t *s)
85
+{
86
+	if (!dstr) return -1;
87
+	if (!s) return 0; /*append empty string*/
88
+	return dstr_append(dstr, s->s, s->len);
89
+}
90
+
91
+int dstr_get_data_length(dstring_t *dstr)
92
+{
93
+	if (!dstr) return 0;
94
+	else return dstr->len;
95
+}
96
+
97
+int dstr_get_data(dstring_t *dstr, char *dst)
98
+{
99
+	dlink_element_t *e;
100
+	dstr_buff_t* buff;
101
+	
102
+	if (!dstr) return -1;
103
+	e = dlink_start_walk(&dstr->buffers);
104
+	while (e) {
105
+		buff = (dstr_buff_t*)dlink_element_data(e);
106
+		memcpy(dst, buff->data, buff->used);
107
+		dst += buff->used;
108
+		e = dlink_next_element(e);
109
+	}
110
+	return 0;
111
+}
112
+
113
+int dstr_get_str(dstring_t *dstr, str_t *dst)
114
+{
115
+	int res = 0;
116
+	
117
+	if (!dst) return -1;
118
+	dst->len = dstr_get_data_length(dstr);
119
+	if (dst->len > 0) {
120
+		dst->s = (char*)cds_malloc(dst->len);
121
+		if (!dst->s) {
122
+			res = -1;
123
+			dst->len = 0;
124
+			dst->s = NULL;
125
+		}
126
+		else res = dstr_get_data(dstr, dst->s);
127
+	} else dst->s = NULL;
128
+
129
+	return res;
130
+}
131
+
132
+
133
+int dstr_init(dstring_t *dstr, int buff_size)
134
+{
135
+	if (!dstr) return -1;
136
+	dstr->buff_size = buff_size;
137
+	dstr->len = 0;
138
+	dlink_init(&dstr->buffers);
139
+	return 0;
140
+}
141
+
142
+int dstr_destroy(dstring_t *dstr)
143
+{
144
+	dlink_element_t *e,*n;
145
+	if (!dstr) return -1;
146
+	/* dlink_destroy(&dstr->buffers); */
147
+	e = dlink_start_walk(&dstr->buffers);
148
+	while (e) {
149
+		n = dlink_next_element(e);
150
+		dlink_remove(&dstr->buffers, e);
151
+		dlink_element_free(e);
152
+		/* dlink_element_free_pkg(e); */
153
+		e = n;
154
+	}
155
+	return 0;
156
+}
157
+
0 158
new file mode 100644
... ...
@@ -0,0 +1,64 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#ifndef __DSTRING_H
27
+#define __DSTRING_H
28
+
29
+#include <cds/sstr.h>
30
+#include <cds/dlink.h>
31
+
32
+#ifdef __cplusplus
33
+extern "C" {
34
+#endif
35
+
36
+typedef struct dstr_buff {
37
+	int len;
38
+	int used;
39
+	char data[1];
40
+} dstr_buff_t;
41
+
42
+/** Dynamic string structure. It is used
43
+ * for muliple appends of any strings. */
44
+typedef struct dstring {
45
+	dlink_t buffers;
46
+	/** the length of whole string */
47
+	int len;
48
+	int buff_size;
49
+} dstring_t;
50
+
51
+int dstr_append_zt(dstring_t *dstr, const char *s);
52
+int dstr_append(dstring_t *dstr, const char *s, int len);
53
+int dstr_append_str(dstring_t *dstr, const str_t *s);
54
+int dstr_get_data_length(dstring_t *dstr);
55
+int dstr_get_data(dstring_t *dstr, char *dst);
56
+int dstr_get_str(dstring_t *dstr, str_t *dst);
57
+int dstr_init(dstring_t *dstr, int buff_size);
58
+int dstr_destroy(dstring_t *dstr);
59
+
60
+#ifdef __cplusplus
61
+}
62
+#endif
63
+	
64
+#endif
0 65
new file mode 100644
... ...
@@ -0,0 +1,173 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#include <cds/hash_table.h>
27
+#include <stdio.h>
28
+#include <stdlib.h>
29
+#include <string.h>
30
+
31
+int ht_init(hash_table_t *ht, hash_func_t hash_func, key_cmp_func_t cmp_keys, int size)
32
+{
33
+	if (!ht) return -1;
34
+	if ((!hash_func) || (!cmp_keys)) return -1;
35
+
36
+	ht->cslots = (ht_cslot_t*)malloc(size * sizeof(ht_cslot_t));
37
+	if (!ht->cslots) return -1;
38
+	memset(ht->cslots, 0, size * sizeof(ht_cslot_t));
39
+
40
+	ht->size = size;
41
+	ht->hash = hash_func;
42
+	ht->cmp = cmp_keys;
43
+
44
+	ht->find_cnt = 0;
45
+	ht->cmp_cnt = 0;
46
+	ht->nocmp_cnt = 0;
47
+	ht->missed_cnt = 0;
48
+	return 0;
49
+}
50
+
51
+void ht_destroy(hash_table_t *ht)
52
+{
53
+	ht_element_t *e, *n;
54
+	int i;
55
+	
56
+	if (!ht) return;
57
+	if (ht->cslots) {
58
+		for (i = 0; i < ht->size; i++) {
59
+			e = ht->cslots[i].first;
60
+			while (e) {
61
+				n = e->next;
62
+				free(e);
63
+				e = n;
64
+			}
65
+		}
66
+		free(ht->cslots);
67
+	}
68
+	ht->cslots = NULL;
69
+}
70
+
71
+int ht_add(hash_table_t *ht, ht_key_t key, ht_data_t data)
72
+{
73
+	int h;
74
+	ht_element_t *new_e;
75
+	
76
+	if (!ht) return -1;
77
+	new_e = (ht_element_t*)malloc(sizeof(ht_element_t));
78
+	if (!new_e) return -1;
79
+	new_e->next = NULL;
80
+	new_e->key = key;
81
+	new_e->data = data;
82
+	
83
+	h = ht->hash(key) % ht->size;
84
+	if (h < 0) h = -h;
85
+	
86
+	if (!ht->cslots[h].last) {
87
+		ht->cslots[h].first = new_e;
88
+	}
89
+	else {
90
+		ht->cslots[h].last->next = new_e;
91
+	}
92
+
93
+	ht->cslots[h].cnt++;
94
+	ht->cslots[h].last = new_e;
95
+	return 0;
96
+}
97
+
98
+ht_data_t ht_find(hash_table_t *ht, ht_key_t key)
99
+{
100
+	int h;
101
+	ht_element_t *e;
102
+
103
+	if (!ht) return NULL;
104
+	
105
+	ht->find_cnt++;	//monitor
106
+	
107
+	h = ht->hash(key) % ht->size;
108
+	if (h < 0) h = -h;
109
+	e = ht->cslots[h].first;
110
+	if (!e) ht->nocmp_cnt++;	//monitor
111
+	while (e) {
112
+		ht->cmp_cnt++;	//monitor
113
+		if (ht->cmp(e->key, key) == 0) return e->data;
114
+		e = e->next;
115
+	}
116
+	
117
+	ht->missed_cnt++;	//monitor
118
+	return NULL;
119
+}
120
+
121
+ht_data_t ht_remove(hash_table_t *ht, ht_key_t key)
122
+{
123
+	int h;
124
+	ht_element_t *e,*p;
125
+	ht_data_t data;
126
+	
127
+	if (!ht) return NULL;
128
+	h = ht->hash(key) % ht->size;
129
+	if (h < 0) h = -h;
130
+	e = ht->cslots[h].first;
131
+	p = NULL;
132
+	while (e) {
133
+		if (ht->cmp(e->key, key) == 0) {
134
+			if (p) p->next = e->next;
135
+			else ht->cslots[h].first = e->next;
136
+			ht->cslots[h].cnt--;
137
+			if (!e->next) ht->cslots[h].last = p;
138
+			data = e->data;
139
+			free(e);
140
+			return data;
141
+		}
142
+		p = e;
143
+		e = e->next;
144
+	}
145
+	return NULL;
146
+}
147
+
148
+void ht_get_statistic(hash_table_t *ht, ht_statistic_t *s)
149
+{
150
+	if (!s) return;
151
+	if (!ht) {
152
+		s->find_cnt = 0;
153
+		s->cmp_cnt = 0;
154
+		s->nocmp_cnt = 0;
155
+		s->missed_cnt = 0;
156
+	}
157
+	else {
158
+		s->find_cnt = ht->find_cnt;
159
+		s->cmp_cnt = ht->cmp_cnt;
160
+		s->nocmp_cnt = ht->nocmp_cnt;
161
+		s->missed_cnt = ht->missed_cnt;
162
+	}
163
+}
164
+
165
+void ht_clear_statistic(hash_table_t *ht)
166
+{
167
+	if (!ht) return;
168
+	
169
+	ht->find_cnt = 0;
170
+	ht->cmp_cnt = 0;
171
+	ht->nocmp_cnt = 0;
172
+	ht->missed_cnt = 0;
173
+}
0 174
new file mode 100644
... ...
@@ -0,0 +1,77 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#ifndef __HASH_TABLE_H
27
+#define __HASH_TABLE_H
28
+
29
+typedef struct ht_statistic {
30
+	int find_cnt;
31
+	/** count of comparations during find operations */
32
+	int cmp_cnt;
33
+	/** count of finds which started in empty slot (-> no compares) */
34
+	int nocmp_cnt;
35
+	/** count of finds returning NULL */
36
+	int missed_cnt;
37
+} ht_statistic_t;
38
+
39
+typedef const void* ht_key_t;
40
+typedef void* ht_data_t;
41
+
42
+typedef int (*hash_func_t)(ht_key_t k);
43
+typedef int (*key_cmp_func_t)(ht_key_t a, ht_key_t b);
44
+
45
+typedef struct ht_element {
46
+	ht_key_t key;
47
+	ht_data_t data;
48
+	struct ht_element *next;
49
+} ht_element_t;
50
+
51
+typedef struct ht_cslot {
52
+	ht_element_t *first;
53
+	ht_element_t *last;
54
+	int cnt;
55
+} ht_cslot_t;
56
+
57
+typedef struct hash_table {
58
+	hash_func_t hash;
59
+	key_cmp_func_t cmp;
60
+	ht_cslot_t *cslots;
61
+	int size;
62
+
63
+	int find_cnt;
64
+	int cmp_cnt;
65
+	int nocmp_cnt;
66
+	int missed_cnt;
67
+} hash_table_t;
68
+
69
+int ht_init(hash_table_t *ht, hash_func_t hash_func, key_cmp_func_t cmp_keys, int size);
70
+void ht_destroy(hash_table_t *ht);
71
+int ht_add(hash_table_t *ht, ht_key_t key, ht_data_t data);
72
+ht_data_t ht_remove(hash_table_t *ht, ht_key_t key);
73
+ht_data_t ht_find(hash_table_t *ht, ht_key_t key);
74
+void ht_get_statistic(hash_table_t *ht, ht_statistic_t *s);
75
+void ht_clear_statistic(hash_table_t *ht);
76
+
77
+#endif
0 78
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#ifndef __LIST_H
27
+#define __LIST_H
28
+
29
+#define DOUBLE_LINKED_LIST_ADD(first,last,e)	do { \
30
+		if (last) last->next = e; \
31
+		else first = e; \
32
+		e->next = NULL; \
33
+		e->prev = last; \
34
+		last = e; \
35
+	} while (0)
36
+
37
+#define DOUBLE_LINKED_LIST_REMOVE(first,last,e)	do { \
38
+		if (e->next) e->next->prev = e->prev; \
39
+		else last = e->prev; \
40
+		if (e->prev) e->prev->next = e->next; \
41
+		else first = e->next; \
42
+		e->next = NULL; \
43
+		e->prev = NULL; \
44
+	} while (0)
45
+
46
+#define LINKED_LIST_ADD(first,last,e)	do { \
47
+		if (last) last->next = e; \
48
+		else first = e; \
49
+		e->next = NULL; \
50
+		last = e; \
51
+	} while (0)
52
+
53
+#endif
0 54
new file mode 100644
... ...
@@ -0,0 +1,52 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#ifndef __LOGGER_H
27
+#define __LOGGER_H
28
+
29
+#ifndef SER
30
+
31
+#include <stdio.h>
32
+
33
+#define ERROR_LOG(a,args...)		do{printf(a,##args);}while(0)
34
+#define DEBUG_LOG(a,args...)		do{printf(a,##args);}while(0)
35
+#define TRACE_LOG(a,args...)		do{printf(a,##args);}while(0)
36
+#define WARN_LOG(a,args...)			do{printf(a,##args);}while(0)
37
+#define FLUSH_LOG()					do{fflush(stdout);}while(0)
38
+
39
+#else
40
+/* TODO: logging for SER */
41
+
42
+#include "dprint.h"
43
+
44
+#define ERROR_LOG(a,args...)		LOG(L_ERR,a,##args)
45
+#define DEBUG_LOG(a,args...)		LOG(L_ERR,a,##args)
46
+#define TRACE_LOG(a,args...)		LOG(L_ERR,a,##args)
47
+#define WARN_LOG(a,args...)			LOG(L_WARN,a,##args)
48
+#define FLUSH_LOG()					do{}while(0)
49
+
50
+#endif
51
+
52
+#endif
0 53
new file mode 100644
... ...
@@ -0,0 +1,165 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+/* Common Data Structures - functions for memory allocation and deallocation
27
+ * and may be other memory management. */
28
+
29
+#include <stdio.h>
30
+#include <stdlib.h>
31
+#include <cds/memory.h>
32
+
33
+#ifdef SER
34
+
35
+#include <mem/mem.h>
36
+#include <mem/shm_mem.h>
37
+
38
+static void* shm_malloc_x(unsigned int size);
39
+static void shm_free_x(void *ptr);
40
+
41
+cds_malloc_func cds_malloc = shm_malloc_x;
42
+cds_free_func cds_free = shm_free_x;
43
+
44
+#else
45
+
46
+cds_malloc_func cds_malloc = malloc;
47
+cds_free_func cds_free = free;
48
+
49
+#endif
50
+
51
+void cds_set_memory_functions(cds_malloc_func _malloc, cds_free_func _free)
52
+{
53
+	cds_malloc = _malloc;
54
+	cds_free = _free;
55
+}
56
+
57
+#ifdef SER
58
+
59
+static void* shm_malloc_x(unsigned int size)
60
+{
61
+	return shm_malloc(size);
62
+}
63
+
64
+static void shm_free_x(void *ptr)
65
+{
66
+	shm_free(ptr);
67
+}
68
+
69
+#endif
70
+
71
+/*
72
+#ifdef SER
73
+
74
+static gen_lock_t *mem_mutex = NULL;
75
+int *allocated_cnt = NULL;
76
+
77
+#else
78
+
79
+static int allocated_cnt = 0;
80
+
81
+#endif
82
+
83
+void debug_mem_init()
84
+{
85
+#ifdef SER
86
+	mem_mutex = lock_alloc();
87
+	allocated_cnt = shm_malloc(sizeof(int));
88
+	*allocated_cnt = 0;
89
+	debug_print_allocated_mem();
90
+#else
91
+	allocated_cnt = 0;
92
+#endif
93
+}
94
+
95
+void *debug_malloc(int size)
96
+{
97
+#ifdef SER
98
+	void *m = NULL;
99
+	lock_get(mem_mutex);
100
+	if (allocated_cnt) (*allocated_cnt)++;
101
+	lock_release(mem_mutex);
102
+	m = shm_malloc(size);
103
+	LOG(L_INFO, "debug_malloc(): %p\n", m);
104
+	return m;
105
+#else
106
+	allocated_cnt++;
107
+	return malloc(size);
108
+#endif
109
+}
110
+
111
+void debug_free(void *block)
112
+{
113
+#ifdef SER
114
+	LOG(L_INFO, "debug_free(): %p\n", block);
115
+	shm_free(block);
116
+	lock_get(mem_mutex);
117
+	if (allocated_cnt) (*allocated_cnt)--;
118
+	lock_release(mem_mutex);
119
+#else
120
+	free(block);
121
+	allocated_cnt--;
122
+#endif
123
+}
124
+
125
+void *debug_malloc_ex(int size, const char *file, int line)
126
+{
127
+#ifdef SER
128
+	void *m = NULL;
129
+	lock_get(mem_mutex);
130
+	if (allocated_cnt) (*allocated_cnt)++;
131
+	lock_release(mem_mutex);
132
+	m = shm_malloc(size);
133
+	LOG(L_INFO, "ALLOC: %s:%d -> %p\n", file, line, m);
134
+	return m;
135
+#else
136
+	allocated_cnt++;
137
+	return malloc(size);
138
+#endif
139
+}
140
+
141
+void debug_free_ex(void *block, const char *file, int line)
142
+{
143
+#ifdef SER
144
+	LOG(L_INFO, "FREE: %s:%d -> %p\n", file, line, block);
145
+	shm_free(block);
146
+	lock_get(mem_mutex);
147
+	if (allocated_cnt) (*allocated_cnt)--;
148
+	lock_release(mem_mutex);
149
+#else
150
+	free(block);
151
+	allocated_cnt--;
152
+#endif
153
+}
154
+
155
+void debug_print_allocated_mem()
156
+{
157
+#ifdef SER
158
+	lock_get(mem_mutex);
159
+	LOG(L_INFO, "There are allocated: %d memory blocks\n", *allocated_cnt);
160
+	lock_release(mem_mutex);
161
+#else
162
+	printf("There are allocated: %d memory blocks\n", allocated_cnt);
163
+#endif
164
+}
165
+*/
0 166
new file mode 100644
... ...
@@ -0,0 +1,47 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#ifndef __CDS_MEMORY_H
27
+#define __CDS_MEMORY_H
28
+
29
+#ifdef __cplusplus
30
+extern "C" {
31
+#endif
32
+
33
+typedef void*(*cds_malloc_func)(unsigned int size);
34
+typedef void(*cds_free_func)(void *ptr);
35
+
36
+extern cds_malloc_func cds_malloc;
37
+extern cds_free_func cds_free;
38
+
39
+void cds_set_memory_functions(cds_malloc_func _malloc, cds_free_func _free);
40
+
41
+#ifdef __cplusplus
42
+}
43
+#endif
44
+
45
+
46
+#endif
47
+
0 48
new file mode 100644
... ...
@@ -0,0 +1,151 @@
1
+/* 
2
+ * Copyright (C) 2005 iptelorg GmbH
3
+ *
4
+ * This file is part of ser, a free SIP server.
5
+ *
6
+ * ser is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ * For a license to use the ser software under conditions
12
+ * other than those described here, or to purchase support for this
13
+ * software, please contact iptel.org by e-mail at the following addresses:
14
+ *    info@iptel.org
15
+ *
16
+ * ser is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with this program; if not, write to the Free Software
23
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
+ */
25
+
26
+#include <stdio.h>
27
+#include <cds/msg_queue.h>
28
+#include <cds/memory.h>
29
+
30
+mq_message_t *create_message_ex(int data_len)
31
+{
32
+	mq_message_t *m;
33
+	if (data_len < 0) data_len = 0;
34
+	m = cds_malloc(data_len + sizeof(mq_message_t));
35
+	if (!m) return NULL;
36
+	m->data_len = data_len;
37
+	m->data = (((char *)m) + sizeof(mq_message_t));
38
+	m->next = NULL;
39
+	m->allocation_style = message_allocated_with_data;
40
+	return m;
41
+}
42
+
43
+void free_message(mq_message_t *msg)
44
+{
45
+	switch (msg->allocation_style) {
46
+		case message_allocated_with_data: 
47
+				break;
48
+		case message_holding_data_ptr: 
49
+				if (msg->data) cds_free(msg->data);
50
+				break;
51
+	}
52
+	cds_free(msg);
53
+}
54
+
55
+int push_message(msg_queue_t *q, mq_message_t *m)
56
+{
57
+	if ((!q) || (!m)) return -1;
58
+	m->next = NULL;
59
+	
60
+	if (q->use_mutex) cds_mutex_lock(&q->q_mutex);
61
+	if (q->last) q->last->next = m;
62
+	else {
63
+		q->first = m;
64