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 /* Declare a char array for use with native_handle_init */
27 #define NATIVE_HANDLE_DECLARE_STORAGE(name, maxFds, maxInts) \
28     alignas(native_handle_t) char name[                            \
29       sizeof(native_handle_t) + sizeof(int) * (maxFds + maxInts)]
30 
31 typedef struct native_handle
32 {
33     int version;        /* sizeof(native_handle_t) */
34     int numFds;         /* number of file-descriptors at &data[0] */
35     int numInts;        /* number of ints at &data[numFds] */
36 #if defined(__clang__)
37 #pragma clang diagnostic push
38 #pragma clang diagnostic ignored "-Wzero-length-array"
39 #endif
40     int data[0];        /* numFds + numInts ints */
41 #if defined(__clang__)
42 #pragma clang diagnostic pop
43 #endif
44 } native_handle_t;
45 
46 /*
47  * native_handle_close
48  *
49  * closes the file descriptors contained in this native_handle_t
50  *
51  * return 0 on success, or a negative error code on failure
52  *
53  */
54 int native_handle_close(const native_handle_t* h);
55 
56 /*
57  * native_handle_init
58  *
59  * Initializes a native_handle_t from storage.  storage must be declared with
60  * NATIVE_HANDLE_DECLARE_STORAGE.  numFds and numInts must not respectively
61  * exceed maxFds and maxInts used to declare the storage.
62  */
63 native_handle_t* native_handle_init(char* storage, int numFds, int numInts);
64 
65 /*
66  * native_handle_create
67  *
68  * creates a native_handle_t and initializes it. must be destroyed with
69  * native_handle_delete().
70  *
71  */
72 native_handle_t* native_handle_create(int numFds, int numInts);
73 
74 /*
75  * native_handle_clone
76  *
77  * creates a native_handle_t and initializes it from another native_handle_t.
78  * Must be destroyed with native_handle_delete().
79  *
80  */
81 native_handle_t* native_handle_clone(const native_handle_t* handle);
82 
83 /*
84  * native_handle_delete
85  *
86  * frees a native_handle_t allocated with native_handle_create().
87  * This ONLY frees the memory allocated for the native_handle_t, but doesn't
88  * close the file descriptors; which can be achieved with native_handle_close().
89  *
90  * return 0 on success, or a negative error code on failure
91  *
92  */
93 int native_handle_delete(native_handle_t* h);
94 
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* NATIVE_HANDLE_H_ */
101