diff --git a/CMakeLists.txt b/CMakeLists.txt index e96ffb886..db6cc5ea8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -358,9 +358,6 @@ set(toxcore_SOURCES toxcore/tox_events.h toxcore/tox_log_level.c toxcore/tox_log_level.h - toxcore/tox_memory.c - toxcore/tox_memory.h - toxcore/tox_memory_impl.h toxcore/tox_options.c toxcore/tox_options.h toxcore/tox_private.c diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index e5e24a22c..3c4ca3a33 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -290,7 +290,7 @@ int main(int argc, char *argv[]) IP ip; ip_init(&ip, enable_ipv6); - const Tox_Memory *mem = os_memory(); + const Memory *mem = os_memory(); const Tox_Random *rng = os_random(); const Network *ns = os_network(); diff --git a/testing/support/BUILD.bazel b/testing/support/BUILD.bazel index 6a5c367d9..c8cb0d2d4 100644 --- a/testing/support/BUILD.bazel +++ b/testing/support/BUILD.bazel @@ -50,7 +50,6 @@ cc_library( "//c-toxcore/toxcore:network", "//c-toxcore/toxcore:tox", "//c-toxcore/toxcore:tox_events", - "//c-toxcore/toxcore:tox_memory", "//c-toxcore/toxcore:tox_options", "//c-toxcore/toxcore:tox_random", "@psocket", diff --git a/testing/support/doubles/fake_memory.hh b/testing/support/doubles/fake_memory.hh index 7a4d50f3e..08512a9e6 100644 --- a/testing/support/doubles/fake_memory.hh +++ b/testing/support/doubles/fake_memory.hh @@ -7,7 +7,7 @@ #include "../public/memory.hh" // Forward declaration -struct Tox_Memory; +struct Memory; namespace tox::test { @@ -30,9 +30,9 @@ public: void set_observer(Observer observer); /** - * @brief Returns C-compatible Tox_Memory struct. + * @brief Returns C-compatible Memory struct. */ - struct Tox_Memory c_memory() override; + struct Memory c_memory() override; size_t current_allocation() const; size_t max_allocation() const; diff --git a/testing/support/public/memory.hh b/testing/support/public/memory.hh index 1039c4872..98eecd334 100644 --- a/testing/support/public/memory.hh +++ b/testing/support/public/memory.hh @@ -5,7 +5,7 @@ #include // Forward declaration -struct Tox_Memory; +struct Memory; namespace tox::test { @@ -21,9 +21,9 @@ public: virtual void free(void *ptr) = 0; /** - * @brief Returns C-compatible Tox_Memory struct. + * @brief Returns C-compatible Memory struct. */ - virtual struct Tox_Memory c_memory() = 0; + virtual struct Memory c_memory() = 0; }; } // namespace tox::test diff --git a/testing/support/public/simulated_environment.hh b/testing/support/public/simulated_environment.hh index e430fe342..6342c3b96 100644 --- a/testing/support/public/simulated_environment.hh +++ b/testing/support/public/simulated_environment.hh @@ -13,7 +13,7 @@ #include #endif -#include "../../../toxcore/tox_memory_impl.h" +#include "../../../toxcore/mem.h" #include "../../../toxcore/tox_private.h" #include "../../../toxcore/tox_random_impl.h" #include "../doubles/fake_clock.hh" @@ -34,7 +34,7 @@ struct ScopedToxSystem { // C structs struct Network c_network; struct Tox_Random c_random; - struct Tox_Memory c_memory; + struct Memory c_memory; // The main struct passed to tox_new Tox_System system; diff --git a/testing/support/public/simulation.hh b/testing/support/public/simulation.hh index bbc2bdb90..b835db8af 100644 --- a/testing/support/public/simulation.hh +++ b/testing/support/public/simulation.hh @@ -16,8 +16,8 @@ #include #endif +#include "../../../toxcore/mem.h" #include "../../../toxcore/tox.h" -#include "../../../toxcore/tox_memory_impl.h" #include "../../../toxcore/tox_private.h" #include "../../../toxcore/tox_random_impl.h" #include "../doubles/fake_clock.hh" @@ -172,7 +172,7 @@ private: public: struct Network c_network; struct Tox_Random c_random; - struct Tox_Memory c_memory; + struct Memory c_memory; struct IP ip; }; diff --git a/testing/support/src/fake_memory.cc b/testing/support/src/fake_memory.cc index 3af44c8a4..69dfb0dfc 100644 --- a/testing/support/src/fake_memory.cc +++ b/testing/support/src/fake_memory.cc @@ -4,13 +4,13 @@ #include #include -#include "../../../toxcore/tox_memory_impl.h" +#include "../../../toxcore/mem.h" namespace tox::test { // --- Trampolines --- -static const Tox_Memory_Funcs kFakeMemoryVtable = { +static const Memory_Funcs kFakeMemoryVtable = { .malloc_callback = [](void *obj, uint32_t size) { return static_cast(obj)->malloc(size); }, .realloc_callback @@ -127,7 +127,7 @@ void FakeMemory::set_failure_injector(FailureInjector injector) void FakeMemory::set_observer(Observer observer) { observer_ = std::move(observer); } -struct Tox_Memory FakeMemory::c_memory() { return Tox_Memory{&kFakeMemoryVtable, this}; } +struct Memory FakeMemory::c_memory() { return Memory{&kFakeMemoryVtable, this}; } size_t FakeMemory::current_allocation() const { return current_allocation_.load(); } diff --git a/toxav/toxav.c b/toxav/toxav.c index 2b71145c8..f1e5066e3 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -86,7 +86,7 @@ typedef struct DecodeTimeStats { } DecodeTimeStats; struct ToxAV { - const struct Tox_Memory *mem; + const struct Memory *mem; Logger *log; Tox *tox; MSISession *msi; diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 69a8ae06c..5ed763e28 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -54,20 +54,6 @@ cc_library( deps = [":attributes"], ) -cc_library( - name = "tox_memory", - srcs = ["tox_memory.c"], - hdrs = [ - "tox_memory.h", - "tox_memory_impl.h", - ], - visibility = ["//c-toxcore:__subpackages__"], - deps = [ - ":ccompat", - ":tox_attributes", - ], -) - cc_library( name = "os_memory", srcs = ["os_memory.c"], @@ -75,7 +61,7 @@ cc_library( visibility = ["//c-toxcore:__subpackages__"], deps = [ ":attributes", - ":tox_memory", + ":mem", ], ) @@ -89,8 +75,8 @@ cc_library( visibility = ["//c-toxcore:__subpackages__"], deps = [ ":ccompat", + ":mem", ":tox_attributes", - ":tox_memory", ], ) @@ -115,7 +101,6 @@ cc_library( deps = [ ":attributes", ":ccompat", - ":tox_memory", ], ) diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index 7689dd006..abc146e49 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -147,9 +147,6 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/tox_events.h \ ../toxcore/tox_log_level.c \ ../toxcore/tox_log_level.h \ - ../toxcore/tox_memory.c \ - ../toxcore/tox_memory.h \ - ../toxcore/tox_memory_impl.h \ ../toxcore/tox_options.c \ ../toxcore/tox_options.h \ ../toxcore/tox_pack.c \ diff --git a/toxcore/events/events_alloc.h b/toxcore/events/events_alloc.h index 993b04d50..935eb7d8e 100644 --- a/toxcore/events/events_alloc.h +++ b/toxcore/events/events_alloc.h @@ -17,19 +17,19 @@ extern "C" { #endif -struct Tox_Memory; +struct Memory; struct Tox_Events { Tox_Event *_Nonnull events; uint32_t events_size; uint32_t events_capacity; - const struct Tox_Memory *_Nonnull mem; + const struct Memory *_Nonnull mem; }; typedef struct Tox_Events_State { Tox_Err_Events_Iterate error; - const struct Tox_Memory *_Nonnull mem; + const struct Memory *_Nonnull mem; Tox_Events *_Nonnull events; } Tox_Events_State; diff --git a/toxcore/group_announce_test.cc b/toxcore/group_announce_test.cc index d0ed5e576..a72944fc4 100644 --- a/toxcore/group_announce_test.cc +++ b/toxcore/group_announce_test.cc @@ -20,7 +20,7 @@ using tox::test::SimulatedEnvironment; struct Announces : ::testing::Test { protected: SimulatedEnvironment env; - Tox_Memory c_mem_; + Memory c_mem_; Mono_Time *mono_time_ = nullptr; GC_Announces_List *gca_ = nullptr; GC_Announce _ann1; @@ -124,7 +124,7 @@ TEST_F(Announces, AnnouncesGetAndCleanup) struct AnnouncesPack : ::testing::Test { protected: SimulatedEnvironment env; - Tox_Memory c_mem_; + Memory c_mem_; std::vector announces_; Logger *logger_ = nullptr; diff --git a/toxcore/group_moderation_test.cc b/toxcore/group_moderation_test.cc index 2e8719632..e4b4df2fa 100644 --- a/toxcore/group_moderation_test.cc +++ b/toxcore/group_moderation_test.cc @@ -210,7 +210,7 @@ TEST(SanctionsList, PackUnpackSanctionsCreds) struct SanctionsListMod : ::testing::Test { protected: SimulatedEnvironment env; - Tox_Memory c_mem_; + Memory c_mem_; Tox_Random c_rng_; Extended_Public_Key pk; diff --git a/toxcore/mem.c b/toxcore/mem.c index ce33fb4a0..dcd5ff525 100644 --- a/toxcore/mem.c +++ b/toxcore/mem.c @@ -8,23 +8,22 @@ #include #include "ccompat.h" -#include "tox_memory.h" void *mem_balloc(const Memory *mem, uint32_t size) { - void *const ptr = tox_memory_malloc(mem, size); + void *const ptr = mem->funcs->malloc_callback(mem->user_data, size); return ptr; } void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size) { - void *const new_ptr = tox_memory_realloc(mem, ptr, size); + void *const new_ptr = mem->funcs->realloc_callback(mem->user_data, ptr, size); return new_ptr; } void *mem_alloc(const Memory *mem, uint32_t size) { - void *const ptr = tox_memory_malloc(mem, size); + void *const ptr = mem_balloc(mem, size); if (ptr != nullptr) { memset(ptr, 0, size); } @@ -39,10 +38,7 @@ void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size) return nullptr; } - void *const ptr = tox_memory_malloc(mem, bytes); - if (ptr != nullptr) { - memset(ptr, 0, bytes); - } + void *const ptr = mem_alloc(mem, bytes); return ptr; } @@ -54,11 +50,12 @@ void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size) return nullptr; } - void *const new_ptr = tox_memory_realloc(mem, ptr, bytes); + void *const new_ptr = mem_brealloc(mem, ptr, bytes); return new_ptr; } void mem_delete(const Memory *mem, void *ptr) { - tox_memory_dealloc(mem, ptr); + mem->funcs->dealloc_callback(mem->user_data, ptr); } + diff --git a/toxcore/mem.h b/toxcore/mem.h index 6784525bd..dafe8f3f4 100644 --- a/toxcore/mem.h +++ b/toxcore/mem.h @@ -12,13 +12,31 @@ #include // uint*_t #include "attributes.h" -#include "tox_memory.h" #ifdef __cplusplus extern "C" { #endif -typedef Tox_Memory Memory; +/** @brief Allocate a byte array, similar to malloc. */ +typedef void *_Nullable memory_malloc_cb(void *_Nullable self, uint32_t size); +/** @brief Reallocate a byte array, similar to realloc. */ +typedef void *_Nullable memory_realloc_cb(void *_Nullable self, void *_Nullable ptr, uint32_t size); +/** + * @brief Deallocate a byte or object array, similar to free. + */ +typedef void memory_dealloc_cb(void *_Nullable self, void *_Nullable ptr); + +/** @brief Functions wrapping standard C memory allocation functions. */ +typedef struct Memory_Funcs { + memory_malloc_cb *_Nonnull malloc_callback; + memory_realloc_cb *_Nonnull realloc_callback; + memory_dealloc_cb *_Nonnull dealloc_callback; +} Memory_Funcs; + +typedef struct Memory { + const Memory_Funcs *_Nonnull funcs; + void *_Nullable user_data; +} Memory; /** * @brief Allocate an array of a given size for built-in types. @@ -37,9 +55,12 @@ void *_Nullable mem_balloc(const Memory *_Nonnull mem, uint32_t size); void *_Nullable mem_brealloc(const Memory *_Nonnull mem, void *_Nullable ptr, uint32_t size); /** - * @brief Allocate a single object. + * @brief Allocate a single zero-initialised object. * * Always use as `(T *)mem_alloc(mem, sizeof(T))`. + * + * @param mem The memory allocator. + * @param size Size in bytes of each element. */ void *_Nullable mem_alloc(const Memory *_Nonnull mem, uint32_t size); @@ -75,3 +96,4 @@ void mem_delete(const Memory *_Nonnull mem, void *_Nullable ptr); #endif #endif /* C_TOXCORE_TOXCORE_MEM_H */ + diff --git a/toxcore/onion_client_test.cc b/toxcore/onion_client_test.cc index 1a3223ce8..64047a398 100644 --- a/toxcore/onion_client_test.cc +++ b/toxcore/onion_client_test.cc @@ -292,7 +292,7 @@ TEST_F(OnionClientTest, OOBReadInHandleAnnounceResponse) std::uint64_t initial_recv_time = onion_testonly_get_last_packet_recv(alice.get_onion_client()); // Setup Memory - Tox_Memory mem_struct = env.fake_memory().c_memory(); + Memory mem_struct = env.fake_memory().c_memory(); const Memory *mem = &mem_struct; // Observer @@ -493,7 +493,7 @@ TEST_F(OnionClientTest, OnionAnnounceResponse_TooShort) bool triggered = false; // Setup Memory - Tox_Memory mem_struct = env.fake_memory().c_memory(); + Memory mem_struct = env.fake_memory().c_memory(); const Memory *mem = &mem_struct; bob_socket->set_recv_observer([&](const std::vector &data, const IP_Port &from) { @@ -637,7 +637,7 @@ TEST_F(OnionClientTest, SharedKeyCacheUseAfterFreeRegression) onion_add_bs_path_node(alice.get_onion_client(), &ip, pk); } - Tox_Memory mem_struct = env.fake_memory().c_memory(); + Memory mem_struct = env.fake_memory().c_memory(); const Memory *mem = &mem_struct; int total_decryption_failures = 0; @@ -795,7 +795,7 @@ TEST_F(OnionClientTest, SharedKeyReuseOnEviction) int total_decrypted = 0; int total_failed = 0; - Tox_Memory mem_struct = env.fake_memory().c_memory(); + Memory mem_struct = env.fake_memory().c_memory(); const Memory *mem = &mem_struct; auto observer = [&](OnionNode *node, const std::vector &data) { diff --git a/toxcore/os_memory.c b/toxcore/os_memory.c index 7275a45bc..0910f97f8 100644 --- a/toxcore/os_memory.c +++ b/toxcore/os_memory.c @@ -6,8 +6,7 @@ #include #include "attributes.h" -#include "tox_memory.h" -#include "tox_memory_impl.h" // IWYU pragma: keep +#include "mem.h" static void *os_malloc(void *_Nonnull self, uint32_t size) { @@ -27,14 +26,14 @@ static void os_dealloc(void *_Nonnull self, void *_Nullable ptr) free(ptr); } -static const Tox_Memory_Funcs os_memory_funcs = { +static const Memory_Funcs os_memory_funcs = { os_malloc, os_realloc, os_dealloc, }; -const Tox_Memory os_memory_obj = {&os_memory_funcs}; +const Memory os_memory_obj = {&os_memory_funcs}; -const Tox_Memory *os_memory(void) +const Memory *os_memory(void) { return &os_memory_obj; } diff --git a/toxcore/os_memory.h b/toxcore/os_memory.h index cfbe6a2c7..530843b83 100644 --- a/toxcore/os_memory.h +++ b/toxcore/os_memory.h @@ -5,18 +5,19 @@ #ifndef C_TOXCORE_TOXCORE_OS_MEMORY_H #define C_TOXCORE_TOXCORE_OS_MEMORY_H -#include "tox_memory.h" +#include "mem.h" #ifdef __cplusplus extern "C" { #endif -extern const Tox_Memory os_memory_obj; +extern const Memory os_memory_obj; -const Tox_Memory *os_memory(void); +const Memory *os_memory(void); #ifdef __cplusplus } /* extern "C" */ #endif #endif /* C_TOXCORE_TOXCORE_OS_MEMORY_H */ + diff --git a/toxcore/ping_array_test.cc b/toxcore/ping_array_test.cc index dc0e54c85..8ded39b3c 100644 --- a/toxcore/ping_array_test.cc +++ b/toxcore/ping_array_test.cc @@ -22,14 +22,14 @@ struct Ping_Array_Deleter { using Ping_Array_Ptr = std::unique_ptr; struct Mono_Time_Deleter { - Mono_Time_Deleter(Tox_Memory mem) + Mono_Time_Deleter(Memory mem) : mem_(mem) { } void operator()(Mono_Time *arr) { mono_time_free(&mem_, arr); } private: - Tox_Memory mem_; + Memory mem_; }; using Mono_Time_Ptr = std::unique_ptr; diff --git a/toxcore/sort_test_util.hh b/toxcore/sort_test_util.hh index 487f88b78..53786baa9 100644 --- a/toxcore/sort_test_util.hh +++ b/toxcore/sort_test_util.hh @@ -10,7 +10,7 @@ #include "sort.h" -struct Tox_Memory; +struct Memory; template constexpr Sort_Funcs sort_funcs() @@ -42,7 +42,7 @@ constexpr Sort_Funcs sort_funcs() // A realistic test case where we have a struct with some stuff and an expensive value we compare. struct Some_Type { - const Tox_Memory *mem; + const Memory *mem; std::array compare_value; const char *name; diff --git a/toxcore/tox_memory.c b/toxcore/tox_memory.c deleted file mode 100644 index a2d6ad8a8..000000000 --- a/toxcore/tox_memory.c +++ /dev/null @@ -1,60 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2025 The TokTok team. - * Copyright © 2013 Tox project. - */ -#include "tox_memory.h" - -#include - -#include "ccompat.h" -#include "tox_memory_impl.h" // IWYU pragma: keep - -Tox_Memory *tox_memory_new(const Tox_Memory_Funcs *funcs, void *user_data) -{ - const Tox_Memory bootstrap = {funcs, user_data}; - - Tox_Memory *mem = (Tox_Memory *)tox_memory_alloc(&bootstrap, sizeof(Tox_Memory)); - - if (mem == nullptr) { - return nullptr; - } - - *mem = bootstrap; - - return mem; -} - -void tox_memory_free(Tox_Memory *mem) -{ - if (mem == nullptr) { - return; - } - - tox_memory_dealloc(mem, mem); -} - -void *tox_memory_malloc(const Tox_Memory *mem, uint32_t size) -{ - void *const ptr = mem->funcs->malloc_callback(mem->user_data, size); - return ptr; -} - -void *tox_memory_alloc(const Tox_Memory *mem, uint32_t size) -{ - void *const ptr = tox_memory_malloc(mem, size); - if (ptr != nullptr) { - memset(ptr, 0, size); - } - return ptr; -} - -void *tox_memory_realloc(const Tox_Memory *mem, void *ptr, uint32_t size) -{ - void *const new_ptr = mem->funcs->realloc_callback(mem->user_data, ptr, size); - return new_ptr; -} - -void tox_memory_dealloc(const Tox_Memory *mem, void *ptr) -{ - mem->funcs->dealloc_callback(mem->user_data, ptr); -} diff --git a/toxcore/tox_memory.h b/toxcore/tox_memory.h deleted file mode 100644 index c872bf4da..000000000 --- a/toxcore/tox_memory.h +++ /dev/null @@ -1,75 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2025 The TokTok team. - * Copyright © 2013 Tox project. - */ - -/** - * Memory allocation and deallocation functions. - */ -#ifndef C_TOXCORE_TOXCORE_TOX_MEMORY_H -#define C_TOXCORE_TOXCORE_TOX_MEMORY_H - -#include // uint*_t - -#include "tox_attributes.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** @brief Functions wrapping standard C memory allocation functions. */ -typedef struct Tox_Memory_Funcs Tox_Memory_Funcs; - -/** - * @brief A dynamic memory allocator. - */ -typedef struct Tox_Memory Tox_Memory; - -/** - * @brief Allocates a new allocator using itself to allocate its own memory. - * - * The passed `user_data` is stored and passed to allocator callbacks. It must - * outlive the `Tox_Memory` object, since it may be used by the callback invoked - * in `tox_memory_free`. - * - * @return NULL if allocation fails. - */ -Tox_Memory *_Nullable tox_memory_new(const Tox_Memory_Funcs *_Nonnull funcs, void *_Nullable user_data); - -/** - * @brief Destroys the allocator using its own deallocation function. - * - * The stored `user_data` will not be deallocated. - */ -void tox_memory_free(Tox_Memory *_Nullable mem); - -/** - * @brief Allocate an array of a given size for built-in types. - * - * The array will not be initialised. Supported built-in types are - * `uint8_t`, `int8_t`, and `int16_t`. - */ -void *_Nullable tox_memory_malloc(const Tox_Memory *_Nonnull mem, uint32_t size); - -/** - * @brief Allocate a single zero-initialised object. - * - * Always use as `(T *)tox_memory_alloc(mem, sizeof(T))`. Unlike `calloc`, this - * does not support allocating arrays. Use `malloc` and `memset` for that. - * - * @param mem The memory allocator. - * @param size Size in bytes of each element. - */ -void *_Nullable tox_memory_alloc(const Tox_Memory *_Nonnull mem, uint32_t size); - -/** @brief Resize a memory chunk vector. */ -void *_Nullable tox_memory_realloc(const Tox_Memory *_Nonnull mem, void *_Nullable ptr, uint32_t size); - -/** @brief Free an array, object, or object vector. */ -void tox_memory_dealloc(const Tox_Memory *_Nonnull mem, void *_Nullable ptr); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* C_TOXCORE_TOXCORE_TOX_MEMORY_H */ diff --git a/toxcore/tox_memory_impl.h b/toxcore/tox_memory_impl.h deleted file mode 100644 index 789c57f95..000000000 --- a/toxcore/tox_memory_impl.h +++ /dev/null @@ -1,49 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2016-2025 The TokTok team. - * Copyright © 2013 Tox project. - */ - -/** - * Datatypes, functions and includes for the core networking. - */ -#ifndef C_TOXCORE_TOXCORE_TOX_MEMORY_IMPL_H -#define C_TOXCORE_TOXCORE_TOX_MEMORY_IMPL_H - -#include // uint*_t - -#include "tox_memory.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** @brief Allocate a byte array, similar to malloc. */ -typedef void *tox_memory_malloc_cb(void *self, uint32_t size); -/** @brief Reallocate a byte array, similar to realloc. */ -typedef void *tox_memory_realloc_cb(void *self, void *ptr, uint32_t size); -/** - * @brief Deallocate a byte or object array, similar to free. - * - * Note that `tox_memory_free` will use this callback to deallocate itself, so - * once the deallocation is done, the allocator data structures can no longer be - * referenced. - */ -typedef void tox_memory_dealloc_cb(void *self, void *ptr); - -/** @brief Functions wrapping standard C memory allocation functions. */ -struct Tox_Memory_Funcs { - tox_memory_malloc_cb *malloc_callback; - tox_memory_realloc_cb *realloc_callback; - tox_memory_dealloc_cb *dealloc_callback; -}; - -struct Tox_Memory { - const Tox_Memory_Funcs *funcs; - void *user_data; -}; - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* C_TOXCORE_TOXCORE_TOX_MEMORY_IMPL_H */ diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h index d5713d58a..a7f709da8 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -25,7 +25,7 @@ typedef struct Tox_System { void *_Nullable mono_time_user_data; const struct Tox_Random *_Nullable rng; const struct Network *_Nullable ns; - const struct Tox_Memory *_Nullable mem; + const struct Memory *_Nullable mem; } Tox_System; Tox_System tox_default_system(void); diff --git a/toxcore/tox_random.c b/toxcore/tox_random.c index 8f1432d43..a086ef57f 100644 --- a/toxcore/tox_random.c +++ b/toxcore/tox_random.c @@ -4,12 +4,12 @@ #include "tox_random.h" #include "ccompat.h" -#include "tox_memory.h" +#include "mem.h" #include "tox_random_impl.h" -Tox_Random *tox_random_new(const Tox_Random_Funcs *funcs, void *user_data, const Tox_Memory *mem) +Tox_Random *tox_random_new(const Tox_Random_Funcs *funcs, void *user_data, const Memory *mem) { - Tox_Random *rng = (Tox_Random *)tox_memory_alloc(mem, sizeof(Tox_Random)); + Tox_Random *rng = (Tox_Random *)mem_alloc(mem, sizeof(Tox_Random)); if (rng == nullptr) { return nullptr; @@ -28,7 +28,7 @@ void tox_random_free(Tox_Random *rng) if (rng == nullptr || rng->mem == nullptr) { return; } - tox_memory_dealloc(rng->mem, rng); + mem_delete(rng->mem, rng); } void tox_random_bytes(const Tox_Random *rng, uint8_t *bytes, uint32_t length) diff --git a/toxcore/tox_random.h b/toxcore/tox_random.h index 3339a41cd..1c877dc28 100644 --- a/toxcore/tox_random.h +++ b/toxcore/tox_random.h @@ -8,8 +8,8 @@ #include #include -#include "tox_attributes.h" -#include "tox_memory.h" +#include "attributes.h" +#include "mem.h" #ifdef __cplusplus extern "C" { @@ -19,7 +19,7 @@ typedef struct Tox_Random_Funcs Tox_Random_Funcs; typedef struct Tox_Random Tox_Random; -Tox_Random *_Nullable tox_random_new(const Tox_Random_Funcs *_Nonnull funcs, void *_Nullable user_data, const Tox_Memory *_Nonnull mem); +Tox_Random *_Nullable tox_random_new(const Tox_Random_Funcs *_Nonnull funcs, void *_Nullable user_data, const Memory *_Nonnull mem); void tox_random_free(Tox_Random *_Nullable rng); diff --git a/toxcore/tox_random_impl.h b/toxcore/tox_random_impl.h index 8f494848c..de69895af 100644 --- a/toxcore/tox_random_impl.h +++ b/toxcore/tox_random_impl.h @@ -7,7 +7,7 @@ #include -#include "tox_memory.h" +#include "mem.h" #include "tox_random.h" #ifdef __cplusplus @@ -45,7 +45,7 @@ struct Tox_Random { const Tox_Random_Funcs *funcs; void *user_data; - const Tox_Memory *mem; + const Memory *mem; }; #ifdef __cplusplus