1 // SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause 2 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ 3 #include <libarena/common.h> 4 #include <libarena/asan.h> 5 #include <libarena/buddy.h> 6 7 struct buddy __arena buddy; 8 volatile u32 zero = 0; 9 10 /* 11 * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program 12 * linking the arena spinlock provides exactly one definition, so that the array 13 * is emitted once rather than once per translation unit. 14 */ 15 struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES]; 16 arena_fls(__u64 word)17int arena_fls(__u64 word) 18 { 19 if (!word) 20 return 0; 21 22 return 64 - __builtin_clzll(word); 23 } 24 25 SEC("syscall") arena_get_info(struct arena_get_info_args * args)26__weak int arena_get_info(struct arena_get_info_args *args) 27 { 28 args->arena_base = arena_base(&arena); 29 30 return 0; 31 } 32 33 SEC("syscall") arena_alloc_reserve(struct arena_alloc_reserve_args * args)34__weak int arena_alloc_reserve(struct arena_alloc_reserve_args *args) 35 { 36 return bpf_arena_reserve_pages(&arena, NULL, args->nr_pages); 37 } 38 39 SEC("syscall") arena_buddy_reset(void)40__weak int arena_buddy_reset(void) 41 { 42 buddy_destroy(&buddy); 43 44 return buddy_init(&buddy); 45 } 46 47 SEC("syscall") arena_buddy_destroy(void)48__weak int arena_buddy_destroy(void) 49 { 50 return buddy_destroy(&buddy); 51 } 52 arena_malloc(size_t size)53__weak void __arena *arena_malloc(size_t size) 54 { 55 return buddy_alloc(&buddy, size); 56 } 57 arena_free(void __arena * ptr)58__weak void arena_free(void __arena *ptr) 59 { 60 buddy_free(&buddy, ptr); 61 } 62 63 64 char _license[] SEC("license") = "GPL"; 65