blob: 60936cd03de2fff75d265d6c77e682fcc59ccc58 [file] [log] [blame]
Christopher Ferris723cf9b2017-01-19 20:08:48 -08001/*
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
Christopher Ferrisd226a512017-07-14 10:37:19 -070024namespace unwindstack {
25
Christopher Ferris723cf9b2017-01-19 20:08:48 -080026void MemoryFake::SetMemory(uint64_t addr, const void* memory, size_t length) {
27 const uint8_t* src = reinterpret_cast<const uint8_t*>(memory);
28 for (size_t i = 0; i < length; i++, addr++) {
29 auto value = data_.find(addr);
30 if (value != data_.end()) {
31 value->second = src[i];
32 } else {
33 data_.insert({ addr, src[i] });
34 }
35 }
36}
37
Josh Gaob8377632017-10-18 11:52:53 -070038size_t MemoryFake::Read(uint64_t addr, void* memory, size_t size) {
Christopher Ferris723cf9b2017-01-19 20:08:48 -080039 uint8_t* dst = reinterpret_cast<uint8_t*>(memory);
40 for (size_t i = 0; i < size; i++, addr++) {
41 auto value = data_.find(addr);
42 if (value == data_.end()) {
Josh Gao29c53782017-09-26 14:11:37 -070043 return i;
Christopher Ferris723cf9b2017-01-19 20:08:48 -080044 }
45 dst[i] = value->second;
46 }
Josh Gao29c53782017-09-26 14:11:37 -070047 return size;
Christopher Ferris723cf9b2017-01-19 20:08:48 -080048}
Christopher Ferrisd226a512017-07-14 10:37:19 -070049
50} // namespace unwindstack