Browse code

Shared memory versions of mk_proxy and free_proxy.

This patch adds function mk_shm_proxy, which is the shared memory
version of function mk_proxy. In addition to that it adds function
free_shm_proxy which is the shared memory version of free_proxy.

To avoid code duplication, we turned the body of the original
hostent_cpy function into macro called HOSTENT_CPY, the macro takes
malloc and free functions as parameters. The macro is then called in
the body of hostent_cpy with pkg_malloc and pkg_free as parameters and
in the body of hostent_shm_cpy with shm_malloc and shm_free as
parameters.

We did the same to mk_proxy function. The original body of the function
was converted into macro MK_PROXY which, again, takes memory allocation
functions as we all the hostent copy function as parameters.

Jan Janak authored on 31/03/2009 00:47:44
Showing 2 changed files
... ...
@@ -41,6 +41,7 @@
41 41
 #include "error.h"
42 42
 #include "dprint.h"
43 43
 #include "mem/mem.h"
44
+#include "mem/shm_mem.h"
44 45
 
45 46
 #include <string.h>
46 47
 #include <stdlib.h>
... ...
@@ -75,107 +76,136 @@ static struct proxy_l* find_proxy(str *name, unsigned short port, int proto)
75 76
 }
76 77
 
77 78
 
79
+#define HOSTENT_CPY(dst, src, he_malloc, he_free)						\
80
+	do {																\
81
+		unsigned len,len2;												\
82
+		int r,i;														\
83
+																		\
84
+		/* start copying the host entry.. */							\
85
+		/* copy h_name */												\
86
+		len=strlen(src->h_name)+1;										\
87
+		dst->h_name=(char*)he_malloc(sizeof(char) * len);				\
88
+		if (dst->h_name) strncpy(dst->h_name,src->h_name, len);			\
89
+		else{															\
90
+			ser_error=ret=E_OUT_OF_MEM;									\
91
+			goto error;													\
92
+		}																\
93
+																		\
94
+		/* copy h_aliases */											\
95
+		len=0;															\
96
+		if (src->h_aliases)												\
97
+			for (;src->h_aliases[len];len++);							\
98
+		dst->h_aliases=(char**)he_malloc(sizeof(char*)*(len+1));		\
99
+		if (dst->h_aliases==0){											\
100
+			ser_error=ret=E_OUT_OF_MEM;									\
101
+			he_free(dst->h_name);										\
102
+			goto error;													\
103
+		}																\
104
+		memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );		\
105
+		for (i=0;i<len;i++){											\
106
+			len2=strlen(src->h_aliases[i])+1;							\
107
+			dst->h_aliases[i]=(char*)he_malloc(sizeof(char)*len2);		\
108
+			if (dst->h_aliases==0){										\
109
+				ser_error=ret=E_OUT_OF_MEM;								\
110
+				he_free(dst->h_name);									\
111
+				for(r=0; r<i; r++)	he_free(dst->h_aliases[r]);			\
112
+				he_free(dst->h_aliases);								\
113
+				goto error;												\
114
+			}															\
115
+			strncpy(dst->h_aliases[i], src->h_aliases[i], len2);		\
116
+		}																\
117
+		/* copy h_addr_list */											\
118
+		len=0;															\
119
+		if (src->h_addr_list)											\
120
+			for (;src->h_addr_list[len];len++);							\
121
+		dst->h_addr_list=(char**)he_malloc(sizeof(char*)*(len+1));		\
122
+		if (dst->h_addr_list==0){										\
123
+			ser_error=ret=E_OUT_OF_MEM;									\
124
+			he_free(dst->h_name);										\
125
+			for(r=0; dst->h_aliases[r]; r++)							\
126
+				he_free(dst->h_aliases[r]);								\
127
+			he_free(dst->h_aliases[r]);									\
128
+			he_free(dst->h_aliases);									\
129
+			goto error;													\
130
+		}																\
131
+		memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );	\
132
+		for (i=0;i<len;i++){											\
133
+			dst->h_addr_list[i]=										\
134
+				(char*)he_malloc(sizeof(char)*src->h_length);			\
135
+			if (dst->h_addr_list[i]==0){								\
136
+				ser_error=ret=E_OUT_OF_MEM;								\
137
+				he_free(dst->h_name);									\
138
+				for(r=0; dst->h_aliases[r]; r++)						\
139
+					he_free(dst->h_aliases[r]);							\
140
+				he_free(dst->h_aliases[r]);								\
141
+				he_free(dst->h_aliases);								\
142
+				for (r=0; r<i;r++) he_free(dst->h_addr_list[r]);		\
143
+				he_free(dst->h_addr_list);								\
144
+				goto error;												\
145
+			}															\
146
+			memcpy(dst->h_addr_list[i], src->h_addr_list[i],			\
147
+				   src->h_length);										\
148
+		}																\
149
+																		\
150
+		/* copy h_addr_type & length */									\
151
+		dst->h_addrtype=src->h_addrtype;								\
152
+		dst->h_length=src->h_length;									\
153
+		/*finished hostent copy */										\
154
+																		\
155
+		return 0;														\
156
+	} while(0)
157
+
158
+
159
+#define FREE_HOSTENT(dst, he_free)						\
160
+	do {												\
161
+		int r;											\
162
+		if (dst->h_name) he_free(dst->h_name);			\
163
+		if (dst->h_aliases){							\
164
+			for(r=0; dst->h_aliases[r]; r++) {			\
165
+				he_free(dst->h_aliases[r]);				\
166
+			}											\
167
+			he_free(dst->h_aliases);					\
168
+		}												\
169
+		if (dst->h_addr_list){							\
170
+			for (r=0; dst->h_addr_list[r];r++) {		\
171
+				he_free(dst->h_addr_list[r]);			\
172
+			}											\
173
+			he_free(dst->h_addr_list);					\
174
+		}												\
175
+	} while(0)
176
+
177
+
78 178
 
