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