#ifndef WONKY_MALLOC_H
#define WONKY_MALLOC_H WONKY_MALLOC_H
#include <wonky_malloc.hh>
#include <program.h>
#include <map.h>
#include <lexer.h>
#include <stdlib.h>
#include <stdio.h>/*TODO remove this after implementing longjmp in memfail*/
#define NUMBER_OF_MAP_NODES_IN_INCREMENTATION_OF_MAP_MEMORY_SEGMENT 2048
#define NUMBER_OF_TOKENS_IN_INCREMENTATION_OF_TOKEN_MEMORY_SEGMENT 1024
#define NUMBER_OF_BYTES_IN_INCREMENTATION_OF_GENERIC_MEMORY_SEGMENT 1024
/*
if the memory requested in a wonky_wonky_malloc can't fit in the current segment
then if it overflows by 1/REALLOC_TRIGGER of the capacity of the segment,
then the segment is realloced
*/
#define REALLOC_TRIGGER 2
struct Memory_Segment
{
size_t capacity;
size_t size;
struct Memory_Segment *previous;
unsigned char *data;
};
struct Memory
{
struct Memory_Segment *map_nodes;
struct Memory_Segment *tokens;
struct Memory_Segment *generic;
};
void wonky_memory_init();
void wonky_memory_delete();
void delete_memory_segment(struct Memory_Segment *segment);
void* wonky_malloc(size_t size);
void* wonky_realloc(void *old_mem,size_t new_size);
void* wonky_calloc(size_t nmemb,size_t size);
struct Map* wonky_malloc_map_node();
struct token* wonky_malloc_token();
struct Memory_Segment* get_memory_segment(struct Memory_Segment *previous,size_t atleast_this_size);
void resize_memory_segment(size_t new_capacity,struct Memory_Segment *to_be_resized);
struct Memory_Segment* calloc_memory_segment(struct Memory_Segment *previous,size_t increment_size,size_t element_size);
void wonky_memfail();
static inline void wonky_free(void *x) {}
static inline void wonky_free_token(void *x) {}
static inline void wonky_free_map_node(void *x) {}
#endif