Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / PrivateHeap.h
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *    Copyright 2019 Google Inc. All Rights Reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 #pragma once
20
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <sys/types.h>
24
25 #include <cstddef>
26 #include <type_traits>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif // __cplusplus
31
32 // Initializes the heap (set start and end blocks)
33 void PrivateHeapInit(void * heap, size_t size);
34
35 // Allocates a new block on the specified heap
36 void * PrivateHeapAlloc(void * heap, size_t size);
37
38 // Marks the specified block as free
39 void PrivateHeapFree(void * ptr);
40
41 // Reallocate a block to a new size
42 // Special arguments:
43 //    - null ptr value represents an Alloc
44 //    - zero size represents a Free
45 void * PrivateHeapRealloc(void * heap, void * ptr, size_t size);
46
47 void PrivateHeapDump(void * ptr);
48
49 #ifdef __cplusplus
50 } // extern "C"
51
52 namespace internal {
53
54 // Heap structure, exposed for tests
55 //
56 //  +---------+---------+-----+                      +---------+-----------+
57 //  | prev: 0 | next: n | ... |  ....<n_bytes> ....  | prev: n | next: ... |
58 //  +---------+---------+-----+                      +---------+-----------+
59 //
60 struct PrivateHeapBlockHeader
61 {
62     uint32_t prevBytes;
63     uint32_t nextBytes;
64     uint32_t state;
65     uint32_t checksum; // super-basic attempt to detect errors
66 };
67
68 } // namespace internal
69
70 constexpr size_t kPrivateHeapAllocationAlignment = std::alignment_of<max_align_t>::value;
71
72 #endif // ifdef __cplusplus