lokinet/llarp/mem.hpp

24 lines
442 B
C++
Raw Normal View History

2018-01-26 14:17:51 +00:00
#ifndef LLARP_MEM_HPP
#define LLARP_MEM_HPP
2018-01-29 14:27:24 +00:00
#include <llarp/mem.h>
2018-02-01 13:21:00 +00:00
#include <cmath>
2018-01-29 14:27:24 +00:00
namespace llarp {
2018-02-01 13:21:00 +00:00
template <typename T>
static constexpr size_t alignment() {
size_t idx = 0;
size_t sz = sizeof(T);
2018-02-21 00:11:26 +00:00
while (sz) {
++idx;
sz >>= 1;
}
return 1 << idx;
2018-01-26 14:17:51 +00:00
}
2018-01-31 19:59:26 +00:00
2018-02-01 13:21:00 +00:00
template <typename T>
static T *Alloc(llarp_alloc *mem = &llarp_g_mem) {
return static_cast<T *>(mem->alloc(sizeof(T), alignment<T>()));
}
} // namespace llarp
2018-01-26 14:17:51 +00:00
#endif