Added some memory debugging facilities to the adc_message structures.

This commit is contained in:
Jan Vidar Krey 2009-08-04 03:37:30 +02:00
parent faaa3d14e4
commit c17ae06b93

View File

@ -30,13 +30,68 @@
uhub_assert(X->references >= 0); uhub_assert(X->references >= 0);
#else #else
#define ADC_MSG_ASSERT(X) do { } while(0) #define ADC_MSG_ASSERT(X) do { } while(0)
#endif #endif /* DEBUG */
#ifdef MSG_MEMORY_DEBUG
#undef msg_malloc
#undef msg_malloc_zero
#undef msg_free
static void* msg_malloc(size_t size)
{
void* ptr = valloc(size);
LOG_MEMORY("msg_malloc: %p %d", ptr, (int) size);
return ptr;
}
static void* msg_malloc_zero(size_t size)
{
void* ptr = msg_malloc(size);
memset(ptr, 0, size);
return ptr;
}
static void msg_free(void* ptr)
{
LOG_MEMORY("msg_free: %p", ptr);
// hub_free(ptr);
}
#include <sys/mman.h>
static void adc_msg_protect(struct adc_message* cmd)
{
LOG_MEMORY("msg_prot: %p %d", cmd, cmd->capacity);
mprotect(cmd, sizeof(cmd), PROT_READ);
mprotect(cmd->cache, sizeof(cmd->capacity), PROT_READ);
}
static void adc_msg_unprotect(struct adc_message* cmd)
{
LOG_MEMORY("msg_unprot: %p %d", cmd, cmd->capacity);
mprotect(cmd, sizeof(cmd), PROT_READ | PROT_WRITE);
mprotect(cmd->cache, sizeof(cmd->capacity), PROT_READ | PROT_WRITE);
}
#else
#define msg_malloc(X) hub_malloc(X)
#define msg_malloc_zero(X) hub_malloc_zero(X)
#define msg_free(X) hub_free(X)
#endif /* MSG_MEMORY_DEBUG */
struct adc_message* adc_msg_incref(struct adc_message* msg) struct adc_message* adc_msg_incref(struct adc_message* msg)
{ {
if (!msg) return 0; if (!msg) return 0;
#ifndef ADC_MESSAGE_INCREF #ifndef ADC_MESSAGE_INCREF
#ifdef MSG_MEMORY_DEBUG
adc_msg_unprotect(msg);
#endif
msg->references++; msg->references++;
#ifdef MSG_MEMORY_DEBUG
adc_msg_protect(msg);
#endif
return msg; return msg;
#else #else
struct adc_message* copy = adc_msg_copy(msg); struct adc_message* copy = adc_msg_copy(msg);
@ -62,18 +117,19 @@ static int adc_msg_grow(struct adc_message* msg, size_t size)
newsize += 2; /* termination */ newsize += 2; /* termination */
newsize += (newsize % sizeof(size_t)); /* alignment padding */ newsize += (newsize % sizeof(size_t)); /* alignment padding */
buf = hub_malloc_zero(newsize); buf = msg_malloc_zero(newsize);
if (!buf) if (!buf)
return 0; return 0;
if (msg->cache) if (msg->cache)
{ {
memcpy(buf, msg->cache, msg->length); memcpy(buf, msg->cache, msg->length);
hub_free(msg->cache); msg_free(msg->cache);
} }
msg->cache = buf; msg->cache = buf;
msg->capacity = newsize; msg->capacity = newsize;
return 1; return 1;
} }
@ -151,11 +207,20 @@ void adc_msg_free(struct adc_message* msg)
if (msg->references > 0) if (msg->references > 0)
{ {
#ifdef MSG_MEMORY_DEBUG
adc_msg_unprotect(msg);
#endif
msg->references--; msg->references--;
#ifdef MSG_MEMORY_DEBUG
adc_msg_protect(msg);
#endif
} }
else else
{ {
hub_free(msg->cache); #ifdef MSG_MEMORY_DEBUG
adc_msg_unprotect(msg);
#endif
msg_free(msg->cache);
if (msg->feature_cast_include) if (msg->feature_cast_include)
{ {
@ -171,7 +236,7 @@ void adc_msg_free(struct adc_message* msg)
msg->feature_cast_exclude = 0; msg->feature_cast_exclude = 0;
} }
hub_free(msg); msg_free(msg);
} }
} }
@ -179,7 +244,7 @@ void adc_msg_free(struct adc_message* msg)
struct adc_message* adc_msg_copy(const struct adc_message* cmd) struct adc_message* adc_msg_copy(const struct adc_message* cmd)
{ {
char* tmp = 0; char* tmp = 0;
struct adc_message* copy = (struct adc_message*) hub_malloc_zero(sizeof(struct adc_message)); struct adc_message* copy = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
if (!copy) return NULL; /* OOM */ if (!copy) return NULL; /* OOM */
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
@ -231,6 +296,9 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
ADC_MSG_ASSERT(copy); ADC_MSG_ASSERT(copy);
#ifdef MSG_MEMORY_DEBUG
adc_msg_protect(copy);
#endif
return copy; return copy;
} }
@ -255,7 +323,7 @@ struct adc_message* adc_msg_parse_verify(struct hub_user* u, const char* line, s
struct adc_message* adc_msg_parse(const char* line, size_t length) struct adc_message* adc_msg_parse(const char* line, size_t length)
{ {
struct adc_message* command = (struct adc_message*) hub_malloc_zero(sizeof(struct adc_message)); struct adc_message* command = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
char prefix = line[0]; char prefix = line[0];
size_t n = 0; size_t n = 0;
char temp_sid[5]; char temp_sid[5];
@ -269,7 +337,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
if (!is_printable_utf8(line, length)) if (!is_printable_utf8(line, length))
{ {
LOG_DEBUG("Dropped message with non-printable UTF-8 characters."); LOG_DEBUG("Dropped message with non-printable UTF-8 characters.");
hub_free(command); msg_free(command);
return NULL; return NULL;
} }
@ -280,7 +348,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
if (!adc_msg_grow(command, length + need_terminate)) if (!adc_msg_grow(command, length + need_terminate))
{ {
hub_free(command); msg_free(command);
return NULL; /* OOM */ return NULL; /* OOM */
} }
@ -352,8 +420,8 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
{ {
list_destroy(command->feature_cast_include); list_destroy(command->feature_cast_include);
list_destroy(command->feature_cast_exclude); list_destroy(command->feature_cast_exclude);
hub_free(command->cache); msg_free(command->cache);
hub_free(command); msg_free(command);
return NULL; /* OOM */ return NULL; /* OOM */
} }
@ -444,6 +512,9 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
ADC_MSG_ASSERT(command); ADC_MSG_ASSERT(command);
#ifdef MSG_MEMORY_DEBUG
adc_msg_protect(command);
#endif
return command; return command;
} }
@ -456,7 +527,7 @@ struct adc_message* adc_msg_create(const char* line)
struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size) struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
{ {
struct adc_message* msg = (struct adc_message*) hub_malloc_zero(sizeof(struct adc_message)); struct adc_message* msg = (struct adc_message*) msg_malloc_zero(sizeof(struct adc_message));
if (!msg) if (!msg)
return NULL; /* OOM */ return NULL; /* OOM */
@ -465,7 +536,7 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
if (!adc_msg_grow(msg, size+1)) if (!adc_msg_grow(msg, size+1))
{ {
hub_free(msg); msg_free(msg);
return NULL; /* OOM */ return NULL; /* OOM */
} }
@ -485,6 +556,10 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
msg->cmd = fourcc; msg->cmd = fourcc;
msg->priority = 0; msg->priority = 0;
#ifdef MSG_MEMORY_DEBUG
adc_msg_protect(msg);
#endif
return msg; return msg;
} }
@ -624,6 +699,10 @@ void adc_msg_terminate(struct adc_message* cmd)
adc_msg_cache_append(cmd, "\n", 1); adc_msg_cache_append(cmd, "\n", 1);
} }
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
#ifdef MSG_MEMORY_DEBUG
adc_msg_protect(cmd);
#endif
} }
/* FIXME: this looks bogus */ /* FIXME: this looks bogus */
@ -631,6 +710,10 @@ void adc_msg_unterminate(struct adc_message* cmd)
{ {
ADC_MSG_ASSERT(cmd); ADC_MSG_ASSERT(cmd);
#ifdef MSG_MEMORY_DEBUG
adc_msg_unprotect(cmd);
#endif
if (cmd->length > 0 && cmd->cache[cmd->length-1] == '\n') if (cmd->length > 0 && cmd->cache[cmd->length-1] == '\n')
{ {
cmd->length--; cmd->length--;
@ -781,7 +864,7 @@ int adc_msg_unescape_length(const char* str)
char* adc_msg_unescape(const char* string) char* adc_msg_unescape(const char* string)
{ {
char* new_string = hub_malloc(adc_msg_unescape_length(string)+1); char* new_string = msg_malloc(adc_msg_unescape_length(string)+1);
char* ptr = (char*) new_string; char* ptr = (char*) new_string;
char* str = (char*) string; char* str = (char*) string;
int escaped = 0; int escaped = 0;