cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
mem_os.h File Reference

Cross-platform wrappers for OS-level virtual memory management. More...

#include "base/base_macros.h"
#include "base/base_types.h"
Include dependency graph for mem_os.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

usize mem_os_page_size (void)
 Retrieves the operating system's virtual memory page size.
void * mem_os_reserve (usize size)
 Reserves a contiguous block of virtual address space.
b32 mem_os_commit (void *ptr, usize size)
 Commits physical RAM to a previously reserved virtual address block.
void mem_os_decommit (void *ptr, usize size)
 Decommits physical RAM, returning it to the OS.
void mem_os_release (void *ptr, usize size)
 Releases a reserved virtual address block entirely back to the OS.

Detailed Description

Cross-platform wrappers for OS-level virtual memory management.

Provides a unified API over Windows VirtualAlloc and POSIX mmap. Separates the concepts of reserving virtual address space from committing physical RAM pages. This is the lowest layer of the memory subsystem — most application code should use MemArena rather than calling these directly.

Reserve vs Commit
Virtual memory operates in two phases:
  • Reserve: Claims a range of virtual address space. Uses no physical RAM. Near-instant regardless of size. Think of it as calling dibs on addresses.
  • Commit: Maps physical RAM to a sub-range of a reserved block. Only committed pages can be read or written.
Usage
// Reserve 1 GB (free on all modern OSes, no physical RAM used):
void *mem = mem_os_reserve(GB(1));
// Commit only the first 64 KB (physical RAM now used):
mem_os_commit(mem, KB(64));
// Use the committed memory:
int *buf = (int *)mem;
buf[0] = 42;
// Return physical RAM but keep the address space:
mem_os_decommit(mem, KB(64));
// Release the entire reservation:
mem_os_release(mem, GB(1));
#define KB(x)
Definition base_types.h:119
#define GB(x)
Definition base_types.h:121
void mem_os_release(void *ptr, usize size)
Releases a reserved virtual address block entirely back to the OS.
Definition mem_os.c:50
b32 mem_os_commit(void *ptr, usize size)
Commits physical RAM to a previously reserved virtual address block.
Definition mem_os.c:29
void * mem_os_reserve(usize size)
Reserves a contiguous block of virtual address space.
Definition mem_os.c:18
void mem_os_decommit(void *ptr, usize size)
Decommits physical RAM, returning it to the OS.
Definition mem_os.c:39

Definition in file mem_os.h.