Skip to content

Commit

Permalink
Consistent ref counting inc and dec functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed May 22, 2019
1 parent bd34d8c commit f974b91
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
15 changes: 10 additions & 5 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static void subhier_clean(struct mosquitto_db *db, struct mosquitto__subhier **s
leaf = nextleaf;
}
if(peer->retained){
db__msg_store_deref(db, &peer->retained);
db__msg_store_ref_dec(db, &peer->retained);
}
subhier_clean(db, &peer->children);
mosquitto__free(peer->topic);
Expand Down Expand Up @@ -232,7 +232,12 @@ void db__msg_store_clean(struct mosquitto_db *db)
}
}

void db__msg_store_deref(struct mosquitto_db *db, struct mosquitto_msg_store **store)
void db__msg_store_ref_inc(struct mosquitto_msg_store *store)
{
store->ref_count++;
}

void db__msg_store_ref_dec(struct mosquitto_db *db, struct mosquitto_msg_store **store)
{
(*store)->ref_count--;
if((*store)->ref_count == 0){
Expand Down Expand Up @@ -271,7 +276,7 @@ static void db__message_remove(struct mosquitto_db *db, struct mosquitto_msg_dat
msg_data->msg_count12--;
msg_data->msg_bytes12 -= item->store->payloadlen;
}
db__msg_store_deref(db, &item->store);
db__msg_store_ref_dec(db, &item->store);
}

mosquitto_property_free_all(&item->properties);
Expand Down Expand Up @@ -450,7 +455,7 @@ int db__message_insert(struct mosquitto_db *db, struct mosquitto *context, uint1
msg->prev = NULL;
msg->next = NULL;
msg->store = stored;
msg->store->ref_count++;
db__msg_store_ref_inc(msg->store);
msg->mid = mid;
msg->timestamp = mosquitto_time();
msg->direction = dir;
Expand Down Expand Up @@ -543,7 +548,7 @@ void db__messages_delete_list(struct mosquitto_db *db, struct mosquitto_client_m

DL_FOREACH_SAFE(*head, tail, tmp){
DL_DELETE(*head, tail);
db__msg_store_deref(db, &tail->store);
db__msg_store_ref_dec(db, &tail->store);
mosquitto_property_free_all(&tail->properties);
mosquitto__free(tail);
}
Expand Down
2 changes: 1 addition & 1 deletion src/handle_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void connection_check_acl(struct mosquitto_db *db, struct mosquitto *context, st
msg_tail->store->qos, msg_tail->store->retain, MOSQ_ACL_READ) != MOSQ_ERR_SUCCESS){

DL_DELETE((*head), msg_tail);
db__msg_store_deref(db, &msg_tail->store);
db__msg_store_ref_dec(db, &msg_tail->store);
mosquitto_property_free_all(&msg_tail->properties);
mosquitto__free(msg_tail);
}
Expand Down
3 changes: 2 additions & 1 deletion src/mosquitto_broker_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,8 @@ int db__message_store(struct mosquitto_db *db, const struct mosquitto *source, u
int db__message_store_find(struct mosquitto *context, uint16_t mid, struct mosquitto_msg_store **stored);
void db__msg_store_add(struct mosquitto_db *db, struct mosquitto_msg_store *store);
void db__msg_store_remove(struct mosquitto_db *db, struct mosquitto_msg_store *store);
void db__msg_store_deref(struct mosquitto_db *db, struct mosquitto_msg_store **store);
void db__msg_store_ref_inc(struct mosquitto_msg_store *store);
void db__msg_store_ref_dec(struct mosquitto_db *db, struct mosquitto_msg_store **store);
void db__msg_store_clean(struct mosquitto_db *db);
void db__msg_store_compact(struct mosquitto_db *db);
int db__message_reconnect_reset(struct mosquitto_db *db, struct mosquitto *context);
Expand Down
2 changes: 1 addition & 1 deletion src/persist_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static int persist__client_msg_restore(struct mosquitto_db *db, struct P_client_
return 1;
}
cmsg->store = load->store;
cmsg->store->ref_count++;
db__msg_store_ref_inc(cmsg->store);

context = persist__find_or_add_context(db, chunk->client_id, 0);
if(!context){
Expand Down
10 changes: 5 additions & 5 deletions src/subs.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ static int subs__process(struct mosquitto_db *db, struct mosquitto__subhier *hie
}
#endif
if(hier->retained){
db__msg_store_deref(db, &hier->retained);
db__msg_store_ref_dec(db, &hier->retained);
#ifdef WITH_SYS_TREE
db->retained_count--;
#endif
}
if(stored->payloadlen){
hier->retained = stored;
hier->retained->ref_count++;
db__msg_store_ref_inc(hier->retained);
#ifdef WITH_SYS_TREE
db->retained_count++;
#endif
Expand Down Expand Up @@ -803,7 +803,7 @@ int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const ch
clients - this is required because websockets client calls
db__message_write(), which could remove the message if ref_count==0.
*/
(*stored)->ref_count++;
db__msg_store_ref_inc(*stored);

HASH_FIND(hh, db->subs, tokens->topic, tokens->topic_len, subhier);
if(subhier){
Expand All @@ -818,7 +818,7 @@ int sub__messages_queue(struct mosquitto_db *db, const char *source_id, const ch
sub__topic_tokens_free(tokens);

/* Remove our reference and free if needed. */
db__msg_store_deref(db, stored);
db__msg_store_ref_dec(db, stored);

return rc;
}
Expand Down Expand Up @@ -981,7 +981,7 @@ static int retain__process(struct mosquitto_db *db, struct mosquitto__subhier *b
struct mosquitto_msg_store *retained;

if(branch->retained->message_expiry_time > 0 && now > branch->retained->message_expiry_time){
db__msg_store_deref(db, &branch->retained);
db__msg_store_ref_dec(db, &branch->retained);
branch->retained = NULL;
#ifdef WITH_SYS_TREE
db->retained_count--;
Expand Down

0 comments on commit f974b91

Please sign in to comment.