Fix SignalHandlerTest with ASAN
[folly.git] / folly / experimental / hazptr / memory_resource.cpp
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
17 #include "memory_resource.h"
18
19 namespace folly {
20 namespace hazptr {
21
22 memory_resource** default_mr_ptr() {
23   /* library-local */ static memory_resource* default_mr =
24       new_delete_resource();
25   DEBUG_PRINT(&default_mr << " " << default_mr);
26   return &default_mr;
27 }
28
29 memory_resource* get_default_resource() {
30   DEBUG_PRINT("");
31   return *default_mr_ptr();
32 }
33
34 void set_default_resource(memory_resource* mr) {
35   DEBUG_PRINT("");
36   *default_mr_ptr() = mr;
37 }
38
39 memory_resource* new_delete_resource() {
40   class new_delete : public memory_resource {
41    public:
42     void* allocate(
43         const size_t bytes,
44         const size_t alignment = folly::max_align_v) override {
45       (void)alignment;
46       void* p = static_cast<void*>(new char[bytes]);
47       DEBUG_PRINT(this << " " << p << " " << bytes);
48       return p;
49     }
50     void deallocate(
51         void* p,
52         const size_t bytes,
53         const size_t alignment = folly::max_align_v) override {
54       (void)alignment;
55       (void)bytes;
56       DEBUG_PRINT(p << " " << bytes);
57       delete[] static_cast<char*>(p);
58     }
59   };
60   static new_delete mr;
61   return &mr;
62 }
63
64 } // namespace hazptr
65 } // namespace folly