blob: 73b44399fee89f83d3bc8544cac0abd31e97da45 [file] [log] [blame]
Chenghan Zhoud4f44d22020-06-18 15:42:06 -04001// 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 Zhoud4f44d22020-06-18 15:42:06 -040018#include "pw_malloc/malloc.h"
Armando Montanez1ee925c2020-06-29 13:15:28 -070019#include "pw_preprocessor/compiler.h"
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040020#include "pw_preprocessor/util.h"
21
22namespace {
23std::aligned_storage_t<sizeof(pw::allocator::FreeListHeapBuffer<>),
24 alignof(pw::allocator::FreeListHeapBuffer<>)>
25 buf;
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040026} // namespace
27pw::allocator::FreeListHeapBuffer<>* pw_freelist_heap;
28
29#if __cplusplus
30extern "C" {
Wyatt Hepler63afc002021-02-08 13:20:26 -080031#endif // __cplusplus
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040032// Define the global heap variables.
Scott James Remnant516255c2021-07-22 12:36:11 -070033void pw_MallocInit(uint8_t* heap_low_addr, uint8_t* heap_high_addr) {
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040034 std::span<std::byte> pw_allocator_freelist_raw_heap =
Scott James Remnant516255c2021-07-22 12:36:11 -070035 std::span(reinterpret_cast<std::byte*>(heap_low_addr),
36 heap_high_addr - heap_low_addr);
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040037 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".
46void* __wrap_malloc(size_t size) { return pw_freelist_heap->Allocate(size); }
47
48void __wrap_free(void* ptr) { pw_freelist_heap->Free(ptr); }
49
50void* __wrap_realloc(void* ptr, size_t size) {
51 return pw_freelist_heap->Realloc(ptr, size);
52}
53
54void* __wrap_calloc(size_t num, size_t size) {
55 return pw_freelist_heap->Calloc(num, size);
56}
57
Wyatt Hepler63afc002021-02-08 13:20:26 -080058void* __wrap__malloc_r(struct _reent*, size_t size) {
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040059 return pw_freelist_heap->Allocate(size);
60}
61
Wyatt Hepler63afc002021-02-08 13:20:26 -080062void __wrap__free_r(struct _reent*, void* ptr) { pw_freelist_heap->Free(ptr); }
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040063
Wyatt Hepler63afc002021-02-08 13:20:26 -080064void* __wrap__realloc_r(struct _reent*, void* ptr, size_t size) {
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040065 return pw_freelist_heap->Realloc(ptr, size);
66}
67
Wyatt Hepler63afc002021-02-08 13:20:26 -080068void* __wrap__calloc_r(struct _reent*, size_t num, size_t size) {
Chenghan Zhoud4f44d22020-06-18 15:42:06 -040069 return pw_freelist_heap->Calloc(num, size);
70}
71#if __cplusplus
72}
Wyatt Hepler63afc002021-02-08 13:20:26 -080073#endif // __cplusplus