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