-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemoryalloc.cpp
43 lines (36 loc) · 911 Bytes
/
memoryalloc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//@ {
//@ "targets":[{"name":"memoryalloc.o","type":"object"
//@ ,"dependencies":[{"ref":"jemalloc","rel":"external"}]}]
//@ }
#include "memoryalloc.hpp"
#include "error.hpp"
#include <cstdlib>
#include <cstdint>
#include <jemalloc/jemalloc.h>
using namespace Anja;
#ifdef __AVX__
static constexpr int ALIGNMENT=32;
#else
static constexpr int ALIGNMENT=16;
#endif
void* Anja::memoryAllocate(size_t N)
{
void* ret=mallocx(N,MALLOCX_ALIGN(ALIGNMENT));
if(ret==NULL)
{throw Error("Failed to allocate more memory");}
return ret;
}
void Anja::memoryFree(void* buffer)
{
if(buffer!=NULL)
{dallocx(buffer,MALLOCX_ALIGN(ALIGNMENT));}
}
void* Anja::memoryRealloc(void* buffer,size_t N)
{
if(buffer==nullptr)
{return memoryAllocate(N);}
void* ret=rallocx(buffer,N,MALLOCX_ALIGN(ALIGNMENT));
if(ret==nullptr)
{throw Error("It was not possible to extend current memory block");}
return ret;
}