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 | /** |
| 26 | * @file pal_scratch.c |
| 27 | * @brief Platform Abstraction Layer - Scratchpad Implementation |
| 28 | * |
| 29 | * @author SeregonWar |
| 30 | * @version 1.0.0 |
| 31 | * @date 2026-02-13 |
| 32 | * |
| 33 | */ |
| 34 | #include "pal_scratch.h" |
| 35 | |
| 36 | #include <stdatomic.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #ifndef PAL_SCRATCH_SIZE |
| 40 | #define PAL_SCRATCH_SIZE (1024U * 1024U) |
| 41 | #endif |
| 42 | |
| 43 | static _Alignas(4096) uint8_t g_scratch[PAL_SCRATCH_SIZE]; |
| 44 | static atomic_int g_scratch_busy = ATOMIC_VAR_INIT(0); |
| 45 | static atomic_int g_scratch_prefaulted = ATOMIC_VAR_INIT(0); |
| 46 | |
| 47 | static void pal_scratch_prefault_once(void) |
| 48 | { |
| 49 | int expected = 0; |
| 50 | if (!atomic_compare_exchange_strong(&g_scratch_prefaulted, &expected, 1)) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | for (size_t i = 0U; i < (size_t)PAL_SCRATCH_SIZE; i += 4096U) { |
| 55 | g_scratch[i] = 0U; |
| 56 | } |
| 57 | g_scratch[PAL_SCRATCH_SIZE - 1U] = 0U; |
| 58 | } |
| 59 | |
| 60 | int pal_scratch_acquire(uint8_t **out, size_t need) |
| 61 | { |
| 62 | if (out == NULL) { |
| 63 | return -1; |
| 64 | } |
| 65 | *out = NULL; |
| 66 | |
| 67 | if ((need == 0U) || (need > (size_t)PAL_SCRATCH_SIZE)) { |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | pal_scratch_prefault_once(); |
| 72 | |
| 73 | int expected = 0; |
| 74 | if (!atomic_compare_exchange_strong(&g_scratch_busy, &expected, 1)) { |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | memset(g_scratch, 0, need); |
| 79 | *out = g_scratch; |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | void pal_scratch_release(uint8_t *ptr) |
| 84 | { |
| 85 | if (ptr != g_scratch) { |
| 86 | return; |
| 87 | } |
| 88 | atomic_store(&g_scratch_busy, 0); |
| 89 | } |
| 90 | |
| 91 | size_t pal_scratch_capacity(void) |
| 92 | { |
| 93 | return (size_t)PAL_SCRATCH_SIZE; |
| 94 | } |
| 95 |