Browse code

Added select_buf.[ch]

Michal Matyska authored on 21/06/2006 21:08:48
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,130 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2005-2006 iptelorg GmbH
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ *
27
+ * History:
28
+ * --------
29
+ *	2006-06-16  static buffer for select results (mma)
30
+ *	            each process owns a separate space
31
+ *	            each request starts using the buffer from the start
32
+ *
33
+ */
34
+
35
+#include <stdio.h>
36
+#include <stdlib.h>
37
+#include <string.h>
38
+#include <unistd.h>
39
+#include <ctype.h>
40
+
41
+#include "dprint.h"
42
+#include "mem/mem.h"
43
+
44
+/*
45
+ * Placeholder for the buffer
46
+ *
47
+ * two buffers are actually used to cover the different size requests
48
+ * assuming that resize can move the result to newly allocated space
49
+ * and comparing two selects from the script could require two static buffers
50
+ *
51
+ * if more static buffers need to be valid at the same time change
52
+ * the following constant
53
+ */
54
+
55
+#define MAX_BUFFERS 2
56
+#define BUFFER_GRANULARITY 256
57
+
58
+typedef struct stat_buffer_ {
59
+	char *b;
60
+	int size;
61
+	int offset;
62
+} stat_buffer_t;
63
+
64
+static stat_buffer_t buffer[MAX_BUFFERS];
65
+static int active_buffer=-1;
66
+
67
+#define ALLOC_SIZE(req_size) (((req_size/BUFFER_GRANULARITY)+1)*BUFFER_GRANULARITY)
68
+
69
+int allocate_buffer(int req_size) {
70
+	int size=ALLOC_SIZE(req_size);
71
+	
72
+	if (buffer[active_buffer].b == NULL) {
73
+		if ((buffer[active_buffer].b=pkg_malloc(size))==NULL)
74
+			return 0;
75
+		buffer[active_buffer].size=size;
76
+		buffer[active_buffer].offset=0;
77
+		return 1;
78
+	}
79
+	
80
+	active_buffer = (active_buffer?active_buffer:MAX_BUFFERS)-1;
81
+	if (buffer[active_buffer].size >= req_size) {
82
+		buffer[active_buffer].offset = 0;
83
+		return 1;
84
+	}
85
+	
86
+	if (pkg_realloc(buffer[active_buffer].b,size)) {
87
+		buffer[active_buffer].size=size;
88
+		buffer[active_buffer].offset=0;
89
+		return 1;
90
+	}
91
+	
92
+	return 0;
93
+}
94
+
95
+/*
96
+ * Request for space from buffer
97
+ *
98
+ * Returns:  NULL  memory allocation failure (no more space)
99
+ *           pointer to the space on success
100
+ */
101
+
102
+char* get_static_buffer(int req_size) {
103
+	char *p = NULL;
104
+	if ((buffer[active_buffer].size >= buffer[active_buffer].offset + req_size)
105
+			|| (allocate_buffer(req_size))) {
106
+		/* enough space in current buffer or allocation successful */
107
+		p = buffer[active_buffer].b+buffer[active_buffer].offset;
108
+		buffer[active_buffer].offset += req_size;	
109
+		return p;
110
+	}
111
+	return NULL;
112
+}
113
+
114
+/* Internal function - called before request is going to be processed
115
+ *
116
+ * Reset offset to unused space
117
+ */
118
+
119
+int reset_static_buffer() {
120
+	int i;
121
+
122
+	if (active_buffer == -1) {
123
+		memset(buffer, 0, sizeof(buffer));
124
+	} else {
125
+		for (i=0; i<MAX_BUFFERS; i++)
126
+			buffer[i].offset=0;
127
+	}
128
+	active_buffer=0;
129
+	return 0;
130
+}
0 131
new file mode 100644
... ...
@@ -0,0 +1,54 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2005-2006 iptelorg GmbH
5
+ *
6
+ * This file is part of ser, a free SIP server.
7
+ *
8
+ * ser is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * For a license to use the ser software under conditions
14
+ * other than those described here, or to purchase support for this
15
+ * software, please contact iptel.org by e-mail at the following addresses:
16
+ *    info@iptel.org
17
+ *
18
+ * ser is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program; if not, write to the Free Software
25
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
+ *
27
+ * History:
28
+ * --------
29
+ *	2006-06-16  static buffer for select results (mma)
30
+ *	            each process owns a separate space
31
+ *	            each request starts using the buffer from the start
32
+ *
33
+ */
34
+
35
+#ifndef SELECT_BUFFER_H
36
+#define SELECT_BUFFER_H
37
+
38
+/*
39
+ * Request for space from buffer
40
+ *
41
+ * Returns:  NULL  memory allocation failure (no more space)
42
+ *           pointer to the space on success
43
+ */
44
+
45
+char* get_static_buffer(int req_size);
46
+
47
+/* Internal function - called before request is going to be processed
48
+ *
49
+ * Reset offset to unused space
50
+ */
51
+
52
+int reset_static_buffer();
53
+
54
+#endif /* SELECT_BUFFER_H */