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
|
|
|
|
|
|
|
|
// BPZ = Blocks Per Zone, which is the max
|
|
|
|
// number of blocks for a new zone
|
2024-10-19 17:20:13 -04:00
|
|
|
enum { BPZ = 128, PAGES_TINY = 16, PAGES_SMALL = 64, MEM_ALIGN = 8 };
|
2024-09-21 06:17:27 -04:00
|
|
|
|
|
|
|
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 {
|
2024-10-18 08:45:37 -04:00
|
|
|
Block *free;
|
|
|
|
Block *used;
|
2024-09-21 06:17:27 -04:00
|
|
|
size_t size;
|
|
|
|
struct Zone *prev;
|
|
|
|
struct Zone *next;
|
|
|
|
block_type_t type;
|
|
|
|
} 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];
|
2024-12-03 03:28:17 -05:00
|
|
|
extern Zone *zones[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);
|
2024-10-18 08:45:37 -04:00
|
|
|
size_t ksize(void *virt_addr);
|
|
|
|
size_t vsize(void *virt_addr);
|