17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2009 The Android Open Source Project
37ec681f3Smrg *
47ec681f3Smrg * Licensed under the Apache License, Version 2.0 (the "License");
57ec681f3Smrg * you may not use this file except in compliance with the License.
67ec681f3Smrg * You may obtain a copy of the License at
77ec681f3Smrg *
87ec681f3Smrg *      http://www.apache.org/licenses/LICENSE-2.0
97ec681f3Smrg *
107ec681f3Smrg * Unless required by applicable law or agreed to in writing, software
117ec681f3Smrg * distributed under the License is distributed on an "AS IS" BASIS,
127ec681f3Smrg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137ec681f3Smrg * See the License for the specific language governing permissions and
147ec681f3Smrg * limitations under the License.
157ec681f3Smrg */
167ec681f3Smrg
177ec681f3Smrg#ifndef NATIVE_HANDLE_H_
187ec681f3Smrg#define NATIVE_HANDLE_H_
197ec681f3Smrg
207ec681f3Smrg#include <stdalign.h>
217ec681f3Smrg
227ec681f3Smrg#ifdef __cplusplus
237ec681f3Smrgextern "C" {
247ec681f3Smrg#endif
257ec681f3Smrg
267ec681f3Smrg#define NATIVE_HANDLE_MAX_FDS 1024
277ec681f3Smrg#define NATIVE_HANDLE_MAX_INTS 1024
287ec681f3Smrg
297ec681f3Smrg/* Declare a char array for use with native_handle_init */
307ec681f3Smrg#define NATIVE_HANDLE_DECLARE_STORAGE(name, maxFds, maxInts) \
317ec681f3Smrg    alignas(native_handle_t) char (name)[                            \
327ec681f3Smrg      sizeof(native_handle_t) + sizeof(int) * ((maxFds) + (maxInts))]
337ec681f3Smrg
347ec681f3Smrgtypedef struct native_handle
357ec681f3Smrg{
367ec681f3Smrg    int version;        /* sizeof(native_handle_t) */
377ec681f3Smrg    int numFds;         /* number of file-descriptors at &data[0] */
387ec681f3Smrg    int numInts;        /* number of ints at &data[numFds] */
397ec681f3Smrg#if defined(__clang__)
407ec681f3Smrg#pragma clang diagnostic push
417ec681f3Smrg#pragma clang diagnostic ignored "-Wzero-length-array"
427ec681f3Smrg#endif
437ec681f3Smrg    int data[0];        /* numFds + numInts ints */
447ec681f3Smrg#if defined(__clang__)
457ec681f3Smrg#pragma clang diagnostic pop
467ec681f3Smrg#endif
477ec681f3Smrg} native_handle_t;
487ec681f3Smrg
497ec681f3Smrgtypedef const native_handle_t* buffer_handle_t;
507ec681f3Smrg
517ec681f3Smrg/*
527ec681f3Smrg * native_handle_close
537ec681f3Smrg *
547ec681f3Smrg * closes the file descriptors contained in this native_handle_t
557ec681f3Smrg *
567ec681f3Smrg * return 0 on success, or a negative error code on failure
577ec681f3Smrg *
587ec681f3Smrg */
597ec681f3Smrgint native_handle_close(const native_handle_t* h);
607ec681f3Smrg
617ec681f3Smrg/*
627ec681f3Smrg * native_handle_init
637ec681f3Smrg *
647ec681f3Smrg * Initializes a native_handle_t from storage.  storage must be declared with
657ec681f3Smrg * NATIVE_HANDLE_DECLARE_STORAGE.  numFds and numInts must not respectively
667ec681f3Smrg * exceed maxFds and maxInts used to declare the storage.
677ec681f3Smrg */
687ec681f3Smrgnative_handle_t* native_handle_init(char* storage, int numFds, int numInts);
697ec681f3Smrg
707ec681f3Smrg/*
717ec681f3Smrg * native_handle_create
727ec681f3Smrg *
737ec681f3Smrg * creates a native_handle_t and initializes it. must be destroyed with
747ec681f3Smrg * native_handle_delete(). Note that numFds must be <= NATIVE_HANDLE_MAX_FDS,
757ec681f3Smrg * numInts must be <= NATIVE_HANDLE_MAX_INTS, and both must be >= 0.
767ec681f3Smrg *
777ec681f3Smrg */
787ec681f3Smrgnative_handle_t* native_handle_create(int numFds, int numInts);
797ec681f3Smrg
807ec681f3Smrg/*
817ec681f3Smrg * native_handle_clone
827ec681f3Smrg *
837ec681f3Smrg * creates a native_handle_t and initializes it from another native_handle_t.
847ec681f3Smrg * Must be destroyed with native_handle_delete().
857ec681f3Smrg *
867ec681f3Smrg */
877ec681f3Smrgnative_handle_t* native_handle_clone(const native_handle_t* handle);
887ec681f3Smrg
897ec681f3Smrg/*
907ec681f3Smrg * native_handle_delete
917ec681f3Smrg *
927ec681f3Smrg * frees a native_handle_t allocated with native_handle_create().
937ec681f3Smrg * This ONLY frees the memory allocated for the native_handle_t, but doesn't
947ec681f3Smrg * close the file descriptors; which can be achieved with native_handle_close().
957ec681f3Smrg *
967ec681f3Smrg * return 0 on success, or a negative error code on failure
977ec681f3Smrg *
987ec681f3Smrg */
997ec681f3Smrgint native_handle_delete(native_handle_t* h);
1007ec681f3Smrg
1017ec681f3Smrg
1027ec681f3Smrg#ifdef __cplusplus
1037ec681f3Smrg}
1047ec681f3Smrg#endif
1057ec681f3Smrg
1067ec681f3Smrg#endif /* NATIVE_HANDLE_H_ */
107