AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
vmem.h File Reference
#include <core/system.h>

Go to the source code of this file.

Data Structures

struct  vmem_seg
 A segment of an arena: a range that is either free or allocated. More...
struct  vmem
 An arena: a range, and its segments. More...

Macros

#define VMEM_LISTS   (sizeof(size_t) * 8)
#define VMEM_HASH   256

Functions

int vmem_init (struct vmem *vm, const char *name, uintptr_t base, size_t size, size_t quantum)
 Sets up an arena: the range [base, base + size), all free.
struct vmem_seg * vmem_alloc (struct vmem *vm, size_t size)
 Allocates a range from an arena.
struct vmem_seg * vmem_find (struct vmem *vm, uintptr_t base)
 Finds the allocated segment that starts at an address.
void vmem_free (struct vmem *vm, struct vmem_seg *seg)
 Frees an allocated segment.

Macro Definition Documentation

◆ VMEM_HASH

#define VMEM_HASH   256

Definition at line 38 of file vmem.h.

Referenced by hash_chain().

◆ VMEM_LISTS

#define VMEM_LISTS   (sizeof(size_t) * 8)

Definition at line 37 of file vmem.h.

Referenced by vmem_alloc().

Function Documentation

◆ vmem_alloc()

struct vmem_seg * vmem_alloc ( struct vmem * vm,
size_t size )

Allocates a range from an arena.

Parameters
vmthe arena
sizethe size wanted, in bytes (rounded up to a multiple of the quantum)
Returns
the allocated segment, or NULL if there is no free range that large, or no memory for the record of what's left

Definition at line 134 of file vmem.c.

References vmem_seg::base, vmem::free, vmem_seg::free, free_list(), hash_chain(), vmem::inuse, kmalloc(), list_insert(), log2_ceil(), log2_floor(), M_ZERO, NULL, vmem::quantum, seg_link(), seg_list_remove(), vmem_seg::size, vmem_alloc(), and VMEM_LISTS.

Referenced by kvmem_alloc(), and vmem_alloc().

◆ vmem_find()

struct vmem_seg * vmem_find ( struct vmem * vm,
uintptr_t base )

Finds the allocated segment that starts at an address.

Parameters
vmthe arena
basethe address
Returns
the segment, or NULL if no allocated segment starts there

Definition at line 193 of file vmem.c.

References vmem_seg::base, hash_chain(), NULL, and vmem_find().

Referenced by kfree(), and vmem_find().

◆ vmem_free()

void vmem_free ( struct vmem * vm,
struct vmem_seg * seg )

Frees an allocated segment.

It merges with the free segments next to it, so that the arena doesn't fragment. Freeing a free segment panics.

Parameters
vmthe arena
segthe segment, which vmem_alloc() gave

Definition at line 223 of file vmem.c.

References free_list(), hash_chain(), vmem::inuse, list_insert(), NULL, panic, seg_list_remove(), seg_merge(), and vmem_free().

Referenced by kfree(), kvmem_alloc(), and vmem_free().

◆ vmem_init()

int vmem_init ( struct vmem * vm,
const char * name,
uintptr_t base,
size_t size,
size_t quantum )

Sets up an arena: the range [base, base + size), all free.

Parameters
vmthe arena
nameits name, for messages
basethe range's start
sizeits size, in bytes (rounded down to a multiple of quantum)
quantumthe unit of allocation: every segment's size is a multiple of it
Returns
0, or -ENOMEM if there was no memory for the first segment

Definition at line 104 of file vmem.c.

References vmem_seg::base, free_list(), kmalloc(), list_insert(), M_ZERO, memset(), vmem::name, NULL, vmem::quantum, seg_link(), vmem::size, vmem_seg::size, and vmem_init().

Referenced by kvmem_setup(), and vmem_init().