win_jemalloc.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #if defined(OS_WIN)
  10. #ifndef ROCKSDB_JEMALLOC
  11. #error This file can only be part of jemalloc aware build
  12. #endif
  13. #include <stdexcept>
  14. #include "jemalloc/jemalloc.h"
  15. #include "port/win/port_win.h"
  16. #if defined(ZSTD) && defined(ZSTD_STATIC_LINKING_ONLY)
  17. #include <zstd.h>
  18. #if (ZSTD_VERSION_NUMBER >= 500)
  19. namespace ROCKSDB_NAMESPACE {
  20. namespace port {
  21. void* JemallocAllocateForZSTD(void* /* opaque */, size_t size) {
  22. return je_malloc(size);
  23. }
  24. void JemallocDeallocateForZSTD(void* /* opaque */, void* address) {
  25. je_free(address);
  26. }
  27. ZSTD_customMem GetJeZstdAllocationOverrides() {
  28. return {JemallocAllocateForZSTD, JemallocDeallocateForZSTD, nullptr};
  29. }
  30. } // namespace port
  31. } // namespace ROCKSDB_NAMESPACE
  32. #endif // (ZSTD_VERSION_NUMBER >= 500)
  33. #endif // defined(ZSTD) defined(ZSTD_STATIC_LINKING_ONLY)
  34. // Global operators to be replaced by a linker when this file is
  35. // a part of the build
  36. namespace ROCKSDB_NAMESPACE {
  37. namespace port {
  38. void* jemalloc_aligned_alloc(size_t size, size_t alignment) noexcept {
  39. return je_aligned_alloc(alignment, size);
  40. }
  41. void jemalloc_aligned_free(void* p) noexcept { je_free(p); }
  42. } // namespace port
  43. } // namespace ROCKSDB_NAMESPACE
  44. void* operator new(size_t size) {
  45. void* p = je_malloc(size);
  46. if (!p) {
  47. throw std::bad_alloc();
  48. }
  49. return p;
  50. }
  51. void* operator new[](size_t size) {
  52. void* p = je_malloc(size);
  53. if (!p) {
  54. throw std::bad_alloc();
  55. }
  56. return p;
  57. }
  58. void operator delete(void* p) {
  59. if (p) {
  60. je_free(p);
  61. }
  62. }
  63. void operator delete[](void* p) {
  64. if (p) {
  65. je_free(p);
  66. }
  67. }
  68. #endif