|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 Freie Universität Berlin |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 5 | + * General Public License v2.1. See the file LICENSE in the top level |
| 6 | + * directory for more details. |
| 7 | + */ |
| 8 | +/** |
| 9 | + * @ingroup pkg_tlsf_malloc |
| 10 | + * @ingroup pkg |
| 11 | + * @ingroup sys |
| 12 | + * @{ |
| 13 | + * @file |
| 14 | + * |
| 15 | + * @brief Reentrant definitions to replace newlib's malloc with TLSF. |
| 16 | + * @author Juan I Carrano |
| 17 | + * |
| 18 | + * Newlib-nano implements malloc/free/etc in terms of the reentrant definitions |
| 19 | + * in _malloc_r/_free_r/etc so the latter are the one that have to be |
| 20 | + * overwritten. |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#include <string.h> |
| 25 | +#include <reent.h> |
| 26 | +#include <errno.h> |
| 27 | + |
| 28 | +#include "irq.h" |
| 29 | +#include "tlsf.h" |
| 30 | +#include "tlsf-malloc.h" |
| 31 | +#include "tlsf-malloc-internal.h" |
| 32 | + |
| 33 | + |
| 34 | +/* TODO: Add defines for other compilers */ |
| 35 | +#if defined(__GNUC__) && !defined(__clang__) /* Clang supports __GNUC__ but |
| 36 | + * not the alloc_size() |
| 37 | + * attribute */ |
| 38 | + |
| 39 | +#define ATTR_MALLOCR __attribute__((malloc, alloc_size(2))) |
| 40 | +#define ATTR_CALLOCR __attribute__((malloc, alloc_size(2,3))) |
| 41 | +#define ATTR_MALIGNR __attribute__((alloc_align(2), alloc_size(3), malloc)) |
| 42 | +#define ATTR_REALLOCR __attribute__((alloc_size(3))) |
| 43 | + |
| 44 | +#else /* No GNU C -> no alias attribute */ |
| 45 | + |
| 46 | +#define ATTR_MALLOCR |
| 47 | +#define ATTR_CALLOCR |
| 48 | +#define ATTR_MALIGNR |
| 49 | +#define ATTR_REALLOCR |
| 50 | + |
| 51 | +#endif /* __GNUC__ */ |
| 52 | + |
| 53 | +/** |
| 54 | + * Allocate a block of size "bytes" |
| 55 | + */ |
| 56 | +ATTR_MALLOCR void *_malloc_r(struct _reent *reent_ptr, size_t bytes) |
| 57 | +{ |
| 58 | + unsigned old_state = irq_disable(); |
| 59 | + void *result = tlsf_malloc(tlsf_malloc_gheap, bytes); |
| 60 | + |
| 61 | + if (result == NULL) { |
| 62 | + reent_ptr->_errno = ENOMEM; |
| 63 | + } |
| 64 | + |
| 65 | + irq_restore(old_state); |
| 66 | + return result; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Allocate and clear a block of size "bytes*count" |
| 71 | + */ |
| 72 | +ATTR_CALLOCR void *_calloc_r(struct _reent *reent_ptr, size_t count, size_t bytes) |
| 73 | +{ |
| 74 | + void *result = _malloc_r(reent_ptr, count * bytes); |
| 75 | + |
| 76 | + if (result != NULL) { |
| 77 | + memset(result, 0, count * bytes); |
| 78 | + } |
| 79 | + return result; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Allocate an aligned memory block. |
| 84 | + */ |
| 85 | +ATTR_MALIGNR void *_memalign_r(struct _reent *reent_ptr, size_t align, size_t bytes) |
| 86 | +{ |
| 87 | + unsigned old_state = irq_disable(); |
| 88 | + void *result = tlsf_memalign(tlsf_malloc_gheap, align, bytes); |
| 89 | + |
| 90 | + if (result == NULL) { |
| 91 | + reent_ptr->_errno = ENOMEM; |
| 92 | + } |
| 93 | + |
| 94 | + irq_restore(old_state); |
| 95 | + return result; |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Deallocate and reallocate with a different size. |
| 100 | + */ |
| 101 | +ATTR_REALLOCR void *_realloc_r(struct _reent *reent_ptr, void *ptr, size_t size) |
| 102 | +{ |
| 103 | + unsigned old_state = irq_disable(); |
| 104 | + void *result = tlsf_realloc(tlsf_malloc_gheap, ptr, size); |
| 105 | + |
| 106 | + if (result == NULL) { |
| 107 | + reent_ptr->_errno = ENOMEM; |
| 108 | + } |
| 109 | + |
| 110 | + irq_restore(old_state); |
| 111 | + return result; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Deallocate a block of data. |
| 116 | + */ |
| 117 | +void _free_r(struct _reent *reent_ptr, void *ptr) |
| 118 | +{ |
| 119 | + unsigned old_state = irq_disable(); |
| 120 | + (void)reent_ptr; |
| 121 | + |
| 122 | + tlsf_free(tlsf_malloc_gheap, ptr); |
| 123 | + irq_restore(old_state); |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * @} |
| 128 | + */ |
0 commit comments