Skip to content

Commit

Permalink
refactor(discord-refcount.c): complete rewrite using anomap
Browse files Browse the repository at this point in the history
  • Loading branch information
Anotra committed Feb 15, 2024
1 parent d0e7c5a commit 8b09803
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 202 deletions.
17 changes: 6 additions & 11 deletions include/discord-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {
#include "queue.h"
#include "priority_queue.h"
#include "attributes.h"
#include "anomap.h"

/** @brief Return 1 if string isn't considered empty */
#define NOT_EMPTY_STR(str) ((str) && *(str))
Expand Down Expand Up @@ -938,17 +939,9 @@ void discord_gateway_dispatch(struct discord_gateway *gw);
struct discord_refcounter {
/** `DISCORD_REFCOUNT` logging module */
struct logconf conf;
/** amount of individual user's data held for automatic cleanup */
int length;
/** cap before increase */
int capacity;
/**
* individual user's data held for automatic cleanup
* @note datatype declared at discord-refcount.c
*/
struct _discord_ref *refs;
/** global lock */
pthread_mutex_t *g_lock;

struct anomap *maps[16];
pthread_mutex_t locks[16];
};

/**
Expand All @@ -971,6 +964,7 @@ void discord_refcounter_init(struct discord_refcounter *rc,
* @param should_free whether `data` cleanup should be followed by a free()
*/
void discord_refcounter_add_internal(struct discord_refcounter *rc,
const char * name,
void *data,
void (*cleanup)(void *data),
bool should_free);
Expand All @@ -985,6 +979,7 @@ void discord_refcounter_add_internal(struct discord_refcounter *rc,
* @param should_free whether `data` cleanup should be followed by a free()
*/
void discord_refcounter_add_client(struct discord_refcounter *rc,
const char * name,
void *data,
void (*cleanup)(struct discord *client,
void *data),
Expand Down
3 changes: 2 additions & 1 deletion src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ discord_get_channel_at_pos(struct discord *client,
&& CCORD_RESOURCE_UNAVAILABLE
== discord_refcounter_incr(&client->refcounter, ret->data))
{
discord_refcounter_add_client(&client->refcounter, ret->data,
discord_refcounter_add_client(&client->refcounter,
"discord_get_channel_at_pos", ret->data,
ret->cleanup, false);
}

Expand Down
2 changes: 1 addition & 1 deletion src/discord-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ _on_shard_disconnected(struct discord *client,
memset(guild, 0, sizeof *guild); \
discord_guild_from_json(buf, size, guild); \
discord_refcounter_add_internal( \
&client->refcounter, guild, \
&client->refcounter, "cache_guild", guild, \
(void (*)(void *))discord_guild_cleanup, true); \
} while (0)

Expand Down
1 change: 1 addition & 0 deletions src/discord-gateway_dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ discord_gateway_dispatch(struct discord_gateway *gw)
== discord_refcounter_incr(&client->refcounter, event_data))
{
discord_refcounter_add_internal(&client->refcounter,
"discord_gateway_dispatch",
event_data,
dispatch[event].cleanup, true);
}
Expand Down
5 changes: 3 additions & 2 deletions src/discord-messagecommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ discord_message_commands_try_perform(struct discord_message_commands *cmds,
if (CCORD_RESOURCE_UNAVAILABLE
== discord_refcounter_incr(&client->refcounter, event_data))
{
discord_refcounter_add_internal(&client->refcounter, event_data,
_discord_message_cleanup_v, false);
discord_refcounter_add_internal(
&client->refcounter, "discord_message_commands_try_perform",
event_data, _discord_message_cleanup_v, false);
}
callback(client, event_data);
event_data->content = tmp; /* retrieve original ptr */
Expand Down
Loading

0 comments on commit 8b09803

Please sign in to comment.