Seregon/zftpd

Zero-copy FTP/HTTP Daemon compatible with all POSIX systems

C/11.0 KB/No license
include/pal_alloc.h
zftpd / include / pal_alloc.h
1/*
2MIT License
3 
4Copyright (c) 2026 Seregon
5 
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12 
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15 
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24/**
25 * @file pal_alloc.h
26 * @brief Platform Abstraction Layer - Memory Allocation
27 *
28 * @author Seregon
29 * @version 1.0.0
30 *
31 * PLATFORMS: FreeBSD (PS4/PS5 kqueue), Linux (epoll)
32 * DESIGN: Single-threaded, non-blocking I/O
33 *
34 */
35#ifndef PAL_ALLOC_H
36#define PAL_ALLOC_H
37 
38#include <stddef.h>
39#include <stdatomic.h>
40#include <stdint.h>
41 
42#ifndef PAL_ALLOC_HARD_FAIL
43#define PAL_ALLOC_HARD_FAIL 0
44#endif
45 
46typedef struct {
47 uint64_t alloc_calls;
48 uint64_t free_calls;
49 uint64_t calloc_calls;
50 uint64_t realloc_calls;
51 uint64_t aligned_calls;
52 uint64_t failures;
53 uint64_t bytes_in_use;
54 uint64_t bytes_peak;
55} pal_alloc_stats_t;
56 
57typedef struct pal_allocator {
58 uint8_t *base;
59 size_t size;
60 uint32_t max_order;
61 atomic_flag lock;
62 atomic_int initialized;
63 atomic_uint_fast64_t alloc_calls;
64 atomic_uint_fast64_t free_calls;
65 atomic_uint_fast64_t calloc_calls;
66 atomic_uint_fast64_t realloc_calls;
67 atomic_uint_fast64_t aligned_calls;
68 atomic_uint_fast64_t failures;
69 atomic_uint_fast64_t bytes_in_use;
70 atomic_uint_fast64_t bytes_peak;
71 void *free_lists[(26U - 5U) + 1U];
72} pal_allocator_t;
73 
74int pal_allocator_init(pal_allocator_t *a, void *buffer, size_t size);
75void pal_allocator_get_stats(pal_allocator_t *a, pal_alloc_stats_t *out);
76void pal_allocator_reset_stats(pal_allocator_t *a);
77void *pal_allocator_malloc(pal_allocator_t *a, size_t size);
78void pal_allocator_free(pal_allocator_t *a, void *ptr);
79void *pal_allocator_calloc(pal_allocator_t *a, size_t nmemb, size_t size);
80void *pal_allocator_realloc(pal_allocator_t *a, void *ptr, size_t size);
81void *pal_allocator_aligned_alloc(pal_allocator_t *a, size_t alignment, size_t size);
82int pal_allocator_posix_memalign(pal_allocator_t *a, void **memptr, size_t alignment, size_t size);
83 
84int pal_alloc_init(void *buffer, size_t size);
85int pal_alloc_init_default(void);
86size_t pal_alloc_arena_size(void);
87size_t pal_alloc_arena_free_approx(void);
88void pal_alloc_get_stats(pal_alloc_stats_t *out);
89void pal_alloc_reset_stats(void);
90 
91void *pal_malloc(size_t size);
92void pal_free(void *ptr);
93void *pal_calloc(size_t nmemb, size_t size);
94void *pal_realloc(void *ptr, size_t size);
95void *pal_aligned_alloc(size_t alignment, size_t size);
96int pal_posix_memalign(void **memptr, size_t alignment, size_t size);
97 
98#endif
99