-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptr_tag.hpp
More file actions
59 lines (48 loc) · 1.48 KB
/
Copy pathptr_tag.hpp
File metadata and controls
59 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Copyright 2025, Aleksandar Colic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#ifndef STL_PTR_TAG_HPP
#define STL_PTR_TAG_HPP
#include <bit>
#include <cassert>
#include <cstddef>
#include "types.hpp"
namespace stl {
/**
* Pointer tagging.
* Since malloc's returned address is guaranteed to be aligned at least as std::max_align_t, we will
* use unused last bits in pointer to store additonal info.
*/
static constexpr uptr tag_bits = alignof(std::max_align_t) - 1;
constexpr uptr raw(const void* ptr) noexcept
{
return std::bit_cast<uptr>(ptr);
}
constexpr uptr tag(const void* ptr) noexcept
{
return raw(ptr) & tag_bits;
}
constexpr void* clear_tag(const void* ptr) noexcept
{
return std::bit_cast<void*>(raw(ptr) & ~tag_bits);
}
constexpr void* set_tag(const void* ptr, uptr tag) noexcept
{
assert((tag & ~tag_bits) == 0);
return std::bit_cast<void*>(raw(clear_tag(ptr)) | (tag & tag_bits));
}
} // namespace stl
#endif // STL_PTR_TAG_HPP