Skip to content

Commit 510520d

Browse files
Merge pull request #289 from chillenzer/fix-allow-init-with-0
Allow init with 0 mem size
2 parents ba2f417 + f594fa7 commit 510520d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

include/mallocMC/creationPolicies/FlatterScatter.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,12 @@ namespace mallocMC::CreationPolicies
411411
{
412412
using MyHeap = FlatterScatterAlloc::Heap<T_HeapConfig, T_HashConfig, T_AlignmentPolicy>;
413413
auto numBlocks = MyHeap::numBlocks(memsize);
414+
if(numBlocks == 0U)
415+
{
416+
// This is not just an optimisation. The call to `getValidWorkDiv` below really dislikes the 0 extent
417+
// that we'd give it, so better stop here to not run into division by zero.
418+
return;
419+
}
414420
auto numPagesPerBlock = MyHeap::MyAccessBlock::numPages();
415421

416422
alpaka::KernelCfg<TAcc> const kernelCfg

test/unit/source/Allocator.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
mallocMC: Memory Allocator for Many Core Architectures.
3+
4+
Copyright 2025 Helmholtz-Zentrum Dresden - Rossendorf
5+
6+
Author(s): Julian Johannes Lenz, Rene Widera
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
*/
26+
27+
#include "mallocMC/allocator.hpp"
28+
29+
#include "mallocMC/alignmentPolicies/Shrink.hpp"
30+
#include "mallocMC/creationPolicies/FlatterScatter.hpp"
31+
#include "mallocMC/distributionPolicies/Noop.hpp"
32+
#include "mallocMC/oOMPolicies/ReturnNull.hpp"
33+
#include "mallocMC/reservePoolPolicies/AlpakaBuf.hpp"
34+
35+
#include <alpaka/example/ExampleDefaultAcc.hpp>
36+
37+
#include <catch2/catch_test_macros.hpp>
38+
#include <mallocMC/mallocMC.hpp>
39+
using Dim = alpaka::DimInt<1>;
40+
using Idx = std::size_t;
41+
42+
// Define the device accelerator
43+
using Acc = alpaka::ExampleDefaultAcc<Dim, Idx>;
44+
45+
TEST_CASE("Allocator")
46+
{
47+
SECTION("can be initialised with 0 memory.")
48+
{
49+
auto const platform = alpaka::Platform<Acc>{};
50+
auto const dev = alpaka::getDevByIdx(platform, 0);
51+
auto queue = alpaka::Queue<Acc, alpaka::Blocking>{dev};
52+
53+
mallocMC::Allocator<
54+
Acc,
55+
mallocMC::CreationPolicies::FlatterScatter<>,
56+
mallocMC::DistributionPolicies::Noop,
57+
mallocMC::OOMPolicies::ReturnNull,
58+
mallocMC::ReservePoolPolicies::AlpakaBuf<Acc>,
59+
mallocMC::AlignmentPolicies::Shrink<>>
60+
allocator{dev, queue, 0};
61+
}
62+
}

0 commit comments

Comments
 (0)