brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · a11bcfc Raw
84 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// <any>12 13// Check that we're consistently using the same allocation functions to14// allocate/deallocate/construct/destroy objects in std::any.15// See https://llvm.org/PR45099 for details.16 17#include <any>18#include <cassert>19#include <cstddef>20#include <new>21 22// Make sure we don't fit in std::any's SBO23int allocated_count   = 0;24int constructed_count = 0;25 26struct Large {27  Large() { ++constructed_count; }28 29  Large(const Large&) { ++constructed_count; }30 31  ~Large() { --constructed_count; }32 33  char big[sizeof(std::any) + 1];34 35  static void* operator new(size_t n) {36    ++allocated_count;37    return ::operator new(n);38  }39 40  static void operator delete(void* ptr) {41    --allocated_count;42    ::operator delete(ptr);43  }44};45 46// Make sure we fit in std::any's SBO47struct Small {48  Small() { ++constructed_count; }49 50  Small(const Small&) { ++constructed_count; }51 52  ~Small() { --constructed_count; }53 54  static void* operator new(size_t n) {55    ++allocated_count;56    return ::operator new(n);57  }58 59  static void operator delete(void* ptr) {60    --allocated_count;61    ::operator delete(ptr);62  }63};64 65int main(int, char**) {66  // Test large types67  {68    [[maybe_unused]] std::any a = Large();69    assert(constructed_count == 1);70  }71  assert(allocated_count == 0);72  assert(constructed_count == 0);73 74  // Test small types75  {76    [[maybe_unused]] std::any a = Small();77    assert(constructed_count == 1);78  }79  assert(allocated_count == 0);80  assert(constructed_count == 0);81 82  return 0;83}84