blob: 70a77ed5f10e71c78d1bada8f6763a1d293bf0b3 [file] [log] [blame]
John Reck34781b22017-07-05 16:39:36 -07001/*
2 * Copyright (C) 2017 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 "ProfileDataContainer.h"
18
Dan Albert55a3ec92017-10-12 13:27:26 -070019#include <errno.h>
20
John Reck34781b22017-07-05 16:39:36 -070021#include <log/log.h>
22#include <cutils/ashmem.h>
23
24#include <sys/mman.h>
25
26namespace android {
27namespace uirenderer {
28
29void ProfileDataContainer::freeData() {
30 if (mIsMapped) {
31 munmap(mData, sizeof(ProfileData));
32 } else {
33 delete mData;
34 }
35 mIsMapped = false;
36 mData = nullptr;
37}
38
39void ProfileDataContainer::rotateStorage() {
40 // If we are mapped we want to stop using the ashmem backend and switch to malloc
41 // We are expecting a switchStorageToAshmem call to follow this, but it's not guaranteed
42 // If we aren't sitting on top of ashmem then just do a reset() as it's functionally
43 // equivalent do a free, malloc, reset.
44 if (mIsMapped) {
45 freeData();
46 mData = new ProfileData;
47 }
48 mData->reset();
49}
50
51void ProfileDataContainer::switchStorageToAshmem(int ashmemfd) {
52 int regionSize = ashmem_get_size_region(ashmemfd);
53 if (regionSize < 0) {
54 int err = errno;
55 ALOGW("Failed to get ashmem region size from fd %d, err %d %s", ashmemfd, err, strerror(err));
56 return;
57 }
58 if (regionSize < static_cast<int>(sizeof(ProfileData))) {
59 ALOGW("Ashmem region is too small! Received %d, required %u",
60 regionSize, static_cast<unsigned int>(sizeof(ProfileData)));
61 return;
62 }
63 ProfileData* newData = reinterpret_cast<ProfileData*>(
64 mmap(NULL, sizeof(ProfileData), PROT_READ | PROT_WRITE,
65 MAP_SHARED, ashmemfd, 0));
66 if (newData == MAP_FAILED) {
67 int err = errno;
68 ALOGW("Failed to move profile data to ashmem fd %d, error = %d",
69 ashmemfd, err);
70 return;
71 }
72
73 newData->mergeWith(*mData);
74 freeData();
75 mData = newData;
76 mIsMapped = true;
77}
78
79} /* namespace uirenderer */
Dan Albert55a3ec92017-10-12 13:27:26 -070080} /* namespace android */