ashmem-host.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2008 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. #include <cutils/ashmem.h>
  17. /*
  18. * Implementation of the user-space ashmem API for the simulator, which lacks
  19. * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
  20. */
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <limits.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include <utils/Compat.h>
  32. int ashmem_create_region(const char* /*ignored*/, size_t size) {
  33. char pattern[PATH_MAX];
  34. snprintf(pattern, sizeof(pattern), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid());
  35. int fd = mkstemp(pattern);
  36. if (fd == -1) return -1;
  37. unlink(pattern);
  38. if (TEMP_FAILURE_RETRY(ftruncate(fd, size)) == -1) {
  39. close(fd);
  40. return -1;
  41. }
  42. return fd;
  43. }
  44. int ashmem_set_prot_region(int /*fd*/, int /*prot*/) {
  45. return 0;
  46. }
  47. int ashmem_pin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
  48. return 0 /*ASHMEM_NOT_PURGED*/;
  49. }
  50. int ashmem_unpin_region(int /*fd*/, size_t /*offset*/, size_t /*len*/) {
  51. return 0 /*ASHMEM_IS_UNPINNED*/;
  52. }
  53. int ashmem_get_size_region(int fd)
  54. {
  55. struct stat buf;
  56. int result = fstat(fd, &buf);
  57. if (result == -1) {
  58. return -1;
  59. }
  60. /*
  61. * Check if this is an "ashmem" region.
  62. * TODO: This is very hacky, and can easily break.
  63. * We need some reliable indicator.
  64. */
  65. if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
  66. errno = ENOTTY;
  67. return -1;
  68. }
  69. return buf.st_size;
  70. }
  71. void ashmem_init() {}