1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <inttypes.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include "MemoryFake.h"
23 
24 namespace unwindstack {
25 
SetMemoryBlock(uint64_t addr,size_t length,uint8_t value)26 void MemoryFake::SetMemoryBlock(uint64_t addr, size_t length, uint8_t value) {
27   for (size_t i = 0; i < length; i++, addr++) {
28     auto entry = data_.find(addr);
29     if (entry != data_.end()) {
30       entry->second = value;
31     } else {
32       data_.insert({addr, value});
33     }
34   }
35 }
36 
SetMemory(uint64_t addr,const void * memory,size_t length)37 void MemoryFake::SetMemory(uint64_t addr, const void* memory, size_t length) {
38   const uint8_t* src = reinterpret_cast<const uint8_t*>(memory);
39   for (size_t i = 0; i < length; i++, addr++) {
40     auto value = data_.find(addr);
41     if (value != data_.end()) {
42       value->second = src[i];
43     } else {
44       data_.insert({ addr, src[i] });
45     }
46   }
47 }
48 
Read(uint64_t addr,void * memory,size_t size)49 size_t MemoryFake::Read(uint64_t addr, void* memory, size_t size) {
50   uint8_t* dst = reinterpret_cast<uint8_t*>(memory);
51   for (size_t i = 0; i < size; i++, addr++) {
52     auto value = data_.find(addr);
53     if (value == data_.end()) {
54       return i;
55     }
56     dst[i] = value->second;
57   }
58   return size;
59 }
60 
61 }  // namespace unwindstack
62