opuntiaOS - an operating system targeting x86 and ARMv7
at master 1.4 kB view raw
1/* 2 * Copyright (C) 2020-2022 The opuntiaOS Project Authors. 3 * + Contributed by Nikita Melekhin <nimelehin@gmail.com> 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9#ifndef _BOOT_LIBBOOT_MEM_MALLOC_H 10#define _BOOT_LIBBOOT_MEM_MALLOC_H 11 12#include <libboot/mem/mem.h> 13 14static inline void malloc_init(void* addr, size_t size) 15{ 16 extern void* _malloc_next_addr; 17 extern void* _malloc_end_addr; 18 19 _malloc_next_addr = addr; 20 _malloc_end_addr = _malloc_next_addr + size; 21} 22 23// Current implementation is a simple linear allocator. 24static void* malloc(size_t size) 25{ 26 extern void* _malloc_next_addr; 27 extern void* _malloc_end_addr; 28 if (!_malloc_next_addr) { 29 return NULL; 30 } 31 if (_malloc_next_addr >= _malloc_end_addr) { 32 return NULL; 33 } 34 35 void* res = _malloc_next_addr; 36 37 size = align_size(size, sizeof(void*)); 38 _malloc_next_addr += size; 39 return res; 40} 41 42// Current implementation is a simple linear allocator. 43static inline void* malloc_aligned(size_t size, size_t alignment) 44{ 45 extern void* _malloc_next_addr; 46 if ((size_t)_malloc_next_addr % alignment) { 47 malloc(alignment - ((size_t)_malloc_next_addr % alignment)); 48 } 49 return malloc(size); 50} 51 52static inline void free(void* ptr) 53{ 54 UNUSED(ptr); 55} 56 57#endif // _BOOT_LIBBOOT_MEM_MALLOC_H