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"
18 #include "pw_boot_armv7m/boot.h"
19 #include "pw_malloc/malloc.h"
20 #include "pw_preprocessor/compiler.h"
21 #include "pw_preprocessor/util.h"
22
23 namespace {
24 std::aligned_storage_t<sizeof(pw::allocator::FreeListHeapBuffer<>),
25 alignof(pw::allocator::FreeListHeapBuffer<>)>
26 buf;
27 } // namespace
28 pw::allocator::FreeListHeapBuffer<>* pw_freelist_heap;
29
30 #if __cplusplus
31 extern "C" {
32 #endif // __cplusplus
33 // Define the global heap variables.
pw_MallocInit()34 void pw_MallocInit() {
35 // pw_boot_heap_low_addr and pw_boot_heap_high_addr specifies the heap region
36 // from the linker script in "pw_boot_armv7m".
37 std::span<std::byte> pw_allocator_freelist_raw_heap =
38 std::span(reinterpret_cast<std::byte*>(&pw_boot_heap_low_addr),
39 &pw_boot_heap_high_addr - &pw_boot_heap_low_addr);
40 pw_freelist_heap = new (&buf)
41 pw::allocator::FreeListHeapBuffer(pw_allocator_freelist_raw_heap);
42 }
43
44 // Wrapper functions for malloc, free, realloc and calloc.
45 // With linker options "-Wl --wrap=<function name>", linker will link
46 // "__wrap_<function name>" with "<function_name>", and calling
47 // "<function name>" will call "__wrap_<function name>" instead
48 // Linker options are set in a config in "pw_malloc:pw_malloc_config".
__wrap_malloc(size_t size)49 void* __wrap_malloc(size_t size) { return pw_freelist_heap->Allocate(size); }
50
__wrap_free(void * ptr)51 void __wrap_free(void* ptr) { pw_freelist_heap->Free(ptr); }
52
__wrap_realloc(void * ptr,size_t size)53 void* __wrap_realloc(void* ptr, size_t size) {
54 return pw_freelist_heap->Realloc(ptr, size);
55 }
56
__wrap_calloc(size_t num,size_t size)57 void* __wrap_calloc(size_t num, size_t size) {
58 return pw_freelist_heap->Calloc(num, size);
59 }
60
__wrap__malloc_r(struct _reent *,size_t size)61 void* __wrap__malloc_r(struct _reent*, size_t size) {
62 return pw_freelist_heap->Allocate(size);
63 }
64
__wrap__free_r(struct _reent *,void * ptr)65 void __wrap__free_r(struct _reent*, void* ptr) { pw_freelist_heap->Free(ptr); }
66
__wrap__realloc_r(struct _reent *,void * ptr,size_t size)67 void* __wrap__realloc_r(struct _reent*, void* ptr, size_t size) {
68 return pw_freelist_heap->Realloc(ptr, size);
69 }
70
__wrap__calloc_r(struct _reent *,size_t num,size_t size)71 void* __wrap__calloc_r(struct _reent*, size_t num, size_t size) {
72 return pw_freelist_heap->Calloc(num, size);
73 }
74 #if __cplusplus
75 }
76 #endif // __cplusplus
77