AquilaOS documentation
How it works, and its code
Toggle main menu visibility
Loading...
Searching...
No Matches
vmem.h
Go to the documentation of this file.
1
#ifndef _MM_VMEM_H
2
#define _MM_VMEM_H
3
4
/*
5
* vmem allocates ranges of addresses (or of anything numbered), as
6
* Bonwick and Adams describe it in "Magazines and Vmem" (2001), and as
7
* Solaris, NetBSD and FreeBSD have it, without its caches.
8
*
9
* An arena is a range cut into segments, in address order, some free and
10
* some allocated. The free segments are on lists by size: list i has
11
* those of 2^i to 2^(i+1) - 1 quanta. A request for n quanta takes the
12
* first segment of the first non-empty list whose segments are all big
13
* enough (an "instant fit"), and what's left of that segment stays free.
14
* The allocated segments are in a hash table by their start, which is all
15
* that freeing one needs. A freed segment merges with the free segments
16
* next to it.
17
*/
18
19
#include <
core/system.h
>
20
21
struct
malloc_type
;
22
24
struct
vmem_seg
{
25
uintptr_t
base
;
26
size_t
size
;
/* in bytes */
27
int
free
;
28
29
struct
vmem_seg
*
prev
, *
next
;
/* the arena's segments, in address order */
30
struct
vmem_seg
*
lprev
, *
lnext
;
/* its free list, or its hash chain */
31
32
/* what the allocation is, for its user (kvmem: kmalloc()'s object) */
33
struct
malloc_type
*
type
;
34
size_t
bytes
;
35
};
36
37
#define VMEM_LISTS (sizeof(size_t) * 8)
38
#define VMEM_HASH 256
39
41
struct
vmem
{
42
const
char
*
name
;
43
size_t
quantum
;
/* every size is a multiple of it */
44
45
struct
vmem_seg
*
segs
;
/* the first segment, in address order */
46
struct
vmem_seg
*
free
[
VMEM_LISTS
];
/* the free segments, by size */
47
struct
vmem_seg
*
hash
[
VMEM_HASH
];
/* the allocated segments, by start */
48
49
size_t
size
;
/* the arena's size */
50
size_t
inuse
;
/* how much of it is allocated */
51
};
52
53
/* Sets up an arena: [base, base + size), all free. */
54
int
vmem_init
(
struct
vmem
*vm,
const
char
*name,
uintptr_t
base
,
size_t
size
,
size_t
quantum);
55
56
/* Allocates a segment of `size` bytes, rounded up to quanta. */
57
struct
vmem_seg
*
vmem_alloc
(
struct
vmem
*vm,
size_t
size
);
58
59
/* Finds the allocated segment that starts at `base`. */
60
struct
vmem_seg
*
vmem_find
(
struct
vmem
*vm,
uintptr_t
base
);
61
62
/* Frees an allocated segment. */
63
void
vmem_free
(
struct
vmem
*vm,
struct
vmem_seg
*
seg
);
64
65
#endif
/* ! _MM_VMEM_H */
system.h
uintptr_t
__SIZE_TYPE__ uintptr_t
Definition
stddef.h:12
malloc_type
A malloc type: what an allocation is for, which /proc/kvmem counts by.
Definition
kvmem.h:14
seg
A received segment, with its header in host byte order.
Definition
tcp_input.c:26
vmem_seg
A segment of an arena: a range that is either free or allocated.
Definition
vmem.h:24
vmem_seg::type
struct malloc_type * type
Definition
vmem.h:33
vmem_seg::base
uintptr_t base
Definition
vmem.h:25
vmem_seg::free
int free
Definition
vmem.h:27
vmem_seg::bytes
size_t bytes
Definition
vmem.h:34
vmem_seg::size
size_t size
Definition
vmem.h:26
vmem_seg::lprev
struct vmem_seg * lprev
Definition
vmem.h:30
vmem_seg::prev
struct vmem_seg * prev
Definition
vmem.h:29
vmem_seg::next
struct vmem_seg * next
Definition
vmem.h:29
vmem_seg::lnext
struct vmem_seg * lnext
Definition
vmem.h:30
vmem
An arena: a range, and its segments.
Definition
vmem.h:41
vmem::free
struct vmem_seg * free[VMEM_LISTS]
Definition
vmem.h:46
vmem::segs
struct vmem_seg * segs
Definition
vmem.h:45
vmem::size
size_t size
Definition
vmem.h:49
vmem::name
const char * name
Definition
vmem.h:42
vmem::hash
struct vmem_seg * hash[VMEM_HASH]
Definition
vmem.h:47
vmem::quantum
size_t quantum
Definition
vmem.h:43
vmem::inuse
size_t inuse
Definition
vmem.h:50
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.
Definition
vmem.c:104
vmem_alloc
struct vmem_seg * vmem_alloc(struct vmem *vm, size_t size)
Allocates a range from an arena.
Definition
vmem.c:134
VMEM_LISTS
#define VMEM_LISTS
Definition
vmem.h:37
vmem_free
void vmem_free(struct vmem *vm, struct vmem_seg *seg)
Frees an allocated segment.
Definition
vmem.c:223
VMEM_HASH
#define VMEM_HASH
Definition
vmem.h:38
vmem_find
struct vmem_seg * vmem_find(struct vmem *vm, uintptr_t base)
Finds the allocated segment that starts at an address.
Definition
vmem.c:193
kernel
include
mm
vmem.h
Generated by
1.18.0