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