• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typedef const native_handle_t* buffer_handle_t;
47 
48 /*
49  * native_handle_close
50  *
51  * closes the file descriptors contained in this native_handle_t
52  *
53  * return 0 on success, or a negative error code on failure
54  *
55  */
56 int native_handle_close(const native_handle_t* h);
57 
58 /*
59  * native_handle_init
60  *
61  * Initializes a native_handle_t from storage.  storage must be declared with
62  * NATIVE_HANDLE_DECLARE_STORAGE.  numFds and numInts must not respectively
63  * exceed maxFds and maxInts used to declare the storage.
64  */
65 native_handle_t* native_handle_init(char* storage, int numFds, int numInts);
66 
67 /*
68  * native_handle_create
69  *
70  * creates a native_handle_t and initializes it. must be destroyed with
71  * native_handle_delete().
72  *
73  */
74 native_handle_t* native_handle_create(int numFds, int numInts);
75 
76 /*
77  * native_handle_clone
78  *
79  * creates a native_handle_t and initializes it from another native_handle_t.
80  * Must be destroyed with native_handle_delete().
81  *
82  */
83 native_handle_t* native_handle_clone(const native_handle_t* handle);
84 
85 /*
86  * native_handle_delete
87  *
88  * frees a native_handle_t allocated with native_handle_create().
89  * This ONLY frees the memory allocated for the native_handle_t, but doesn't
90  * close the file descriptors; which can be achieved with native_handle_close().
91  *
92  * return 0 on success, or a negative error code on failure
93  *
94  */
95 int native_handle_delete(native_handle_t* h);
96 
97 
98 #ifdef __cplusplus
99 }
100 #endif
101 
102 #endif /* NATIVE_HANDLE_H_ */
103