source: trunk/GDE/RAxML/mem_alloc.c

Last change on this file was 13845, checked in by westram, 10 years ago
  • remove executable flag
File size: 2.3 KB
Line 
1
2#define MEM_ALLOC_NO_GUARDS 1
3
4#include "axml.h"
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#if !defined(__APPLE__)
11#include <malloc.h>
12#endif
13
14
15#ifdef _RAXML_USE_LLALLOC
16
17// the llalloc library implementation in lockless_alloc/ll_alloc.c exports the alloction functions prefixed
18// with 'llalloc'. The following are the forward declarations of the llalloc* functions
19
20#define PREFIX(X)   llalloc##X
21
22void *PREFIX(memalign)(size_t align, size_t size);
23void *PREFIX(malloc)(size_t size);
24void *PREFIX(realloc)(void *p, size_t size);
25int PREFIX(posix_memalign)(void **p, size_t align, size_t size);
26void *PREFIX(calloc)(size_t n, size_t size);
27void PREFIX(free)(void *p);
28
29
30// wrappers that forward the rax_* functions to the corresponding llalloc* functions
31
32
33void *rax_memalign(size_t align, size_t size) 
34{
35  return PREFIX(memalign)(align, size);
36}
37
38void *rax_malloc( size_t size ) 
39{
40  return PREFIX(malloc)(size);
41}
42void *rax_realloc( void *p, size_t size ) {
43  return PREFIX(realloc)(p, size);
44}
45
46
47void rax_free(void *p) {
48  PREFIX(free)(p);
49}
50
51int rax_posix_memalign(void **p, size_t align, size_t size) {
52  return PREFIX(posix_memalign)(p, align, size);
53}
54void *rax_calloc(size_t n, size_t size) {
55  return PREFIX(calloc)(n,size);
56}
57
58void *rax_malloc_aligned(size_t size) 
59{
60  const size_t BYTE_ALIGNMENT = 32;
61  return rax_memalign(BYTE_ALIGNMENT, size);
62 
63}
64
65#else
66// if llalloc should not be used, forward the rax_* functions to the corresponding standard function
67
68
69
70void *rax_malloc( size_t size ) 
71{
72  void 
73    *ptr = (void *)NULL;
74 
75  int 
76    res = posix_memalign(&ptr, BYTE_ALIGNMENT, size);
77
78  if(res != 0) 
79    assert(0);
80 
81  return ptr;
82}
83
84void *rax_realloc(void *p, size_t size, boolean needsMemoryAlignment) 
85{ 
86  //it's actually not that easy to implement an aligned realloc
87  //because we need to know the size of the array pointed to by
88  //the pointer passed as argument
89  //hence I added this boolean flag that should increase programmer
90  //awareness about this issue
91 
92  if(needsMemoryAlignment)
93    {
94      assert(0);
95      return (void*)NULL;
96    }
97  else
98    return realloc(p, size);
99}
100
101void rax_free(void *p) 
102{
103  free(p);
104}
105
106void *rax_calloc(size_t n, size_t size) 
107{
108   void 
109     *ptr = rax_malloc(size * n);
110
111   if (ptr == (void*)NULL) 
112     return (void*)NULL;
113
114   memset(ptr, 0, size * n);
115
116   return ptr;
117}
118
119
120
121#endif
122
123
124
Note: See TracBrowser for help on using the repository browser.