• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- options.h -----------------------------------------------*- 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 SCUDO_OPTIONS_H_
10 #define SCUDO_OPTIONS_H_
11 
12 #include "atomic_helpers.h"
13 #include "common.h"
14 #include "memtag.h"
15 
16 namespace scudo {
17 
18 enum class OptionBit {
19   MayReturnNull,
20   FillContents0of2,
21   FillContents1of2,
22   DeallocTypeMismatch,
23   DeleteSizeMismatch,
24   TrackAllocationStacks,
25   UseOddEvenTags,
26   UseMemoryTagging,
27   AddLargeAllocationSlack,
28 };
29 
30 struct Options {
31   u32 Val;
32 
getOptions33   bool get(OptionBit Opt) const { return Val & (1U << static_cast<u32>(Opt)); }
34 
getFillContentsModeOptions35   FillContentsMode getFillContentsMode() const {
36     return static_cast<FillContentsMode>(
37         (Val >> static_cast<u32>(OptionBit::FillContents0of2)) & 3);
38   }
39 };
40 
useMemoryTagging(Options Options)41 template <typename Config> bool useMemoryTagging(Options Options) {
42   return allocatorSupportsMemoryTagging<Config>() &&
43          Options.get(OptionBit::UseMemoryTagging);
44 }
45 
46 struct AtomicOptions {
47   atomic_u32 Val = {};
48 
loadAtomicOptions49   Options load() const { return Options{atomic_load_relaxed(&Val)}; }
50 
clearAtomicOptions51   void clear(OptionBit Opt) {
52     atomic_fetch_and(&Val, ~(1U << static_cast<u32>(Opt)),
53                      memory_order_relaxed);
54   }
55 
setAtomicOptions56   void set(OptionBit Opt) {
57     atomic_fetch_or(&Val, 1U << static_cast<u32>(Opt), memory_order_relaxed);
58   }
59 
setFillContentsModeAtomicOptions60   void setFillContentsMode(FillContentsMode FillContents) {
61     u32 Opts = atomic_load_relaxed(&Val), NewOpts;
62     do {
63       NewOpts = Opts;
64       NewOpts &= ~(3U << static_cast<u32>(OptionBit::FillContents0of2));
65       NewOpts |= static_cast<u32>(FillContents)
66                  << static_cast<u32>(OptionBit::FillContents0of2);
67     } while (!atomic_compare_exchange_strong(&Val, &Opts, NewOpts,
68                                              memory_order_relaxed));
69   }
70 };
71 
72 } // namespace scudo
73 
74 #endif // SCUDO_OPTIONS_H_
75