• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / jorgen.edelbo_390

12 Aug 2024 12:13PM UTC coverage: 91.108% (+0.02%) from 91.085%
jorgen.edelbo_390

Pull #7979

Evergreen

jedelbo
Create test file in file-format 24
Pull Request #7979: Create test file in file-format 24

102766 of 181590 branches covered (56.59%)

19 of 19 new or added lines in 1 file covered. (100.0%)

1020 existing lines in 55 files now uncovered.

217365 of 238579 relevant lines covered (91.11%)

5621116.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.66
/src/realm/array_basic_tpl.hpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#ifndef REALM_ARRAY_BASIC_TPL_HPP
20
#define REALM_ARRAY_BASIC_TPL_HPP
21

22
#include <algorithm>
23
#include <limits>
24

25
#include <realm/array_basic.hpp>
26
#include <realm/node.hpp>
27

28
namespace realm {
29

30
template <class T>
31
inline BasicArray<T>::BasicArray(Allocator& allocator) noexcept
32
    : Node(allocator)
4,343,022✔
33
{
8,694,519✔
34
}
8,694,519✔
35

36
template <class T>
37
inline MemRef BasicArray<T>::create_array(size_t init_size, Allocator& allocator)
38
{
115,725✔
39
    size_t byte_size_0 = calc_aligned_byte_size(init_size); // Throws
115,725✔
40
    // Adding zero to Array::initial_capacity to avoid taking the
41
    // address of that member
42
    size_t byte_size = std::max(byte_size_0, Node::initial_capacity + 0); // Throws
115,725✔
43

44
    MemRef mem = allocator.alloc(byte_size); // Throws
115,725✔
45

46
    bool is_inner_bptree_node = false;
115,725✔
47
    bool has_refs = false;
115,725✔
48
    bool context_flag = false;
115,725✔
49
    int width = sizeof(T);
115,725✔
50
    init_header(mem.get_addr(), is_inner_bptree_node, has_refs, context_flag, wtype_Multiply, width, init_size,
115,725✔
51
                byte_size);
115,725✔
52

53
    return mem;
115,725✔
54
}
115,725✔
55

56

57
template <class T>
58
inline void BasicArray<T>::create(NodeHeader::Type type, bool context_flag)
59
{
115,725✔
60
    REALM_ASSERT(type == NodeHeader::type_Normal);
115,725✔
61
    REALM_ASSERT(!context_flag);
115,725✔
62
    size_t length = 0;
115,725✔
63
    MemRef mem = create_array(length, get_alloc()); // Throws
115,725✔
64
    init_from_mem(mem);
115,725✔
65
}
115,725✔
66

67

68
template <class T>
69
inline void BasicArray<T>::add(T value)
70
{
45,009✔
71
    insert(m_size, value);
45,009✔
72
}
45,009✔
73

74

75
template <class T>
76
inline T BasicArray<T>::get(size_t ndx) const noexcept
77
{
17,101,374✔
78
    return *(reinterpret_cast<const T*>(m_data) + ndx);
17,101,374✔
79
}
17,101,374✔
80

81

82
template <class T>
83
inline T BasicArray<T>::get(const char* header, size_t ndx) noexcept
84
{
85
    const char* data = get_data_from_header(header);
86
    // This casting assumes that T can be aliged on an 8-bype
87
    // boundary (since data is aligned on an 8-byte boundary.)
88
    return *(reinterpret_cast<const T*>(data) + ndx);
89
}
90

91

92
template <class T>
93
inline void BasicArray<T>::set(size_t ndx, T value)
94
{
1,692,900✔
95
    REALM_ASSERT_3(ndx, <, m_size);
1,692,900✔
96
    if (get(ndx) == value)
1,692,900✔
97
        return;
45,561✔
98

99
    // Check if we need to copy before modifying
100
    copy_on_write(); // Throws
1,647,339✔
101

102
    // Set the value
103
    T* data = reinterpret_cast<T*>(m_data) + ndx;
1,647,339✔
104
    *data = value;
1,647,339✔
105
}
1,647,339✔
106

107
template <class T>
108
void BasicArray<T>::insert(size_t ndx, T value)
109
{
2,263,491✔
110
    REALM_ASSERT_3(ndx, <=, m_size);
2,263,491✔
111

112
    // Check if we need to copy before modifying
113
    copy_on_write(); // Throws
2,263,491✔
114

115
    // Make room for the new value
116
    const auto old_size = m_size;
2,263,491✔
117
    alloc(m_size + 1, sizeof(T)); // Throws
2,263,491✔
118

119
    // Move values below insertion
120
    if (ndx != old_size) {
2,263,491✔
121
        char* src_begin = m_data + ndx * sizeof(T);
4,566✔
122
        char* src_end = m_data + old_size * sizeof(T);
4,566✔
123
        char* dst_end = src_end + sizeof(T);
4,566✔
124
        std::copy_backward(src_begin, src_end, dst_end);
4,566✔
125
    }
4,566✔
126

127
    // Set the value
128
    T* data = reinterpret_cast<T*>(m_data) + ndx;
2,263,491✔
129
    *data = value;
2,263,491✔
130
}
2,263,491✔
131

132
template <class T>
133
void BasicArray<T>::erase(size_t ndx)
134
{
101,499✔
135
    REALM_ASSERT_3(ndx, <, m_size);
101,499✔
136

137
    // Check if we need to copy before modifying
138
    copy_on_write(); // Throws
101,499✔
139

140
    // move data under deletion up
141
    if (ndx < m_size - 1) {
101,499✔
142
        char* dst_begin = m_data + ndx * sizeof(T);
81,771✔
143
        const char* src_begin = dst_begin + sizeof(T);
81,771✔
144
        const char* src_end = m_data + m_size * sizeof(T);
81,771✔
145
        realm::safe_copy_n(src_begin, src_end - src_begin, dst_begin);
81,771✔
146
    }
81,771✔
147

148
    // Update size (also in header)
149
    --m_size;
101,499✔
150
    set_header_size(m_size);
101,499✔
151
}
101,499✔
152

153
template <class T>
154
void BasicArray<T>::truncate(size_t to_size)
155
{
960✔
156
    REALM_ASSERT(is_attached());
960✔
157
    REALM_ASSERT_3(to_size, <=, m_size);
960✔
158

159
    copy_on_write(); // Throws
960✔
160

161
    // Update size in accessor and in header. This leaves the capacity
162
    // unchanged.
163
    m_size = to_size;
960✔
164
    set_header_size(to_size);
960✔
165
}
960✔
166

167
template <class T>
168
inline void BasicArray<T>::clear()
169
{
840✔
170
    truncate(0); // Throws
840✔
171
}
840✔
172

173
template <class T>
174
size_t BasicArray<T>::calc_byte_len(size_t for_size, size_t) const
175
{
2,263,431✔
176
    // FIXME: Consider calling `calc_aligned_byte_size(size)`
177
    // instead. Note however, that calc_byte_len() is supposed to return
178
    // the unaligned byte size. It is probably the case that no harm
179
    // is done by returning the aligned version, and most callers of
180
    // calc_byte_len() will actually benefit if calc_byte_len() was
181
    // changed to always return the aligned byte size.
182
    return header_size + for_size * sizeof(T);
2,263,431✔
183
}
2,263,431✔
184

185
template <class T>
186
size_t BasicArray<T>::calc_item_count(size_t bytes, size_t) const noexcept
UNCOV
187
{
×
UNCOV
188
    size_t bytes_without_header = bytes - header_size;
×
UNCOV
189
    return bytes_without_header / sizeof(T);
×
UNCOV
190
}
×
191

192
template <class T>
193
inline size_t BasicArray<T>::find_first(T value, size_t begin, size_t end) const
194
{
576✔
195
    if (end == npos)
576✔
UNCOV
196
        end = m_size;
×
197
    REALM_ASSERT(begin <= m_size && end <= m_size && begin <= end);
576✔
198
    const T* data = reinterpret_cast<const T*>(m_data);
576✔
199
    const T* i = std::find(data + begin, data + end, value);
576✔
200
    return i == data + end ? not_found : size_t(i - data);
576✔
201
}
576✔
202

203
template <class T>
204
size_t BasicArrayNull<T>::find_first_null(size_t begin, size_t end) const
205
{
60✔
206
    size_t sz = Node::size();
60✔
207
    if (end == npos)
60✔
UNCOV
208
        end = sz;
×
209
    REALM_ASSERT(begin <= sz && end <= sz && begin <= end);
60✔
210
    while (begin != end) {
240✔
211
        if (this->is_null(begin))
240✔
212
            return begin;
60✔
213
        begin++;
180✔
214
    }
180✔
UNCOV
215
    return not_found;
×
216
}
60✔
217

218
template <class T>
219
inline size_t BasicArray<T>::calc_aligned_byte_size(size_t size)
220
{
115,725✔
221
    size_t max = std::numeric_limits<size_t>::max();
115,725✔
222
    size_t max_2 = max & ~size_t(7); // Allow for upwards 8-byte alignment
115,725✔
223
    if (size > (max_2 - header_size) / sizeof(T))
115,725✔
UNCOV
224
        throw std::overflow_error("Byte size overflow");
×
225
    size_t byte_size = header_size + size * sizeof(T);
115,725✔
226
    REALM_ASSERT_3(byte_size, >, 0);
115,725✔
227
    size_t aligned_byte_size = ((byte_size - 1) | 7) + 1; // 8-byte alignment
115,725✔
228
    return aligned_byte_size;
115,725✔
229
}
115,725✔
230

231
} // namespace realm
232

233
#endif // REALM_ARRAY_BASIC_TPL_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc