Simple example of using two allocators in one application.

Example to simulate using two allocators in one application.  This will
be useful for scenarios in openenclave.
This commit is contained in:
Matthew Parkinson
2019-01-23 11:08:21 +00:00
parent 5240b3f8c6
commit 28a28bb1ed
3 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
#undef IS_ADDRESS_SPACE_CONSTRAINED
#define OPEN_ENCLAVE
#define OPEN_ENCLAVE_SIMULATION
#define USE_RESERVE_MULTIPLE 1
#define NO_BOOTSTRAP_ALLOCATOR
#define IS_ADDRESS_SPACE_CONSTRAINED
#define SNMALLOC_NAME_MANGLE(a) enclave_##a
// Redefine the namespace, so we can have two versions.
#define snmalloc snmalloc_enclave
#include "../../../override/malloc.cc"

View File

@@ -0,0 +1,6 @@
#undef IS_ADDRESS_SPACE_CONSTRAINED
#define SNMALLOC_NAME_MANGLE(a) host_##a
#define NO_BOOTSTRAP_ALLOCATOR
// Redefine the namespace, so we can have two versions.
#define snmalloc snmalloc_host
#include "../../../override/malloc.cc"

View File

@@ -0,0 +1,53 @@
#include "../../../snmalloc.h"
#include <iostream>
#include <stdlib.h>
#include <string.h>
void* oe_base;
void* oe_end;
extern "C" const void* __oe_get_heap_base()
{
return oe_base;
}
extern "C" const void* __oe_get_heap_end()
{
return oe_end;
}
extern "C" void* oe_memset(void* p, int c, size_t size)
{
return memset(p, c, size);
}
extern "C" void oe_abort()
{
abort();
}
extern "C" void* host_malloc(size_t);
extern "C" void host_free(void*);
extern "C" void* enclave_malloc(size_t);
extern "C" void enclave_free(void*);
using namespace snmalloc;
int main()
{
DefaultPal pal;
size_t size = 1ULL << 28;
oe_base = pal.reserve<true>(&size, 0);
oe_end = (uint8_t*)oe_base + size;
std::cout << "Allocated region " << oe_base << " - " << oe_end << std::endl;
auto a = host_malloc(128);
auto b = enclave_malloc(128);
std::cout << "Host alloc " << a << std::endl;
std::cout << "Enclave alloc " << b << std::endl;
host_free(a);
enclave_free(b);
}