[go: up one dir, main page]

Menu

[e222c1]: / common / aligned_alloc.h  Maximize  Restore  History

Download this file

96 lines (78 with data), 2.5 kB

 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
#ifndef ALIGNED_ALLOC
#define ALIGNED_ALLOC
/***************************************************************************
* aligned_alloc.h
*
* Sat Aug 24 23:53:12 2002
* Copyright 2002 Roman Dementiev
* dementiev@mpi-sb.mpg.de
****************************************************************************/
#include "../common/utils.h"
#ifdef STXXL_USE_POSIX_MEMALIGN_ALLOC
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <malloc.h>
#endif
__STXXL_BEGIN_NAMESPACE
#ifdef STXXL_USE_POSIX_MEMALIGN_ALLOC
template < size_t ALIGNMENT >
inline void * aligned_alloc (size_t size)
{
void * result;
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<
">() POSIX VERSION asking for "<<size<<" bytes");
int status = posix_memalign(&result,ALIGNMENT,size);
if(status != 0)
{
// error occured
switch(status)
{
case EINVAL:
STXXL_ERRMSG("posix_memalign call failed with status code EINVAL")
break;
case ENOMEM:
STXXL_ERRMSG("posix_memalign call failed with status code ENOMEM")
break;
default:
STXXL_ERRMSG("posix_memalign call failed with status code "<<status)
}
abort();
}
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<
">() POSIX VERSION asking for "<<size<<
" bytes, returning block starting at "<<result);
return result;
};
template < size_t ALIGNMENT > inline void
aligned_dealloc (void *ptr)
{
STXXL_VERBOSE1("stxxl::aligned_dealloc<"<<ALIGNMENT <<">() POSIX VERSION, ptr = "<<ptr)
free(ptr);
};
#else
template < size_t ALIGNMENT >
inline void * aligned_alloc (size_t size)
{
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<">(), size = "<<size)
// TODO: use posix_memalign() and free() on Linux
char *buffer = new char[size + ALIGNMENT];
char *result = buffer + ALIGNMENT - (((unsigned long) buffer) % (ALIGNMENT));
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<">() address "<<long(result)
<< " lost "<<unsigned(result-buffer)<<" bytes")
assert( unsigned(result-buffer) >= sizeof(char*) );
*(((char **) result) - 1) = buffer;
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<
">(), allocated at "<<std::hex <<((unsigned long)buffer)<<" returning "<< ((unsigned long)result)
<<std::dec)
//abort();
return result;
};
template < size_t ALIGNMENT > inline void
aligned_dealloc (void *ptr)
{
STXXL_VERBOSE2("stxxl::aligned_dealloc(<"<<ALIGNMENT <<">), ptr = "<<long(ptr))
delete[] * (((char **) ptr) - 1);
};
#endif
__STXXL_END_NAMESPACE
#endif