Skip to content

Instantly share code, notes, and snippets.

@postwait
Created July 21, 2020 12:40
Show Gist options
  • Save postwait/beb586466818d789e89c90e07ade2b51 to your computer and use it in GitHub Desktop.
Save postwait/beb586466818d789e89c90e07ade2b51 to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2020, Circonus, Inc.
* All rights reserved.
*/
#ifndef SNOWTH_UTIL_MTEV_MEMORY_SAFE_OBJECT_HPP
#define SNOWTH_UTIL_MTEV_MEMORY_SAFE_OBJECT_HPP
#include <mtev_memory.h>
class mtev_memory_safe_c {
public:
static void * operator new(size_t size) { return mtev_memory_safe_malloc(size); }
static void operator delete(void *p) { mtev_memory_safe_free(p); }
};
class mtev_memory_safe_cleanup_c {
public:
static void * operator new(size_t size) { return mtev_memory_safe_malloc_cleanup(size, cleanup_op); }
virtual void mtev_memory_safe_cleanup() = 0;
static void operator delete(void *p) { mtev_memory_safe_free(p); }
private:
static void cleanup_op(void *p) {
mtev_memory_safe_cleanup_c *c = static_cast<mtev_memory_safe_cleanup_c *>(p);
c->mtev_memory_safe_cleanup();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment