From dcd469c177c82cda5c36a437cedd3d874f433692 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 22 Sep 2014 23:35:09 +0100 Subject: [PATCH] Use own linked list for storing disused contexts for freeing. Seemed to be a problem with getting it to work under a hash and there isn't really any need for a hash. --- lib/mosquitto_internal.h | 2 +- src/context.c | 28 +++++++++++++++++++++++----- src/loop.c | 7 ++----- src/mosquitto.c | 5 +---- src/mosquitto_broker.h | 3 +++ src/read_handle_server.c | 6 +++--- 6 files changed, 33 insertions(+), 18 deletions(-) diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 495035ea..531c9a81 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -235,7 +235,7 @@ struct mosquitto { #ifdef WITH_BROKER UT_hash_handle hh_id; UT_hash_handle hh_sock; - UT_hash_handle hh_for_free; + struct mosquitto *for_free_next; # ifdef WITH_BRIDGE UT_hash_handle hh_bridge; # endif diff --git a/src/context.c b/src/context.c index 037d0f97..8f02f19e 100644 --- a/src/context.c +++ b/src/context.c @@ -142,11 +142,6 @@ void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, b context->address = NULL; } - HASH_FIND(hh_for_free, db->contexts_for_free, context, sizeof(void *), ctx_tmp); - if(ctx_tmp){ - HASH_DELETE(hh_for_free, db->contexts_for_free, context); - } - if(context->id){ assert(db); /* db can only be NULL here if the client hasn't sent a CONNECT and hence wouldn't have an id. */ @@ -202,3 +197,26 @@ void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *ctxt) _mosquitto_socket_close(db, ctxt); } +void mosquitto__add_context_to_disused(struct mosquitto_db *db, struct mosquitto *context) +{ + if(db->ll_for_free){ + context->for_free_next = db->ll_for_free; + db->ll_for_free = context; + }else{ + db->ll_for_free = context; + } +} + +void mosquitto__free_disused_contexts(struct mosquitto_db *db) +{ + assert(db); + struct mosquitto *context, *next; + + context = db->ll_for_free; + while(context){ + next = context->for_free_next; + mqtt3_context_cleanup(db, context, true); + context = next; + } +} + diff --git a/src/loop.c b/src/loop.c index 42d20894..a4083213 100644 --- a/src/loop.c +++ b/src/loop.c @@ -93,10 +93,7 @@ int mosquitto_main_loop(struct mosquitto_db *db, int *listensock, int listensock } while(run){ - HASH_ITER(hh_for_free, db->contexts_for_free, context, ctxt_tmp){ - HASH_DELETE(hh_for_free, db->contexts_for_free, context); - mqtt3_context_cleanup(db, context, true); - } + mosquitto__free_disused_contexts(db); #ifdef WITH_SYS_TREE if(db->config->sys_interval > 0){ mqtt3_db_sys_update(db, db->config->sys_interval, start_time); @@ -367,7 +364,7 @@ void do_disconnect(struct mosquitto_db *db, struct mosquitto *context) #else if(context->clean_session){ #endif - HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); + mosquitto__add_context_to_disused(db, context); if(context->id){ HASH_DELETE(hh_id, db->contexts_by_id, context); _mosquitto_free(context->id); diff --git a/src/mosquitto.c b/src/mosquitto.c index 5019fe30..f2d81fc2 100644 --- a/src/mosquitto.c +++ b/src/mosquitto.c @@ -381,10 +381,7 @@ int main(int argc, char *argv[]) mqtt3_context_cleanup(&int_db, ctxt, true); } #endif - HASH_ITER(hh_for_free, int_db.contexts_for_free, ctxt, ctxt_tmp){ - HASH_DELETE(hh_for_free, int_db.contexts_for_free, ctxt); - mqtt3_context_cleanup(&int_db, ctxt, true); - } + mosquitto__free_disused_contexts(&int_db); mqtt3_db_close(&int_db); diff --git a/src/mosquitto_broker.h b/src/mosquitto_broker.h index 1c4c40a6..2c94bfce 100644 --- a/src/mosquitto_broker.h +++ b/src/mosquitto_broker.h @@ -223,6 +223,7 @@ struct mosquitto_db{ int subscription_count; int retained_count; #endif + struct mosquitto *ll_for_free; }; enum mqtt3_bridge_direction{ @@ -400,6 +401,8 @@ int mqtt3_subs_clean_session(struct mosquitto_db *db, struct mosquitto *context, struct mosquitto *mqtt3_context_init(struct mosquitto_db *db, int sock); void mqtt3_context_cleanup(struct mosquitto_db *db, struct mosquitto *context, bool do_free); void mqtt3_context_disconnect(struct mosquitto_db *db, struct mosquitto *context); +void mosquitto__add_context_to_disused(struct mosquitto_db *db, struct mosquitto *context); +void mosquitto__free_disused_contexts(struct mosquitto_db *db); /* ============================================================ * Logging functions diff --git a/src/read_handle_server.c b/src/read_handle_server.c index f5889adc..58744462 100644 --- a/src/read_handle_server.c +++ b/src/read_handle_server.c @@ -500,16 +500,16 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context) ws_ctxt_user_head = ws_ctxt_user_head->next; } ws_ctxt_user_head->next = ws_ctxt_user; - HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); + mosquitto__add_context_to_disused(db, context); }else{ - HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); + mosquitto__add_context_to_disused(db, context); HASH_DELETE(hh_sock, db->contexts_by_sock, context); context->sock = INVALID_SOCKET; HASH_ADD(hh_sock, db->contexts_by_sock, sock, sizeof(found_context->sock), found_context); } #else - HASH_ADD_KEYPTR(hh_for_free, db->contexts_for_free, context, sizeof(void *), context); + mosquitto__add_context_to_disused(db, context); HASH_DELETE(hh_sock, db->contexts_by_sock, context); context->sock = INVALID_SOCKET;