1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 #include <rtl/alloc.h>
19 * Custom allocator for STL container to ensure that the base address of
20 * allocated storage is aligned to a specified boundary.
22 template<typename T
, size_t Alignment
>
23 class AlignedAllocator
27 typedef size_t size_type
;
28 typedef std::ptrdiff_t difference_type
;
31 typedef const T
* const_pointer
;
32 typedef T
* void_pointer
;
35 typedef const T
& const_reference
;
37 template<typename Type2
>
40 typedef AlignedAllocator
<Type2
,Alignment
> other
;
45 template<typename Type2
>
46 AlignedAllocator(const AlignedAllocator
<Type2
,Alignment
>&) {}
48 static void construct(T
* p
, const value_type
& val
) { new(p
) value_type(val
); }
49 static void destroy(T
* p
)
54 static size_type
max_size()
56 return std::numeric_limits
<size_type
>::max() / sizeof(value_type
);
59 bool operator== (const AlignedAllocator
&) const { return true; }
60 bool operator!= (const AlignedAllocator
&) const { return false; }
62 static pointer
allocate(size_type n
)
64 return static_cast<pointer
>(rtl_allocateAlignedMemory(Alignment
, n
*sizeof(value_type
)));
67 static void deallocate(pointer p
, size_type
)
69 rtl_freeAlignedMemory(p
);
75 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */