Cleanup reference adc message reference counting somewhat.

This commit is contained in:
Jan Vidar Krey 2010-02-16 17:41:56 +01:00
parent 29c162727c
commit 963416ad73
8 changed files with 107 additions and 138 deletions

View File

@ -23,7 +23,7 @@ EXO_TEST(hub_net_startup, {
EXO_TEST(hub_config_initialize, { EXO_TEST(hub_config_initialize, {
config_defaults(&g_config); config_defaults(&g_config);
g_config.server_port = 15111; g_config.server_port = 65111;
return 1; return 1;
}); });

View File

@ -8,27 +8,27 @@ EXO_TEST(test_message_refc_1, {
}); });
EXO_TEST(test_message_refc_2, { EXO_TEST(test_message_refc_2, {
return g_msg->references == 0; // 0 return g_msg->references == 1;
}); });
EXO_TEST(test_message_refc_3, { EXO_TEST(test_message_refc_3, {
adc_msg_incref(g_msg); adc_msg_incref(g_msg);
return g_msg->references == 1; // 1 return g_msg->references == 2;
}); });
EXO_TEST(test_message_refc_4, { EXO_TEST(test_message_refc_4, {
adc_msg_incref(g_msg); adc_msg_incref(g_msg);
return g_msg->references == 2; // 2 return g_msg->references == 3;
}); });
EXO_TEST(test_message_refc_5, { EXO_TEST(test_message_refc_5, {
adc_msg_free(g_msg); adc_msg_free(g_msg);
return g_msg->references == 1; // 1 return g_msg->references == 2;
}); });
EXO_TEST(test_message_refc_6, { EXO_TEST(test_message_refc_6, {
adc_msg_free(g_msg); adc_msg_free(g_msg);
return g_msg->references == 0; // 0 return g_msg->references == 1;
}); });
EXO_TEST(test_message_refc_7, { EXO_TEST(test_message_refc_7, {

View File

@ -26,6 +26,7 @@
uhub_assert(X->capacity); \ uhub_assert(X->capacity); \
uhub_assert(X->length); \ uhub_assert(X->length); \
uhub_assert(X->length <= X->capacity); \ uhub_assert(X->length <= X->capacity); \
uhub_assert(X->references > 0); \
uhub_assert(X->length == strlen(X->cache)); uhub_assert(X->length == strlen(X->cache));
#define ADC_MSG_NULL_ON_FREE #define ADC_MSG_NULL_ON_FREE
#else #else
@ -65,7 +66,6 @@ static void msg_free(void* ptr)
struct adc_message* adc_msg_incref(struct adc_message* msg) struct adc_message* adc_msg_incref(struct adc_message* msg)
{ {
if (!msg) return 0;
#ifndef ADC_MESSAGE_INCREF #ifndef ADC_MESSAGE_INCREF
msg->references++; msg->references++;
#ifdef MSG_MEMORY_DEBUG #ifdef MSG_MEMORY_DEBUG
@ -184,11 +184,9 @@ void adc_msg_free(struct adc_message* msg)
ADC_MSG_ASSERT(msg); ADC_MSG_ASSERT(msg);
if (msg->references > 0)
{
msg->references--; msg->references--;
}
else if (msg->references == 0)
{ {
#ifdef ADC_MSG_NULL_ON_FREE #ifdef ADC_MSG_NULL_ON_FREE
if (msg->cache) if (msg->cache)
@ -233,7 +231,7 @@ struct adc_message* adc_msg_copy(const struct adc_message* cmd)
copy->length = cmd->length; copy->length = cmd->length;
copy->capacity = 0; copy->capacity = 0;
copy->priority = cmd->priority; copy->priority = cmd->priority;
copy->references = 0; copy->references = 1;
copy->feature_cast_include = 0; copy->feature_cast_include = 0;
copy->feature_cast_exclude = 0; copy->feature_cast_exclude = 0;
@ -334,6 +332,7 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
command->cmd = FOURCC(line[0], line[1], line[2], line[3]); command->cmd = FOURCC(line[0], line[1], line[2], line[3]);
command->priority = 0; command->priority = 0;
command->references = 1;
switch (prefix) switch (prefix)
{ {
@ -417,7 +416,6 @@ struct adc_message* adc_msg_parse(const char* line, size_t length)
if (n == 10) if (n == 10)
ok = 0; ok = 0;
break; break;
case 'D': case 'D':
@ -497,7 +495,6 @@ 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*) msg_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 */
@ -524,7 +521,7 @@ struct adc_message* adc_msg_construct(fourcc_t fourcc, size_t size)
msg->cmd = fourcc; msg->cmd = fourcc;
msg->priority = 0; msg->priority = 0;
msg->references = 1;
return msg; return msg;
} }
@ -842,9 +839,6 @@ 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 = msg_malloc(adc_msg_unescape_length(string)+1); char* new_string = msg_malloc(adc_msg_unescape_length(string)+1);
@ -863,14 +857,12 @@ char* adc_msg_unescape(const char* string)
*ptr++ = '\n'; *ptr++ = '\n';
else else
*ptr++ = *str; *ptr++ = *str;
escaped = 0; escaped = 0;
} else { } else {
if (*str == '\\') if (*str == '\\')
escaped = 1; escaped = 1;
else else
*ptr++ = *str; *ptr++ = *str;
} }
str++; str++;
} }

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey * Copyright (C) 2007-2010, Jan Vidar Krey
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -674,7 +674,6 @@ int hub_handle_info_login(struct hub_info* hub, struct hub_user* user, struct ad
/* Private ID must never be broadcasted - drop it! */ /* Private ID must never be broadcasted - drop it! */
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_PRIVATE_ID); adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_PRIVATE_ID);
code = set_credentials(hub, user, cmd); code = set_credentials(hub, user, cmd);
/* Note: this must be done *after* set_credentials. */ /* Note: this must be done *after* set_credentials. */
@ -798,3 +797,5 @@ int hub_handle_info(struct hub_info* hub, struct hub_user* user, const struct ad
return 0; return 0;
} }

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * uhub - A tiny ADC p2p connection hub
* Copyright (C) 2007-2009, Jan Vidar Krey * Copyright (C) 2007-2010, Jan Vidar Krey
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by

View File

@ -24,35 +24,6 @@
/* FIXME: This should not be needed! */ /* FIXME: This should not be needed! */
extern struct hub_info* g_hub; extern struct hub_info* g_hub;
#ifdef DEBUG_SENDQ
void debug_sendq_send(struct hub_user* user, int sent, int total)
{
LOG_DUMP("SEND: sd=%d, %d/%d bytes\n", user->connection->sd, sent, total);
if (sent == -1)
{
int err = net_error();
LOG_DUMP(" errno: %d - %s\n", err, net_error_string(err));
}
}
void debug_sendq_recv(struct hub_user* user, int received, int max, const char* buffer)
{
LOG_DUMP("RECV: %d/%d bytes\n", received, (int) max);
if (received == -1)
{
int err = net_error();
LOG_DUMP(" errno: %d - %s\n", err, net_error_string(err));
}
else if (received > 0)
{
char* data = hub_malloc_zero(received + 1);
memcpy(data, buffer, received);
LOG_DUMP("RECV: \"%s\"\n", data);
hub_free(data);
}
}
#endif
int handle_net_read(struct hub_user* user) int handle_net_read(struct hub_user* user)
{ {
static char buf[MAX_RECV_BUF]; static char buf[MAX_RECV_BUF];

View File

@ -83,7 +83,14 @@ void user_set_state(struct hub_user* user, enum user_state state)
void user_set_info(struct hub_user* user, struct adc_message* cmd) void user_set_info(struct hub_user* user, struct adc_message* cmd)
{ {
adc_msg_free(user->info); adc_msg_free(user->info);
if (cmd)
{
user->info = adc_msg_incref(cmd); user->info = adc_msg_incref(cmd);
}
else
{
user->info = 0;
}
} }
void user_update_info(struct hub_user* u, struct adc_message* cmd) void user_update_info(struct hub_user* u, struct adc_message* cmd)

View File

@ -248,7 +248,6 @@ int uman_send_user_list(struct hub_info* hub, struct hub_user* target)
return ret; return ret;
} }
void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving) void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving)
{ {
struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6); struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6);
@ -262,7 +261,6 @@ void uman_send_quit_message(struct hub_info* hub, struct hub_user* leaving)
adc_msg_free(command); adc_msg_free(command);
} }
sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user) sid_t uman_get_free_sid(struct hub_info* hub, struct hub_user* user)
{ {
sid_t sid = sid_alloc(hub->users->sids, user); sid_t sid = sid_alloc(hub->users->sids, user);