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 { empty()18goldfish_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)28void 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()38AutoGoldfishDmaContext::AutoGoldfishDmaContext() : m_ctx(empty()) {} 39 AutoGoldfishDmaContext(goldfish_dma_context * ctx)40AutoGoldfishDmaContext::AutoGoldfishDmaContext(goldfish_dma_context *ctx) 41 : m_ctx(*ctx) { 42 *ctx = empty(); 43 } 44 ~AutoGoldfishDmaContext()45AutoGoldfishDmaContext::~AutoGoldfishDmaContext() { 46 destroy(&m_ctx); 47 } 48 reset(goldfish_dma_context * ctx)49void 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()59goldfish_dma_context AutoGoldfishDmaContext::release() { 60 goldfish_dma_context copy = m_ctx; 61 m_ctx = empty(); 62 return copy; 63 } 64 65