opuntiaOS - an operating system targeting x86 and ARMv7
at master 1.4 kB view raw
1#ifndef _LIBC_MALLOC_MALLOC_H 2#define _LIBC_MALLOC_MALLOC_H 3 4#include <stdbool.h> 5#include <stddef.h> 6#include <stdint.h> 7#include <sys/cdefs.h> 8#include <sys/types.h> 9 10__BEGIN_DECLS 11 12#define MALLOC_DEFAULT_BLOCK_SIZE 4096 13#define MALLOC_MAX_ALLOCATED_BLOCKS 64 14 15#define FLAG_ALLOCATED (0x1) 16#define FLAG_SLAB (0x2) 17struct __malloc_header { 18 size_t size; 19 uint32_t flags; 20 struct __malloc_header* next; 21 struct __malloc_header* prev; 22}; 23typedef struct __malloc_header malloc_header_t; 24 25static inline bool block_has_flags(malloc_header_t* block, uint32_t flags) 26{ 27 return ((block->flags & flags) == flags); 28} 29 30static inline void block_set_flags(malloc_header_t* block, uint32_t flags) 31{ 32 block->flags |= flags; 33} 34 35static inline void block_rem_flags(malloc_header_t* block, uint32_t flags) 36{ 37 block->flags &= (~flags); 38} 39 40static inline bool block_is_allocated(malloc_header_t* block) 41{ 42 return block_has_flags(block, FLAG_ALLOCATED); 43} 44 45static inline bool block_is_free(malloc_header_t* block) 46{ 47 return !block_has_flags(block, FLAG_ALLOCATED); 48} 49 50static inline bool block_is_slab(malloc_header_t* block) 51{ 52 return block_has_flags(block, FLAG_SLAB); 53} 54 55void _malloc_init(); 56void _slab_init(); 57 58void* malloc(size_t); 59void free(void*); 60void* calloc(size_t, size_t); 61void* realloc(void*, size_t); 62 63void* slab_alloc(size_t); 64void slab_free(malloc_header_t* mem_header); 65 66__END_DECLS 67 68#endif // _LIBC_MALLOC_MALLOC_H