summaryrefslogtreecommitdiff
path: root/mem/framer.h
blob: e2a694553ff4581aeedb87a520e8872d3a4218ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef MEM_FRAMER_H
#define MEM_FRAMER_H

#include <rt.h>

#include "allocator.h"

/* Represents a free frame as part of the freelist, this is done
   in order to avoid having to do annoying casts */
typedef struct mem_framer_freed {
	struct mem_framer_freed *next;
} MemFramerFreed;

/* Allocator for frames: aligned regions of memory of same size */
typedef struct mem_framer {
	MemAllocator allocator;

	/* The next frame to be given off to the caller
	   in case the freelist is empty */
	usize next;

	/* The end of the region covered by the memory allocator */
	usize end;

	/* The address of the last frame that has been added in
	   the freelist
	   NOTE: the freelist is a singly-linked list comprising
	         all frames that have been previously allocated
	         and then freed */
	MemFramerFreed *free;

	/* The size of a frame. This value also defines the alignment
	   that the frames must respect */
	uint blksz;
} MemFramer;

MemFramer *mem_framer_install(void *addr, usize end, uint blksz);
void *mem_framer_alloc(MemAllocator *self, usize n);
void mem_framer_free(MemAllocator *self, void *ptr);

#endif