Custom New With Alignment
#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__)
_aligned_free(ptr);
#else
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX