@@ -230,52 +230,58 @@ static int add_msghdr(conn *c)
230
230
static conn * * freeconns ;
231
231
static int freetotal ;
232
232
static int freecurr ;
233
+ /* Lock for connection freelist */
234
+ static pthread_mutex_t conn_lock = PTHREAD_MUTEX_INITIALIZER ;
233
235
234
236
235
237
static void conn_init (void ) {
236
238
freetotal = 200 ;
237
239
freecurr = 0 ;
238
- if ((freeconns = ( conn * * ) malloc ( sizeof (conn * ) * freetotal )) == NULL ) {
239
- fprintf (stderr , "malloc() \n" );
240
+ if ((freeconns = calloc ( freetotal , sizeof (conn * ))) == NULL ) {
241
+ fprintf (stderr , "Failed to allocate connection structures \n" );
240
242
}
241
243
return ;
242
244
}
243
245
244
246
/*
245
- * Returns a connection from the freelist, if any. Should call this using
246
- * conn_from_freelist() for thread safety.
247
+ * Returns a connection from the freelist, if any.
247
248
*/
248
- conn * do_conn_from_freelist () {
249
+ conn * conn_from_freelist () {
249
250
conn * c ;
250
251
252
+ pthread_mutex_lock (& conn_lock );
251
253
if (freecurr > 0 ) {
252
254
c = freeconns [-- freecurr ];
253
255
} else {
254
256
c = NULL ;
255
257
}
258
+ pthread_mutex_unlock (& conn_lock );
256
259
257
260
return c ;
258
261
}
259
262
260
263
/*
261
- * Adds a connection to the freelist. 0 = success. Should call this using
262
- * conn_add_to_freelist() for thread safety.
264
+ * Adds a connection to the freelist. 0 = success.
263
265
*/
264
- bool do_conn_add_to_freelist (conn * c ) {
266
+ bool conn_add_to_freelist (conn * c ) {
267
+ bool ret = true;
268
+ pthread_mutex_lock (& conn_lock );
265
269
if (freecurr < freetotal ) {
266
270
freeconns [freecurr ++ ] = c ;
267
- return false;
271
+ ret = false;
268
272
} else {
269
273
/* try to enlarge free connections array */
270
- conn * * new_freeconns = realloc (freeconns , sizeof (conn * ) * freetotal * 2 );
274
+ size_t newsize = freetotal * 2 ;
275
+ conn * * new_freeconns = realloc (freeconns , sizeof (conn * ) * newsize );
271
276
if (new_freeconns ) {
272
- freetotal *= 2 ;
277
+ freetotal = newsize ;
273
278
freeconns = new_freeconns ;
274
279
freeconns [freecurr ++ ] = c ;
275
- return false;
280
+ ret = false;
276
281
}
277
282
}
278
- return true;
283
+ pthread_mutex_unlock (& conn_lock );
284
+ return ret ;
279
285
}
280
286
281
287
static const char * prot_text (enum protocol prot ) {
@@ -598,44 +604,50 @@ static void conn_set_state(conn *c, enum conn_states state) {
598
604
static char * * freesuffix ;
599
605
static int freesuffixtotal ;
600
606
static int freesuffixcurr ;
607
+ /* Lock for alternative item suffix freelist */
608
+ static pthread_mutex_t suffix_lock = PTHREAD_MUTEX_INITIALIZER ;
601
609
602
610
static void suffix_init (void ) {
603
611
freesuffixtotal = 500 ;
604
612
freesuffixcurr = 0 ;
605
613
606
- freesuffix = ( char * * ) malloc ( sizeof (char * ) * freesuffixtotal );
614
+ freesuffix = calloc ( freesuffixtotal , sizeof (char * ));
607
615
if (freesuffix == NULL ) {
608
- fprintf (stderr , "malloc() \n" );
616
+ fprintf (stderr , "Failed to allocate suffix pool \n" );
609
617
}
610
618
return ;
611
619
}
612
620
613
621
/*
614
- * Returns a suffix buffer from the freelist, if any. Should call this using
615
- * suffix_from_freelist() for thread safety.
622
+ * Returns a suffix buffer from the freelist, if any.
616
623
*/
617
- char * do_suffix_from_freelist () {
624
+ char * suffix_from_freelist () {
618
625
char * s ;
619
626
627
+ pthread_mutex_lock (& suffix_lock );
620
628
if (freesuffixcurr > 0 ) {
621
629
s = freesuffix [-- freesuffixcurr ];
622
630
} else {
623
631
/* If malloc fails, let the logic fall through without spamming
624
632
* STDERR on the server. */
625
633
s = malloc ( SUFFIX_SIZE );
626
634
}
635
+ pthread_mutex_unlock (& suffix_lock );
627
636
628
637
return s ;
629
638
}
630
639
631
640
/*
632
641
* Adds a connection to the freelist. 0 = success. Should call this using
633
- * conn_add_to_freelist () for thread safety.
642
+ * suffix_add_to_freelist () for thread safety.
634
643
*/
635
- bool do_suffix_add_to_freelist (char * s ) {
644
+ bool suffix_add_to_freelist (char * s ) {
645
+ bool ret = true;
646
+
647
+ pthread_mutex_lock (& suffix_lock );
636
648
if (freesuffixcurr < freesuffixtotal ) {
637
649
freesuffix [freesuffixcurr ++ ] = s ;
638
- return false;
650
+ ret = false;
639
651
} else {
640
652
/* try to enlarge free connections array */
641
653
char * * new_freesuffix = realloc (freesuffix ,
@@ -644,10 +656,11 @@ bool do_suffix_add_to_freelist(char *s) {
644
656
freesuffixtotal *= 2 ;
645
657
freesuffix = new_freesuffix ;
646
658
freesuffix [freesuffixcurr ++ ] = s ;
647
- return false;
659
+ ret = false;
648
660
}
649
661
}
650
- return true;
662
+ pthread_mutex_unlock (& suffix_lock );
663
+ return ret ;
651
664
}
652
665
653
666
/*
0 commit comments