1/*
2 * Copyright (C) 2019 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 "IsoPage.h"
29#include "StdLibExtras.h"
30#include "VMAllocate.h"
31
32namespace bmalloc {
33
34// IsoSharedPage never becomes empty state again after we allocate some cells from IsoSharedPage. This makes IsoSharedPage management super simple.
35// This is because empty IsoSharedPage is still split into various different objects that should keep some part of virtual memory region dedicated.
36// We cannot set up bump allocation for such a page. Not freeing IsoSharedPages are OK since IsoSharedPage is only used for the lower tier of IsoHeap.
37template<typename Config, typename Type>
38void IsoSharedPage::free(const std::lock_guard<Mutex>&, api::IsoHeap<Type>& handle, void* ptr)
39{
40 auto& heapImpl = handle.impl();
41 uint8_t index = *indexSlotFor<Config>(ptr) & IsoHeapImplBase::maxAllocationFromSharedMask;
42 // IsoDeallocator::deallocate is called from delete operator. This is dispatched by vtable if virtual destructor exists.
43 // If vptr is replaced to the other vptr, we may accidentally chain this pointer to the incorrect HeapImplBase, which totally breaks the IsoHeap's goal.
44 // To harden that, we validate that this pointer is actually allocated for a specific HeapImplBase here by checking whether this pointer is listed in HeapImplBase's shared cells.
45 RELEASE_BASSERT(heapImpl.m_sharedCells[index] == ptr);
46 heapImpl.m_availableShared |= (1U << index);
47}
48
49inline VariadicBumpAllocator IsoSharedPage::startAllocating()
50{
51 static constexpr bool verbose = false;
52
53 if (verbose) {
54 fprintf(stderr, "%p: starting shared allocation.\n", this);
55 fprintf(stderr, "%p: preparing to shared bump.\n", this);
56 }
57
58 char* payloadEnd = reinterpret_cast<char*>(this) + IsoSharedPage::pageSize;
59 unsigned remaining = static_cast<unsigned>(roundDownToMultipleOf<alignmentForIsoSharedAllocation>(static_cast<uintptr_t>(IsoSharedPage::pageSize - sizeof(IsoSharedPage))));
60
61 return VariadicBumpAllocator(payloadEnd, remaining);
62}
63
64inline void IsoSharedPage::stopAllocating()
65{
66 static constexpr bool verbose = false;
67
68 if (verbose)
69 fprintf(stderr, "%p: stopping shared allocation.\n", this);
70}
71
72} // namespace bmalloc
73
74