79 179
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
80 180
 static int hostent_cpy(struct hostent *dst, struct hostent* src)
81 181
 {
82
-	unsigned len,len2;
83
-	int r,ret,i;
84
-
85
-	/* start copying the host entry.. */
86
-	/* copy h_name */
87
-	len=strlen(src->h_name)+1;
88
-	dst->h_name=(char*)pkg_malloc(sizeof(char) * len);
89
-	if (dst->h_name) strncpy(dst->h_name,src->h_name, len);
90
-	else{
91
-		ser_error=ret=E_OUT_OF_MEM;
92
-		goto error;
93
-	}
94
-
95
-	/* copy h_aliases */
96
-	len=0;
97
-	if (src->h_aliases)
98
-		for (;src->h_aliases[len];len++);
99
-	dst->h_aliases=(char**)pkg_malloc(sizeof(char*)*(len+1));
100
-	if (dst->h_aliases==0){
101
-		ser_error=ret=E_OUT_OF_MEM;
102
-		pkg_free(dst->h_name);
103
-		goto error;
104
-	}
105
-	memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );
106
-	for (i=0;i<len;i++){
107
-		len2=strlen(src->h_aliases[i])+1;
108
-		dst->h_aliases[i]=(char*)pkg_malloc(sizeof(char)*len2);
109
-		if (dst->h_aliases==0){
110
-			ser_error=ret=E_OUT_OF_MEM;
111
-			pkg_free(dst->h_name);
112
-			for(r=0; r<i; r++)	pkg_free(dst->h_aliases[r]);
113
-			pkg_free(dst->h_aliases);
114
-			goto error;
115
-		}
116
-		strncpy(dst->h_aliases[i], src->h_aliases[i], len2);
117
-	}
118
-	/* copy h_addr_list */
119
-	len=0;
120
-	if (src->h_addr_list)
121
-		for (;src->h_addr_list[len];len++);
122
-	dst->h_addr_list=(char**)pkg_malloc(sizeof(char*)*(len+1));
123
-	if (dst->h_addr_list==0){
124
-		ser_error=ret=E_OUT_OF_MEM;
125
-		pkg_free(dst->h_name);
126
-		for(r=0; dst->h_aliases[r]; r++)	pkg_free(dst->h_aliases[r]);
127
-		pkg_free(dst->h_aliases[r]);
128
-		pkg_free(dst->h_aliases);
129
-		goto error;
130
-	}
131
-	memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );
132
-	for (i=0;i<len;i++){
133
-		dst->h_addr_list[i]=(char*)pkg_malloc(sizeof(char)*src->h_length);
134
-		if (dst->h_addr_list[i]==0){
135
-			ser_error=ret=E_OUT_OF_MEM;
136
-			pkg_free(dst->h_name);
137
-			for(r=0; dst->h_aliases[r]; r++)	pkg_free(dst->h_aliases[r]);
138
-			pkg_free(dst->h_aliases[r]);
139
-			pkg_free(dst->h_aliases);
140
-			for (r=0; r<i;r++) pkg_free(dst->h_addr_list[r]);
141
-			pkg_free(dst->h_addr_list);
142
-			goto error;
143
-		}
144
-		memcpy(dst->h_addr_list[i], src->h_addr_list[i], src->h_length);
145
-	}
146
-
147
-	/* copy h_addr_type & length */
148
-	dst->h_addrtype=src->h_addrtype;
149
-	dst->h_length=src->h_length;
150
-	/*finished hostent copy */
151
-	
152
-	return 0;
153
-
182
+	int ret;
183
+	HOSTENT_CPY(dst, src, pkg_malloc, pkg_free);
154 184
 error:
155 185
 	LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
156 186
 	return ret;
157 187
 }
158 188
 
159 189
 
190
+static int hostent_shm_cpy(struct hostent *dst, struct hostent* src)
191
+{
192
+	int ret;
193
+	HOSTENT_CPY(dst, src, shm_malloc, shm_free);
194
+error:
195
+	LOG(L_CRIT, "ERROR: hostent_shm_cpy: memory allocation failure\n");
196
+	return ret;
197
+}
198
+
160 199
 
