summaryrefslogtreecommitdiff
path: root/sc/inc/stlalgorithm.hxx
blob: 3c7014446fa41afe55ee528a9de318436fa0931c (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_SC_INC_STLALGORITHM_HXX
#define INCLUDED_SC_INC_STLALGORITHM_HXX

#include <functional>
#include <limits>

#include <rtl/alloc.h>

namespace sc {

/**
 * Custom allocator for STL container to ensure that the base address of
 * allocated storage is aligned to a specified boundary.
 */
template<typename T, size_t _Alignment>
class AlignedAllocator
{
public:
    typedef T value_type;
    typedef size_t size_type;
    typedef std::ptrdiff_t difference_type;

    typedef T* pointer;
    typedef const T* const_pointer;
    typedef T* void_pointer;

    typedef T& reference;
    typedef const T& const_reference;

    template<typename _Type2>
    struct rebind
    {
        typedef AlignedAllocator<_Type2,_Alignment> other;
    };

    AlignedAllocator() {}
    ~AlignedAllocator() {}

    template<typename _Type2>
    AlignedAllocator(const AlignedAllocator<_Type2,_Alignment>&) {}

    void construct(T* p, const value_type& val) { new(p) value_type(val); }
    void destroy(T* p)
    {
        p->~value_type();
        (void)p; // avoid bogus MSVC '12 "unreferenced formal parameter" warning
    }

    size_type max_size() const
    {
        return std::numeric_limits<size_type>::max() / sizeof(value_type);
    }

    bool operator== (const AlignedAllocator&) const { return true; }
    bool operator!= (const AlignedAllocator&) const { return false; }

    pointer allocate(size_type n)
    {
        return (pointer)rtl_allocateAlignedMemory(_Alignment, n*sizeof(value_type));
    }

    void deallocate(pointer p, size_type)
    {
        rtl_freeAlignedMemory(p);
    }
};

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */