| 1 |
/* |
| 2 |
* Copyright (C) 2009 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef NATIVE_HANDLE_H_ |
| 18 |
#define NATIVE_HANDLE_H_ |
| 19 |
|
| 20 |
#include <stdalign.h> |
| 21 |
|
| 22 |
#ifdef __cplusplus |
| 23 |
extern "C" { |
| 24 |
#endif |
| 25 |
|
| 26 |
#define NATIVE_HANDLE_MAX_FDS 1024 |
| 27 |
#define NATIVE_HANDLE_MAX_INTS 1024 |
| 28 |
|
| 29 |
/* Declare a char array for use with native_handle_init */ |
| 30 |
#define NATIVE_HANDLE_DECLARE_STORAGE(name, maxFds, maxInts) \ |
| 31 |
alignas(native_handle_t) char (name)[ \ |
| 32 |
sizeof(native_handle_t) + sizeof(int) * ((maxFds) + (maxInts))] |
| 33 |
|
| 34 |
typedef struct native_handle |
| 35 |
{ |
| 36 |
int version; /* sizeof(native_handle_t) */ |
| 37 |
int numFds; /* number of file-descriptors at &data[0] */ |
| 38 |
int numInts; /* number of ints at &data[numFds] */ |
| 39 |
#if defined(__clang__) |
| 40 |
#pragma clang diagnostic push |
| 41 |
#pragma clang diagnostic ignored "-Wzero-length-array" |
| 42 |
#endif |
| 43 |
int data[0]; /* numFds + numInts ints */ |
| 44 |
#if defined(__clang__) |
| 45 |
#pragma clang diagnostic pop |
| 46 |
#endif |
| 47 |
} native_handle_t; |
| 48 |
|
| 49 |
typedef const native_handle_t* buffer_handle_t; |
| 50 |
|
| 51 |
/* |
| 52 |
* native_handle_close |
| 53 |
* |
| 54 |
* closes the file descriptors contained in this native_handle_t |
| 55 |
* |
| 56 |
* return 0 on success, or a negative error code on failure |
| 57 |
* |
| 58 |
*/ |
| 59 |
int native_handle_close(const native_handle_t* h); |
| 60 |
|
| 61 |
/* |
| 62 |
* native_handle_init |
| 63 |
* |
| 64 |
* Initializes a native_handle_t from storage. storage must be declared with |
| 65 |
* NATIVE_HANDLE_DECLARE_STORAGE. numFds and numInts must not respectively |
| 66 |
* exceed maxFds and maxInts used to declare the storage. |
| 67 |
*/ |
| 68 |
native_handle_t* native_handle_init(char* storage, int numFds, int numInts); |
| 69 |
|
| 70 |
/* |
| 71 |
* native_handle_create |
| 72 |
* |
| 73 |
* creates a native_handle_t and initializes it. must be destroyed with |
| 74 |
* native_handle_delete(). Note that numFds must be <= NATIVE_HANDLE_MAX_FDS, |
| 75 |
* numInts must be <= NATIVE_HANDLE_MAX_INTS, and both must be >= 0. |
| 76 |
* |
| 77 |
*/ |
| 78 |
native_handle_t* native_handle_create(int numFds, int numInts); |
| 79 |
|
| 80 |
/* |
| 81 |
* native_handle_clone |
| 82 |
* |
| 83 |
* creates a native_handle_t and initializes it from another native_handle_t. |
| 84 |
* Must be destroyed with native_handle_delete(). |
| 85 |
* |
| 86 |
*/ |
| 87 |
native_handle_t* native_handle_clone(const native_handle_t* handle); |
| 88 |
|
| 89 |
/* |
| 90 |
* native_handle_delete |
| 91 |
* |
| 92 |
* frees a native_handle_t allocated with native_handle_create(). |
| 93 |
* This ONLY frees the memory allocated for the native_handle_t, but doesn't |
| 94 |
* close the file descriptors; which can be achieved with native_handle_close(). |
| 95 |
* |
| 96 |
* return 0 on success, or a negative error code on failure |
| 97 |
* |
| 98 |
*/ |
| 99 |
int native_handle_delete(native_handle_t* h); |
| 100 |
|
| 101 |
|
| 102 |
#ifdef __cplusplus |
| 103 |
} |
| 104 |
#endif |
| 105 |
|
| 106 |
#endif /* NATIVE_HANDLE_H_ */ |