Skip to content

Commit

Permalink
cleanup: Remove old type-ordered event getters.
Browse files Browse the repository at this point in the history
These are quite expensive, because they go through all events to index
in a typed array that no longer exists. Clients should index in the
union array and find the event they want themselves, or use dispatch.
  • Loading branch information
iphydf committed Jan 13, 2024
1 parent b333204 commit f46323e
Show file tree
Hide file tree
Showing 47 changed files with 68 additions and 1,563 deletions.
8 changes: 4 additions & 4 deletions auto_tests/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ TCP_test_SOURCES = ../auto_tests/TCP_test.c
TCP_test_CFLAGS = $(AUTOTEST_CFLAGS)
TCP_test_LDADD = $(AUTOTEST_LDADD)

tox_events_test_SOURCES = ../auto_tests/tox_events_test.c
tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_events_test_LDADD = $(AUTOTEST_LDADD)

tox_dispatch_test_SOURCES = ../auto_tests/tox_dispatch_test.c
tox_dispatch_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_dispatch_test_LDADD = $(AUTOTEST_LDADD)

tox_events_test_SOURCES = ../auto_tests/tox_events_test.c
tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_events_test_LDADD = $(AUTOTEST_LDADD)

tox_many_tcp_test_SOURCES = ../auto_tests/tox_many_tcp_test.c
tox_many_tcp_test_CFLAGS = $(AUTOTEST_CFLAGS)
tox_many_tcp_test_LDADD = $(AUTOTEST_LDADD)
Expand Down
22 changes: 12 additions & 10 deletions auto_tests/tox_events_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,23 @@ static bool await_message(Tox **toxes)
Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr);

if (events != nullptr) {
ck_assert(tox_events_get_friend_message_size(events) == 1);
const Tox_Event_Friend_Message *msg_event = tox_events_get_friend_message(events, 0);
uint32_t events_size = tox_events_get_size(events);
ck_assert(events_size == 1);

const Tox_Event_Friend_Message *msg_event = nullptr;
for (uint32_t j = 0; j < events_size; ++j) {
const Tox_Event *ev = tox_events_get(events, j);
if (tox_event_get_type(ev) == TOX_EVENT_FRIEND_MESSAGE) {
msg_event = tox_event_get_friend_message(ev);
}
}

ck_assert(msg_event != nullptr);
ck_assert(tox_event_friend_message_get_message_length(msg_event) == sizeof("hello"));
const uint8_t *msg = tox_event_friend_message_get_message(msg_event);
ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0,
"message was not expected 'hello' but '%s'", (const char *)msg);

const uint32_t event_count = tox_events_get_size(events);
for (uint32_t j = 0; j < event_count; ++j) {
const Tox_Event *event = tox_events_get(events, j);
if (tox_event_get_type(event) == TOX_EVENT_FRIEND_MESSAGE) {
ck_assert(tox_event_get_friend_message(event) == msg_event);
}
}

tox_events_free(events);
return true;
}
Expand Down
22 changes: 0 additions & 22 deletions other/event_tooling/generate_event_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,28 +404,6 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " tox_events_add(events, &event);\n";
f << " return " << event_name_l << ";\n}\n\n";

// get
f << "const Tox_Event_" << event_name << " *tox_events_get_" << event_name_l << "(const Tox_Events *events, uint32_t index)\n{\n";
f << " uint32_t " << event_name_l << "_index = 0;\n";
f << " const uint32_t size = tox_events_get_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (" << event_name_l << "_index > index) {\n";
f << " return nullptr;\n }\n\n";
f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n";
f << " const Tox_Event_" << event_name << " *" << event_name_l << " = events->events[i].data." << event_name_l << ";\n";
f << " if (" << event_name_l << "_index == index) {\n";
f << " return " << event_name_l << ";\n }\n";
f << " ++" << event_name_l << "_index;\n }\n }\n\n return nullptr;\n}\n\n";

// get size
f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n";
f << " uint32_t " << event_name_l << "_size = 0;\n";
f << " const uint32_t size = tox_events_get_size(events);\n\n";
f << " for (uint32_t i = 0; i < size; ++i) {\n";
f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n";
f << " ++" << event_name_l << "_size;\n }\n }\n\n";
f << " return " << event_name_l << "_size;\n}\n\n";

// unpack
f << "bool tox_event_" << event_name_l << "_unpack(\n";
f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n";
Expand Down
1 change: 1 addition & 0 deletions toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ cc_fuzz_test(
srcs = ["tox_events_fuzz_test.cc"],
corpus = ["//tools/toktok-fuzzer/corpus:tox_events_fuzz_test"],
deps = [
":tox_dispatch",
":tox_events",
"//c-toxcore/testing/fuzzing:fuzz_support",
],
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/conference_connected.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,42 +118,6 @@ static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_E
return conference_connected;

Check warning on line 118 in toxcore/events/conference_connected.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_connected.c#L117-L118

Added lines #L117 - L118 were not covered by tests
}

const Tox_Event_Conference_Connected *tox_events_get_conference_connected(const Tox_Events *events, uint32_t index)
{
uint32_t conference_connected_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (conference_connected_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) {
const Tox_Event_Conference_Connected *conference_connected = events->events[i].data.conference_connected;
if (conference_connected_index == index) {
return conference_connected;
}
++conference_connected_index;
}
}

return nullptr;
}

