Roman Kiryanov | b6fce5b | 2018-09-27 16:18:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | namespace { |
| 18 | goldfish_dma_context empty() { |
Roman Kiryanov | be1c0c2 | 2018-10-02 18:07:14 -0700 | [diff] [blame] | 19 | goldfish_dma_context ctx; |
| 20 | |
| 21 | ctx.mapped_addr = 0; |
| 22 | ctx.size = 0; |
| 23 | ctx.fd = -1; |
| 24 | |
Roman Kiryanov | b6fce5b | 2018-09-27 16:18:58 -0700 | [diff] [blame] | 25 | return ctx; |
| 26 | } |
| 27 | |
| 28 | void destroy(goldfish_dma_context *ctx) { |
Roman Kiryanov | be1c0c2 | 2018-10-02 18:07:14 -0700 | [diff] [blame] | 29 | if (ctx->mapped_addr) { |
| 30 | goldfish_dma_unmap(ctx); |
| 31 | } |
| 32 | if (ctx->fd > 0) { |
| 33 | goldfish_dma_free(ctx); |
| 34 | } |
Roman Kiryanov | b6fce5b | 2018-09-27 16:18:58 -0700 | [diff] [blame] | 35 | } |
| 36 | } // namespace |
| 37 | |
| 38 | AutoGoldfishDmaContext::AutoGoldfishDmaContext() : m_ctx(empty()) {} |
| 39 | |
| 40 | AutoGoldfishDmaContext::AutoGoldfishDmaContext(goldfish_dma_context *ctx) |
| 41 | : m_ctx(*ctx) { |
| 42 | *ctx = empty(); |
| 43 | } |
| 44 | |
| 45 | AutoGoldfishDmaContext::~AutoGoldfishDmaContext() { |
| 46 | destroy(&m_ctx); |
| 47 | } |
| 48 | |
| 49 | void AutoGoldfishDmaContext::reset(goldfish_dma_context *ctx) { |
| 50 | destroy(&m_ctx); |
Roman Kiryanov | be1c0c2 | 2018-10-02 18:07:14 -0700 | [diff] [blame] | 51 | if (ctx) { |
| 52 | m_ctx = *ctx; |
| 53 | *ctx = empty(); |
| 54 | } else { |
| 55 | m_ctx = empty(); |
| 56 | } |
Roman Kiryanov | b6fce5b | 2018-09-27 16:18:58 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | goldfish_dma_context AutoGoldfishDmaContext::release() { |
| 60 | goldfish_dma_context copy = m_ctx; |
| 61 | m_ctx = empty(); |
| 62 | return copy; |
| 63 | } |
| 64 | |