1 | /* |
2 | * Copyright (C) 2014-2018 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "AvailableMemory.h" |
29 | #include "Cache.h" |
30 | #include "Gigacage.h" |
31 | #include "Heap.h" |
32 | #include "IsoTLS.h" |
33 | #include "Mutex.h" |
34 | #include "PerHeapKind.h" |
35 | #include "Scavenger.h" |
36 | |
37 | namespace bmalloc { |
38 | namespace api { |
39 | |
40 | // Returns null on failure. |
41 | inline void* tryMalloc(size_t size, HeapKind kind = HeapKind::Primary) |
42 | { |
43 | return Cache::tryAllocate(kind, size); |
44 | } |
45 | |
46 | // Crashes on failure. |
47 | inline void* malloc(size_t size, HeapKind kind = HeapKind::Primary) |
48 | { |
49 | return Cache::allocate(kind, size); |
50 | } |
51 | |
52 | BEXPORT void* mallocOutOfLine(size_t size, HeapKind kind = HeapKind::Primary); |
53 | |
54 | // Returns null on failure. |
55 | inline void* tryMemalign(size_t alignment, size_t size, HeapKind kind = HeapKind::Primary) |
56 | { |
57 | return Cache::tryAllocate(kind, alignment, size); |
58 | } |
59 | |
60 | // Crashes on failure. |
61 | inline void* memalign(size_t alignment, size_t size, HeapKind kind = HeapKind::Primary) |
62 | { |
63 | return Cache::allocate(kind, alignment, size); |
64 | } |
65 | |
66 | // Returns null on failure. |
67 | inline void* tryRealloc(void* object, size_t newSize, HeapKind kind = HeapKind::Primary) |
68 | { |
69 | return Cache::tryReallocate(kind, object, newSize); |
70 | } |
71 | |
72 | // Crashes on failure. |
73 | inline void* realloc(void* object, size_t newSize, HeapKind kind = HeapKind::Primary) |
74 | { |
75 | return Cache::reallocate(kind, object, newSize); |
76 | } |
77 | |
78 | // Returns null on failure. |
79 | // This API will give you zeroed pages that are ready to be used. These pages |
80 | // will page fault on first access. It returns to you memory that initially only |
81 | // uses up virtual address space, not `size` bytes of physical memory. |
82 | BEXPORT void* tryLargeZeroedMemalignVirtual(size_t alignment, size_t size, HeapKind kind = HeapKind::Primary); |
83 | |
84 | inline void free(void* object, HeapKind kind = HeapKind::Primary) |
85 | { |
86 | Cache::deallocate(kind, object); |
87 | } |
88 | |
89 | BEXPORT void freeOutOfLine(void* object, HeapKind kind = HeapKind::Primary); |
90 | |
91 | BEXPORT void freeLargeVirtual(void* object, size_t, HeapKind kind = HeapKind::Primary); |
92 | |
93 | inline void scavengeThisThread() |
94 | { |
95 | for (unsigned i = numHeaps; i--;) |
96 | Cache::scavenge(static_cast<HeapKind>(i)); |
97 | IsoTLS::scavenge(); |
98 | } |
99 | |
100 | BEXPORT void scavenge(); |
101 | |
102 | BEXPORT bool isEnabled(HeapKind kind = HeapKind::Primary); |
103 | |
104 | // ptr must be aligned to vmPageSizePhysical and size must be divisible |
105 | // by vmPageSizePhysical. |
106 | BEXPORT void decommitAlignedPhysical(void* object, size_t, HeapKind = HeapKind::Primary); |
107 | BEXPORT void commitAlignedPhysical(void* object, size_t, HeapKind = HeapKind::Primary); |
108 | |
109 | inline size_t availableMemory() |
110 | { |
111 | return bmalloc::availableMemory(); |
112 | } |
113 | |
114 | #if BPLATFORM(IOS_FAMILY) || BOS(LINUX) |
115 | inline size_t () |
116 | { |
117 | return bmalloc::memoryFootprint(); |
118 | } |
119 | |
120 | inline double percentAvailableMemoryInUse() |
121 | { |
122 | return bmalloc::percentAvailableMemoryInUse(); |
123 | } |
124 | #endif |
125 | |
126 | #if BOS(DARWIN) |
127 | BEXPORT void setScavengerThreadQOSClass(qos_class_t overrideClass); |
128 | #endif |
129 | |
130 | BEXPORT void enableMiniMode(); |
131 | |
132 | } // namespace api |
133 | } // namespace bmalloc |
134 | |