Improve struct layouts for memory usage.

pull/211/merge
Roger A. Light 11 years ago
parent ea8537c048
commit d30d711c3b

@ -111,17 +111,17 @@ enum _mosquitto_transport {
}; };
struct _mosquitto_packet{ struct _mosquitto_packet{
uint8_t command; uint8_t *payload;
uint8_t have_remaining; struct _mosquitto_packet *next;
uint8_t remaining_count;
uint16_t mid;
uint32_t remaining_mult; uint32_t remaining_mult;
uint32_t remaining_length; uint32_t remaining_length;
uint32_t packet_length; uint32_t packet_length;
uint32_t to_process; uint32_t to_process;
uint32_t pos; uint32_t pos;
uint8_t *payload; uint16_t mid;
struct _mosquitto_packet *next; uint8_t command;
uint8_t have_remaining;
uint8_t remaining_count;
}; };
struct mosquitto_message_all{ struct mosquitto_message_all{
@ -169,11 +169,11 @@ struct mosquitto {
char *tls_certfile; char *tls_certfile;
char *tls_keyfile; char *tls_keyfile;
int (*tls_pw_callback)(char *buf, int size, int rwflag, void *userdata); int (*tls_pw_callback)(char *buf, int size, int rwflag, void *userdata);
int tls_cert_reqs;
char *tls_version; char *tls_version;
char *tls_ciphers; char *tls_ciphers;
char *tls_psk; char *tls_psk;
char *tls_psk_identity; char *tls_psk_identity;
int tls_cert_reqs;
bool tls_insecure; bool tls_insecure;
#endif #endif
bool want_write; bool want_write;
@ -189,6 +189,7 @@ struct mosquitto {
pthread_t thread_id; pthread_t thread_id;
#endif #endif
#ifdef WITH_BROKER #ifdef WITH_BROKER
bool is_dropping;
bool is_bridge; bool is_bridge;
struct _mqtt3_bridge *bridge; struct _mqtt3_bridge *bridge;
struct mosquitto_client_msg *msgs; struct mosquitto_client_msg *msgs;
@ -198,11 +199,10 @@ struct mosquitto {
struct _mosquitto_acl_user *acl_list; struct _mosquitto_acl_user *acl_list;
struct _mqtt3_listener *listener; struct _mqtt3_listener *listener;
time_t disconnect_t; time_t disconnect_t;
int pollfd_index;
struct _mosquitto_packet *out_packet_last; struct _mosquitto_packet *out_packet_last;
bool is_dropping;
struct _mosquitto_subhier **subs; struct _mosquitto_subhier **subs;
int sub_count; int sub_count;
int pollfd_index;
# ifdef WITH_WEBSOCKETS # ifdef WITH_WEBSOCKETS
struct libwebsocket_context *ws_context; struct libwebsocket_context *ws_context;
struct libwebsocket *wsi; struct libwebsocket *wsi;

@ -455,39 +455,39 @@ int mqtt3_db_message_store(struct mosquitto_db *db, const char *source, uint16_t
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
} }
temp->source_mid = source_mid; temp->source_mid = source_mid;
temp->msg.mid = 0; temp->mid = 0;
temp->msg.qos = qos; temp->qos = qos;
temp->msg.retain = retain; temp->retain = retain;
if(topic){ if(topic){
temp->msg.topic = _mosquitto_strdup(topic); temp->topic = _mosquitto_strdup(topic);
if(!temp->msg.topic){ if(!temp->topic){
_mosquitto_free(temp->source_id); _mosquitto_free(temp->source_id);
_mosquitto_free(temp); _mosquitto_free(temp);
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
} }
}else{ }else{
temp->msg.topic = NULL; temp->topic = NULL;
} }
temp->msg.payloadlen = payloadlen; temp->payloadlen = payloadlen;
if(payloadlen){ if(payloadlen){
temp->msg.payload = _mosquitto_malloc(sizeof(char)*payloadlen); temp->payload = _mosquitto_malloc(sizeof(char)*payloadlen);
if(!temp->msg.payload){ if(!temp->payload){
if(temp->source_id) _mosquitto_free(temp->source_id); if(temp->source_id) _mosquitto_free(temp->source_id);
if(temp->msg.topic) _mosquitto_free(temp->msg.topic); if(temp->topic) _mosquitto_free(temp->topic);
if(temp->msg.payload) _mosquitto_free(temp->msg.payload); if(temp->payload) _mosquitto_free(temp->payload);
_mosquitto_free(temp); _mosquitto_free(temp);
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
} }
memcpy(temp->msg.payload, payload, sizeof(char)*payloadlen); memcpy(temp->payload, payload, sizeof(char)*payloadlen);
}else{ }else{
temp->msg.payload = NULL; temp->payload = NULL;
} }
if(!temp->source_id || (payloadlen && !temp->msg.payload)){ if(!temp->source_id || (payloadlen && !temp->payload)){
if(temp->source_id) _mosquitto_free(temp->source_id); if(temp->source_id) _mosquitto_free(temp->source_id);
if(temp->msg.topic) _mosquitto_free(temp->msg.topic); if(temp->topic) _mosquitto_free(temp->topic);
if(temp->msg.payload) _mosquitto_free(temp->msg.payload); if(temp->payload) _mosquitto_free(temp->payload);
_mosquitto_free(temp); _mosquitto_free(temp);
return 1; return 1;
} }
@ -687,8 +687,8 @@ int mqtt3_db_message_release(struct mosquitto_db *db, struct mosquitto *context,
} }
} }
if(tail->mid == mid && tail->direction == dir){ if(tail->mid == mid && tail->direction == dir){
qos = tail->store->msg.qos; qos = tail->store->qos;
topic = tail->store->msg.topic; topic = tail->store->topic;
retain = tail->retain; retain = tail->retain;
source_id = tail->store->source_id; source_id = tail->store->source_id;
@ -748,10 +748,10 @@ int mqtt3_db_message_write(struct mosquitto_db *db, struct mosquitto *context)
mid = tail->mid; mid = tail->mid;
retries = tail->dup; retries = tail->dup;
retain = tail->retain; retain = tail->retain;
topic = tail->store->msg.topic; topic = tail->store->topic;
qos = tail->qos; qos = tail->qos;
payloadlen = tail->store->msg.payloadlen; payloadlen = tail->store->payloadlen;
payload = tail->store->msg.payload; payload = tail->store->payload;
switch(tail->state){ switch(tail->state){
case mosq_ms_publish_qos0: case mosq_ms_publish_qos0:

@ -144,21 +144,26 @@ struct _mosquitto_subhier {
struct mosquitto_msg_store{ struct mosquitto_msg_store{
UT_hash_handle hh; UT_hash_handle hh;
dbid_t db_id; dbid_t db_id;
int ref_count;
char *source_id; char *source_id;
char **dest_ids; char **dest_ids;
int dest_id_count; int dest_id_count;
int ref_count;
char *topic;
void *payload;
uint32_t payloadlen;
uint16_t source_mid; uint16_t source_mid;
struct mosquitto_message msg; uint16_t mid;
uint8_t qos;
bool retain;
}; };
struct mosquitto_client_msg{ struct mosquitto_client_msg{
struct mosquitto_client_msg *next; struct mosquitto_client_msg *next;
struct mosquitto_msg_store *store; struct mosquitto_msg_store *store;
time_t timestamp;
uint16_t mid; uint16_t mid;
int qos; uint8_t qos;
bool retain; bool retain;
time_t timestamp;
enum mosquitto_msg_direction direction; enum mosquitto_msg_direction direction;
enum mosquitto_msg_state state; enum mosquitto_msg_state state;
bool dup; bool dup;
@ -169,8 +174,8 @@ struct _mosquitto_unpwd{
char *password; char *password;
#ifdef WITH_TLS #ifdef WITH_TLS
unsigned int password_len; unsigned int password_len;
unsigned char *salt;
unsigned int salt_len; unsigned int salt_len;
unsigned char *salt;
#endif #endif
UT_hash_handle hh; UT_hash_handle hh;
}; };
@ -261,8 +266,10 @@ struct _mqtt3_bridge{
int address_count; int address_count;
time_t primary_retry; time_t primary_retry;
bool round_robin; bool round_robin;
int keepalive; bool try_private;
bool try_private_accepted;
bool clean_session; bool clean_session;
int keepalive;
struct _mqtt3_bridge_topic *topics; struct _mqtt3_bridge_topic *topics;
int topic_count; int topic_count;
bool topic_remapping; bool topic_remapping;
@ -280,8 +287,6 @@ struct _mqtt3_bridge{
int restart_timeout; int restart_timeout;
int threshold; int threshold;
bool lazy_reconnect; bool lazy_reconnect;
bool try_private;
bool try_private_accepted;
#ifdef WITH_TLS #ifdef WITH_TLS
char *tls_cafile; char *tls_cafile;
char *tls_capath; char *tls_capath;

@ -138,7 +138,7 @@ static int mqtt3_db_message_store_write(struct mosquitto_db *db, FILE *db_fptr)
assert(db_fptr); assert(db_fptr);
HASH_ITER(hh, db->msg_store, stored, stored_tmp){ HASH_ITER(hh, db->msg_store, stored, stored_tmp){
if(!strncmp(stored->msg.topic, "$SYS", 4)){ if(!strncmp(stored->topic, "$SYS", 4)){
/* Don't save $SYS messages as retained otherwise they can give /* Don't save $SYS messages as retained otherwise they can give
* misleading information when reloaded. They should still be saved * misleading information when reloaded. They should still be saved
* because a disconnected durable client may have them in their * because a disconnected durable client may have them in their
@ -149,8 +149,8 @@ static int mqtt3_db_message_store_write(struct mosquitto_db *db, FILE *db_fptr)
} }
length = htonl(sizeof(dbid_t) + 2+strlen(stored->source_id) + length = htonl(sizeof(dbid_t) + 2+strlen(stored->source_id) +
sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint16_t) +
2+strlen(stored->msg.topic) + sizeof(uint32_t) + 2+strlen(stored->topic) + sizeof(uint32_t) +
stored->msg.payloadlen + sizeof(uint8_t) + sizeof(uint8_t)); stored->payloadlen + sizeof(uint8_t) + sizeof(uint8_t));
i16temp = htons(DB_CHUNK_MSG_STORE); i16temp = htons(DB_CHUNK_MSG_STORE);
write_e(db_fptr, &i16temp, sizeof(uint16_t)); write_e(db_fptr, &i16temp, sizeof(uint16_t));
@ -169,28 +169,28 @@ static int mqtt3_db_message_store_write(struct mosquitto_db *db, FILE *db_fptr)
i16temp = htons(stored->source_mid); i16temp = htons(stored->source_mid);
write_e(db_fptr, &i16temp, sizeof(uint16_t)); write_e(db_fptr, &i16temp, sizeof(uint16_t));
i16temp = htons(stored->msg.mid); i16temp = htons(stored->mid);
write_e(db_fptr, &i16temp, sizeof(uint16_t)); write_e(db_fptr, &i16temp, sizeof(uint16_t));
slen = strlen(stored->msg.topic); slen = strlen(stored->topic);
i16temp = htons(slen); i16temp = htons(slen);
write_e(db_fptr, &i16temp, sizeof(uint16_t)); write_e(db_fptr, &i16temp, sizeof(uint16_t));
write_e(db_fptr, stored->msg.topic, slen); write_e(db_fptr, stored->topic, slen);
i8temp = (uint8_t )stored->msg.qos; i8temp = (uint8_t )stored->qos;
write_e(db_fptr, &i8temp, sizeof(uint8_t)); write_e(db_fptr, &i8temp, sizeof(uint8_t));
if(force_no_retain == false){ if(force_no_retain == false){
i8temp = (uint8_t )stored->msg.retain; i8temp = (uint8_t )stored->retain;
}else{ }else{
i8temp = 0; i8temp = 0;
} }
write_e(db_fptr, &i8temp, sizeof(uint8_t)); write_e(db_fptr, &i8temp, sizeof(uint8_t));
i32temp = htonl(stored->msg.payloadlen); i32temp = htonl(stored->payloadlen);
write_e(db_fptr, &i32temp, sizeof(uint32_t)); write_e(db_fptr, &i32temp, sizeof(uint32_t));
if(stored->msg.payloadlen){ if(stored->payloadlen){
write_e(db_fptr, stored->msg.payload, (unsigned int)stored->msg.payloadlen); write_e(db_fptr, stored->payload, (unsigned int)stored->payloadlen);
} }
} }
@ -284,7 +284,7 @@ static int _db_subs_retain_write(struct mosquitto_db *db, FILE *db_fptr, struct
sub = sub->next; sub = sub->next;
} }
if(node->retained){ if(node->retained){
if(strncmp(node->retained->msg.topic, "$SYS", 4)){ if(strncmp(node->retained->topic, "$SYS", 4)){
/* Don't save $SYS messages. */ /* Don't save $SYS messages. */
length = htonl(sizeof(dbid_t)); length = htonl(sizeof(dbid_t));
@ -645,7 +645,7 @@ static int _db_retain_chunk_restore(struct mosquitto_db *db, FILE *db_fptr)
store_id = i64temp; store_id = i64temp;
HASH_FIND(hh, db->msg_store, &store_id, sizeof(dbid_t), store); HASH_FIND(hh, db->msg_store, &store_id, sizeof(dbid_t), store);
if(store){ if(store){
mqtt3_db_messages_queue(db, NULL, store->msg.topic, store->msg.qos, store->msg.retain, store); mqtt3_db_messages_queue(db, NULL, store->topic, store->qos, store->retain, store);
}else{ }else{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Corrupt database whilst restoring a retained message."); _mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Corrupt database whilst restoring a retained message.");
return MOSQ_ERR_INVAL; return MOSQ_ERR_INVAL;

@ -525,7 +525,7 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context)
msg_prev = NULL; msg_prev = NULL;
while(msg_tail){ while(msg_tail){
if(msg_tail->direction == mosq_md_out){ if(msg_tail->direction == mosq_md_out){
if(mosquitto_acl_check(db, context, msg_tail->store->msg.topic, MOSQ_ACL_READ) == MOSQ_ERR_ACL_DENIED){ if(mosquitto_acl_check(db, context, msg_tail->store->topic, MOSQ_ACL_READ) == MOSQ_ERR_ACL_DENIED){
msg_tail->store->ref_count--; msg_tail->store->ref_count--;
if(msg_prev){ if(msg_prev){
msg_prev->next = msg_tail->next; msg_prev->next = msg_tail->next;

@ -89,7 +89,7 @@ static int _subs_process(struct mosquitto_db *db, struct _mosquitto_subhier *hie
db->retained_count--; db->retained_count--;
#endif #endif
} }
if(stored->msg.payloadlen){ if(stored->payloadlen){
hier->retained = stored; hier->retained = stored;
hier->retained->ref_count++; hier->retained->ref_count++;
#ifdef WITH_SYS_TREE #ifdef WITH_SYS_TREE
@ -618,14 +618,14 @@ static int _retain_process(struct mosquitto_db *db, struct mosquitto_msg_store *
int qos; int qos;
uint16_t mid; uint16_t mid;
rc = mosquitto_acl_check(db, context, retained->msg.topic, MOSQ_ACL_READ); rc = mosquitto_acl_check(db, context, retained->topic, MOSQ_ACL_READ);
if(rc == MOSQ_ERR_ACL_DENIED){ if(rc == MOSQ_ERR_ACL_DENIED){
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
}else if(rc != MOSQ_ERR_SUCCESS){ }else if(rc != MOSQ_ERR_SUCCESS){
return rc; return rc;
} }
qos = retained->msg.qos; qos = retained->qos;
if(qos > sub_qos) qos = sub_qos; if(qos > sub_qos) qos = sub_qos;
if(qos > 0){ if(qos > 0){

Loading…
Cancel
Save