1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/agent/asan/heaps/simple_block_heap.h"
16 :
17 : #include "base/logging.h"
18 :
19 : namespace agent {
20 : namespace asan {
21 : namespace heaps {
22 :
23 E : SimpleBlockHeap::SimpleBlockHeap(HeapInterface* heap) : heap_(heap) {
24 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap);
25 E : }
26 :
27 E : SimpleBlockHeap::~SimpleBlockHeap() {
28 E : }
29 :
30 E : HeapType SimpleBlockHeap::GetHeapType() const {
31 E : return heap_->GetHeapType();
32 E : }
33 :
34 E : uint32 SimpleBlockHeap::GetHeapFeatures() const {
35 E : return heap_->GetHeapFeatures();
36 E : }
37 :
38 E : void* SimpleBlockHeap::Allocate(size_t bytes) {
39 E : return heap_->Allocate(bytes);
40 E : }
41 :
42 E : bool SimpleBlockHeap::Free(void* alloc) {
43 E : return heap_->Free(alloc);
44 E : }
45 :
46 E : bool SimpleBlockHeap::IsAllocated(const void* alloc) {
47 E : return heap_->IsAllocated(alloc);
48 E : }
49 :
50 E : size_t SimpleBlockHeap::GetAllocationSize(const void* alloc) {
51 E : return heap_->GetAllocationSize(alloc);;
52 E : }
53 :
54 E : void SimpleBlockHeap::Lock() {
55 E : heap_->Lock();
56 E : }
57 :
58 E : void SimpleBlockHeap::Unlock() {
59 E : heap_->Unlock();
60 E : }
61 :
62 E : bool SimpleBlockHeap::TryLock() {
63 E : return heap_->TryLock();
64 E : }
65 :
66 : void* SimpleBlockHeap::AllocateBlock(size_t size,
67 : size_t min_left_redzone_size,
68 : size_t min_right_redzone_size,
69 E : BlockLayout* layout) {
70 E : DCHECK_NE(static_cast<BlockLayout*>(nullptr), layout);
71 :
72 : // Plan the block layout.
73 : if (!BlockPlanLayout(kShadowRatio, kShadowRatio, size, min_left_redzone_size,
74 E : min_right_redzone_size, layout)) {
75 E : return nullptr;
76 : }
77 :
78 : // Allocate space for the block. If the allocation fails heap_ will
79 : // return NULL and we'll simply pass it on.
80 E : void* alloc = heap_->Allocate(layout->block_size);
81 E : DCHECK_EQ(0u, reinterpret_cast<uintptr_t>(alloc) % kShadowRatio);
82 E : return alloc;
83 E : }
84 :
85 E : bool SimpleBlockHeap::FreeBlock(const BlockInfo& block_info) {
86 E : DCHECK_NE(static_cast<BlockHeader*>(nullptr), block_info.header);
87 :
88 E : if (!heap_->Free(block_info.header))
89 i : return false;
90 :
91 E : return true;
92 E : }
93 :
94 : } // namespace heaps
95 : } // namespace asan
96 : } // namespace agent
|