native_handle.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2007 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. #define LOG_TAG "NativeHandle"
  17. #include <stdint.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <cutils/log.h>
  23. #include <cutils/native_handle.h>
  24. native_handle_t* native_handle_create(int numFds, int numInts)
  25. {
  26. native_handle_t* h = malloc(
  27. sizeof(native_handle_t) + sizeof(int)*(numFds+numInts));
  28. h->version = sizeof(native_handle_t);
  29. h->numFds = numFds;
  30. h->numInts = numInts;
  31. return h;
  32. }
  33. int native_handle_delete(native_handle_t* h)
  34. {
  35. if (h) {
  36. if (h->version != sizeof(native_handle_t))
  37. return -EINVAL;
  38. free(h);
  39. }
  40. return 0;
  41. }
  42. int native_handle_close(const native_handle_t* h)
  43. {
  44. if (h->version != sizeof(native_handle_t))
  45. return -EINVAL;
  46. const int numFds = h->numFds;
  47. int i;
  48. for (i=0 ; i<numFds ; i++) {
  49. close(h->data[i]);
  50. }
  51. return 0;
  52. }