summaryrefslogtreecommitdiff
path: root/indra/llcommon/llalignedarray.h
blob: 8248f8218669673eac5931301cc87cc6c28a318f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
 * @file llalignedarray.h
 * @brief A static array which obeys alignment restrictions and mimics std::vector accessors.
 *
 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_LLALIGNEDARRAY_H
#define LL_LLALIGNEDARRAY_H

#include "llmemory.h"

template <class T, U32 alignment>
class LLAlignedArray
{
public:
    T* mArray;
    U32 mElementCount;
    U32 mCapacity;

    LLAlignedArray();
    ~LLAlignedArray();

    void push_back(const T& elem);
    U32 size() const { return mElementCount; }
    void resize(U32 size);
    T* append(S32 N);
    T& operator[](int idx);
    const T& operator[](int idx) const;
};

template <class T, U32 alignment>
LLAlignedArray<T, alignment>::LLAlignedArray()
{
    llassert(alignment >= 16);
    mArray = NULL;
    mElementCount = 0;
    mCapacity = 0;
}

template <class T, U32 alignment>
LLAlignedArray<T, alignment>::~LLAlignedArray()
{
    ll_aligned_free<alignment>(mArray);
    mArray = NULL;
    mElementCount = 0;
    mCapacity = 0;
}

template <class T, U32 alignment>
void LLAlignedArray<T, alignment>::push_back(const T& elem)
{
    T* old_buf = NULL;
    if (mCapacity <= mElementCount)
    {
        mCapacity++;
        mCapacity *= 2;
        T* new_buf = (T*) ll_aligned_malloc<alignment>(mCapacity*sizeof(T));
        if (mArray)
        {
            ll_memcpy_nonaliased_aligned_16((char*)new_buf, (char*)mArray, sizeof(T)*mElementCount);
        }
        old_buf = mArray;
        mArray = new_buf;
    }

    mArray[mElementCount++] = elem;

    //delete old array here to prevent error on a.push_back(a[0])
    ll_aligned_free<alignment>(old_buf);
}

template <class T, U32 alignment>
void LLAlignedArray<T, alignment>::resize(U32 size)
{
    if (mCapacity < size)
    {
        mCapacity = size+mCapacity*2;
        T* new_buf = mCapacity > 0 ? (T*) ll_aligned_malloc<alignment>(mCapacity*sizeof(T)) : NULL;
        if (mArray)
        {
            ll_memcpy_nonaliased_aligned_16((char*) new_buf, (char*) mArray, sizeof(T)*mElementCount);
            ll_aligned_free<alignment>(mArray);
        }

        /*for (U32 i = mElementCount; i < mCapacity; ++i)
        {
            new(new_buf+i) T();
        }*/
        mArray = new_buf;
    }

    mElementCount = size;
}


template <class T, U32 alignment>
T& LLAlignedArray<T, alignment>::operator[](int idx)
{
    if (idx < 0 || unsigned(idx) >= mElementCount)
    {
        LL_ERRS() << "Out of bounds LLAlignedArray, requested: " << (S32)idx << " size: " << mElementCount << LL_ENDL;
    }
    return mArray[idx];
}

template <class T, U32 alignment>
const T& LLAlignedArray<T, alignment>::operator[](int idx) const
{
    if (idx < 0 || unsigned(idx) >= mElementCount)
    {
        LL_ERRS() << "Out of bounds LLAlignedArray, requested: " << (S32)idx << " size: " << mElementCount << LL_ENDL;
    }
    return mArray[idx];
}

template <class T, U32 alignment>
T* LLAlignedArray<T, alignment>::append(S32 N)
{
    U32 sz = size();
    resize(sz+N);
    return &((*this)[sz]);
}

#endif