It's my first allocator ever • December 29, 2025
Probably my last project of 2025, wishing you a great end of year!
It's not very impressive, but we all start somewhere, right?
I also plan to learn and understand all the allocators
in order to create a small allocator library using TMP and CRTP Mixin ;)
Max Point objects in memory: 2305843009213693951(0,10)(1,11)(2,12)(3,13)(4,14)(5,15)(6,16)(7,17)(8,18)(9,19)// Copyright (c) December 2025 Félix-Olivier Dumas. All rights reserved.// Licensed under the terms described in the LICENSE file
#include<iostream>structPoint {
int x, y;
Point(int a, int b) : x(a), y(b) {}
voidprint() { std::cout << "(" << x << "," << y << ")\n"; }
};
template<typename T>
structAllocator {
T* allocate(std::size_t n) {
returnstatic_cast<T*>(::operatornew(sizeof(T) * n));
}
voiddeallocate(T* ptr, std::size_t n) {
::operatordelete(ptr);
}
template<typename... Args>
voidconstruct(T* ptr, Args&&... args) {
new (ptr) T(std::forward<Args>(args)...);
}
voiddestroy(T* ptr) {
ptr->~T();
}
std::size_tmax_size() const {
return std::numeric_limits<std::size_t>::max() / sizeof(T);
}
};intmain() {
std::size_t n = 10;
Allocator<Point> alloc;
Point* base = alloc.allocate(n);
std::cout << "Max Point objects in memory: "
<< alloc.max_size() << "\n";
for (std::size_t i = 0; i < n; i++)
alloc.construct(base + i, i, i + 10);
for (std::size_t i = 0; i < n; i++)
base[i].print();
for (std::size_t i = 0; i < n; i++)
alloc.destroy(base);
alloc.deallocate(base, n);
}