# xalloc

C++ Embedded allocator with minimal memory footprint. Heavily inspired by [rhempel/umm_malloc](https://github.com/rhempel/umm_malloc).

Not thouroughly tested, so do not use unless you want to debug corrupted memory.

## usage

### config.hpp
```c++
#ifndef _CONFIG
#define _CONFIG

namespace config{
	namespace heap {
		static constexpr auto blockSize = 4;
		static constexpr bool enableMagicChecking = true;
		static constexpr bool enableAssert = true;
	}
}

#endif
```

### application.cpp
```c++
#include "heap.hpp"
#include <memory>

// create pool for data allocation
xly::heap::Block pool[128];
// or in dedicated section (depends on linker script)
// xly::heap::Block pool[size] __attribute__ ((section ("heap"))); //where size must fit into that section

// create instance of allocator
xly::heap::Allocator allocator(pool, std::size(pool));


// override default new/delete calls and map them to our allocator
// define in **only one** source file!
void* operator new(std::size_t sz) {
	return allocator.xnew(sz, __STDCPP_DEFAULT_NEW_ALIGNMENT__);
}
void* operator new[](std::size_t sz, std::align_val_t alignment) {
	return allocator.xnew(sz, static_cast<std::size_t>(alignment));
}
void* operator new(std::size_t sz, std::align_val_t alignment) {
	return allocator.xnew(sz, static_cast<std::size_t>(alignment));
}
void operator delete(void* ptr, std::size_t sz) {
	allocator.xdelete(ptr);
}

void operator delete(void* ptr) noexcept {
	allocator.xdelete(ptr);
}

void operator delete[](void* ptr, std::size_t sz) {
	allocator.xdelete(ptr);
}

void operator delete[](void* ptr) {
	allocator.xdelete(ptr);
}

//application code
int foo(void) {

	// ...

	// dont't be stupid and use smart pointers (unique_ptr/shared_ptr)...
	auto bar = std::unique_ptr<std::uint32_t>(new std::uint32_t {0xDEADBEEF});

	// ...;
}
```