[go: up one dir, main page]

Menu

[8bf46b]: / common / aligned_alloc.h  Maximize  Restore  History

Download this file

48 lines (36 with data), 1.6 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
#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"
__STXXL_BEGIN_NAMESPACE
template < size_t ALIGNMENT >
inline void * aligned_alloc (size_t size, size_t meta_info_size = 0)
{
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<">(), size = "<<size<<", meta info size = "<<meta_info_size)
char *buffer = new char[size + ALIGNMENT + sizeof(char*) + meta_info_size];
char *reserve_buffer = buffer + sizeof(char*) + meta_info_size;
char *result = reserve_buffer + ALIGNMENT -
(((unsigned long) reserve_buffer) % (ALIGNMENT)) - meta_info_size;
STXXL_VERBOSE1("stxxl::aligned_alloc<"<<ALIGNMENT <<">() address 0x"<<std::hex<<long(result)
<< std::dec<<" lost "<<unsigned(result-buffer)<<" bytes")
assert( int(result-buffer) >= int(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)
return result;
};
template < size_t ALIGNMENT > inline void
aligned_dealloc (void *ptr)
{
STXXL_VERBOSE2("stxxl::aligned_dealloc(<"<<ALIGNMENT <<">), ptr = 0x"<<std::hex<<(unsigned long)(ptr)<<std::dec)
delete[] * (((char **) ptr) - 1);
};
__STXXL_END_NAMESPACE
#endif