blob: 280262d105b137eae1dae83f835ad171f4e0eb7f [file] [log] [blame]
Roman Kiryanovb6fce5b2018-09-27 16:18:58 -07001/*
2 * Copyright (C) 2018 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#include "auto_goldfish_dma_context.h"
16
17namespace {
18goldfish_dma_context empty() {
Roman Kiryanovbe1c0c22018-10-02 18:07:14 -070019 goldfish_dma_context ctx;
20
21 ctx.mapped_addr = 0;
22 ctx.size = 0;
23 ctx.fd = -1;
24
Roman Kiryanovb6fce5b2018-09-27 16:18:58 -070025 return ctx;
26}
27
28void destroy(goldfish_dma_context *ctx) {
Roman Kiryanovbe1c0c22018-10-02 18:07:14 -070029 if (ctx->mapped_addr) {
30 goldfish_dma_unmap(ctx);
31 }
32 if (ctx->fd > 0) {
33 goldfish_dma_free(ctx);
34 }
Roman Kiryanovb6fce5b2018-09-27 16:18:58 -070035}
36} // namespace
37
38AutoGoldfishDmaContext::AutoGoldfishDmaContext() : m_ctx(empty()) {}
39
40AutoGoldfishDmaContext::AutoGoldfishDmaContext(goldfish_dma_context *ctx)
41 : m_ctx(*ctx) {
42 *ctx = empty();
43}
44
45AutoGoldfishDmaContext::~AutoGoldfishDmaContext() {
46 destroy(&m_ctx);
47}
48
49void AutoGoldfishDmaContext::reset(goldfish_dma_context *ctx) {
50 destroy(&m_ctx);
Roman Kiryanovbe1c0c22018-10-02 18:07:14 -070051 if (ctx) {
52 m_ctx = *ctx;
53 *ctx = empty();
54 } else {
55 m_ctx = empty();
56 }
Roman Kiryanovb6fce5b2018-09-27 16:18:58 -070057}
58
59goldfish_dma_context AutoGoldfishDmaContext::release() {
60 goldfish_dma_context copy = m_ctx;
61 m_ctx = empty();
62 return copy;
63}
64