2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef NATIVE_HANDLE_H_
18 #define NATIVE_HANDLE_H_
26 #define NATIVE_HANDLE_MAX_FDS 1024
27 #define NATIVE_HANDLE_MAX_INTS 1024
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))]
34 typedef struct native_handle
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"
43 int data
[0]; /* numFds + numInts ints */
44 #if defined(__clang__)
45 #pragma clang diagnostic pop
49 typedef const native_handle_t
* buffer_handle_t
;
54 * closes the file descriptors contained in this native_handle_t
56 * return 0 on success, or a negative error code on failure
59 int native_handle_close(const native_handle_t
* h
);
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.
68 native_handle_t
* native_handle_init(char* storage
, int numFds
, int numInts
);
71 * native_handle_create
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.
78 native_handle_t
* native_handle_create(int numFds
, int numInts
);
83 * creates a native_handle_t and initializes it from another native_handle_t.
84 * Must be destroyed with native_handle_delete().
87 native_handle_t
* native_handle_clone(const native_handle_t
* handle
);
90 * native_handle_delete
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().
96 * return 0 on success, or a negative error code on failure
99 int native_handle_delete(native_handle_t
* h
);
106 #endif /* NATIVE_HANDLE_H_ */