uint32_t tox_events_get_conference_connected_size(const Tox_Events *events)
{
uint32_t conference_connected_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) {
++conference_connected_size;
}
}

return conference_connected_size;
}

bool tox_event_conference_connected_unpack(
Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,42 +180,6 @@ static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events
return conference_invite;

Check warning on line 180 in toxcore/events/conference_invite.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_invite.c#L179-L180

Added lines #L179 - L180 were not covered by tests
}

const Tox_Event_Conference_Invite *tox_events_get_conference_invite(const Tox_Events *events, uint32_t index)
{
uint32_t conference_invite_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (conference_invite_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) {
const Tox_Event_Conference_Invite *conference_invite = events->events[i].data.conference_invite;
if (conference_invite_index == index) {
return conference_invite;
}
++conference_invite_index;
}
}

return nullptr;
}

uint32_t tox_events_get_conference_invite_size(const Tox_Events *events)
{
uint32_t conference_invite_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) {
++conference_invite_size;
}
}

return conference_invite_size;
}

bool tox_event_conference_invite_unpack(
Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,42 +196,6 @@ static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Event
return conference_message;

Check warning on line 196 in toxcore/events/conference_message.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_message.c#L195-L196

Added lines #L195 - L196 were not covered by tests
}

const Tox_Event_Conference_Message *tox_events_get_conference_message(const Tox_Events *events, uint32_t index)
{
uint32_t conference_message_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (conference_message_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) {
const Tox_Event_Conference_Message *conference_message = events->events[i].data.conference_message;
if (conference_message_index == index) {
return conference_message;
}
++conference_message_index;
}
}

return nullptr;
}

uint32_t tox_events_get_conference_message_size(const Tox_Events *events)
{
uint32_t conference_message_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) {
++conference_message_size;
}
}

return conference_message_size;
}

bool tox_event_conference_message_unpack(
Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/conference_peer_list_changed.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,42 +118,6 @@ static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_li
return conference_peer_list_changed;

Check warning on line 118 in toxcore/events/conference_peer_list_changed.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_peer_list_changed.c#L117-L118

Added lines #L117 - L118 were not covered by tests
}

const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, uint32_t index)
{
uint32_t conference_peer_list_changed_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (conference_peer_list_changed_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) {
const Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = events->events[i].data.conference_peer_list_changed;
if (conference_peer_list_changed_index == index) {
return conference_peer_list_changed;
}
++conference_peer_list_changed_index;
}
}

return nullptr;
}

uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events)
{
uint32_t conference_peer_list_changed_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) {
++conference_peer_list_changed_size;
}
}

return conference_peer_list_changed_size;
}

bool tox_event_conference_peer_list_changed_unpack(
Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,42 +179,6 @@ static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_E
return conference_peer_name;

Check warning on line 179 in toxcore/events/conference_peer_name.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_peer_name.c#L178-L179

Added lines #L178 - L179 were not covered by tests
}

const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name(const Tox_Events *events, uint32_t index)
{
uint32_t conference_peer_name_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (conference_peer_name_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) {
const Tox_Event_Conference_Peer_Name *conference_peer_name = events->events[i].data.conference_peer_name;
if (conference_peer_name_index == index) {
return conference_peer_name;
}
++conference_peer_name_index;
}
}

return nullptr;
}

uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events)
{
uint32_t conference_peer_name_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) {
++conference_peer_name_size;
}
}

return conference_peer_name_size;
}

bool tox_event_conference_peer_name_unpack(
Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,42 +179,6 @@ static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *e
return conference_title;

Check warning on line 179 in toxcore/events/conference_title.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_title.c#L178-L179

Added lines #L178 - L179 were not covered by tests
}

const Tox_Event_Conference_Title *tox_events_get_conference_title(const Tox_Events *events, uint32_t index)
{
uint32_t conference_title_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (conference_title_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) {
const Tox_Event_Conference_Title *conference_title = events->events[i].data.conference_title;
if (conference_title_index == index) {
return conference_title;
}
++conference_title_index;
}
}

return nullptr;
}

uint32_t tox_events_get_conference_title_size(const Tox_Events *events)
{
uint32_t conference_title_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) {
++conference_title_size;
}
}

return conference_title_size;
}

bool tox_event_conference_title_unpack(
Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
36 changes: 0 additions & 36 deletions toxcore/events/file_chunk_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,42 +171,6 @@ static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Event
return file_chunk_request;

Check warning on line 171 in toxcore/events/file_chunk_request.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_chunk_request.c#L170-L171

Added lines #L170 - L171 were not covered by tests
}

const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request(const Tox_Events *events, uint32_t index)
{
uint32_t file_chunk_request_index = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (file_chunk_request_index > index) {
return nullptr;
}

if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) {
const Tox_Event_File_Chunk_Request *file_chunk_request = events->events[i].data.file_chunk_request;
if (file_chunk_request_index == index) {
return file_chunk_request;
}
++file_chunk_request_index;
}
}

return nullptr;
}

uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events)
{
uint32_t file_chunk_request_size = 0;
const uint32_t size = tox_events_get_size(events);

for (uint32_t i = 0; i < size; ++i) {
if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) {
++file_chunk_request_size;
}
}

return file_chunk_request_size;
}

bool tox_event_file_chunk_request_unpack(
Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem)
{
Expand Down
Loading

0 comments on commit f46323e

Please sign in to comment.