memory_allocator.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "rocksdb/memory_allocator.h"
  6. #include "memory/jemalloc_nodump_allocator.h"
  7. #include "memory/memkind_kmem_allocator.h"
  8. #include "rocksdb/utilities/customizable_util.h"
  9. #include "rocksdb/utilities/object_registry.h"
  10. #include "rocksdb/utilities/options_type.h"
  11. #include "utilities/memory_allocators.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. namespace {
  14. static std::unordered_map<std::string, OptionTypeInfo> ma_wrapper_type_info = {
  15. {"target", OptionTypeInfo::AsCustomSharedPtr<MemoryAllocator>(
  16. 0, OptionVerificationType::kByName, OptionTypeFlags::kNone)},
  17. };
  18. static int RegisterBuiltinAllocators(ObjectLibrary& library,
  19. const std::string& /*arg*/) {
  20. library.AddFactory<MemoryAllocator>(
  21. DefaultMemoryAllocator::kClassName(),
  22. [](const std::string& /*uri*/, std::unique_ptr<MemoryAllocator>* guard,
  23. std::string* /*errmsg*/) {
  24. guard->reset(new DefaultMemoryAllocator());
  25. return guard->get();
  26. });
  27. library.AddFactory<MemoryAllocator>(
  28. CountedMemoryAllocator::kClassName(),
  29. [](const std::string& /*uri*/, std::unique_ptr<MemoryAllocator>* guard,
  30. std::string* /*errmsg*/) {
  31. guard->reset(new CountedMemoryAllocator(
  32. std::make_shared<DefaultMemoryAllocator>()));
  33. return guard->get();
  34. });
  35. library.AddFactory<MemoryAllocator>(
  36. JemallocNodumpAllocator::kClassName(),
  37. [](const std::string& /*uri*/, std::unique_ptr<MemoryAllocator>* guard,
  38. std::string* errmsg) {
  39. if (JemallocNodumpAllocator::IsSupported(errmsg)) {
  40. JemallocAllocatorOptions options;
  41. guard->reset(new JemallocNodumpAllocator(options));
  42. }
  43. return guard->get();
  44. });
  45. library.AddFactory<MemoryAllocator>(
  46. MemkindKmemAllocator::kClassName(),
  47. [](const std::string& /*uri*/, std::unique_ptr<MemoryAllocator>* guard,
  48. std::string* errmsg) {
  49. if (MemkindKmemAllocator::IsSupported(errmsg)) {
  50. guard->reset(new MemkindKmemAllocator());
  51. }
  52. return guard->get();
  53. });
  54. size_t num_types;
  55. return static_cast<int>(library.GetFactoryCount(&num_types));
  56. }
  57. } // namespace
  58. MemoryAllocatorWrapper::MemoryAllocatorWrapper(
  59. const std::shared_ptr<MemoryAllocator>& t)
  60. : target_(t) {
  61. RegisterOptions("", &target_, &ma_wrapper_type_info);
  62. }
  63. Status MemoryAllocator::CreateFromString(
  64. const ConfigOptions& options, const std::string& value,
  65. std::shared_ptr<MemoryAllocator>* result) {
  66. static std::once_flag once;
  67. std::call_once(once, [&]() {
  68. RegisterBuiltinAllocators(*(ObjectLibrary::Default().get()), "");
  69. });
  70. ConfigOptions copy = options;
  71. copy.invoke_prepare_options = true;
  72. return LoadManagedObject<MemoryAllocator>(copy, value, result);
  73. }
  74. } // namespace ROCKSDB_NAMESPACE