Loading...
Searching...
No Matches
mem.h
Go to the documentation of this file.
/*
* SPDX-License-Identifier: ISC
*
* Copyright (C) 2018-2020 Michael Drake <tlsa@netsurf-browser.org>
*/
#ifndef CYAML_MEM_H
#define CYAML_MEM_H
#include "cyaml/cyaml.h"
static inline void cyaml__free(
const cyaml_config_t *config,
void *ptr)
{
config->mem_fn(config->mem_ctx, ptr, 0);
}
static inline void * cyaml__realloc(
const cyaml_config_t *config,
void *ptr,
size_t current_size,
size_t new_size,
bool clean)
{
uint8_t *temp = config->mem_fn(config->mem_ctx, ptr, new_size);
if (temp == NULL) {
return NULL;
}
if (clean && (new_size > current_size)) {
memset(temp + current_size, 0, new_size - current_size);
}
return temp;
}
static inline void * cyaml__alloc(
const cyaml_config_t *config,
size_t size,
bool clean)
{
return cyaml__realloc(config, NULL, 0, size, clean);
}
static inline char * cyaml__strdup(
const cyaml_config_t *config,
const char *str,
size_t *len_out)
{
size_t len = strlen(str) + 1;
char *dup = cyaml__alloc(config, len, false);
if (dup == NULL) {
return NULL;
}
memcpy(dup, str, len);
if (len_out != NULL) {
*len_out = len - 1;
}
return dup;
}
#endif
cyaml.h
CYAML library public header.
cyaml_config_t
struct cyaml_config cyaml_config_t
cyaml__realloc
static void * cyaml__realloc(const cyaml_config_t *config, void *ptr, size_t current_size, size_t new_size, bool clean)
Definition mem.h:46
cyaml__free
static void cyaml__free(const cyaml_config_t *config, void *ptr)
Definition mem.h:23
cyaml__strdup
static char * cyaml__strdup(const cyaml_config_t *config, const char *str, size_t *len_out)
Definition mem.h:90
cyaml__alloc
static void * cyaml__alloc(const cyaml_config_t *config, size_t size, bool clean)
Definition mem.h:74
cyaml_config::mem_ctx
void * mem_ctx
Definition cyaml.h:1485
cyaml_config::mem_fn
cyaml_mem_fn_t mem_fn
Definition cyaml.h:1475