-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmalloc.h
More file actions
51 lines (43 loc) · 1.73 KB
/
kmalloc.h
File metadata and controls
51 lines (43 loc) · 1.73 KB
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
42
43
44
45
46
47
48
49
50
51
#ifndef _kmalloc_H_
#define _kmalloc_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/mman.h>
#define PAGE_SIZE 4096
#define BYTE_ALIGN 8
/* Represents the header of an allocated/free memory block. The format in
* memory is as follows:
* - Block size (int, 4 bytes)
* - Allocated (int, 4 bytes): 0 if free, 1 if allocated
* - Next free block (pointer, 8 bytes)
* - Previous free block (pointer, 8 bytes)
* - Payload (allocated block only)
* - Padding (only if extra bytes are needed to enforce 8-byte alignment)
*/
typedef struct mem_block {
unsigned int size, allocated;
mem_block *prev, *next;
} mem_block;
/* Allocates the initial heap area and sets global variables `head` and
* `heap_address`. The page size is given by `PAGE_SIZE`, which is 4096.
* If `size_of_region % PAGE_SIZE != 0`, extra bytes will be added to the heap
* area. Returns true if the heap area is successfully set and false if there
* is insufficient contiguous memory.
*/
bool heap_init(unsigned int heap_size);
/* Returns a pointer to the start of the payload or `NULL` if there is not
* enough contiguous free space within the memory allocated by `heap_init()`
* to satisfy the request. If the size requested is not a multiple of 8 bytes,
* extra bytes will be added to enforce 8-byte alignment.
*/
void *kmalloc(unsigned int num_bytes);
/* Returns a pointer to the start of the payload or `NULL` if there is not
* enough contiguous free space within the memory allocated by `heap_init()`
* to satisfy the request. The memory is zeroed out.
*/
void *kcalloc(unsigned int num_elements, unsigned int element_size);
/* Frees the target block given the address of the start of the payload. */
void kfree(void *ptr);
#endif