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 75
 }
76 76
 
77 77
 
78
+#define HOSTENT_CPY(dst, src, he_malloc, he_free)						\
79
+	do {																\
80
+		unsigned len,len2;												\
81
+		int r,i;														\
82
+																		\
83
+		/* start copying the host entry.. */							\
84
+		/* copy h_name */												\
85
+		len=strlen(src->h_name)+1;										\
86
+		dst->h_name=(char*)he_malloc(sizeof(char) * len);				\
87
+		if (dst->h_name) strncpy(dst->h_name,src->h_name, len);			\
88
+		else{															\
89
+			ser_error=ret=E_OUT_OF_MEM;									\
90
+			goto error;													\
91
+		}																\
92
+																		\
93
+		/* copy h_aliases */											\
94
+		len=0;															\
95
+		if (src->h_aliases)												\
96
+			for (;src->h_aliases[len];len++);							\
97
+		dst->h_aliases=(char**)he_malloc(sizeof(char*)*(len+1));		\
98
+		if (dst->h_aliases==0){											\
99
+			ser_error=ret=E_OUT_OF_MEM;									\
100
+			he_free(dst->h_name);										\
101
+			goto error;													\
102
+		}																\
103
+		memset((void*)dst->h_aliases, 0, sizeof(char*) * (len+1) );		\
104
+		for (i=0;i<len;i++){											\
105
+			len2=strlen(src->h_aliases[i])+1;							\
106
+			dst->h_aliases[i]=(char*)he_malloc(sizeof(char)*len2);		\
107
+			if (dst->h_aliases==0){										\
108
+				ser_error=ret=E_OUT_OF_MEM;								\
109
+				he_free(dst->h_name);									\
110
+				for(r=0; r<i; r++)	he_free(dst->h_aliases[r]);			\
111
+				he_free(dst->h_aliases);								\
112
+				goto error;												\
113
+			}															\
114
+			strncpy(dst->h_aliases[i], src->h_aliases[i], len2);		\
115
+		}																\
116
+		/* copy h_addr_list */											\
117
+		len=0;															\
118
+		if (src->h_addr_list)											\
119
+			for (;src->h_addr_list[len];len++);							\
120
+		dst->h_addr_list=(char**)he_malloc(sizeof(char*)*(len+1));		\
121
+		if (dst->h_addr_list==0){										\
122
+			ser_error=ret=E_OUT_OF_MEM;									\
123
+			he_free(dst->h_name);										\
124
+			for(r=0; dst->h_aliases[r]; r++)							\
125
+				he_free(dst->h_aliases[r]);								\
126
+			he_free(dst->h_aliases[r]);									\
127
+			he_free(dst->h_aliases);									\
128
+			goto error;													\
129
+		}																\
130
+		memset((void*)dst->h_addr_list, 0, sizeof(char*) * (len+1) );	\
131
+		for (i=0;i<len;i++){											\
132
+			dst->h_addr_list[i]=										\
133
+				(char*)he_malloc(sizeof(char)*src->h_length);			\
134
+			if (dst->h_addr_list[i]==0){								\
135
+				ser_error=ret=E_OUT_OF_MEM;								\
136
+				he_free(dst->h_name);									\
137
+				for(r=0; dst->h_aliases[r]; r++)						\
138
+					he_free(dst->h_aliases[r]);							\
139
+				he_free(dst->h_aliases[r]);								\
140
+				he_free(dst->h_aliases);								\
141
+				for (r=0; r<i;r++) he_free(dst->h_addr_list[r]);		\
142
+				he_free(dst->h_addr_list);								\
143
+				goto error;												\
144
+			}															\
145
+			memcpy(dst->h_addr_list[i], src->h_addr_list[i],			\
146
+				   src->h_length);										\
147
+		}																\
148
+																		\
149
+		/* copy h_addr_type & length */									\
150
+		dst->h_addrtype=src->h_addrtype;								\
151
+		dst->h_length=src->h_length;									\
152
+		/*finished hostent copy */										\
153
+																		\
154
+		return 0;														\
155
+	} while(0)
156
+
157
+
158
+#define FREE_HOSTENT(dst, he_free)						\
159
+	do {												\
160
+		int r;											\
161
+		if (dst->h_name) he_free(dst->h_name);			\
162
+		if (dst->h_aliases){							\
163
+			for(r=0; dst->h_aliases[r]; r++) {			\
164
+				he_free(dst->h_aliases[r]);				\
165
+			}											\
166
+			he_free(dst->h_aliases);					\
167
+		}												\
168
+		if (dst->h_addr_list){							\
169
+			for (r=0; dst->h_addr_list[r];r++) {		\
170
+				he_free(dst->h_addr_list[r]);			\
171
+			}											\
172
+			he_free(dst->h_addr_list);					\
173
+		}												\
174
+	} while(0)
175
+
176
+
78 177
 
79 178
 /* copies a hostent structure*, returns 0 on success, <0 on error*/
80 179
 static int hostent_cpy(struct hostent *dst, struct hostent* src)
81 180
 {
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
-
181
+	int ret;
182
+	HOSTENT_CPY(dst, src, pkg_malloc, pkg_free);
154 183
 error:
155 184
 	LOG(L_CRIT, "ERROR: hostent_cpy: memory allocation failure\n");
156 185
 	return ret;
157 186
 }
158 187
 
159 188
 
189
+static int hostent_shm_cpy(struct hostent *dst, struct hostent* src)
190
+{
191
+	int ret;
192
+	HOSTENT_CPY(dst, src, shm_malloc, shm_free);
193
+error:
194
+	LOG(L_CRIT, "ERROR: hostent_shm_cpy: memory allocation failure\n");
195
+	return ret;
196
+}
197
+
160 198
 
161
-void free_hostent(struct hostent *dst)
199
+void free_hostent(struct hostent* dst)
162 200
 {
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
-	}
201
+	FREE_HOSTENT(dst, pkg_free);
177 202
 }
178 203
 
204
+void free_shm_hostent(struct hostent* dst)
205
+{
206
+	FREE_HOSTENT(dst, shm_free);
207
+}
179 208
 
180 209
 
181 210
 
... ...
@@ -195,46 +225,56 @@ error:
195 195
 }
196 196
 
197 197
 
198
-
198
+#define MK_PROXY(name, port, protocol, p_malloc, p_free, he_cpy)		\
199
+	do {																\
200
+		struct proxy_l* p;												\
201
+		struct hostent* he;												\
202
+		char proto;														\
203
+																		\
204
+		p=(struct proxy_l*) p_malloc(sizeof(struct proxy_l));			\
205
+		if (p==0){														\
206
+			ser_error=E_OUT_OF_MEM;										\
207
+			ERR("ERROR: mk_proxy: memory allocation failure\n");		\
208
+			goto error;													\
209
+		}																\
210
+		memset(p,0,sizeof(struct proxy_l));								\
211
+		p->name=*name;													\
212
+		p->port=port;													\
213
+																		\
214
+		DBG("DEBUG: mk_proxy: doing DNS lookup...\n");					\
215
+		proto=protocol;													\
216
+		he=sip_resolvehost(name, &(p->port), &proto);					\
217
+		if (he==0){														\
218
+			ser_error=E_BAD_ADDRESS;									\
219
+			LOG(L_CRIT, "ERROR: mk_proxy: could not resolve hostname:"	\
220
+				" \"%.*s\"\n", name->len, name->s);						\
221
+			p_free(p);													\
222
+			goto error;													\
223
+		}																\
224
+		if (he_cpy(&(p->host), he)!=0){									\
225
+			p_free(p);													\
226
+			goto error;													\
227
+		}																\
228
+		p->proto=proto;													\
229
+		p->ok=1;														\
230
+		return p;														\
231
+	error:																\
232
+		return 0;														\
233
+																		\
234
+	} while(0)
199 235
 
200 236
 /* same as add_proxy, but it doesn't add the proxy to the list
201 237
  * uses also SRV if possible & port==0 (quick hack) */
202 238
 
203 239
 struct proxy_l* mk_proxy(str* name, unsigned short port, int protocol)
204 240
 {
205
-	struct proxy_l* p;
206
-	struct hostent* he;
207
-	char proto;
241
+	MK_PROXY(name, port, protocol, pkg_malloc, pkg_free, hostent_cpy);
242
+}
208 243
 
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 244
 
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;
245
+struct proxy_l* mk_shm_proxy(str* name, unsigned short port, int protocol)
246
+{
247
+	MK_PROXY(name, port, protocol, shm_malloc, shm_free, hostent_shm_cpy);
238 248
 }
239 249
 
240 250
 
... ...
@@ -281,3 +321,9 @@ void free_proxy(struct proxy_l* p)
281 281
 {
282 282
 	if (p) free_hostent(&p->host);
283 283
 }
284
+
285
+
286
+void free_shm_proxy(struct proxy_l* p)
287
+{
288
+	if (p) free_shm_hostent(&p->host);
289
+}
... ...
@@ -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