Set hooks function

FossilOrigin-Name: 6e10de2914a965ef1a5ed7f06db7083c6c1a139b8ec9a835f56a826e1d8b9384
This commit is contained in:
nekobit 2022-10-15 20:44:53 +00:00
parent 5287e5c824
commit cef85c7d77
3 changed files with 24 additions and 6 deletions

View File

@ -15,6 +15,7 @@
#ifndef MASTODONT_H
#define MASTODONT_H
#include <mastodont_hooks.h>
#include <mastodont_types.h>
#include <mastodont_timeline.h>
#include <mastodont_list.h>
@ -49,9 +50,6 @@ int mstdnt_init(mstdnt_t* data);
*/
void mstdnt_cleanup(mstdnt_t* data);
//! Call's free() or a library derivative
void mstdnt_free(void*);
/*!
* Cleans up a storage struct.
*

View File

@ -22,7 +22,7 @@
#define mstdnt_calloc (*_mstdnt_hooks_def.calloc)
#define mstdnt_realloc (*_mstdnt_hooks_def.realloc)
struct _mstdnt_hooks
struct mstdnt_hooks
{
void* (*malloc)(size_t size);
void (*free)(void* ptr);
@ -30,7 +30,14 @@ struct _mstdnt_hooks
void* (*realloc)(void* ptr, size_t size);
};
/**
* Sets up mstdnt_hooks (and cJSON hooks)
*
* @param data Pointer to the mstdnt data
*/
void mstdnt_set_hooks(struct mstdnt_hooks* data);
/** Hooks for Mastodont functions */
extern struct _mstdnt_hooks _mstdnt_hooks_def;
extern struct mstdnt_hooks _mstdnt_hooks_def;
#endif // MASTODONT_HOOKS_H

View File

@ -14,11 +14,24 @@
*/
#include <stdlib.h>
#include <cjson/cJSON.h>
#include <mastodont_hooks.h>
struct _mstdnt_hooks _mstdnt_hooks_def = {
struct mstdnt_hooks _mstdnt_hooks_def = {
.malloc = malloc,
.free = free,
.calloc = calloc,
.realloc = realloc,
};
void mstdnt_set_hooks(struct mstdnt_hooks* hooks)
{
cJSON_Hooks cjson_hooks = {
.malloc_fn = hooks->malloc,
.free_fn = hooks->free,
};
cJSON_InitHooks(&hooks);
_mstdnt_hooks_def = *hooks;
}