aboutsummaryrefslogtreecommitdiff
path: root/libc/src/__support/alloc-checker.h
blob: 0c4c22edff7862f8510b205cf6e020d60c621dd7 (plain)
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
//===-- Libc specific custom operator new and delete ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC___SUPPORT_ALLOC_CHECKER_H
#define LLVM_LIBC_SRC___SUPPORT_ALLOC_CHECKER_H

#include "hdr/func/aligned_alloc.h"
#include "hdr/func/malloc.h"
#include "src/__support/CPP/new.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/properties/os.h"

namespace LIBC_NAMESPACE_DECL {

class AllocChecker {
  bool success = false;

  LIBC_INLINE AllocChecker &operator=(bool status) {
    success = status;
    return *this;
  }

public:
  LIBC_INLINE AllocChecker() = default;

  LIBC_INLINE operator bool() const { return success; }

  LIBC_INLINE static void *alloc(size_t s, AllocChecker &ac) {
    void *mem = ::malloc(s);
    ac = (mem != nullptr);
    return mem;
  }

  LIBC_INLINE static void *aligned_alloc(size_t s, std::align_val_t align,
                                         AllocChecker &ac) {
#ifdef LIBC_TARGET_OS_IS_WINDOWS
    // std::aligned_alloc is not available on Windows because std::free on
    // Windows cannot deallocate any over-aligned memory. Microsoft provides an
    // alternative for std::aligned_alloc named _aligned_malloc, but it must be
    // paired with _aligned_free instead of std::free.
    void *mem = ::_aligned_malloc(static_cast<size_t>(align), s);
#else
    void *mem = ::aligned_alloc(static_cast<size_t>(align), s);
#endif
    ac = (mem != nullptr);
    return mem;
  }
};

} // namespace LIBC_NAMESPACE_DECL

LIBC_INLINE void *operator new(size_t size,
                               LIBC_NAMESPACE::AllocChecker &ac) noexcept {
  return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);
}

LIBC_INLINE void *operator new(size_t size, std::align_val_t align,
                               LIBC_NAMESPACE::AllocChecker &ac) noexcept {
  return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);
}

LIBC_INLINE void *operator new[](size_t size,
                                 LIBC_NAMESPACE::AllocChecker &ac) noexcept {
  return LIBC_NAMESPACE::AllocChecker::alloc(size, ac);
}

LIBC_INLINE void *operator new[](size_t size, std::align_val_t align,
                                 LIBC_NAMESPACE::AllocChecker &ac) noexcept {
  return LIBC_NAMESPACE::AllocChecker::aligned_alloc(size, align, ac);
}

#endif // LLVM_LIBC_SRC___SUPPORT_ALLOC_CHECKER_H