/src/hermes/lib/VM/gcs/AlignedStorage.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #include "hermes/VM/AlignedStorage.h" |
9 | | |
10 | | #include "hermes/Support/OSCompat.h" |
11 | | #include "hermes/VM/StorageProvider.h" |
12 | | |
13 | | #include <cassert> |
14 | | #include <utility> |
15 | | |
16 | | namespace hermes { |
17 | | namespace vm { |
18 | | |
19 | 893 | void swap(AlignedStorage &a, AlignedStorage &b) { |
20 | 893 | using std::swap; |
21 | | |
22 | 893 | swap(a.lowLim_, b.lowLim_); |
23 | 893 | swap(a.provider_, b.provider_); |
24 | 893 | } |
25 | | |
26 | | llvh::ErrorOr<AlignedStorage> AlignedStorage::create( |
27 | 0 | StorageProvider *provider) { |
28 | 0 | return create(provider, nullptr); |
29 | 0 | } |
30 | | |
31 | | llvh::ErrorOr<AlignedStorage> AlignedStorage::create( |
32 | | StorageProvider *provider, |
33 | 94 | const char *name) { |
34 | 94 | auto result = provider->newStorage(name); |
35 | 94 | if (!result) { |
36 | 0 | return result.getError(); |
37 | 0 | } |
38 | 94 | return AlignedStorage{provider, *result}; |
39 | 94 | } |
40 | | |
41 | | AlignedStorage::AlignedStorage(StorageProvider *provider, void *lowLim) |
42 | 94 | : lowLim_(static_cast<char *>(lowLim)), provider_(provider) { |
43 | 94 | assert( |
44 | 94 | AlignedStorage::start(lowLim_) == lowLim_ && |
45 | 94 | "The lower limit of this storage must be aligned"); |
46 | 94 | } |
47 | | |
48 | 799 | AlignedStorage::AlignedStorage(AlignedStorage &&that) : AlignedStorage() { |
49 | 799 | swap(*this, that); |
50 | 799 | assert( |
51 | 799 | AlignedStorage::start(lowLim_) == lowLim_ && |
52 | 799 | "The lower limit of this storage must be aligned"); |
53 | 799 | } |
54 | | |
55 | 94 | AlignedStorage &AlignedStorage::operator=(AlignedStorage that) { |
56 | 94 | swap(*this, that); |
57 | 94 | assert( |
58 | 94 | AlignedStorage::start(lowLim_) == lowLim_ && |
59 | 94 | "The lower limit of this storage must be aligned"); |
60 | 94 | return *this; |
61 | 94 | } |
62 | | |
63 | 940 | AlignedStorage::~AlignedStorage() { |
64 | 940 | if (provider_) { |
65 | 94 | provider_->deleteStorage(lowLim_); |
66 | 94 | } |
67 | 940 | } |
68 | | |
69 | 0 | void AlignedStorage::markUnused(char *from, char *to) { |
70 | 0 | assert(from <= to && "Unused region boundaries inverted"); |
71 | 0 | assert(lowLim() <= from && to <= hiLim() && "Unused region out-of-bounds"); |
72 | 0 | #ifndef HERMESVM_ALLOW_HUGE_PAGES |
73 | 0 | oscompat::vm_unused(from, to - from); |
74 | 0 | #endif |
75 | 0 | } |
76 | | |
77 | | } // namespace vm |
78 | | } // namespace hermes |