Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Created December 3, 2018 17:34
Show Gist options
  • Save lambdageek/fa5b6bec1235ef4a59d6421d30baa5d8 to your computer and use it in GitHub Desktop.
Save lambdageek/fa5b6bec1235ef4a59d6421d30baa5d8 to your computer and use it in GitHub Desktop.
#include <new>
#include <glib.h>
#include <stdlib.h>
//#include <stdio.h>
// C++17 will also require:
// void* operator new ( std::size_t count, std::align_val_t al);
// void* operator new[]( std::size_t count, std::align_val_t al);
// void operator delete ( void* ptr, std::align_val_t al );
// void operator delete[]( void* ptr, std::align_val_t al );
static void *
our_allocator (size_t n);
static void
our_deallocator (void *p);
void *
operator new (size_t n) noexcept(false) {
return our_allocator (n);
}
void *
operator new[] (size_t n) noexcept(false) {
// printf ("array\n");
return our_allocator (n);
}
void *
operator new (size_t n, const std::nothrow_t&) noexcept (true) {
return our_allocator (n);
}
void
operator delete (void *p) noexcept(true) {
our_deallocator (p);
}
void
operator delete[] (void *p) noexcept(true) {
// printf ("free array\n");
our_deallocator (p);
}
static void *
our_allocator (size_t n)
{
void *p = g_malloc (n);
// printf ("alloc 0x%zx -> %p\n", n, p);
return p;
}
static void
our_deallocator (void *p)
{
// printf ("free %p\n", p);
g_free (p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment