Browse code

- corrected memory leaks in route serialization - better str_dup for debugging

Vaclav Kubart authored on 01/09/2006 08:53:45
Showing 6 changed files
... ...
@@ -1,3 +1,8 @@
1
+2006-09-01
2
+	* corrected memory leaks in route serialization
3
+	* str_dup separated to macro and implementation function -> better for
4
+	debugging and memory leak search
5
+
1 6
 2006-08-08
2 7
 	* added functions for rr_t serialization/deserialization
3 8
 
... ...
@@ -40,9 +40,10 @@ int serialize_route(sstream_t *ss, rr_t **_r)
40 40
 			s.len = (*_r)->len;
41 41
 		}
42 42
 
43
-		res = serialize_str(ss, &s) | res;
43
+		res = serialize_str_ex(ss, &s) | res;
44 44
 		if (is_input_sstream(ss)) {
45 45
 			rr_t *pkg_rr = NULL;
46
+			
46 47
 			parse_rr_body(s.s, s.len, &pkg_rr);
47 48
 			rr_dup(_r, pkg_rr);
48 49
 		}
... ...
@@ -210,6 +210,22 @@ int serialize_str(sstream_t *ss, str_t *s)
210 210
 	return res;
211 211
 }
212 212
 
213
+int serialize_str_ex(sstream_t *ss, str_t *s)
214
+{
215
+	int res = 0;
216
+	
217
+	if (!s) return -1; 
218
+	
219
+	if (serialize_int(ss, &s->len) != 0) return -1;
220
+	if (is_input_sstream(ss)) {
221
+		if (s->len == 0) s->s = NULL;
222
+		else res = sstream_get_str_ex(ss, s->len, s); /* doesn't duplicate read string */
223
+	}
224
+	else res = sstream_put(ss, s->s, s->len);
225
+
226
+	return res;
227
+}
228
+
213 229
 int serialize_char(sstream_t *ss, char *c)
214 230
 {
215 231
 	if (!c) return -1;
... ...
@@ -66,6 +66,7 @@ int serialize_uint(sstream_t *ss, unsigned int *num);
66 66
 int serialize_char(sstream_t *ss, char *c);
67 67
 int serialize_uchar(sstream_t *ss, unsigned char *c);
68 68
 int serialize_str(sstream_t *ss, str_t *s);
69
+int serialize_str_ex(sstream_t *ss, str_t *s); /* doesn't duplicate read strings */
69 70
 
70 71
 #ifdef __cplusplus
71 72
 }
... ...
@@ -79,7 +79,28 @@ str_t zt2str(char *str)
79 79
 	return s;
80 80
 }
81 81
 
82
-int str_dup(str_t* dst, const str_t* src)
82
+int str_dup_dbg(str_t* dst, const str_t* src, const char *file, int line)
83
+{
84
+	if (!dst) return -1;
85
+
86
+	dst->len = 0;
87
+	dst->s = NULL;
88
+	if (!src) return 0;
89
+	if ( (!src->s) || (src->len < 1)) return 0;
90
+
91
+		/* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
92
+	INFO("str_dup called from %s:%d\n", file, line);
93
+	dst->s = cds_malloc(src->len);
94
+	if (!dst->s) {
95
+		/* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
96
+		return -1;
97
+	}
98
+	memcpy(dst->s, src->s, src->len);
99
+	dst->len = src->len;
100
+	return 0;
101
+}
102
+
103
+int str_dup_impl(str_t* dst, const str_t* src)
83 104
 {
84 105
 	if (!dst) return -1;
85 106
 
... ...
@@ -60,7 +60,11 @@ str_t zt2str(char *str);
60 60
 int is_str_empty(const str_t *s);
61 61
 
62 62
 /** duplicate string into given destination (data region is newly allocated) */
63
-int str_dup(str_t* dst, const str_t* src);
63
+int str_dup_impl(str_t* dst, const str_t* src);
64
+int str_dup_dbg(str_t* dst, const str_t* src, const char *file, int line);
65
+/*#define str_dup(dst,src)	str_dup_dbg(dst,src,__FILE__,__LINE__)*/
66
+#define str_dup(dst,src)	str_dup_impl(dst,src)
67
+
64 68
 
65 69
 /** duplicate string into newly allocated destination (data and str structure are newly allocated) */
66 70
 str_t *str_dup_new(const str_t* src);
... ...
@@ -141,7 +145,6 @@ int replace_str(const str_t *src, str_t *dst, const str_t *sample, const str_t *
141 145
         (str)->len += (app_len);                         \
142 146
     } while(0)
143 147
 
144
-
145 148
 #ifdef __cplusplus
146 149
 }
147 150
 #endif