From fd3ea3d11805aef555eca08e2c2a0e1a40714726 Mon Sep 17 00:00:00 2001 From: Chia-I Wu Date: Fri, 23 Sep 2016 11:13:52 +0800 Subject: [PATCH] Add native_handle_clone libhwbinder's Parcel::readNativeHandleNoDup returns a temporary native_handle_t. We want a way to save the temporary handle for later use. Change-Id: I16f32043aa8b7d2c0aa57d67551500259b411410 --- include/cutils/native_handle.h | 9 +++++++++ libcutils/native_handle.c | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/cutils/native_handle.h b/include/cutils/native_handle.h index 268c5d3f5..ae9135d61 100644 --- a/include/cutils/native_handle.h +++ b/include/cutils/native_handle.h @@ -49,6 +49,15 @@ int native_handle_close(const native_handle_t* h); */ native_handle_t* native_handle_create(int numFds, int numInts); +/* + * native_handle_clone + * + * creates a native_handle_t and initializes it from another native_handle_t. + * Must be destroyed with native_handle_delete(). + * + */ +native_handle_t* native_handle_clone(const native_handle_t* handle); + /* * native_handle_delete * diff --git a/libcutils/native_handle.c b/libcutils/native_handle.c index 61fa38ed4..429076855 100644 --- a/libcutils/native_handle.c +++ b/libcutils/native_handle.c @@ -44,6 +44,27 @@ native_handle_t* native_handle_create(int numFds, int numInts) return h; } +native_handle_t* native_handle_clone(const native_handle_t* handle) +{ + native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts); + int i; + + for (i = 0; i < handle->numFds; i++) { + clone->data[i] = dup(handle->data[i]); + if (clone->data[i] < 0) { + clone->numFds = i; + native_handle_close(clone); + native_handle_delete(clone); + return NULL; + } + } + + memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds], + sizeof(int) * handle->numInts); + + return clone; +} + int native_handle_delete(native_handle_t* h) { if (h) {