mirror of
https://github.com/paboyle/Grid.git
synced 2025-04-10 22:20:45 +01:00
94 lines
3.4 KiB
C++
94 lines
3.4 KiB
C++
/*************************************************************************************
|
|
|
|
Grid physics library, www.github.com/paboyle/Grid
|
|
|
|
Source file: ./lib/AlignedAllocator.h
|
|
|
|
Copyright (C) 2015
|
|
|
|
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
|
|
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
See the full license in the file "LICENSE" in the top level distribution directory
|
|
*************************************************************************************/
|
|
/* END LEGAL */
|
|
#pragma once
|
|
|
|
NAMESPACE_BEGIN(Grid);
|
|
|
|
template<typename _Tp>
|
|
class alignedAllocator {
|
|
public:
|
|
typedef std::size_t size_type;
|
|
typedef std::ptrdiff_t difference_type;
|
|
typedef _Tp* pointer;
|
|
typedef const _Tp* const_pointer;
|
|
typedef _Tp& reference;
|
|
typedef const _Tp& const_reference;
|
|
typedef _Tp value_type;
|
|
|
|
template<typename _Tp1> struct rebind { typedef alignedAllocator<_Tp1> other; };
|
|
alignedAllocator() throw() { }
|
|
alignedAllocator(const alignedAllocator&) throw() { }
|
|
template<typename _Tp1> alignedAllocator(const alignedAllocator<_Tp1>&) throw() { }
|
|
~alignedAllocator() throw() { }
|
|
pointer address(reference __x) const { return &__x; }
|
|
size_type max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
|
|
|
|
pointer allocate(size_type __n, const void* _p= 0)
|
|
{
|
|
size_type bytes = __n*sizeof(_Tp);
|
|
|
|
profilerAllocate(bytes);
|
|
|
|
_Tp *ptr = (_Tp*) MemoryManager::CpuAllocate(bytes);
|
|
|
|
assert( ( (_Tp*)ptr != (_Tp *)NULL ) );
|
|
|
|
return ptr;
|
|
}
|
|
|
|
void deallocate(pointer __p, size_type __n)
|
|
{
|
|
size_type bytes = __n * sizeof(_Tp);
|
|
|
|
profilerFree(bytes);
|
|
|
|
MemoryManager::CpuFree((void *)__p,bytes);
|
|
}
|
|
|
|
// FIXME: hack for the copy constructor, eventually it must be avoided
|
|
void construct(pointer __p, const _Tp& __val) { new((void *)__p) _Tp(__val); };
|
|
//void construct(pointer __p, const _Tp& __val) { };
|
|
void construct(pointer __p) { };
|
|
void destroy(pointer __p) { };
|
|
};
|
|
template<typename _Tp> inline bool operator==(const alignedAllocator<_Tp>&, const alignedAllocator<_Tp>&){ return true; }
|
|
template<typename _Tp> inline bool operator!=(const alignedAllocator<_Tp>&, const alignedAllocator<_Tp>&){ return false; }
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// Template typedefs
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
template<class T> using commAllocator = alignedAllocator<T>;
|
|
template<class T> using Vector = std::vector<T,alignedAllocator<T> >;
|
|
template<class T> using commVector = std::vector<T,alignedAllocator<T> >;
|
|
template<class T> using Matrix = std::vector<std::vector<T,alignedAllocator<T> > >;
|
|
|
|
NAMESPACE_END(Grid);
|
|
|
|
|