42_KFS/headers/alloc.h

91 lines
2.4 KiB
C
Raw Normal View History

#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
enum { BPZ = 128, PAGES_TINY = 16, PAGES_SMALL = 64, MEM_ALIGN = 8 };
typedef enum { TINY, SMALL, LARGE } block_type_t;
/* METADATA:
* ptr: the ptr to return with kmalloc (aligned)
* 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;
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).
*/
extern Zone *kzones[3];
extern Zone *vzones[3];
/*----------- 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 ---------*/
int new_kzone(block_type_t type, size_t size);
int new_vzone(block_type_t type, size_t size);
/*----------------------------*/
void *kmalloc(size_t size);
void kfree(void *ptr);
void *krealloc(void *ptr, size_t size);
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);