AquilaOS documentation
How it works, and its code
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
21struct malloc_type;
22
24struct vmem_seg {
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) */
34 size_t bytes;
35};
36
37#define VMEM_LISTS (sizeof(size_t) * 8)
38#define VMEM_HASH 256
39
41struct 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. */
54int 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. */
57struct vmem_seg *vmem_alloc(struct vmem *vm, size_t size);
58
59/* Finds the allocated segment that starts at `base`. */
60struct vmem_seg *vmem_find(struct vmem *vm, uintptr_t base);
61
62/* Frees an allocated segment. */
63void vmem_free(struct vmem *vm, struct vmem_seg *seg);
64
65#endif /* ! _MM_VMEM_H */
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
A malloc type: what an allocation is for, which /proc/kvmem counts by.
Definition kvmem.h:14
A received segment, with its header in host byte order.
Definition tcp_input.c:26
A segment of an arena: a range that is either free or allocated.
Definition vmem.h:24
struct malloc_type * type
Definition vmem.h:33
uintptr_t base
Definition vmem.h:25
int free
Definition vmem.h:27
size_t bytes
Definition vmem.h:34
size_t size
Definition vmem.h:26
struct vmem_seg * lprev
Definition vmem.h:30
struct vmem_seg * prev
Definition vmem.h:29
struct vmem_seg * next
Definition vmem.h:29
struct vmem_seg * lnext
Definition vmem.h:30
An arena: a range, and its segments.
Definition vmem.h:41
struct vmem_seg * free[VMEM_LISTS]
Definition vmem.h:46
struct vmem_seg * segs
Definition vmem.h:45
size_t size
Definition vmem.h:49
const char * name
Definition vmem.h:42
struct vmem_seg * hash[VMEM_HASH]
Definition vmem.h:47
size_t quantum
Definition vmem.h:43
size_t inuse
Definition vmem.h:50
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
struct vmem_seg * vmem_alloc(struct vmem *vm, size_t size)
Allocates a range from an arena.
Definition vmem.c:134
#define VMEM_LISTS
Definition vmem.h:37
void vmem_free(struct vmem *vm, struct vmem_seg *seg)
Frees an allocated segment.
Definition vmem.c:223
#define VMEM_HASH
Definition vmem.h:38
struct vmem_seg * vmem_find(struct vmem *vm, uintptr_t base)
Finds the allocated segment that starts at an address.
Definition vmem.c:193