1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
* libfyaml-test-allocator.c - libfyaml test public allocator interface
*
* Copyright (c) 2024 Pantelis Antoniou <pantelis.antoniou@konsulko.com>
*
* SPDX-License-Identifier: MIT
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include <unistd.h>
#include <limits.h>
#include <stdint.h>
#include <check.h>
#include <libfyaml.h>
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0])))
#endif
static const char *builtin_allocators[] = {
"linear",
"malloc",
"mremap",
"dedup",
"auto",
NULL,
};
START_TEST(allocator_builtins)
{
const char **pp, *p;
/* verify that all the builtins are available */
pp = builtin_allocators;
while ((p = *pp++) != NULL) {
fprintf(stderr, "checking builtin allocator: %s\n", p);
ck_assert(fy_allocator_is_available(p));
}
}
END_TEST
static void test_allocator_alignment(struct fy_allocator *a, int tag)
{
size_t sz, align;
void *p;
/* 1, 2, 4, 8, 16 bytes allocations */
for (sz = 1; sz <= 16; sz <<= 1) {
align = sz;
/* allocate and check alignment */
p = fy_allocator_alloc(a, tag, sz, align);
ck_assert_ptr_ne(p, NULL);
ck_assert(((uintptr_t)p & (align - 1)) == 0);
}
}
START_TEST(allocator_linear_buf)
{
/* align to 64 bits */
union {
char buf[1024];
uint64_t dummy;
} u;
struct fy_linear_allocator_cfg lcfg;
struct fy_allocator *a = NULL;
int tag = -1;
void *p;
memset(&lcfg, 0, sizeof(lcfg));
lcfg.buf = u.buf;
lcfg.size = sizeof(u.buf);
/* create */
a = fy_allocator_create("linear", &lcfg);
ck_assert_ptr_ne(a, NULL);
/* get the tag */
tag = fy_allocator_get_tag(a);
ck_assert_int_ne(tag, -1);
test_allocator_alignment(a, tag);
/* allocate something too large to fit */
p = fy_allocator_alloc(a, tag, sizeof(u.buf) + 1, 16);
ck_assert_ptr_eq(p, NULL);
/* destroy */
fy_allocator_destroy(a);
}
END_TEST
START_TEST(allocator_linear_alloc)
{
struct fy_linear_allocator_cfg lcfg;
struct fy_allocator *a = NULL;
int tag = -1;
void *p;
memset(&lcfg, 0, sizeof(lcfg));
lcfg.size = 1024;
/* create */
a = fy_allocator_create("linear", &lcfg);
ck_assert_ptr_ne(a, NULL);
/* get the tag */
tag = fy_allocator_get_tag(a);
ck_assert_int_ne(tag, -1);
test_allocator_alignment(a, tag);
/* allocate something too large to fit */
p = fy_allocator_alloc(a, tag, 1024 + 1, 16);
ck_assert_ptr_eq(p, NULL);
/* destroy */
fy_allocator_destroy(a);
}
END_TEST
START_TEST(allocator_malloc)
{
struct fy_allocator *a = NULL;
int tag0 = -1, tag1 = -1;
/* create */
a = fy_allocator_create("malloc", NULL);
ck_assert_ptr_ne(a, NULL);
/* get the first tag */
tag0 = fy_allocator_get_tag(a);
ck_assert_int_ne(tag0, -1);
/* get the second tag */
tag1 = fy_allocator_get_tag(a);
ck_assert_int_ne(tag1, -1);
/* tags must be different */
ck_assert_int_ne(tag0, tag1);
test_allocator_alignment(a, tag0);
test_allocator_alignment(a, tag1);
/* destroy */
fy_allocator_destroy(a);
}
END_TEST
START_TEST(allocator_mremap)
{
struct fy_allocator *a = NULL;
int tag0 = -1, tag1 = -1;
/* create (default everything) */
a = fy_allocator_create("mremap", NULL);
ck_assert_ptr_ne(a, NULL);
/* get the first tag */
tag0 = fy_allocator_get_tag(a);
ck_assert_int_ne(tag0, -1);
/* get the second tag */
tag1 = fy_allocator_get_tag(a);
ck_assert_int_ne(tag1, -1);
/* tags must be different */
ck_assert_int_ne(tag0, tag1);
test_allocator_alignment(a, tag0);
test_allocator_alignment(a, tag1);
/* destroy */
fy_allocator_destroy(a);
}
END_TEST
static inline bool
scenario_is_single_tagged(int scenario)
{
return scenario >= FYAST_SINGLE_LINEAR_RANGE &&
scenario <= FYAST_SINGLE_LINEAR_RANGE_DEDUP;
}
static inline bool
scenario_is_dedup(int scenario)
{
return scenario == FYAST_PER_TAG_FREE_DEDUP ||
scenario == FYAST_PER_OBJ_FREE_DEDUP ||
scenario == FYAST_SINGLE_LINEAR_RANGE_DEDUP;
}
static inline bool
scenario_is_fixed_size(int scenario)
{
return scenario >= FYAST_SINGLE_LINEAR_RANGE &&
scenario <= FYAST_SINGLE_LINEAR_RANGE_DEDUP;
}
START_TEST(allocator_auto)
{
static const struct {
int scenario;
const char *name;
} auto_scenarios[] = {
{
.scenario = FYAST_PER_TAG_FREE,
.name = "per-tag-free",
}, {
.scenario = FYAST_PER_TAG_FREE_DEDUP,
.name = "per-tag-free-dedup",
}, {
.scenario = FYAST_PER_OBJ_FREE,
.name = "per-obj-free",
}, {
.scenario = FYAST_PER_OBJ_FREE_DEDUP,
.name = "per-obj-free-dedup",
}, {
.scenario = FYAST_SINGLE_LINEAR_RANGE,
.name = "single-linear-range",
}, {
.scenario = FYAST_SINGLE_LINEAR_RANGE_DEDUP,
.name = "single-linear-range-dedup",
}
};
struct fy_auto_allocator_cfg acfg;
struct fy_allocator *a = NULL;
enum fy_auto_allocator_scenario_type sc;
int tag0 = -1, tag1 = -1;
unsigned int i;
/* create (default everything) */
for (i = 0; i < ARRAY_SIZE(auto_scenarios); i++) {
printf("scenario #%d %s\n", i, auto_scenarios[i].name);
sc = auto_scenarios[i].scenario;
memset(&acfg, 0, sizeof(acfg));
acfg.scenario = sc;
/* for fixed sizes, make it 1MB */
if (scenario_is_fixed_size(sc))
acfg.estimated_max_size = 1 << 20;
a = fy_allocator_create("auto", &acfg);
ck_assert_ptr_ne(a, NULL);
/* get the first tag */
tag0 = fy_allocator_get_tag(a);
ck_assert_int_ne(tag0, -1);
if (!scenario_is_single_tagged(sc)) {
/* get the second tag */
tag1 = fy_allocator_get_tag(a);
ck_assert_int_ne(tag1, -1);
/* tags must be different */
ck_assert_int_ne(tag0, tag1);
}
test_allocator_alignment(a, tag0);
if (!scenario_is_single_tagged(sc))
test_allocator_alignment(a, tag1);
/* destroy */
fy_allocator_destroy(a);
a = NULL;
}
}
END_TEST
START_TEST(allocator_linear_inplace)
{
struct fy_allocator *a;
char buf[FY_LINEAR_ALLOCATOR_IN_PLACE_MIN_SIZE];
int tag;
const void *p;
a = fy_linear_allocator_create_in_place(buf, sizeof(buf));
ck_assert_ptr_ne(a, NULL);
tag = fy_allocator_get_tag(a);
ck_assert_int_ne(tag, -1);
p = fy_allocator_store(a, tag, "Hello", 6, 1);
ck_assert_ptr_ne(p, NULL);
ck_assert(!strcmp(p, "Hello"));
/* no release */
}
END_TEST
START_TEST(allocator_dedup_inplace)
{
struct fy_allocator *a;
char buf[FY_DEDUP_ALLOCATOR_IN_PLACE_MIN_SIZE];
int tag;
const void *p1, *p2;
a = fy_dedup_allocator_create_in_place(buf, sizeof(buf));
ck_assert_ptr_ne(a, NULL);
tag = fy_allocator_get_tag(a);
ck_assert_int_ne(tag, -1);
p1 = fy_allocator_store(a, tag, "Hello", 6, 1);
ck_assert_ptr_ne(p1, NULL);
ck_assert(!strcmp(p1, "Hello"));
p2 = fy_allocator_store(a, tag, "Hello", 6, 1);
ck_assert_ptr_ne(p2, NULL);
ck_assert(!strcmp(p2, "Hello"));
/* dedup must return the same pointer */
ck_assert(p1 == p2);
/* no release */
}
END_TEST
TCase *libfyaml_case_allocator(void)
{
TCase *tc;
tc = tcase_create("allocator");
tcase_add_test(tc, allocator_builtins);
tcase_add_test(tc, allocator_linear_buf);
tcase_add_test(tc, allocator_linear_alloc);
tcase_add_test(tc, allocator_malloc);
tcase_add_test(tc, allocator_mremap);
tcase_add_test(tc, allocator_auto);
tcase_add_test(tc, allocator_linear_inplace);
tcase_add_test(tc, allocator_dedup_inplace);
return tc;
}
|