CYAML Internals
mem.h
Go to the documentation of this file.
1 /*
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (C) 2018-2020 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 
90 static inline char * cyaml__strdup(
91  const cyaml_config_t *config,
92  const char *str,
93  size_t *len_out)
94 {
95  size_t len = strlen(str) + 1;
96  char *dup = cyaml__alloc(config, len, false);
97  if (dup == NULL) {
98  return NULL;
99  }
100 
101  memcpy(dup, str, len);
102 
103  if (len_out != NULL) {
104  *len_out = len - 1;
105  }
106  return dup;
107 }
108 
109 #endif
CYAML library public header.
static void * cyaml__alloc(const cyaml_config_t *config, size_t size, bool clean)
Definition: mem.h:74
static void cyaml__free(const cyaml_config_t *config, void *ptr)
Definition: mem.h:23
static char * cyaml__strdup(const cyaml_config_t *config, const char *str, size_t *len_out)
Definition: mem.h:90
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
Definition: cyaml.h:1419
void * mem_ctx
Definition: cyaml.h:1465
cyaml_mem_fn_t mem_fn
Definition: cyaml.h:1455