Skip to content

Commit b477d1d

Browse files
authored
[libc][type_traits] Add aligned_storage (#94074)
1 parent a4b32c2 commit b477d1d

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ add_header_library(
111111
type_traits/add_lvalue_reference.h
112112
type_traits/add_pointer.h
113113
type_traits/add_rvalue_reference.h
114+
type_traits/aligned_storage.h
114115
type_traits/always_false.h
115116
type_traits/bool_constant.h
116117
type_traits/conditional.h

libc/src/__support/CPP/type_traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "src/__support/CPP/type_traits/add_lvalue_reference.h"
1313
#include "src/__support/CPP/type_traits/add_pointer.h"
1414
#include "src/__support/CPP/type_traits/add_rvalue_reference.h"
15+
#include "src/__support/CPP/type_traits/aligned_storage.h"
1516
#include "src/__support/CPP/type_traits/bool_constant.h"
1617
#include "src/__support/CPP/type_traits/conditional.h"
1718
#include "src/__support/CPP/type_traits/decay.h"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- aligned_storage type_traits --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_ALIGNED_STORAGE_H
10+
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_ALIGNED_STORAGE_H
11+
12+
#include <stddef.h> // size_t
13+
14+
namespace LIBC_NAMESPACE::cpp {
15+
16+
template <size_t Len, size_t Align> struct aligned_storage {
17+
struct type {
18+
alignas(Align) unsigned char data[Len];
19+
};
20+
};
21+
22+
template <size_t Len, size_t Align>
23+
using aligned_storage_t = typename aligned_storage<Len, Align>::type;
24+
25+
} // namespace LIBC_NAMESPACE::cpp
26+
27+
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_ALIGNED_STORAGE_H

libc/test/src/__support/CPP/type_traits_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ TEST(LlvmLibcTypeTraitsTest, add_rvalue_reference_void) {
112112
const volatile void>));
113113
}
114114

115+
TEST(LlvmLibcTypeTraitsTest, aligned_storage) {
116+
struct S {
117+
int a, b;
118+
};
119+
aligned_storage_t<sizeof(S), alignof(S)> buf;
120+
EXPECT_EQ(alignof(buf), alignof(S));
121+
EXPECT_EQ(sizeof(buf), sizeof(S));
122+
}
123+
115124
TEST(LlvmLibcTypeTraitsTest, bool_constant) {
116125
EXPECT_TRUE((bool_constant<true>::value));
117126
EXPECT_FALSE((bool_constant<false>::value));

0 commit comments

Comments
 (0)