2023-01-31 15:41:30 +01:00
|
|
|
/*
|
|
|
|
bro this whole file is fucked up af...
|
|
|
|
|
|
|
|
...or maybe I am...
|
|
|
|
*/
|
|
|
|
|
2023-01-29 20:27:19 +01:00
|
|
|
#include <why2/memory.h>
|
|
|
|
|
2023-01-31 15:41:30 +01:00
|
|
|
#include <stdio.h>
|
2023-01-29 20:27:19 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2023-01-31 15:41:30 +01:00
|
|
|
//LOCAL
|
|
|
|
typedef struct node
|
|
|
|
{
|
|
|
|
void *pointer;
|
|
|
|
struct node *next;
|
|
|
|
struct node *last;
|
|
|
|
} node_t; //DOUBLY LINKED LIST
|
|
|
|
|
|
|
|
node_t *head = NULL; //TODO: Find a way to deallocate this shit at the end
|
|
|
|
|
|
|
|
void push_to_list(void *pointer)
|
|
|
|
{
|
|
|
|
node_t *new_node = malloc(sizeof(node_t)); //CREATE ANOTHER ELEMENT
|
|
|
|
node_t *node_buffer_1;
|
|
|
|
|
|
|
|
new_node -> pointer = pointer; //ALLOCATED POINTER
|
|
|
|
new_node -> next = NULL; //NEXT NODE
|
|
|
|
new_node -> last = NULL; //LAST NODE
|
|
|
|
|
|
|
|
if (head == NULL) //ALL MEMORY IS FREED (THERE AREN'T ANY NODES)
|
|
|
|
{
|
|
|
|
head = malloc(sizeof(node_t)); //INIT HEAD
|
|
|
|
|
|
|
|
new_node -> last = head;
|
|
|
|
|
|
|
|
head -> pointer = NULL;
|
|
|
|
head -> next = new_node; //LINK TO new_node
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
node_buffer_1 = head;
|
|
|
|
|
|
|
|
while (node_buffer_1 -> next != NULL) //GET 'LAST' NODE (next POINTER)
|
|
|
|
{
|
|
|
|
node_buffer_1 = node_buffer_1 -> next;
|
|
|
|
}
|
|
|
|
|
|
|
|
//LINK
|
|
|
|
node_buffer_1 -> next = new_node;
|
|
|
|
new_node -> last = node_buffer_1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 16:41:02 +01:00
|
|
|
void remove_node(node_t *node)
|
|
|
|
{
|
|
|
|
//REMOVE NODE
|
|
|
|
node -> last -> next = node -> next; //last WILL BE NEVER NULL CUZ CAN ONLY BE ANOTHER NODE OR HEAD
|
|
|
|
if (node -> next != NULL)
|
|
|
|
{
|
|
|
|
node -> next -> last = node -> last;
|
|
|
|
}
|
|
|
|
|
|
|
|
//DEALLOCATION
|
|
|
|
free(node); //TODO: Valgrind says node is still reachable, but it is deallocated here (I think)
|
|
|
|
}
|
|
|
|
|
2023-01-31 15:41:30 +01:00
|
|
|
//GLOBAL
|
2023-01-29 20:27:19 +01:00
|
|
|
void *why2_malloc(unsigned long size)
|
|
|
|
{
|
2023-01-31 15:41:30 +01:00
|
|
|
void *allocated = malloc(size);
|
2023-01-29 20:27:19 +01:00
|
|
|
|
2023-01-31 15:41:30 +01:00
|
|
|
push_to_list(allocated);
|
|
|
|
|
|
|
|
return allocated;
|
2023-01-29 20:36:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *why2_calloc(unsigned long element, unsigned long size)
|
|
|
|
{
|
2023-01-31 15:41:30 +01:00
|
|
|
void *allocated = calloc(element, size);
|
|
|
|
|
|
|
|
push_to_list(allocated);
|
2023-01-29 20:36:54 +01:00
|
|
|
|
2023-01-31 15:41:30 +01:00
|
|
|
return allocated;
|
2023-01-29 20:36:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-31 15:52:46 +01:00
|
|
|
void *why2_realloc(void *pointer, unsigned long size)
|
|
|
|
{
|
|
|
|
void *allocated = realloc(pointer, size);
|
|
|
|
|
|
|
|
push_to_list(allocated);
|
|
|
|
|
|
|
|
return allocated;
|
|
|
|
}
|
|
|
|
|
2023-01-29 20:36:54 +01:00
|
|
|
void why2_free(void *pointer)
|
|
|
|
{
|
2023-01-31 15:41:30 +01:00
|
|
|
//VARIABLES
|
|
|
|
node_t *node_buffer = head;
|
|
|
|
|
|
|
|
//FIND pointer IN LINKED LIST
|
|
|
|
while (node_buffer -> next != NULL) //if pointer won't be found, nothing will happen | idk if I wanna fix this or leave it like this
|
|
|
|
{
|
|
|
|
node_buffer = node_buffer -> next;
|
2023-01-31 16:41:02 +01:00
|
|
|
if (node_buffer -> pointer == pointer) break; //FOUND
|
2023-01-31 15:41:30 +01:00
|
|
|
}
|
2023-01-31 16:41:02 +01:00
|
|
|
|
|
|
|
remove_node(node_buffer);
|
|
|
|
free(pointer);
|
2023-01-29 20:27:19 +01:00
|
|
|
}
|