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

ska-sa / spead2 / 5991859444

27 Aug 2023 02:11PM UTC coverage: 75.031% (+0.03%) from 75.0%
5991859444

Pull #239

github

bmerry
Remove an unneeded lambda capture

It was giving a warning on Clang and hence failing CI.
Pull Request #239: Update to require at least C++17

152 of 152 new or added lines in 39 files covered. (100.0%)

5427 of 7233 relevant lines covered (75.03%)

52706.23 hits per line

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

76.92
/src/common_memory_allocator.cpp
1
/* Copyright 2016, 2021, 2023 National Research Foundation (SARAO)
2
 *
3
 * This program is free software: you can redistribute it and/or modify it under
4
 * the terms of the GNU Lesser General Public License as published by the Free
5
 * Software Foundation, either version 3 of the License, or (at your option) any
6
 * later version.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
11
 * details.
12
 *
13
 * You should have received a copy of the GNU Lesser General Public License
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 */
16

17
/**
18
 * @file
19
 */
20

21
#include <spead2/common_memory_pool.h>
22
#include "common_unique.h"
23

24
// Some operating systems only provide MAP_ANON
25
#ifndef MAP_ANONYMOUS
26
#define MAP_ANONYMOUS MAP_ANON
27
#endif
28

29
namespace spead2
30
{
31

32
// An empty pointer used when needed for a return by reference
33
static const std::shared_ptr<memory_allocator> empty_allocator_ptr;
34

35
memory_allocator::legacy_deleter::legacy_deleter(
3✔
36
    std::shared_ptr<memory_allocator> &&allocator, void *user)
3✔
37
    : state(std::make_shared<state_t>(std::move(allocator), user))
3✔
38
{
39
}
3✔
40

41
void memory_allocator::legacy_deleter::operator()(std::uint8_t *ptr) const
3✔
42
{
43
    state->allocator->free(ptr, state->user);
3✔
44
    // Allow the allocator to be reclaimed even if the unique_ptr lingers on.
45
    state->allocator.reset();
3✔
46
}
3✔
47

48
const std::shared_ptr<memory_allocator> &memory_allocator::deleter::get_allocator() const
×
49
{
50
    const legacy_deleter *legacy = target<legacy_deleter>();
×
51
    return (legacy != nullptr) ? legacy->get_allocator() : empty_allocator_ptr;
×
52
}
53

54
void *memory_allocator::deleter::get_user() const
×
55
{
56
    const legacy_deleter *legacy = target<legacy_deleter>();
×
57
    return (legacy != nullptr) ? legacy->get_user() : nullptr;
×
58
}
59

60
void memory_allocator::prefault(std::uint8_t *data, std::size_t size)
2,206✔
61
{
62
    // Pre-fault the memory by touching every page
63
    for (std::size_t i = 0; i < size; i += 4096)
63,034✔
64
        data[i] = 0;
60,828✔
65
}
2,206✔
66

67
memory_allocator::pointer memory_allocator::allocate(std::size_t size, [[maybe_unused]] void *hint)
2,206✔
68
{
69
    auto ptr = detail::make_unique_for_overwrite<std::uint8_t[]>(size);
2,206✔
70
    prefault(ptr.get(), size);
2,206✔
71
    return ptr;
4,408✔
72
}
2,204✔
73

74
void memory_allocator::free(std::uint8_t *ptr, [[maybe_unused]] void *user)
×
75
{
76
    // This implementation is not expected to be called, but is left in place
77
    // in case of 3rd-party allocators that rely on this default implementation.
78
    delete[] ptr;
×
79
}
×
80

81
/////////////////////////////////////////////////////////////////////////////
82

83
#include <sys/mman.h>
84

85
mmap_allocator::mmap_allocator(int flags, bool prefer_huge)
20✔
86
    : flags(flags), prefer_huge(prefer_huge)
20✔
87
{
88
}
20✔
89

90
mmap_allocator::pointer mmap_allocator::allocate(std::size_t size, [[maybe_unused]] void *hint)
79✔
91
{
92
    int use_flags = flags | MAP_ANONYMOUS | MAP_PRIVATE
79✔
93
#ifdef MAP_POPULATE
94
        | MAP_POPULATE
95
#endif
96
    ;
97

98
    std::uint8_t *ptr = (std::uint8_t *) MAP_FAILED;
79✔
99
#ifdef MAP_HUGETLB
100
    if (prefer_huge)
79✔
101
        ptr = (std::uint8_t *) mmap(nullptr, size, PROT_READ | PROT_WRITE, use_flags | MAP_HUGETLB, -1, 0);
2✔
102
#endif
103
    if (ptr == MAP_FAILED)
79✔
104
    {
105
        // Either fallback from prefer-huge, or prefer_huge is false
106
        ptr = (std::uint8_t *) mmap(nullptr, size, PROT_READ | PROT_WRITE, use_flags, -1, 0);
79✔
107
    }
108

109
    if (ptr == MAP_FAILED)
79✔
110
        throw std::bad_alloc();
2✔
111
#ifndef MAP_POPULATE
112
    prefault(ptr, size);
113
#endif
114
    return pointer(ptr, [size](std::uint8_t *ptr) { munmap(ptr, size); });
154✔
115
}
116

117
} // namespace spead2
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