Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #ifndef INCLUDED_SC_INC_STLALGORITHM_HXX
11 : #define INCLUDED_SC_INC_STLALGORITHM_HXX
12 :
13 : #include <functional>
14 : #include <limits>
15 :
16 : #include <rtl/alloc.h>
17 :
18 : namespace sc {
19 :
20 : /**
21 : * Custom allocator for STL container to ensure that the base address of
22 : * allocated storage is aligned to a specified boundary.
23 : */
24 : template<typename T, size_t _Alignment>
25 : class AlignedAllocator
26 : {
27 : public:
28 : typedef T value_type;
29 : typedef size_t size_type;
30 : typedef std::ptrdiff_t difference_type;
31 :
32 : typedef T* pointer;
33 : typedef const T* const_pointer;
34 : typedef T* void_pointer;
35 :
36 : typedef T& reference;
37 : typedef const T& const_reference;
38 :
39 : template<typename _Type2>
40 : struct rebind
41 : {
42 : typedef AlignedAllocator<_Type2,_Alignment> other;
43 : };
44 :
45 24 : AlignedAllocator() {}
46 48 : ~AlignedAllocator() {}
47 :
48 : template<typename _Type2>
49 : AlignedAllocator(const AlignedAllocator<_Type2,_Alignment>&) {}
50 :
51 196 : void construct(T* p, const value_type& val) { new(p) value_type(val); }
52 196 : void destroy(T* p)
53 : {
54 : p->~value_type();
55 : (void)p; // avoid bogus MSVC '12 "unreferenced formal parameter" warning
56 196 : }
57 :
58 8 : size_type max_size() const
59 : {
60 8 : return std::numeric_limits<size_type>::max() / sizeof(value_type);
61 : }
62 :
63 : bool operator== (const AlignedAllocator&) const { return true; }
64 : bool operator!= (const AlignedAllocator&) const { return false; }
65 :
66 28 : pointer allocate(size_type n)
67 : {
68 28 : return (pointer)rtl_allocateAlignedMemory(_Alignment, n*sizeof(value_type));
69 : }
70 :
71 28 : void deallocate(pointer p, size_type)
72 : {
73 28 : rtl_freeAlignedMemory(p);
74 28 : }
75 : };
76 :
77 : }
78 :
79 : #endif
80 :
81 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|