mastodont-c/src/hooks.c

47 lines
1.4 KiB
C

/*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <assert.h>
#include <cjson/cJSON.h>
#include <mastodont_hooks.h>
struct mstdnt_hooks _mstdnt_hooks_def = {
.malloc = malloc,
.free = free,
.calloc = calloc,
.realloc = realloc,
};
void mstdnt_set_hooks(struct mstdnt_hooks* hooks)
{
assert(hooks != NULL);
cJSON_Hooks cjson_hooks = {
.malloc_fn = hooks->malloc,
.free_fn = hooks->free,
};
cJSON_InitHooks(&cjson_hooks);
if (hooks->malloc)
_mstdnt_hooks_def.malloc = hooks->malloc;
if (hooks->free)
_mstdnt_hooks_def.free = hooks->free;
if (hooks->calloc)
_mstdnt_hooks_def.calloc = hooks->calloc;
if (hooks->realloc)
_mstdnt_hooks_def.realloc = hooks->realloc;
}