2024-09-21 06:17:27 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// boolean types
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
// size_t, already in libft.h but for readability
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
// Remove this and replace it with <assert.h> header
|
|
|
|
// for debugging purposes
|
|
|
|
/* #include <assert.h> */
|
|
|
|
#define assert(bool)
|
|
|
|
|
|
|
|
// BPZ = Blocks Per Zone, which is the max
|
|
|
|
// number of blocks for a new zone
|
|
|
|
enum { BPZ = 128, PAGES_TINY = 16, PAGES_SMALL = 64, MEM_ALIGN = 8 };
|
|
|
|
|
|
|
|
typedef enum { TINY, SMALL, LARGE } block_type_t;
|
|
|
|
|
|
|
|
/* METADATA:
|
2024-09-26 10:18:06 -04:00
|
|
|
* ptr: the ptr to return with kmalloc (aligned)
|
2024-09-21 06:17:27 -04:00
|
|
|
* size: the actual size
|
|
|
|
* sub_size: the size asked by the user (different
|
|
|
|
* from size only if krealloc and krealloc size < size)
|
|
|
|
* in_use: bool to track block state
|
|
|
|
* zone: the zone containing the block
|
|
|
|
*
|
|
|
|
* LINKED LIST:
|
|
|
|
* next and prev will never change, it's the original block's
|
|
|
|
* position (initialized when creating the blocks)
|
|
|
|
* next/prev_used: linked list for the
|
|
|
|
* in_use blocks (Block *used in struct Zone)
|
|
|
|
* next/prev_free: linked list for the
|
|
|
|
* available blocks (Block *free in struct Zone)
|
|
|
|
*/
|
|
|
|
typedef struct Block {
|
|
|
|
|
|
|
|
void *ptr;
|
|
|
|
size_t size;
|
|
|
|
size_t sub_size;
|
|
|
|
bool in_use;
|
|
|
|
struct Zone *zone;
|
|
|
|
|
|
|
|
struct Block *prev;
|
|
|
|
struct Block *next;
|
|
|
|
struct Block *prev_used;
|
|
|
|
struct Block *next_used;
|
|
|
|
struct Block *prev_free;
|
|
|
|
struct Block *next_free;
|
|
|
|
} Block;
|
|
|
|
|
|
|
|
/* free is the first list, when creating the blocks
|
|
|
|
* used is a list at the end of the free list, which contains all the blocks
|
|
|
|
* in_use
|
|
|
|
*/
|
|
|
|
typedef struct Zone {
|
|
|
|
size_t size;
|
|
|
|
struct Zone *prev;
|
|
|
|
struct Zone *next;
|
|
|
|
block_type_t type;
|
|
|
|
Block *free;
|
|
|
|
Block *used;
|
|
|
|
} Zone;
|
|
|
|
|
|
|
|
/* Linked list to store all the zones (pages) mapped.
|
|
|
|
* The attribute type is either TINY, SMALL or LARGE.
|
|
|
|
* For TINY and SMALL, the zone will be divided in blocks.
|
|
|
|
* For LARGE, it will be entire page(s).
|
|
|
|
*/
|
2024-09-26 10:18:06 -04:00
|
|
|
extern Zone *kzones[3];
|
|
|
|
extern Zone *vzones[3];
|
2024-09-21 06:17:27 -04:00
|
|
|
|
|
|
|
/*----------- UTILS ----------*/
|
|
|
|
block_type_t get_type(size_t size);
|
|
|
|
size_t get_zone_size(block_type_t type);
|
|
|
|
size_t align_mem(size_t addr);
|
|
|
|
/*----------------------------*/
|
|
|
|
|
|
|
|
/*-------- ALLOCATOR ---------*/
|
2024-09-26 10:18:06 -04:00
|
|
|
int new_kzone(block_type_t type, size_t size);
|
|
|
|
int new_vzone(block_type_t type, size_t size);
|
2024-09-21 06:17:27 -04:00
|
|
|
/*----------------------------*/
|
|
|
|
|
2024-09-26 10:18:06 -04:00
|
|
|
void *kmalloc(size_t size);
|
2024-09-21 06:17:27 -04:00
|
|
|
void kfree(void *ptr);
|
|
|
|
void *krealloc(void *ptr, size_t size);
|
2024-09-26 10:18:06 -04:00
|
|
|
void *vmalloc(size_t size);
|
|
|
|
void vfree(void *ptr);
|
|
|
|
void *vrealloc(void *ptr, size_t size);
|
|
|
|
void show_kalloc_mem(void);
|
|
|
|
void show_valloc_mem(void);
|