feat: malloc init, thread safety
This commit is contained in:
parent
e8e913f3a1
commit
31806c4594
48
inc/malloc.h
48
inc/malloc.h
|
@ -1,11 +1,53 @@
|
|||
#ifndef MALLOC_H
|
||||
# define MALLOC_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <sys/mman.h>
|
||||
# include <pthread.h>
|
||||
|
||||
#define PROUT "42\n"
|
||||
// TODO Remove me (printf)
|
||||
# include <stdio.h>
|
||||
|
||||
#define MAX_ALIGNMENT 16
|
||||
|
||||
typedef struct s_block t_block;
|
||||
typedef struct s_zone t_zone;
|
||||
|
||||
struct s_block {
|
||||
size_t size;
|
||||
int free;
|
||||
t_block *next;
|
||||
};
|
||||
|
||||
struct s_zone {
|
||||
t_block *head;
|
||||
t_zone *next;
|
||||
};
|
||||
|
||||
typedef struct s_zones {
|
||||
size_t pagesize;
|
||||
size_t max_align;
|
||||
size_t tiny_block_max_size;
|
||||
size_t small_block_max_size;
|
||||
t_zone *tiny_zones;
|
||||
t_zone *small_zones;
|
||||
t_block *large_blocks;
|
||||
} t_zones;
|
||||
|
||||
extern pthread_mutex_t g_malloc_mutex;
|
||||
extern t_zones g_zones;
|
||||
|
||||
void *malloc(size_t size);
|
||||
void free(void *ptr);
|
||||
void *realloc(void *ptr, size_t size);
|
||||
|
||||
void show_alloc_mem(void);
|
||||
void show_alloc_mem_ex(void);
|
||||
void show_alloc_sizes(void);
|
||||
|
||||
static void init_malloc(void);
|
||||
void init_tiny_zones(void);
|
||||
void init_small_zones(void);
|
||||
|
||||
#endif
|
||||
|
|
6
main.c
6
main.c
|
@ -1,6 +1,10 @@
|
|||
#include <stdlib.h>
|
||||
#include "inc/malloc.h"
|
||||
|
||||
int main(void) {
|
||||
malloc(42);
|
||||
int *arr = malloc(42);
|
||||
arr = realloc(arr, 42);
|
||||
free(arr);
|
||||
show_alloc_sizes();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
#include "malloc.h"
|
||||
|
||||
void free(void *ptr) {
|
||||
pthread_mutex_lock(&g_malloc_mutex);
|
||||
(void)ptr;
|
||||
pthread_mutex_unlock(&g_malloc_mutex);
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#include "malloc.h"
|
||||
|
||||
pthread_mutex_t g_malloc_mutex;
|
||||
t_zones g_zones;
|
||||
|
||||
void init_tiny_zones(void) {
|
||||
g_zones.tiny_block_max_size = g_zones.pagesize / 2;
|
||||
g_zones.tiny_zones = (t_zone *)mmap(NULL, g_zones.tiny_block_max_size * 128, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
g_zones.tiny_zones->head = NULL;
|
||||
g_zones.tiny_zones->next = NULL;
|
||||
}
|
||||
|
||||
void init_small_zones(void) {
|
||||
g_zones.small_block_max_size = g_zones.tiny_block_max_size * 128;
|
||||
g_zones.small_zones = (t_zone *)mmap(NULL, g_zones.small_block_max_size * 128, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
g_zones.small_zones->head = NULL;
|
||||
g_zones.small_zones->next = NULL;
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void init_malloc(void) {
|
||||
g_zones.pagesize = (size_t)sysconf(_SC_PAGESIZE);
|
||||
g_zones.max_align = MAX_ALIGNMENT;
|
||||
init_tiny_zones();
|
||||
init_small_zones();
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
#include "malloc.h"
|
||||
|
||||
void * malloc(size_t size) {
|
||||
void *malloc(size_t size) {
|
||||
pthread_mutex_lock(&g_malloc_mutex);
|
||||
(void)size;
|
||||
write(1, PROUT, 3);
|
||||
return 0;
|
||||
write(1, "42 from malloc\n", 15);
|
||||
pthread_mutex_unlock(&g_malloc_mutex);
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#include "malloc.h"
|
||||
|
||||
void *realloc(void *ptr, size_t size) {
|
||||
pthread_mutex_lock(&g_malloc_mutex);
|
||||
write(1, "1337\n", 5);
|
||||
(void)size;
|
||||
(void)ptr;
|
||||
pthread_mutex_unlock(&g_malloc_mutex);
|
||||
return ptr;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#include "malloc.h"
|
||||
|
||||
void show_alloc_mem(void) {
|
||||
}
|
||||
|
||||
void show_alloc_mem_ex(void) {
|
||||
}
|
||||
|
||||
void show_alloc_sizes(void) {
|
||||
// TODO remove me (printf)
|
||||
printf("PAGESIZE : %zu bytes\n", g_zones.pagesize);
|
||||
printf("TINY ZONE MAX BLOCK SIZE: %zu bytes\n", g_zones.tiny_block_max_size);
|
||||
printf("SMALL ZONE MAX BLOCK SIZE : %zu bytes\n", g_zones.small_block_max_size);
|
||||
}
|
Loading…
Reference in New Issue