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