Zero-copy FTP/HTTP Daemon compatible with all POSIX systems
| 1 | /* |
| 2 | MIT License |
| 3 | |
| 4 | Copyright (c) 2026 Seregon |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all |
| 14 | copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 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 | |
| 46 | typedef 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 | |
| 57 | typedef 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 | |
| 74 | int pal_allocator_init(pal_allocator_t *a, void *buffer, size_t size); |
| 75 | void pal_allocator_get_stats(pal_allocator_t *a, pal_alloc_stats_t *out); |
| 76 | void pal_allocator_reset_stats(pal_allocator_t *a); |
| 77 | void *pal_allocator_malloc(pal_allocator_t *a, size_t size); |
| 78 | void pal_allocator_free(pal_allocator_t *a, void *ptr); |
| 79 | void *pal_allocator_calloc(pal_allocator_t *a, size_t nmemb, size_t size); |
| 80 | void *pal_allocator_realloc(pal_allocator_t *a, void *ptr, size_t size); |
| 81 | void *pal_allocator_aligned_alloc(pal_allocator_t *a, size_t alignment, size_t size); |
| 82 | int pal_allocator_posix_memalign(pal_allocator_t *a, void **memptr, size_t alignment, size_t size); |
| 83 | |
| 84 | int pal_alloc_init(void *buffer, size_t size); |
| 85 | int pal_alloc_init_default(void); |
| 86 | size_t pal_alloc_arena_size(void); |
| 87 | size_t pal_alloc_arena_free_approx(void); |
| 88 | void pal_alloc_get_stats(pal_alloc_stats_t *out); |
| 89 | void pal_alloc_reset_stats(void); |
| 90 | |
| 91 | void *pal_malloc(size_t size); |
| 92 | void pal_free(void *ptr); |
| 93 | void *pal_calloc(size_t nmemb, size_t size); |
| 94 | void *pal_realloc(void *ptr, size_t size); |
| 95 | void *pal_aligned_alloc(size_t alignment, size_t size); |
| 96 | int pal_posix_memalign(void **memptr, size_t alignment, size_t size); |
| 97 | |
| 98 | #endif |
| 99 |