File: new_aligned.cc

package info (click to toggle)
gcc-arm-none-eabi 15%3A8-2019-q3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 571,828 kB
  • sloc: ansic: 2,937,651; cpp: 881,644; ada: 597,189; makefile: 65,528; asm: 56,499; xml: 46,621; exp: 24,747; sh: 19,684; python: 7,256; pascal: 4,370; awk: 3,497; perl: 2,695; yacc: 316; ml: 285; f90: 234; lex: 198; objc: 194; haskell: 119
file content (119 lines) | stat: -rw-r--r-- 2,766 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (C) 2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++17" }
// { dg-do run { target c++17 } }

#include <new>
#include <memory>
#include <testsuite_hooks.h>

struct Test
{
  Test(std::size_t size, std::size_t a)
  : size(size), alignment(std::align_val_t{a}),
    p(::operator new(size, alignment))
  { }

  ~Test() { ::operator delete(p, size, alignment); }

  std::size_t size;
  std::align_val_t alignment;
  void* p;

  bool valid() const { return p != nullptr; }

  bool aligned() const
  {
    auto ptr = p;
    auto space = size;
    return std::align((std::size_t)alignment, size, ptr, space) == p;
  }
};

// operator new(size_t size, align_val_t alignment) has
// undefined behaviour if the alignment argument is not
// a valid alignment value (i.e. not a power of two).
//
// Unlike posix_memalign there is no requirement that
// alignment >= sizeof(void*).
//
// Unlike aligned_alloc there is no requirement that
// size is an integer multiple of alignment.

void
test01()
{
  // Test small values that would not be valid for
  // posix_memalign or aligned_alloc.

  Test t11{1, 1};
  VERIFY( t11.valid() );
  VERIFY( t11.aligned() );

  Test t21{2, 1};
  VERIFY( t21.valid() );
  VERIFY( t21.aligned() );

  Test t12{1, 2};
  VERIFY( t12.valid() );
  VERIFY( t12.aligned() );

  Test t22{2, 2};
  VERIFY( t22.valid() );
  VERIFY( t22.aligned() );

  Test t32{3, 2};
  VERIFY( t32.valid() );
  VERIFY( t32.aligned() );

  Test t42{4, 2};
  VERIFY( t42.valid() );
  VERIFY( t42.aligned() );

  Test t24{2, 4};
  VERIFY( t24.valid() );
  VERIFY( t24.aligned() );

  Test t34{3, 4};
  VERIFY( t34.valid() );
  VERIFY( t34.aligned() );

  Test t44{4, 4};
  VERIFY( t44.valid() );
  VERIFY( t44.aligned() );

  // Test some larger values.

  Test t128_16{128, 16};
  VERIFY( t128_16.valid() );
  VERIFY( t128_16.aligned() );

  Test t128_64{128, 64};
  VERIFY( t128_64.valid() );
  VERIFY( t128_64.aligned() );

  Test t64_128{64, 128};
  VERIFY( t64_128.valid() );
  VERIFY( t64_128.aligned() );
}

int
main()
{
  test01();
}