161
-void free_hostent(struct hostent *dst)
200
+void free_hostent(struct hostent* dst)
162 201
 {
163
-	int r;
164
-	if (dst->h_name) pkg_free(dst->h_name);
165
-	if (dst->h_aliases){
166
-		for(r=0; dst->h_aliases[r]; r++) {
167
-			pkg_free(dst->h_aliases[r]);
168
-		}
169
-		pkg_free(dst->h_aliases);
170
-	}
171
-	if (dst->h_addr_list){
172
-		for (r=0; dst->h_addr_list[r];r++) { 
173
-			pkg_free(dst->h_addr_list[r]);
174
-		}
175
-		pkg_free(dst->h_addr_list);
176
-	}
202
+	FREE_HOSTENT(dst, pkg_free);
177 203
 }
178 204
 
205
+void free_shm_hostent(struct hostent* dst)
206
+{
207
+	FREE_HOSTENT(dst, shm_free);
208
+}
179 209
 
180 210
 
181 211
 
... ...
@@ -195,46 +225,56 @@ error:
195 225
 }
196 226
 
197 227
 
198
-
228
+#define MK_PROXY(name, port, protocol, p_malloc, p_free, he_cpy)		\
229
+	do {																\
230
+		struct proxy_l* p;												\
231
+		struct hostent* he;												\
232
+		char proto;														\
233
+																		\
234
+		p=(struct proxy_l*) p_malloc(sizeof(struct proxy_l));			\
235
+		if (p==0){														\
236
+			ser_error=E_OUT_OF_MEM;										\
237
+			ERR("ERROR: mk_proxy: memory allocation failure\n");		\
238
+			goto error;													\
239
+		}																\
240
+		memset(p,0,sizeof(struct proxy_l));								\
241
+		p->name=*name;													\
242
+		p->port=port;													\
243
+																		\
244
+		DBG("DEBUG: mk_proxy: doing DNS lookup...\n");					\
245
+		proto=protocol;													\
246
+		he=sip_resolvehost(name, &(p->port), &proto);					\
247
+		if (he==0){														\
248
+			ser_error=E_BAD_ADDRESS;									\
249
+			LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"	\
250
+				" \"%.*s\"\n", name->len, name->s);						\
251
+			p_free(p);													\
252
+			goto error;													\
253
+		}																\
254
+		if (he_cpy(&(p->host), he)!=0){									\
255
+			p_free(p);													\
256
+			goto error;													\
257
+		}																\
258
+		p->proto=proto;													\
259
+		p->ok=1;														\
260
+		return p;														\
261
+	error:																\
262
+		return 0;														\
263
+																		\
264
+	} while(0)
199 265
 
200 266
 /* same as add_proxy, but it doesn't add the proxy to the list
201 267
  * uses also SRV if possible & port==0 (quick hack) */
202 268
 
203 269
 struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
204 270
 {
205
-	struct proxy_l* p;
206
-	struct hostent* he;
207
-	char proto;
271
+	MK_PROXY(name, port, protocol, pkg_malloc, pkg_free, hostent_cpy);
272
+}
208 273
 
209
-	p=(struct proxy_l*) pkg_malloc(sizeof(struct proxy_l));
210
-	if (p==0){
211
-		ser_error=E_OUT_OF_MEM;
212
-		LOG(L_CRIT, "ERROR: mk_proxy: memory allocation failure\n");
213
-		goto error;
214
-	}
215
-	memset(p,0,sizeof(struct proxy_l));
216
-	p->name=*name;
217
-	p->port=port;
218 274
 
219
-	DBG("DEBUG: mk_proxy: doing DNS lookup...\n");
220
-	proto=protocol;
221
-	he=sip_resolvehost(name, &(p->port), &proto);
222
-	if (he==0){
223
-		ser_error=E_BAD_ADDRESS;
224
-		LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"
225
-					" \"%.*s\"\n", name->len, name->s);
226
-		pkg_free(p);
227
-		goto error;
228
-	}
229
-	if (hostent_cpy(&(p->host), he)!=0){
230
-		pkg_free(p);
231
-		goto error;
232
-	}
233
-	p->proto=proto;
234
-	p->ok=1;
235
-	return p;
236
-error:
237
-	return 0;
275
+struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int protocol)
276
+{
277
+	MK_PROXY(name, port, protocol, shm_malloc, shm_free, hostent_shm_cpy);
238 278
 }
239 279
 
240 280
 
... ...
@@ -281,3 +321,9 @@ void free_proxy(struct proxy_l* p)
281 321
 {
282 322
 	if (p) free_hostent(&p->host);
283 323
 }
324
+
325
+
326
+void free_shm_proxy(struct proxy_l* p)
327
+{
328
+	if (p) free_shm_hostent(&p->host);
329
+}
... ...
@@ -62,9 +62,11 @@ extern struct proxy_l* proxies;
62 62
 
63 63
 struct proxy_l* add_proxy(str* name, unsigned short port, int proto);
64 64
 struct proxy_l* mk_proxy(str* name, unsigned short port, int proto);
65
+struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int proto);
65 66
 struct proxy_l* mk_proxy_from_ip(struct ip_addr* ip, unsigned short port,
66 67
 									int proto);
67 68
 void free_proxy(struct proxy_l* p);
69
+void free_shm_proxy(struct proxy_l* p);
68 70
 
69 71
 
70 72