memory_resource.cpp 1.8 KB

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