Files
snmalloc/src/test/func/fixed_region/fixed_region.cc
Matthew Parkinson 4e1f5829a7 Change default chunksize to 1MiB (#229)
This change makes the original 16MiB option not the common option.

It also changes the names of the defines to
  SNMALLOC_USE_LARGE_CHUNKS
  SNMALLOC_USE_SMALL_CHUNKS

The second should be set for Open Enclave configuration, and results in
256KiB chunk sizes.  The first being set builds the original 16MiB chunk
sizes.  If neither is set, then we default to 1MiB chunk sizes.
2020-07-09 13:22:32 +01:00

55 lines
1.3 KiB
C++

#define SNMALLOC_SGX
#define OPEN_ENCLAVE
#define OPEN_ENCLAVE_SIMULATION
#include <iostream>
#include <snmalloc.h>
#ifdef assert
# undef assert
#endif
#define assert please_use_SNMALLOC_ASSERT
extern "C" void* oe_memset_s(void* p, size_t p_size, int c, size_t size)
{
UNUSED(p_size);
return memset(p, c, size);
}
extern "C" void oe_abort()
{
abort();
}
using namespace snmalloc;
int main()
{
auto& mp = *MemoryProviderStateMixin<DefaultPal>::make();
// 28 is large enough to produce a nested allocator.
// It is also large enough for the example to run in.
// For 1MiB superslabs, SUPERSLAB_BITS + 4 is not big enough for the example.
size_t large_class = 28 - SUPERSLAB_BITS;
size_t size = 1ULL << (SUPERSLAB_BITS + large_class);
void* oe_base = mp.reserve<true>(large_class);
void* oe_end = (uint8_t*)oe_base + size;
PALOpenEnclave::setup_initial_range(oe_base, oe_end);
std::cout << "Allocated region " << oe_base << " - " << oe_end << std::endl;
auto a = ThreadAlloc::get();
while (true)
{
auto r1 = a->alloc(100);
// Run until we exhaust the fixed region.
// This should return null.
if (r1 == nullptr)
return 0;
if (oe_base > r1)
abort();
if (oe_end < r1)
abort();
}
}