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