Skip to content

Commit 799a880

Browse files
author
Felipe cruz
committed
Merge pull request #103 from hintjens/master
Fixed list/hash autofree strategy
2 parents 086ab05 + 8f2904f commit 799a880

File tree

10 files changed

+45
-29
lines changed

10 files changed

+45
-29
lines changed

src/czmq_selftest.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ int main (int argc, char *argv [])
3232
{
3333
bool verbose;
3434
if (argc == 2 && streq (argv [1], "-v"))
35-
verbose = TRUE;
35+
verbose = true;
3636
else
37-
verbose = FALSE;
37+
verbose = false;
3838

3939
printf ("Running czmq self tests...\n");
4040

src/zctx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
struct _zctx_t {
6262
void *context; // Our 0MQ context
6363
zlist_t *sockets; // Sockets held by this thread
64-
bool main; // TRUE if we're the main thread
64+
bool main; // True if we're the main thread
6565
int iothreads; // Number of IO threads, default 1
6666
int linger; // Linger timeout, default 0
6767
};
@@ -99,7 +99,7 @@ zctx_new (void)
9999
return NULL;
100100
}
101101
self->iothreads = 1;
102-
self->main = TRUE;
102+
self->main = true;
103103

104104
#if defined (__UNIX__)
105105
// Install signal handler for SIGINT and SIGTERM

src/zfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ zfile_test (bool verbose)
131131
assert (rc == -1);
132132

133133
rc = zfile_exists ("nosuchfile");
134-
assert (rc == FALSE);
134+
assert (rc != true);
135135

136136
rc = (int) zfile_size ("nosuchfile");
137137
assert (rc == -1);

src/zframe.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,17 @@ zframe_strdup (zframe_t *self)
272272

273273

274274
// --------------------------------------------------------------------------
275-
// Return TRUE if frame body is equal to string, excluding terminator
275+
// Return true if frame body is equal to string, excluding terminator
276276

277277
bool
278278
zframe_streq (zframe_t *self, const char *string)
279279
{
280280
assert (self);
281281
if (zframe_size (self) == strlen (string)
282282
&& memcmp (zframe_data (self), string, strlen (string)) == 0)
283-
return TRUE;
283+
return true;
284284
else
285-
return FALSE;
285+
return false;
286286
}
287287

288288

@@ -308,20 +308,20 @@ zframe_zero_copy (zframe_t *self)
308308

309309

310310
// --------------------------------------------------------------------------
311-
// Return TRUE if two frames have identical size and data
311+
// Return true if two frames have identical size and data
312312

313313
bool
314314
zframe_eq (zframe_t *self, zframe_t *other)
315315
{
316316
if (!self || !other)
317-
return FALSE;
317+
return false;
318318
else
319319
if (zframe_size (self) == zframe_size (other)
320320
&& memcmp (zframe_data (self), zframe_data (other),
321321
zframe_size (self)) == 0)
322-
return TRUE;
322+
return true;
323323
else
324-
return FALSE;
324+
return false;
325325
}
326326

327327

src/zhash.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ zhash_destroy (zhash_t **self_p)
194194
item_t *cur_item = self->items [index];
195195
while (cur_item) {
196196
item_t *next_item = cur_item->next;
197-
s_item_destroy (self, cur_item, TRUE);
197+
s_item_destroy (self, cur_item, true);
198198
cur_item = next_item;
199199
}
200200
}
@@ -246,6 +246,10 @@ zhash_insert (zhash_t *self, const char *key, void *value)
246246
self->items = new_items;
247247
self->limit = new_limit;
248248
}
249+
// If necessary, take duplicate of item (string) value
250+
if (self->autofree)
251+
value = strdup ((char *) value);
252+
249253
return s_item_insert (self, key, value)? 0: -1;
250254
}
251255

@@ -268,6 +272,10 @@ zhash_update (zhash_t *self, const char *key, void *value)
268272
else
269273
if (self->autofree)
270274
free (item->value);
275+
276+
// If necessary, take duplicate of item (string) value
277+
if (self->autofree)
278+
value = strdup ((char *) value);
271279
item->value = value;
272280
}
273281
else
@@ -287,7 +295,7 @@ zhash_delete (zhash_t *self, const char *key)
287295

288296
item_t *item = s_item_lookup (self, key);
289297
if (item)
290-
s_item_destroy (self, item, TRUE);
298+
s_item_destroy (self, item, true);
291299
}
292300

293301

@@ -317,7 +325,7 @@ zhash_rename (zhash_t *self, const char *old_key, const char *new_key)
317325
{
318326
item_t *item = s_item_lookup (self, old_key);
319327
if (item) {
320-
s_item_destroy (self, item, FALSE);
328+
s_item_destroy (self, item, false);
321329
item_t *new_item = s_item_lookup (self, new_key);
322330
if (new_item == NULL) {
323331
free (item->key);
@@ -389,7 +397,7 @@ zhash_dup (zhash_t *self)
389397
for (index = 0; index != self->limit; index++) {
390398
item_t *item = self->items [index];
391399
while (item) {
392-
zhash_insert (copy, item->key, strdup (item->value));
400+
zhash_insert (copy, item->key, item->value);
393401
item = item->next;
394402
}
395403
}
@@ -412,7 +420,7 @@ zhash_keys (zhash_t *self)
412420
for (index = 0; index != self->limit; index++) {
413421
item_t *item = self->items [index];
414422
while (item) {
415-
zlist_append (keys, strdup (item->key));
423+
zlist_append (keys, item->key);
416424
item = item->next;
417425
}
418426
}
@@ -498,7 +506,7 @@ zhash_load (zhash_t *self, char *filename)
498506
if (!equals)
499507
break; // Some error, stop parsing it
500508
*equals++ = 0;
501-
zhash_update (self, buffer, strdup (equals));
509+
zhash_update (self, buffer, equals);
502510
}
503511
fclose (handle);
504512
return 0;
@@ -617,12 +625,12 @@ zhash_test (int verbose)
617625
item = zhash_lookup (hash, testset [testnbr].name);
618626
assert (item);
619627
zhash_delete (hash, testset [testnbr].name);
620-
testset [testnbr].exists = FALSE;
628+
testset [testnbr].exists = false;
621629
}
622630
else {
623631
sprintf (testset [testnbr].name, "%x-%x", rand (), rand ());
624632
if (zhash_insert (hash, testset [testnbr].name, "") == 0)
625-
testset [testnbr].exists = TRUE;
633+
testset [testnbr].exists = true;
626634
}
627635
}
628636
// Test 10K lookups

src/zlist.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ zlist_append (zlist_t *self, void *item)
177177
if (!node)
178178
return -1;
179179

180+
// If necessary, take duplicate of (string) item
181+
if (self->autofree)
182+
item = strdup ((char *) item);
183+
180184
node->item = item;
181185
if (self->tail)
182186
self->tail->next = node;
@@ -202,6 +206,10 @@ zlist_push (zlist_t *self, void *item)
202206
if (!node)
203207
return -1;
204208

209+
// If necessary, take duplicate of (string) item
210+
if (self->autofree)
211+
item = strdup ((char *) item);
212+
205213
node->item = item;
206214
node->next = self->head;
207215
self->head = node;

src/zloop.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ s_rebuild_pollset (zloop_t *self)
127127
item_nbr++;
128128
poller = (s_poller_t *) zlist_next (self->pollers);
129129
}
130-
self->dirty = FALSE;
130+
self->dirty = false;
131131
return 0;
132132
}
133133

@@ -234,7 +234,7 @@ zloop_poller (zloop_t *self, zmq_pollitem_t *item, zloop_fn handler, void *arg)
234234
if (zlist_push (self->pollers, poller))
235235
return -1;
236236

237-
self->dirty = TRUE;
237+
self->dirty = true;
238238
if (self->verbose)
239239
zclock_log ("I: zloop: register %s poller (%p, %d)",
240240
item->socket? zsocket_type_str (item->socket): "FD",
@@ -263,7 +263,7 @@ zloop_poller_end (zloop_t *self, zmq_pollitem_t *item)
263263
|| (item->fd && item->fd == poller->item.fd)) {
264264
zlist_remove (self->pollers, poller);
265265
free (poller);
266-
self->dirty = TRUE;
266+
self->dirty = true;
267267
}
268268
poller = (s_poller_t *) zlist_next (self->pollers);
269269
}

src/zmsg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ zmsg_load (zmsg_t *self, FILE *file)
457457
if (!self)
458458
return NULL;
459459

460-
while (TRUE) {
460+
while (true) {
461461
size_t frame_size;
462462
size_t rc = fread (&frame_size, sizeof (frame_size), 1, file);
463463
if (rc == 1) {

src/zsocket.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ zsocket_disconnect (void *self, const char *format, ...)
139139
}
140140

141141
// --------------------------------------------------------------------------
142-
// Poll for input events on the socket. Returns TRUE if there is input
143-
// ready on the socket, else FALSE.
142+
// Poll for input events on the socket. Returns true if there is input
143+
// ready on the socket, else false.
144144

145145
bool
146146
zsocket_poll (void *self, int msecs)
@@ -206,7 +206,7 @@ zsocket_test (bool verbose)
206206
int port = zsocket_bind (writer, "tcp://%s:*", interf);
207207
assert (port >= ZSOCKET_DYNFROM && port <= ZSOCKET_DYNTO);
208208

209-
assert (zsocket_poll (writer, 100) == FALSE);
209+
assert (zsocket_poll (writer, 100) == false);
210210

211211
rc = zsocket_connect (reader, "txp://%s:%d", domain, service);
212212
assert (rc == -1);

src/zstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ zstr_sendf (void *zocket, const char *format, ...)
152152
va_list argptr;
153153
va_start (argptr, format);
154154

155-
int rc = s_zstr_sendf_impl (zocket, FALSE, format, argptr);
155+
int rc = s_zstr_sendf_impl (zocket, false, format, argptr);
156156
va_end (argptr);
157157

158158
return rc;
@@ -168,7 +168,7 @@ zstr_sendfm (void *zocket, const char *format, ...)
168168
va_list argptr;
169169
va_start (argptr, format);
170170

171-
int rc = s_zstr_sendf_impl (zocket, TRUE, format, argptr);
171+
int rc = s_zstr_sendf_impl (zocket, true, format, argptr);
172172
va_end (argptr);
173173

174174
return rc;

0 commit comments

Comments
 (0)