memkind_kmem_allocator.cc 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // Copyright (c) 2019 Intel Corporation
  3. // This source code is licensed under both the GPLv2 (found in the
  4. // COPYING file in the root directory) and Apache 2.0 License
  5. // (found in the LICENSE.Apache file in the root directory).
  6. #ifdef MEMKIND
  7. #include <memkind.h>
  8. #endif // MEMKIND
  9. #include "memory/memkind_kmem_allocator.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. Status MemkindKmemAllocator::PrepareOptions(const ConfigOptions& options) {
  12. std::string message;
  13. if (!IsSupported(&message)) {
  14. return Status::NotSupported(message);
  15. } else {
  16. return MemoryAllocator::PrepareOptions(options);
  17. }
  18. }
  19. #ifdef MEMKIND
  20. void* MemkindKmemAllocator::Allocate(size_t size) {
  21. void* p = memkind_malloc(MEMKIND_DAX_KMEM, size);
  22. if (p == NULL) {
  23. throw std::bad_alloc();
  24. }
  25. return p;
  26. }
  27. void MemkindKmemAllocator::Deallocate(void* p) {
  28. memkind_free(MEMKIND_DAX_KMEM, p);
  29. }
  30. #ifdef ROCKSDB_MALLOC_USABLE_SIZE
  31. size_t MemkindKmemAllocator::UsableSize(void* p,
  32. size_t /*allocation_size*/) const {
  33. return memkind_malloc_usable_size(MEMKIND_DAX_KMEM, p);
  34. }
  35. #endif // ROCKSDB_MALLOC_USABLE_SIZE
  36. #endif // MEMKIND
  37. } // namespace ROCKSDB_NAMESPACE