Pigweed
 
Loading...
Searching...
No Matches
malloc.h
1// Copyright 2024 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#pragma once
15
16#include <cstdint>
17
18#include "pw_allocator/allocator.h"
19#include "pw_bytes/span.h"
20#include "pw_malloc/config.h"
21#include "pw_preprocessor/util.h"
22
23namespace pw::malloc {
24
34void InitSystemAllocator(ByteSpan heap);
35
45void InitSystemAllocator(void* heap_low_addr, void* heap_high_addr);
46
54template <typename AllocatorType>
55void InitSystemAllocator(ByteSpan heap);
56
57} // namespace pw::malloc
58
59#if __cplusplus
60PW_EXTERN_C_START
61#endif
62
64void pw_MallocInit(uint8_t* heap_low_addr, uint8_t* heap_high_addr);
65
66#if __cplusplus
67PW_EXTERN_C_END
68#endif
69
70namespace pw::malloc {
71
85Allocator* GetSystemAllocator();
86
88const PW_MALLOC_METRICS_TYPE& GetSystemMetrics();
89
90// Tmplate method implementations.
91
92template <typename AllocatorType>
93void InitSystemAllocator(ByteSpan heap) {
94 static bool initialized = false;
95 PW_ASSERT(!initialized);
96 auto* allocator = static_cast<AllocatorType*>(GetSystemAllocator());
97 allocator->Init(heap);
98 initialized = true;
99}
100
101} // namespace pw::malloc