Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include <span> |
| 16 | |
| 17 | #include "pw_allocator/freelist_heap.h" |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 18 | #include "pw_malloc/malloc.h" |
Armando Montanez | 1ee925c | 2020-06-29 13:15:28 -0700 | [diff] [blame] | 19 | #include "pw_preprocessor/compiler.h" |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 20 | #include "pw_preprocessor/util.h" |
| 21 | |
| 22 | namespace { |
| 23 | std::aligned_storage_t<sizeof(pw::allocator::FreeListHeapBuffer<>), |
| 24 | alignof(pw::allocator::FreeListHeapBuffer<>)> |
| 25 | buf; |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 26 | } // namespace |
| 27 | pw::allocator::FreeListHeapBuffer<>* pw_freelist_heap; |
| 28 | |
| 29 | #if __cplusplus |
| 30 | extern "C" { |
Wyatt Hepler | 63afc00 | 2021-02-08 13:20:26 -0800 | [diff] [blame] | 31 | #endif // __cplusplus |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 32 | // Define the global heap variables. |
Scott James Remnant | 516255c | 2021-07-22 12:36:11 -0700 | [diff] [blame] | 33 | void pw_MallocInit(uint8_t* heap_low_addr, uint8_t* heap_high_addr) { |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 34 | std::span<std::byte> pw_allocator_freelist_raw_heap = |
Scott James Remnant | 516255c | 2021-07-22 12:36:11 -0700 | [diff] [blame] | 35 | std::span(reinterpret_cast<std::byte*>(heap_low_addr), |
| 36 | heap_high_addr - heap_low_addr); |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 37 | pw_freelist_heap = new (&buf) |
| 38 | pw::allocator::FreeListHeapBuffer(pw_allocator_freelist_raw_heap); |
| 39 | } |
| 40 | |
| 41 | // Wrapper functions for malloc, free, realloc and calloc. |
| 42 | // With linker options "-Wl --wrap=<function name>", linker will link |
| 43 | // "__wrap_<function name>" with "<function_name>", and calling |
| 44 | // "<function name>" will call "__wrap_<function name>" instead |
| 45 | // Linker options are set in a config in "pw_malloc:pw_malloc_config". |
| 46 | void* __wrap_malloc(size_t size) { return pw_freelist_heap->Allocate(size); } |
| 47 | |
| 48 | void __wrap_free(void* ptr) { pw_freelist_heap->Free(ptr); } |
| 49 | |
| 50 | void* __wrap_realloc(void* ptr, size_t size) { |
| 51 | return pw_freelist_heap->Realloc(ptr, size); |
| 52 | } |
| 53 | |
| 54 | void* __wrap_calloc(size_t num, size_t size) { |
| 55 | return pw_freelist_heap->Calloc(num, size); |
| 56 | } |
| 57 | |
Wyatt Hepler | 63afc00 | 2021-02-08 13:20:26 -0800 | [diff] [blame] | 58 | void* __wrap__malloc_r(struct _reent*, size_t size) { |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 59 | return pw_freelist_heap->Allocate(size); |
| 60 | } |
| 61 | |
Wyatt Hepler | 63afc00 | 2021-02-08 13:20:26 -0800 | [diff] [blame] | 62 | void __wrap__free_r(struct _reent*, void* ptr) { pw_freelist_heap->Free(ptr); } |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 63 | |
Wyatt Hepler | 63afc00 | 2021-02-08 13:20:26 -0800 | [diff] [blame] | 64 | void* __wrap__realloc_r(struct _reent*, void* ptr, size_t size) { |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 65 | return pw_freelist_heap->Realloc(ptr, size); |
| 66 | } |
| 67 | |
Wyatt Hepler | 63afc00 | 2021-02-08 13:20:26 -0800 | [diff] [blame] | 68 | void* __wrap__calloc_r(struct _reent*, size_t num, size_t size) { |
Chenghan Zhou | d4f44d2 | 2020-06-18 15:42:06 -0400 | [diff] [blame] | 69 | return pw_freelist_heap->Calloc(num, size); |
| 70 | } |
| 71 | #if __cplusplus |
| 72 | } |
Wyatt Hepler | 63afc00 | 2021-02-08 13:20:26 -0800 | [diff] [blame] | 73 | #endif // __cplusplus |