win_jemalloc.cc 2.0 KB

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