1 // Copyright 2018 The Android Open Source Project
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 "auto_goldfish_dma_context.h"
16 
17 namespace {
empty()18 goldfish_dma_context empty() {
19     goldfish_dma_context ctx;
20 
21     ctx.mapped_addr = 0;
22     ctx.size = 0;
23     ctx.fd = -1;
24 
25     return ctx;
26 }
27 
destroy(goldfish_dma_context * ctx)28 void destroy(goldfish_dma_context *ctx) {
29     if (ctx->mapped_addr) {
30         goldfish_dma_unmap(ctx);
31     }
32     if (ctx->fd > 0) {
33         goldfish_dma_free(ctx);
34     }
35 }
36 }  // namespace
37 
AutoGoldfishDmaContext()38 AutoGoldfishDmaContext::AutoGoldfishDmaContext() : m_ctx(empty()) {}
39 
AutoGoldfishDmaContext(goldfish_dma_context * ctx)40 AutoGoldfishDmaContext::AutoGoldfishDmaContext(goldfish_dma_context *ctx)
41     : m_ctx(*ctx) {
42     *ctx = empty();
43 }
44 
~AutoGoldfishDmaContext()45 AutoGoldfishDmaContext::~AutoGoldfishDmaContext() {
46     destroy(&m_ctx);
47 }
48 
reset(goldfish_dma_context * ctx)49 void AutoGoldfishDmaContext::reset(goldfish_dma_context *ctx) {
50     destroy(&m_ctx);
51     if (ctx) {
52         m_ctx = *ctx;
53         *ctx = empty();
54     } else {
55         m_ctx = empty();
56     }
57 }
58 
release()59 goldfish_dma_context AutoGoldfishDmaContext::release() {
60     goldfish_dma_context copy = m_ctx;
61     m_ctx = empty();
62     return copy;
63 }
64 
65