CYAML Internals
src
mem.h
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: ISC
3
*
4
* Copyright (C) 2018 Michael Drake <tlsa@netsurf-browser.org>
5
*/
6
12
#ifndef CYAML_MEM_H
13
#define CYAML_MEM_H
14
15
#include "
cyaml/cyaml.h
"
16
23
static
inline
void
cyaml__free
(
24
const
cyaml_config_t
*config,
25
void
*ptr)
26
{
27
config->
mem_fn
(config->
mem_ctx
, ptr, 0);
28
}
29
46
static
inline
void
*
cyaml__realloc
(
47
const
cyaml_config_t
*config,
48
void
*ptr,
49
size_t
current_size,
50
size_t
new_size,
51
bool
clean)
52
{
53
uint8_t *temp = config->
mem_fn
(config->
mem_ctx
, ptr, new_size);
54
if
(temp == NULL) {
55
return
NULL;
56
}
57
58
if
(clean && (new_size > current_size)) {
59
memset(temp + current_size, 0, new_size - current_size);
60
}
61
62
return
temp;
63
}
64
74
static
inline
void
*
cyaml__alloc
(
75
const
cyaml_config_t
*config,
76
size_t
size,
77
bool
clean)
78
{
79
return
cyaml__realloc
(config, NULL, 0, size, clean);
80
}
81
89
static
inline
char
*
cyaml__strdup
(
90
const
cyaml_config_t
*config,
91
const
char
*str)
92
{
93
size_t
len = strlen(str) + 1;
94
char
*dup =
cyaml__alloc
(config, len,
false
);
95
if
(dup == NULL) {
96
return
NULL;
97
}
98
99
memcpy(dup, str, len);
100
return
dup;
101
}
102
103
#endif
cyaml_config
Definition:
cyaml.h:1359
cyaml__strdup
static char * cyaml__strdup(const cyaml_config_t *config, const char *str)
Definition:
mem.h:89
cyaml__free
static void cyaml__free(const cyaml_config_t *config, void *ptr)
Definition:
mem.h:23
cyaml.h
CYAML library public header.
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_config::mem_ctx
void * mem_ctx
Definition:
cyaml.h:1405
cyaml_config::mem_fn
cyaml_mem_fn_t mem_fn
Definition:
cyaml.h:1395
cyaml__alloc
static void * cyaml__alloc(const cyaml_config_t *config, size_t size, bool clean)
Definition:
mem.h:74
Generated by
1.8.17