Improve struct layouts for memory usage.

This commit is contained in:
Roger A. Light 2014-11-18 07:42:49 +00:00
parent ea8537c048
commit d30d711c3b
6 changed files with 61 additions and 56 deletions

View File

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

View File

@ -455,39 +455,39 @@ int mqtt3_db_message_store(struct mosquitto_db *db, const char *source, uint16_t
return MOSQ_ERR_NOMEM;
}
temp->source_mid = source_mid;
temp->msg.mid = 0;
temp->msg.qos = qos;
temp->msg.retain = retain;
temp->mid = 0;
temp->qos = qos;
temp->retain = retain;
if(topic){
temp->msg.topic = _mosquitto_strdup(topic);
if(!temp->msg.topic){
temp->topic = _mosquitto_strdup(topic);
if(!temp->topic){
_mosquitto_free(temp->source_id);
_mosquitto_free(temp);
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM;
}
}else{
temp->msg.topic = NULL;
temp->topic = NULL;
}
temp->msg.payloadlen = payloadlen;
temp->payloadlen = payloadlen;
if(payloadlen){
temp->msg.payload = _mosquitto_malloc(sizeof(char)*payloadlen);
if(!temp->msg.payload){
temp->payload = _mosquitto_malloc(sizeof(char)*payloadlen);
if(!temp->payload){
if(temp->source_id) _mosquitto_free(temp->source_id);
if(temp->msg.topic) _mosquitto_free(temp->msg.topic);
if(temp->msg.payload) _mosquitto_free(temp->msg.payload);
if(temp->topic) _mosquitto_free(temp->topic);
if(temp->payload) _mosquitto_free(temp->payload);
_mosquitto_free(temp);
return MOSQ_ERR_NOMEM;
}
memcpy(temp->msg.payload, payload, sizeof(char)*payloadlen);
memcpy(temp->payload, payload, sizeof(char)*payloadlen);
}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->msg.topic) _mosquitto_free(temp->msg.topic);
if(temp->msg.payload) _mosquitto_free(temp->msg.payload);
if(temp->topic) _mosquitto_free(temp->topic);
if(temp->payload) _mosquitto_free(temp->payload);
_mosquitto_free(temp);
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){
qos = tail->store->msg.qos;
topic = tail->store->msg.topic;
qos = tail->store->qos;
topic = tail->store->topic;
retain = tail->retain;
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;
retries = tail->dup;
retain = tail->retain;
topic = tail->store->msg.topic;
topic = tail->store->topic;
qos = tail->qos;
payloadlen = tail->store->msg.payloadlen;
payload = tail->store->msg.payload;
payloadlen = tail->store->payloadlen;
payload = tail->store->payload;
switch(tail->state){
case mosq_ms_publish_qos0:

View File

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

View File

@ -138,7 +138,7 @@ static int mqtt3_db_message_store_write(struct mosquitto_db *db, FILE *db_fptr)
assert(db_fptr);
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
* misleading information when reloaded. They should still be saved
* 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) +
sizeof(uint16_t) + sizeof(uint16_t) +
2+strlen(stored->msg.topic) + sizeof(uint32_t) +
stored->msg.payloadlen + sizeof(uint8_t) + sizeof(uint8_t));
2+strlen(stored->topic) + sizeof(uint32_t) +
stored->payloadlen + sizeof(uint8_t) + sizeof(uint8_t));
i16temp = htons(DB_CHUNK_MSG_STORE);
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);
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));
slen = strlen(stored->msg.topic);
slen = strlen(stored->topic);
i16temp = htons(slen);
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));
if(force_no_retain == false){
i8temp = (uint8_t )stored->msg.retain;
i8temp = (uint8_t )stored->retain;
}else{
i8temp = 0;
}
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));
if(stored->msg.payloadlen){
write_e(db_fptr, stored->msg.payload, (unsigned int)stored->msg.payloadlen);
if(stored->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;
}
if(node->retained){
if(strncmp(node->retained->msg.topic, "$SYS", 4)){
if(strncmp(node->retained->topic, "$SYS", 4)){
/* Don't save $SYS messages. */
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;
HASH_FIND(hh, db->msg_store, &store_id, sizeof(dbid_t), 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{
_mosquitto_log_printf(NULL, MOSQ_LOG_ERR, "Error: Corrupt database whilst restoring a retained message.");
return MOSQ_ERR_INVAL;

View File

@ -525,7 +525,7 @@ int mqtt3_handle_connect(struct mosquitto_db *db, struct mosquitto *context)
msg_prev = NULL;
while(msg_tail){
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--;
if(msg_prev){
msg_prev->next = msg_tail->next;

View File

@ -89,7 +89,7 @@ static int _subs_process(struct mosquitto_db *db, struct _mosquitto_subhier *hie
db->retained_count--;
#endif
}
if(stored->msg.payloadlen){
if(stored->payloadlen){
hier->retained = stored;
hier->retained->ref_count++;
#ifdef WITH_SYS_TREE
@ -618,14 +618,14 @@ static int _retain_process(struct mosquitto_db *db, struct mosquitto_msg_store *
int qos;
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){
return MOSQ_ERR_SUCCESS;
}else if(rc != MOSQ_ERR_SUCCESS){
return rc;
}
qos = retained->msg.qos;
qos = retained->qos;
if(qos > sub_qos) qos = sub_qos;
if(qos > 0){