mirror of
https://github.com/TokTok/c-toxcore
synced 2026-05-24 17:25:34 +00:00
refactor: Delete tox_memory, align on mem and os_memory.
os_memory will be os_mem later.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <cstdint>
|
||||
|
||||
// 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
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <sys/socket.h>
|
||||
#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;
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
#include <sys/socket.h>
|
||||
#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;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,13 +4,13 @@
|
||||
#include <iostream>
|
||||
#include <new>
|
||||
|
||||
#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<FakeMemory *>(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(); }
|
||||
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
+2
-17
@@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
@@ -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 \
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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<GC_Announce> announces_;
|
||||
Logger *logger_ = nullptr;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
+7
-10
@@ -8,23 +8,22 @@
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
|
||||
+25
-3
@@ -12,13 +12,31 @@
|
||||
#include <stdint.h> // 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 */
|
||||
|
||||
|
||||
@@ -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<std::uint8_t> &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<std::uint8_t> &data) {
|
||||
|
||||
+4
-5
@@ -6,8 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
+4
-3
@@ -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 */
|
||||
|
||||
|
||||
@@ -22,14 +22,14 @@ struct Ping_Array_Deleter {
|
||||
using Ping_Array_Ptr = std::unique_ptr<Ping_Array, Ping_Array_Deleter>;
|
||||
|
||||
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<Mono_Time, Mono_Time_Deleter>;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
#include "sort.h"
|
||||
|
||||
struct Tox_Memory;
|
||||
struct Memory;
|
||||
|
||||
template <typename T>
|
||||
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<std::uint32_t, 8> compare_value;
|
||||
const char *name;
|
||||
|
||||
|
||||
@@ -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 <string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
@@ -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 <stdint.h> // 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 */
|
||||
@@ -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 <stdint.h> // 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 */
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user