• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

aligned_alloc.h

00001 /***************************************************************************
00002  *  include/stxxl/bits/common/aligned_alloc.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <[email protected]>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #ifndef STXXL_ALIGNED_ALLOC
00014 #define STXXL_ALIGNED_ALLOC
00015 
00016 #include <stxxl/bits/common/utils.h>
00017 
00018 
00019 __STXXL_BEGIN_NAMESPACE
00020 
00021 template <size_t ALIGNMENT>
00022 inline void * aligned_alloc(size_t size, size_t meta_info_size = 0)
00023 {
00024     STXXL_VERBOSE1("stxxl::aligned_alloc<" << ALIGNMENT << ">(), size = " << size << ", meta info size = " << meta_info_size);
00025     char * buffer = new char[size + ALIGNMENT + sizeof(char *) + meta_info_size];
00026     #ifdef STXXL_ALIGNED_CALLOC
00027     memset(buffer, 0, size + ALIGNMENT + sizeof(char *) + meta_info_size);
00028     #endif
00029     char * reserve_buffer = buffer + sizeof(char *) + meta_info_size;
00030     char * result = reserve_buffer + ALIGNMENT -
00031                     (((unsigned long)reserve_buffer) % (ALIGNMENT)) - meta_info_size;
00032     STXXL_VERBOSE1("stxxl::aligned_alloc<" << ALIGNMENT << ">() address 0x" << std::hex << long(result)
00033                                            << std::dec << " lost " << unsigned(result - buffer) << " bytes");
00034     assert(int(result - buffer) >= int(sizeof(char *)));
00035     *(((char **)result) - 1) = buffer;
00036     STXXL_VERBOSE1("stxxl::aligned_alloc<" << ALIGNMENT <<
00037                    ">(), allocated at " << std::hex << ((unsigned long)buffer) << " returning " << ((unsigned long)result)
00038                                            << std::dec);
00039 
00040     return result;
00041 }
00042 
00043 template <size_t ALIGNMENT>
00044 inline void
00045 aligned_dealloc(void * ptr)
00046 {
00047     STXXL_VERBOSE2("stxxl::aligned_dealloc(<" << ALIGNMENT << ">), ptr = 0x" << std::hex << (unsigned long)(ptr) << std::dec);
00048     delete[] * (((char **)ptr) - 1);
00049 }
00050 
00051 __STXXL_END_NAMESPACE
00052 
00053 #endif // !STXXL_ALIGNED_ALLOC

Generated by  doxygen 1.7.1