MemoryHeapBase.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #define LOG_TAG "MemoryHeapBase"
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/ioctl.h>
  25. #include <cutils/log.h>
  26. #include <cutils/ashmem.h>
  27. #include <cutils/atomic.h>
  28. #include <binder/MemoryHeapBase.h>
  29. namespace android {
  30. // ---------------------------------------------------------------------------
  31. MemoryHeapBase::MemoryHeapBase()
  32. : mFD(-1), mSize(0), mBase(MAP_FAILED),
  33. mDevice(NULL), mNeedUnmap(false), mOffset(0)
  34. {
  35. }
  36. MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
  37. : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
  38. mDevice(0), mNeedUnmap(false), mOffset(0)
  39. {
  40. const size_t pagesize = getpagesize();
  41. size = ((size + pagesize-1) & ~(pagesize-1));
  42. int fd = ashmem_create_region(name == NULL ? "MemoryHeapBase" : name, size);
  43. ALOGE_IF(fd<0, "error creating ashmem region: %s", strerror(errno));
  44. if (fd >= 0) {
  45. if (mapfd(fd, size) == NO_ERROR) {
  46. if (flags & READ_ONLY) {
  47. ashmem_set_prot_region(fd, PROT_READ);
  48. }
  49. }
  50. }
  51. }
  52. MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
  53. : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
  54. mDevice(0), mNeedUnmap(false), mOffset(0)
  55. {
  56. int open_flags = O_RDWR;
  57. if (flags & NO_CACHING)
  58. open_flags |= O_SYNC;
  59. int fd = open(device, open_flags);
  60. ALOGE_IF(fd<0, "error opening %s: %s", device, strerror(errno));
  61. if (fd >= 0) {
  62. const size_t pagesize = getpagesize();
  63. size = ((size + pagesize-1) & ~(pagesize-1));
  64. if (mapfd(fd, size) == NO_ERROR) {
  65. mDevice = device;
  66. }
  67. }
  68. }
  69. MemoryHeapBase::MemoryHeapBase(int fd, size_t size, uint32_t flags, uint32_t offset)
  70. : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
  71. mDevice(0), mNeedUnmap(false), mOffset(0)
  72. {
  73. const size_t pagesize = getpagesize();
  74. size = ((size + pagesize-1) & ~(pagesize-1));
  75. mapfd(dup(fd), size, offset);
  76. }
  77. status_t MemoryHeapBase::init(int fd, void *base, int size, int flags, const char* device)
  78. {
  79. if (mFD != -1) {
  80. return INVALID_OPERATION;
  81. }
  82. mFD = fd;
  83. mBase = base;
  84. mSize = size;
  85. mFlags = flags;
  86. mDevice = device;
  87. return NO_ERROR;
  88. }
  89. status_t MemoryHeapBase::mapfd(int fd, size_t size, uint32_t offset)
  90. {
  91. if (size == 0) {
  92. // try to figure out the size automatically
  93. struct stat sb;
  94. if (fstat(fd, &sb) == 0)
  95. size = sb.st_size;
  96. // if it didn't work, let mmap() fail.
  97. }
  98. if ((mFlags & DONT_MAP_LOCALLY) == 0) {
  99. void* base = (uint8_t*)mmap(0, size,
  100. PROT_READ|PROT_WRITE, MAP_SHARED, fd, offset);
  101. if (base == MAP_FAILED) {
  102. ALOGE("mmap(fd=%d, size=%u) failed (%s)",
  103. fd, uint32_t(size), strerror(errno));
  104. close(fd);
  105. return -errno;
  106. }
  107. //ALOGD("mmap(fd=%d, base=%p, size=%lu)", fd, base, size);
  108. mBase = base;
  109. mNeedUnmap = true;
  110. } else {
  111. mBase = 0; // not MAP_FAILED
  112. mNeedUnmap = false;
  113. }
  114. mFD = fd;
  115. mSize = size;
  116. mOffset = offset;
  117. return NO_ERROR;
  118. }
  119. MemoryHeapBase::~MemoryHeapBase()
  120. {
  121. dispose();
  122. }
  123. void MemoryHeapBase::dispose()
  124. {
  125. int fd = android_atomic_or(-1, &mFD);
  126. if (fd >= 0) {
  127. if (mNeedUnmap) {
  128. //ALOGD("munmap(fd=%d, base=%p, size=%lu)", fd, mBase, mSize);
  129. munmap(mBase, mSize);
  130. }
  131. mBase = 0;
  132. mSize = 0;
  133. close(fd);
  134. }
  135. }
  136. int MemoryHeapBase::getHeapID() const {
  137. return mFD;
  138. }
  139. void* MemoryHeapBase::getBase() const {
  140. return mBase;
  141. }
  142. size_t MemoryHeapBase::getSize() const {
  143. return mSize;
  144. }
  145. uint32_t MemoryHeapBase::getFlags() const {
  146. return mFlags;
  147. }
  148. const char* MemoryHeapBase::getDevice() const {
  149. return mDevice;
  150. }
  151. uint32_t MemoryHeapBase::getOffset() const {
  152. return mOffset;
  153. }
  154. // ---------------------------------------------------------------------------
  155. }; // namespace android