60 lines
1.7 KiB
C
60 lines
1.7 KiB
C
#ifndef __NICKEL_VM_H
|
||
#define __NICKEL_VM_H
|
||
|
||
#include <stddef.h>
|
||
#include <stdint.h>
|
||
#include <stdbool.h>
|
||
|
||
#ifdef __cplusplus
|
||
extern "C" {
|
||
#endif
|
||
|
||
/* VM layer: portable-ish page allocation based on mmap/munmap (Linux + macOS). */
|
||
|
||
typedef struct ni_vm_stats {
|
||
size_t page_size;
|
||
|
||
/* cumulative counters */
|
||
uint64_t reserves; /* number of reserve calls */
|
||
uint64_t releases; /* number of release calls */
|
||
uint64_t commits; /* if supported by impl (optional), else 0 */
|
||
uint64_t decommits; /* if supported by impl (optional), else 0 */
|
||
|
||
size_t bytes_reserved; /* current bytes reserved */
|
||
size_t bytes_peak; /* peak reserved */
|
||
} ni_vm_stats;
|
||
|
||
/* Returns OS page size (usually sysconf(_SC_PAGESIZE)). Never returns 0. */
|
||
size_t ni_vm_page_size(void);
|
||
|
||
/* Reserve anonymous virtual memory.
|
||
- bytes will be rounded up to a page multiple.
|
||
- returns NULL on failure.
|
||
*/
|
||
void* ni_vm_reserve(size_t bytes);
|
||
|
||
/* Release a region previously returned by ni_vm_reserve (page-multiple size). */
|
||
bool ni_vm_release(void* addr, size_t bytes);
|
||
|
||
/* Optional: “commit/decommit” for platforms where it’s meaningful.
|
||
On many Unix systems, reserve implies commit; implementations may no-op.
|
||
*/
|
||
bool ni_vm_commit(void* addr, size_t bytes);
|
||
bool ni_vm_decommit(void* addr, size_t bytes);
|
||
|
||
/* Optional: set guard pages (mprotect) for debug.
|
||
Returns false if unsupported or fails.
|
||
*/
|
||
bool ni_vm_protect_none(void* addr, size_t bytes);
|
||
bool ni_vm_protect_rw(void* addr, size_t bytes);
|
||
|
||
/* Stats are optional but very useful for tests/tools. */
|
||
void ni_vm_get_stats(ni_vm_stats* out_stats);
|
||
void ni_vm_reset_stats(void);
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|
||
|
||
#endif /* __NICKEL_VM_H */
|