-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_config.h
More file actions
216 lines (186 loc) · 8.88 KB
/
Copy pathmemory_config.h
File metadata and controls
216 lines (186 loc) · 8.88 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**************************************************************************/
/* memory_config.h */
/**************************************************************************/
/* Independent Memory Management Module */
/* Configuration system for memory management */
/**************************************************************************/
#pragma once
#include "platform_defines.h"
#include "thread_safe.h"
// Memory tracking levels
enum class MemoryTrackingLevel {
NONE, // No tracking
BASIC, // Basic tracking (total usage)
DETAILED, // Detailed tracking (per-allocation info)
FULL // Full tracking with call stacks
};
// Memory alignment policies
enum class MemoryAlignmentPolicy {
NONE, // No special alignment
STANDARD, // Standard alignment (max_align_t)
CUSTOM, // Custom alignment requirements
PLATFORM_OPTIMAL // Platform-optimal alignment
};
// Memory padding policies
enum class MemoryPaddingPolicy {
NONE, // No padding
DEBUG_ONLY, // Padding only in debug builds
ALWAYS, // Always pad
CONFIGURABLE // Runtime configurable
};
// Memory allocation strategies
enum class MemoryAllocationStrategy {
SYSTEM_DEFAULT, // Use system malloc/free
POOLED, // Use memory pools
CUSTOM, // Custom allocator
HYBRID // Hybrid approach
};
// Error handling policies
enum class MemoryErrorPolicy {
SILENT, // No error reporting
LOG_ONLY, // Log errors but continue
ASSERT_DEBUG, // Assert in debug builds
ASSERT_ALWAYS, // Always assert
EXCEPTION // Throw exceptions
};
// Default configuration template
template<
bool EnableTracking = MEMORY_DEBUG_ENABLED,
bool EnableAlignment = true,
bool EnablePadding = MEMORY_DEBUG_ENABLED,
ThreadSafetyPolicy ThreadPolicy = ThreadSafetyPolicy::STD_ATOMIC,
MemoryTrackingLevel TrackingLevel = EnableTracking ? MemoryTrackingLevel::BASIC : MemoryTrackingLevel::NONE,
MemoryAlignmentPolicy AlignmentPolicy = MemoryAlignmentPolicy::STANDARD,
MemoryPaddingPolicy PaddingPolicy = EnablePadding ? MemoryPaddingPolicy::DEBUG_ONLY : MemoryPaddingPolicy::NONE,
MemoryAllocationStrategy AllocationStrategy = MemoryAllocationStrategy::SYSTEM_DEFAULT,
MemoryErrorPolicy ErrorPolicy = MEMORY_DEBUG_ENABLED ? MemoryErrorPolicy::ASSERT_DEBUG : MemoryErrorPolicy::LOG_ONLY
>
struct MemoryConfig {
// Core features
static constexpr bool ENABLE_TRACKING = EnableTracking;
static constexpr bool ENABLE_ALIGNMENT = EnableAlignment;
static constexpr bool ENABLE_PADDING = EnablePadding;
// Policies
static constexpr ThreadSafetyPolicy THREAD_POLICY = ThreadPolicy;
static constexpr MemoryTrackingLevel TRACKING_LEVEL = TrackingLevel;
static constexpr MemoryAlignmentPolicy ALIGNMENT_POLICY = AlignmentPolicy;
static constexpr MemoryPaddingPolicy PADDING_POLICY = PaddingPolicy;
static constexpr MemoryAllocationStrategy ALLOCATION_STRATEGY = AllocationStrategy;
static constexpr MemoryErrorPolicy ERROR_POLICY = ErrorPolicy;
// Memory layout constants (similar to Godot's layout)
static constexpr memory_size_t SIZE_OFFSET = 0;
static constexpr memory_size_t ELEMENT_OFFSET =
((SIZE_OFFSET + sizeof(memory_uint64_t)) % alignof(memory_uint64_t) == 0) ?
(SIZE_OFFSET + sizeof(memory_uint64_t)) :
((SIZE_OFFSET + sizeof(memory_uint64_t)) + alignof(memory_uint64_t) -
((SIZE_OFFSET + sizeof(memory_uint64_t)) % alignof(memory_uint64_t)));
static constexpr memory_size_t DATA_OFFSET =
((ELEMENT_OFFSET + sizeof(memory_uint64_t)) % alignof(std::max_align_t) == 0) ?
(ELEMENT_OFFSET + sizeof(memory_uint64_t)) :
((ELEMENT_OFFSET + sizeof(memory_uint64_t)) + alignof(std::max_align_t) -
((ELEMENT_OFFSET + sizeof(memory_uint64_t)) % alignof(std::max_align_t)));
// Performance tuning
static constexpr memory_size_t DEFAULT_ALIGNMENT = alignof(std::max_align_t);
static constexpr memory_size_t CACHE_LINE_SIZE = 64; // Common cache line size
static constexpr memory_size_t PAGE_SIZE = 4096; // Common page size
// Validation
static_assert(ELEMENT_OFFSET >= SIZE_OFFSET + sizeof(memory_uint64_t), "Invalid element offset");
static_assert(DATA_OFFSET >= ELEMENT_OFFSET + sizeof(memory_uint64_t), "Invalid data offset");
};
// Predefined configurations
using DefaultConfig = MemoryConfig<>;
using HighPerformanceConfig = MemoryConfig<
false, // EnableTracking
false, // EnableAlignment
false, // EnablePadding
ThreadSafetyPolicy::NONE, // ThreadPolicy
MemoryTrackingLevel::NONE, // TrackingLevel
MemoryAlignmentPolicy::NONE, // AlignmentPolicy
MemoryPaddingPolicy::NONE, // PaddingPolicy
MemoryAllocationStrategy::SYSTEM_DEFAULT, // AllocationStrategy
MemoryErrorPolicy::SILENT // ErrorPolicy
>;
using DebugConfig = MemoryConfig<
true, // EnableTracking
true, // EnableAlignment
true, // EnablePadding
ThreadSafetyPolicy::STD_ATOMIC, // ThreadPolicy
MemoryTrackingLevel::DETAILED, // TrackingLevel
MemoryAlignmentPolicy::STANDARD, // AlignmentPolicy
MemoryPaddingPolicy::ALWAYS, // PaddingPolicy
MemoryAllocationStrategy::SYSTEM_DEFAULT, // AllocationStrategy
MemoryErrorPolicy::ASSERT_ALWAYS // ErrorPolicy
>;
using EmbeddedConfig = MemoryConfig<
false, // EnableTracking
false, // EnableAlignment
false, // EnablePadding
ThreadSafetyPolicy::NONE, // ThreadPolicy
MemoryTrackingLevel::NONE, // TrackingLevel
MemoryAlignmentPolicy::NONE, // AlignmentPolicy
MemoryPaddingPolicy::NONE, // PaddingPolicy
MemoryAllocationStrategy::POOLED, // AllocationStrategy
MemoryErrorPolicy::SILENT // ErrorPolicy
>;
using ThreadSafeConfig = MemoryConfig<
true, // EnableTracking
true, // EnableAlignment
true, // EnablePadding
ThreadSafetyPolicy::STD_ATOMIC, // ThreadPolicy
MemoryTrackingLevel::BASIC, // TrackingLevel
MemoryAlignmentPolicy::STANDARD, // AlignmentPolicy
MemoryPaddingPolicy::DEBUG_ONLY, // PaddingPolicy
MemoryAllocationStrategy::SYSTEM_DEFAULT, // AllocationStrategy
MemoryErrorPolicy::ASSERT_DEBUG // ErrorPolicy
>;
// Runtime configuration (for dynamic settings)
struct MemoryRuntimeConfig {
// Hooks
using AllocationHook = void(*)(void* ptr, memory_size_t size, const char* context);
using DeallocationHook = void(*)(void* ptr, memory_size_t size, const char* context);
using ReallocHook = void(*)(void* old_ptr, void* new_ptr, memory_size_t old_size, memory_size_t new_size, const char* context);
// Runtime settings
bool enable_hooks = false;
AllocationHook allocation_hook = nullptr;
DeallocationHook deallocation_hook = nullptr;
ReallocHook realloc_hook = nullptr;
// Memory limits
memory_size_t max_memory_usage = 0; // 0 = unlimited
memory_size_t warning_threshold = 0; // 0 = no warning
// Debug settings
bool enable_leak_detection = false;
bool enable_double_free_detection = false;
bool enable_bounds_checking = false;
// Performance settings
memory_size_t small_allocation_threshold = 256;
memory_size_t large_allocation_threshold = 1024 * 1024; // 1MB
// Singleton access
static MemoryRuntimeConfig& instance() {
static MemoryRuntimeConfig config;
return config;
}
};
// Configuration validation helpers
template<typename Config>
constexpr bool validate_config() {
// Check for logical inconsistencies
if constexpr (Config::ENABLE_TRACKING && Config::TRACKING_LEVEL == MemoryTrackingLevel::NONE) {
return false;
}
if constexpr (Config::ENABLE_ALIGNMENT && Config::ALIGNMENT_POLICY == MemoryAlignmentPolicy::NONE) {
return false;
}
if constexpr (Config::ENABLE_PADDING && Config::PADDING_POLICY == MemoryPaddingPolicy::NONE) {
return false;
}
return true;
}
// Compile-time configuration validation
#define MEMORY_VALIDATE_CONFIG(Config) \
static_assert(validate_config<Config>(), "Invalid memory configuration");
// Validate predefined configurations
MEMORY_VALIDATE_CONFIG(DefaultConfig);
MEMORY_VALIDATE_CONFIG(HighPerformanceConfig);
MEMORY_VALIDATE_CONFIG(DebugConfig);
MEMORY_VALIDATE_CONFIG(EmbeddedConfig);
MEMORY_VALIDATE_CONFIG(ThreadSafeConfig);