vulcanos/kernel/mem/kheap.h

73 lines
2.5 KiB
C
Raw Normal View History

2019-09-19 12:04:12 +02:00
/**************************************
* iceOS Kernel *
* Developed by Marco 'icebit' Cetica *
* (c) 2019 *
* Released under GPLv3 *
* https://github.com/ice-bit/iceOS *
***************************************/
/*** Heap implementation from James Molloy's tutorial:
2019-09-25 01:00:06 +02:00
http://www.jamesmolloy.co.uk/tutorial_html/7.-The%20Heap.html ***/
2019-09-19 12:04:12 +02:00
2019-09-18 12:25:55 +02:00
/* This heap algorithm uses two different data structures: blocks and holes.
* Blocks: Contiguous areas of memory containing user data
* Holes: Special kind of blocks that are not in use, this is the result
* of free() operation. Those spaces lend to a common problem called "Fragmentation";
* where malloc() cannot use those spaces anymore because they are too small for any
* kind of program. Any modern OS must have a solution to avoid this problem, but to keep
* things simple as possible i wont implement anything like that.
* Blocks/holes contains informations like the magic number(error checking), the type of
* chunk(hole or block) and the size, while the footer contains only a pointer to the header
* (and obviously an error checking flag).
*/
#ifndef KHEAP_H
#define KHEAP_H
2019-09-26 17:45:46 +02:00
#define KHEAP_START 0xC0000000
2019-09-18 12:25:55 +02:00
#define KHEAP_INITIAL_SIZE 0x100000
2019-09-26 17:45:46 +02:00
2019-09-18 12:25:55 +02:00
#define HEAP_INDEX_SIZE 0x20000
#define HEAP_MAGIC 0x123890AB
#define HEAP_MIN_SIZE 0x70000
#include <stdint.h>
2019-09-26 17:45:46 +02:00
#include "ordered_array.h"
2019-09-18 12:25:55 +02:00
2019-09-26 17:45:46 +02:00
// Data structure for single hole/block
2019-09-18 12:25:55 +02:00
typedef struct {
uint32_t magic; // Magic number for error checking
2019-09-26 17:45:46 +02:00
uint8_t is_hole; // 1 if it's an hole, 0 for a block
uint32_t size; // Size of block
2019-09-18 12:25:55 +02:00
} header_t;
typedef struct {
2019-09-26 17:45:46 +02:00
uint32_t magic; // Same as above
header_t *header; // Pointer to the header block
2019-09-18 12:25:55 +02:00
} footer_t;
typedef struct {
2019-09-26 17:45:46 +02:00
ordered_array_t index;
uint32_t start_address; // Begin of allocated space
uint32_t end_adddress; // End of allocated space
uint32_t max_address; // Maximum size heap ca be expanded to
2019-09-18 12:25:55 +02:00
uint8_t supervisor;
uint8_t readonly;
} heap_t;
2019-09-26 17:45:46 +02:00
// Create a new heap
2019-09-18 12:25:55 +02:00
heap_t *create_heap(uint32_t start, uint32_t end, uint32_t max, uint8_t supervisor, uint8_t readonly);
2019-09-26 17:45:46 +02:00
// Allocates a contigious region of memory in size
2019-09-18 12:25:55 +02:00
void *alloc(uint32_t size, uint8_t page_align, heap_t *heap);
2019-09-26 17:45:46 +02:00
// Free a block allocated with alloc
2019-09-18 12:25:55 +02:00
void free(void *p, heap_t *heap);
2019-09-26 17:45:46 +02:00
uint32_t kmalloc_int(uint32_t sz, int align, uint32_t *phys);
uint32_t kmalloc_a(uint32_t sz);
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys);
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys);
uint32_t kmalloc(uint32_t sz);
2019-09-26 17:45:46 +02:00
void kfree(void *p);
2019-09-18 12:25:55 +02:00
#endif