Custom New With Alignment
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
#include <cassert>
#include <cstdint>
#include <iostream>
#include <malloc.h>
#include <new>
/*void* operator new(std::size_t size) {
auto ptr = malloc(size);
if (!ptr)
throw std::bad_alloc{};
std::cout << "new: " << size << ", ptr: " << ptr << '\n';
return ptr;
}
void operator delete(void* ptr, std::size_t size) {
std::cout << "delete: " << size << ", ptr: " << ptr << '\n';
free(ptr);
}
void operator delete(void* ptr) {
std::cout << "delete: ptr: " << ptr << '\n';
free(ptr);
}*/
void* operator new(std::size_t size, std::align_val_t align) {
#if defined(_WIN32) || defined(__CYGWIN__)
auto ptr = _aligned_malloc(size, static_cast<std::size_t>(align));
#else
auto ptr = aligned_alloc(static_cast<std::size_t>(align), size);
#endif
if (!ptr)
throw std::bad_alloc{};
std::cout << "new: " << size << ", align: " << static_cast<std::size_t>(align) << ", ptr: " << ptr << '\n';
return ptr;
}
void operator delete(void* ptr, std::size_t size, std::align_val_t align) noexcept {
std::cout << "delete: " << size << ", align: " << static_cast<std::size_t>(align) << ", ptr : " << ptr << '\n';
#if defined(_WIN32) || defined(__CYGWIN__)
_aligned_free(ptr);
#else
free(ptr);
#endif
}
void operator delete(void* ptr, std::align_val_t align) noexcept {
std::cout << "delete: align: " << static_cast<std::size_t>(align) << ", ptr : " << ptr << '\n';
#if defined(_WIN32) || defined(__CYGWIN__)
Enter to Rename, Shift+Enter to Preview