Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960#ifndef ANT_CAGE_H#define ANT_CAGE_H
#include <stdbool.h>#include <stddef.h>#include <stdint.h>
#ifdef ANT_WASM_EMBEDstatic constexpr size_t ANT_CAGE_MAX_SIZE = 1UL << 30;static constexpr size_t ANT_CAGE_MIN_SIZE = 1UL << 24;#elsestatic constexpr size_t ANT_CAGE_MAX_SIZE = 1ULL << 35;static constexpr size_t ANT_CAGE_MIN_SIZE = 1ULL << 33;static constexpr size_t ANT_CAGE_ALIGNMENT = 1ULL << 23;#endif
extern uintptr_t ant_cage_base_address;extern size_t ant_cage_reserved_size;
void *ant_cage_alloc(size_t size, size_t align);void *ant_cage_reserve(size_t size, size_t align, size_t keep);void ant_cage_free(void *ptr, size_t size);bool ant_cage_set_size(size_t size);
_Noreturn void ant_cage_reject_pointer(const void *ptr);
static inline uintptr_t ant_cage_base(void) { return ant_cage_base_address;}
static inline bool ant_cage_contains(const void *ptr) {#ifdef ANT_WASM_EMBED return ptr != NULL;#else uintptr_t value = (uintptr_t)ptr; uintptr_t base = ant_cage_base_address; return base && value >= base && value - base < ant_cage_reserved_size;#endif}
static inline uint64_t ant_cage_encode(const void *ptr) { if (!ptr) return 0;#ifdef ANT_WASM_EMBED return (uint64_t)(uintptr_t)ptr;#else if (!ant_cage_contains(ptr)) ant_cage_reject_pointer(ptr); return (uint64_t)((uintptr_t)ptr - ant_cage_base_address);#endif}
static inline void *ant_cage_decode(uint64_t offset) {#ifdef ANT_WASM_EMBED return (void *)(uintptr_t)offset;#else return offset ? (void *)(ant_cage_base_address + offset) : NULL;#endif}
#endif