Consistency in namespace-closing comments
[folly.git] / folly / experimental / hazptr / memory_resource.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17
18 #include <folly/experimental/hazptr/debug.h>
19
20 ////////////////////////////////////////////////////////////////////////////////
21 /// Disclaimer: This is intended only as a partial stand-in for
22 /// std::pmr::memory_resource (C++17) as needed for developing a
23 /// hazptr prototype.
24 ////////////////////////////////////////////////////////////////////////////////
25 #include <folly/Portability.h>
26 #include <memory>
27
28 namespace folly {
29 namespace hazptr {
30
31 class memory_resource {
32  public:
33   virtual ~memory_resource() = default;
34   virtual void* allocate(
35       const size_t bytes,
36       const size_t alignment = folly::max_align_v) = 0;
37   virtual void deallocate(
38       void* p,
39       const size_t bytes,
40       const size_t alignment = folly::max_align_v) = 0;
41 };
42
43 memory_resource* get_default_resource();
44 void set_default_resource(memory_resource*);
45 memory_resource* new_delete_resource();
46
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Implementation
49 ////////////////////////////////////////////////////////////////////////////////
50
51 inline memory_resource** default_mr_ptr() {
52   /* library-local */ static memory_resource* default_mr =
53       new_delete_resource();
54   DEBUG_PRINT(&default_mr << " " << default_mr);
55   return &default_mr;
56 }
57
58 inline memory_resource* get_default_resource() {
59   DEBUG_PRINT("");
60   return *default_mr_ptr();
61 }
62
63 inline void set_default_resource(memory_resource* mr) {
64   DEBUG_PRINT("");
65   *default_mr_ptr() = mr;
66 }
67
68 inline memory_resource* new_delete_resource() {
69   class new_delete : public memory_resource {
70    public:
71     void* allocate(
72         const size_t bytes,
73         const size_t alignment = folly::max_align_v) override {
74       (void)alignment;
75       void* p = static_cast<void*>(new char[bytes]);
76       DEBUG_PRINT(this << " " << p << " " << bytes);
77       return p;
78     }
79     void deallocate(
80         void* p,
81         const size_t bytes,
82         const size_t alignment = folly::max_align_v) override {
83       (void)alignment;
84       (void)bytes;
85       DEBUG_PRINT(p << " " << bytes);
86       delete[] static_cast<char*>(p);
87     }
88   };
89   static new_delete mr;
90   return &mr;
91 }
92
93 } // namespace folly
94 } // namespace hazptr