Skip to content

Commit 46d37d2

Browse files
authored
Merge pull request #70366 from al45tair/eng/PR-119137861
[Runtime] Use malloc_type_posix_memalign().
2 parents 2cf7b3a + ca3a60e commit 46d37d2

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ void *swift::swift_slowAllocTyped(size_t size, size_t alignMask,
104104
p = malloc_type_malloc(size, typeId);
105105
} else {
106106
size_t alignment = computeAlignment(alignMask);
107-
p = malloc_type_aligned_alloc(alignment, size, typeId);
107+
108+
// Do not use malloc_type_aligned_alloc() here, because we want this
109+
// to work if `size` is not an integer multiple of `alignment`, which
110+
// was a requirement of the latter in C11 (but not C17 and later).
111+
int err = malloc_type_posix_memalign(&p, alignment, size);
112+
if (err != 0)
113+
p = nullptr;
108114
}
109115
if (!p) swift::crash("Could not allocate memory.");
110116
return p;

unittests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ if(("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "${SWIFT_PRIMARY_VARIANT_SDK}") AND
106106
Metadata.cpp
107107
Enum.cpp
108108
ExtendedExistential.cpp
109+
Heap.cpp
109110
Refcounting.cpp
110111
Stdlib.cpp
111112
StackAllocator.cpp

unittests/runtime/Heap.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//===--- Heap.cpp - Heap tests --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/Runtime/Heap.h"
14+
15+
#include "gtest/gtest.h"
16+
17+
void shouldAlloc(size_t size, size_t alignMask) {
18+
void *ptr = swift::swift_slowAlloc(size, alignMask);
19+
EXPECT_NE(ptr, (void *)NULL)
20+
<< "Allocation failed for size " << size << " and alignment mask "
21+
<< alignMask << ".";
22+
swift::swift_slowDealloc(ptr, size, alignMask);
23+
}
24+
25+
void shouldAlloc(size_t size) {
26+
shouldAlloc(size, 0);
27+
shouldAlloc(size, 1);
28+
shouldAlloc(size, 3);
29+
shouldAlloc(size, 7);
30+
shouldAlloc(size, 15);
31+
shouldAlloc(size, 31);
32+
shouldAlloc(size, 63);
33+
shouldAlloc(size, 4095);
34+
}
35+
36+
TEST(HeapTest, slowAlloc) {
37+
shouldAlloc(1);
38+
shouldAlloc(8);
39+
shouldAlloc(32);
40+
shouldAlloc(1093);
41+
}
42+
43+
void shouldAllocTyped(size_t size, size_t alignMask, swift::MallocTypeId typeId) {
44+
void *ptr = swift::swift_slowAllocTyped(size, alignMask, typeId);
45+
EXPECT_NE(ptr, (void *)NULL)
46+
<< "Typed allocation failed for size " << size << " and alignment mask "
47+
<< alignMask << ".";
48+
swift::swift_slowDealloc(ptr, size, alignMask);
49+
}
50+
51+
void shouldAllocTyped(size_t size, swift::MallocTypeId typeId) {
52+
shouldAlloc(size, 0);
53+
shouldAlloc(size, 1);
54+
shouldAlloc(size, 3);
55+
shouldAlloc(size, 7);
56+
shouldAlloc(size, 15);
57+
shouldAlloc(size, 31);
58+
shouldAlloc(size, 63);
59+
shouldAlloc(size, 4095);
60+
}
61+
62+
void shouldAllocTyped(size_t size) {
63+
shouldAllocTyped(size, 42);
64+
}
65+
66+
TEST(HeapTest, slowAllocTyped) {
67+
shouldAllocTyped(1);
68+
shouldAllocTyped(8);
69+
shouldAllocTyped(32);
70+
shouldAllocTyped(1093);
71+
}
72+

0 commit comments

Comments
 (0)