backup
This commit is contained in:
parent
ed8a843d3d
commit
5aba0ff0c3
3 changed files with 50 additions and 2 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
#include "helpers.h"
|
#include "helpers.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
// Returns the smallest power of 2 greater or equal to s
|
// Returns the smallest power of 2 greater or equal to s
|
||||||
|
|
@ -55,7 +56,31 @@ void *find_free_block(struct bucket *buck)
|
||||||
|
|
||||||
struct bucket *create_bucket(size_t min_alloc_size)
|
struct bucket *create_bucket(size_t min_alloc_size)
|
||||||
{
|
{
|
||||||
|
// Round up
|
||||||
size_t alloc_size = s2p(min_alloc_size);
|
size_t alloc_size = s2p(min_alloc_size);
|
||||||
|
// Get page
|
||||||
void *page =
|
void *page =
|
||||||
mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0);
|
mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, -1, 0);
|
||||||
|
|
||||||
|
if (page == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Init bucket
|
||||||
|
struct bucket *header = page;
|
||||||
|
header->page = page + sizeof(struct bucket);
|
||||||
|
header->block_size = alloc_size;
|
||||||
|
memset(header->free_list, 0, FREE_LIST_SIZE);
|
||||||
|
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct bucket *get_free_bucket(struct bucket *head, size_t size)
|
||||||
|
{
|
||||||
|
while (head != NULL)
|
||||||
|
{
|
||||||
|
if (head->block_size >= size)
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,30 @@
|
||||||
#ifndef HELPERS_H
|
#ifndef HELPERS_H
|
||||||
#define HELPERS_H
|
#define HELPERS_H
|
||||||
|
|
||||||
|
// #define PAGE_SIZE (size_t) sysconf(_SC_PAGE_SIZE)
|
||||||
|
#define PAGE_SIZE 4096
|
||||||
|
#define MIN_BLOCK_SIZE 8
|
||||||
|
#define FREE_LIST_SIZE PAGE_SIZE / MIN_BLOCK_SIZE / sizeof(unsigned char)
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// Header
|
||||||
|
// Contains all informations about the concerned bucket
|
||||||
struct bucket
|
struct bucket
|
||||||
{
|
{
|
||||||
unsigned block_size;
|
unsigned block_size;
|
||||||
unsigned char free_list[64]; // 4096 / MIN_BLOCK_SIZE /
|
unsigned char free_list[FREE_LIST_SIZE]; // 4096 / MIN_BLOCK_SIZE /
|
||||||
// sizeof(unsigned char) (in bits)
|
// sizeof(unsigned char)
|
||||||
char *page;
|
char *page;
|
||||||
|
|
||||||
|
struct bucket *next;
|
||||||
|
|
||||||
|
size_t checksum;
|
||||||
};
|
};
|
||||||
|
|
||||||
size_t get_next_2power(size_t n);
|
size_t get_next_2power(size_t n);
|
||||||
void *find_free_block(struct bucket *buck);
|
void *find_free_block(struct bucket *buck);
|
||||||
struct bucket *create_bucket(size_t min_alloc_size);
|
struct bucket *create_bucket(size_t min_alloc_size);
|
||||||
|
struct bucket *get_free_bucket(struct bucket *head, size_t size);
|
||||||
|
|
||||||
#endif // ! HELPERS_H
|
#endif // ! HELPERS_H
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,20 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "helpers/helpers.h"
|
||||||
|
|
||||||
|
struct bucket *head = NULL;
|
||||||
|
|
||||||
__attribute__((visibility("default"))) void *malloc(size_t size)
|
__attribute__((visibility("default"))) void *malloc(size_t size)
|
||||||
{
|
{
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
struct bucket *buck = get_free_bucket(head, size);
|
||||||
|
if (buck == NULL)
|
||||||
|
{
|
||||||
|
buck = create_bucket(size);
|
||||||
|
return ...;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((visibility("default"))) void free(void *ptr)
|
__attribute__((visibility("default"))) void free(void *ptr)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue