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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#define MEM_ALLOC_NO_GUARDS 1
#include "axml.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <malloc.h>
#endif
#ifdef _RAXML_USE_LLALLOC
// the llalloc library implementation in lockless_alloc/ll_alloc.c exports the alloction functions prefixed
// with 'llalloc'. The following are the forward declarations of the llalloc* functions
#define PREFIX(X) llalloc##X
void *PREFIX(memalign)(size_t align, size_t size);
void *PREFIX(malloc)(size_t size);
void *PREFIX(realloc)(void *p, size_t size);
int PREFIX(posix_memalign)(void **p, size_t align, size_t size);
void *PREFIX(calloc)(size_t n, size_t size);
void PREFIX(free)(void *p);
// wrappers that forward the rax_* functions to the corresponding llalloc* functions
void *rax_memalign(size_t align, size_t size)
{
return PREFIX(memalign)(align, size);
}
void *rax_malloc( size_t size )
{
return PREFIX(malloc)(size);
}
void *rax_realloc( void *p, size_t size ) {
return PREFIX(realloc)(p, size);
}
void rax_free(void *p) {
PREFIX(free)(p);
}
int rax_posix_memalign(void **p, size_t align, size_t size) {
return PREFIX(posix_memalign)(p, align, size);
}
void *rax_calloc(size_t n, size_t size) {
return PREFIX(calloc)(n,size);
}
void *rax_malloc_aligned(size_t size)
{
const size_t BYTE_ALIGNMENT = 32;
return rax_memalign(BYTE_ALIGNMENT, size);
}
#else
// if llalloc should not be used, forward the rax_* functions to the corresponding standard function
static void outOfMemory(void)
{
printf("RAxML was not able to allocate enough memory.\n");
printf("Please check the approximate memory consumption of your dataset using\n");
printf("the memory calculator at http://www.exelixis-lab.org/web/software/raxml/index.html.\n");
printf("RAxML will exit now\n");
errorExit(-1);
}
void *rax_malloc( size_t size )
{
#ifndef WIN32
void
*ptr = (void *)NULL;
int
res = posix_memalign(&ptr, BYTE_ALIGNMENT, size);
if(res != 0)
{
outOfMemory();
assert(0);
}
return ptr;
#else
return _aligned_malloc(size, BYTE_ALIGNMENT);
#endif
}
void *rax_realloc(void *p, size_t size, boolean needsMemoryAlignment)
{
//it's actually not that easy to implement an aligned realloc
//because we need to know the size of the array pointed to by
//the pointer passed as argument
//hence I added this boolean flag that should increase programmer
//awareness about this issue
void
*ptr = (void *)NULL;
if(needsMemoryAlignment)
{
assert(0);
return (void*)NULL;
}
else
{
#ifndef WIN32
ptr = realloc(p, size);
#else
ptr = _aligned_realloc(p, size, BYTE_ALIGNMENT);
#endif
}
if(ptr == (void*)NULL)
{
outOfMemory();
assert(0);
}
return ptr;
}
void rax_free(void *p)
{
#ifndef WIN32
free(p);
#else
_aligned_free(p);
#endif
}
void *rax_calloc(size_t n, size_t size)
{
void
*ptr = rax_malloc(size * n);
memset(ptr, 0, size * n);
return ptr;
}
#endif
|