Line data Source code
1 1 : /** 2 : * @file mm/mm.h 3 : * 4 : * @brief Memory Manager main header 5 : * 6 : * Memory Manager main header 7 : * 8 : * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com> 9 : * SPDX-License-Identifier: GPL-3.0-only 10 : */ 11 : #pragma once 12 : 13 : #include <log/log.h> 14 : 15 : #include <stddef.h> 16 : #include <stdlib.h> 17 : 18 : /** 19 : * @brief A version of the stdlib aligned_alloc() used internally 20 : * @param alignment the requested alignment value in bytes 21 : * @param mem_size the size of the requested memory allocation 22 : * @return a pointer to the newly allocated memory area 23 : * 24 : * In case of memory allocation failure, a log message is taken and the 25 : * simulation is promptly aborted 26 : */ 27 1 : inline void *mm_aligned_alloc(size_t alignment, size_t mem_size) 28 : { 29 : void *ret = aligned_alloc(alignment, mem_size); 30 : 31 : if (__builtin_expect(mem_size && !ret, 0)) { 32 : log_log(LOG_FATAL, "Out of memory!"); 33 : abort(); // TODO: this can be criticized as xmalloc() in gcc. We shall dump partial stats before. 34 : } 35 : return ret; 36 : } 37 : 38 : /** 39 : * @brief A version of the stdlib malloc() used internally 40 : * @param mem_size the size of the requested memory allocation 41 : * @return a pointer to the newly allocated memory area 42 : * 43 : * In case of memory allocation failure, a log message is taken and the 44 : * simulation is promptly aborted 45 : */ 46 1 : inline void *mm_alloc(size_t mem_size) 47 : { 48 : void *ret = malloc(mem_size); 49 : 50 : if (__builtin_expect(mem_size && !ret, 0)) { 51 : log_log(LOG_FATAL, "Out of memory!"); 52 : abort(); // TODO: this can be criticized as xmalloc() in gcc. We shall dump partial stats before. 53 : } 54 : return ret; 55 : } 56 : 57 : /** 58 : * @brief A version of the stdlib realloc() used internally 59 : * @param ptr a pointer to the memory area to reallocate 60 : * @param mem_size the new size of the memory allocation 61 : * @return a pointer to the newly allocated memory area 62 : * 63 : * In case of memory allocation failure, a log message is taken and the 64 : * simulation is promptly aborted 65 : */ 66 1 : inline void *mm_realloc(void *ptr, size_t mem_size) 67 : { 68 : void *ret = realloc(ptr, mem_size); 69 : 70 : if(__builtin_expect(mem_size && !ret, 0)) { 71 : log_log(LOG_FATAL, "Out of memory!"); 72 : abort(); // TODO: this can be criticized as xmalloc() in gcc. We shall dump partial stats before. 73 : } 74 : return ret; 75 : } 76 : 77 : /** 78 : * @brief A version of the stdlib free() used internally 79 : * @param ptr a pointer to the memory area to free 80 : */ 81 1 : inline void mm_free(void *ptr) 82 : { 83 : free(ptr); 84 : }