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