Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/variant/variant_pools.h
45997 views
1
/**************************************************************************/
2
/* variant_pools.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/math/math_defs.h"
34
#include "core/os/memory.h"
35
36
namespace VariantPools {
37
inline constexpr size_t BUCKET_SMALL = 2 * 3 * sizeof(real_t);
38
inline constexpr size_t BUCKET_MEDIUM = 4 * 3 * sizeof(real_t);
39
inline constexpr size_t BUCKET_LARGE = 4 * 4 * sizeof(real_t);
40
41
void *alloc_small();
42
void *alloc_medium();
43
void *alloc_large();
44
45
template <typename T>
46
_FORCE_INLINE_ T *alloc() {
47
if constexpr (sizeof(T) <= BUCKET_SMALL && alignof(real_t) % alignof(T) == 0) {
48
return static_cast<T *>(alloc_small());
49
} else if constexpr (sizeof(T) <= BUCKET_MEDIUM && alignof(real_t) % alignof(T) == 0) {
50
return static_cast<T *>(alloc_medium());
51
} else if constexpr (sizeof(T) <= BUCKET_LARGE && alignof(real_t) % alignof(T) == 0) {
52
return static_cast<T *>(alloc_large());
53
} else {
54
return memnew(T);
55
}
56
}
57
58
void free_small(void *p_ptr);
59
void free_medium(void *p_ptr);
60
void free_large(void *p_ptr);
61
62
template <typename T>
63
_FORCE_INLINE_ void free(T *p_ptr) {
64
if constexpr (sizeof(T) <= BUCKET_SMALL && alignof(real_t) % alignof(T) == 0) {
65
free_small(p_ptr);
66
} else if constexpr (sizeof(T) <= BUCKET_MEDIUM && alignof(real_t) % alignof(T) == 0) {
67
free_medium(p_ptr);
68
} else if constexpr (sizeof(T) <= BUCKET_LARGE && alignof(real_t) % alignof(T) == 0) {
69
free_large(p_ptr);
70
} else {
71
memdelete(p_ptr);
72
}
73
}
74
}; //namespace VariantPools
75
76