MemoryMappedFile.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_USE_GENERIC_MEMORY_MAPPED_FILE
  3. #include "os/File.h"
  4. #include "os/MemoryMappedFile.h"
  5. #include "os/Mutex.h"
  6. #include "utils/dynamic_array.h"
  7. #include "utils/Memory.h"
  8. #include "Baselib.h"
  9. #include "Cpp/ReentrantLock.h"
  10. namespace il2cpp
  11. {
  12. namespace os
  13. {
  14. struct MemoryFileData
  15. {
  16. MemoryFileData(const char* name, int64_t dataSize)
  17. : size(dataSize)
  18. {
  19. size_t nameSize = strlen(name);
  20. mapName = (char*)IL2CPP_MALLOC(nameSize + 1);
  21. strcpy(mapName, name);
  22. handle = IL2CPP_MALLOC_ZERO((size_t)dataSize);
  23. }
  24. ~MemoryFileData()
  25. {
  26. IL2CPP_FREE(mapName);
  27. IL2CPP_FREE(handle);
  28. }
  29. char* mapName;
  30. void* handle;
  31. int64_t size;
  32. };
  33. static baselib::ReentrantLock s_Mutex;
  34. static il2cpp::utils::dynamic_array<MemoryFileData*> s_MemoryFiles;
  35. static il2cpp::utils::dynamic_array<void*> s_MemoryFileViews;
  36. static bool IsMemoryFile(void* possibleMemoryFile)
  37. {
  38. os::FastAutoLock lock(&s_Mutex);
  39. for (il2cpp::utils::dynamic_array<MemoryFileData*>::const_iterator it = s_MemoryFiles.begin(); it != s_MemoryFiles.end(); ++it)
  40. {
  41. if ((*it)->handle == possibleMemoryFile)
  42. return true;
  43. }
  44. return false;
  45. }
  46. static MemoryFileData* FindMemoryFile(void* possibleMemoryFile)
  47. {
  48. for (il2cpp::utils::dynamic_array<MemoryFileData*>::iterator it = s_MemoryFiles.begin(); it != s_MemoryFiles.end(); ++it)
  49. {
  50. if ((*it)->handle == possibleMemoryFile)
  51. {
  52. return *it;
  53. }
  54. }
  55. return NULL;
  56. }
  57. static MemoryFileData* FindMemoryFile(const char* mapName)
  58. {
  59. for (il2cpp::utils::dynamic_array<MemoryFileData*>::iterator it = s_MemoryFiles.begin(); it != s_MemoryFiles.end(); ++it)
  60. {
  61. if (strcmp((*it)->mapName, mapName) == 0)
  62. {
  63. return *it;
  64. }
  65. }
  66. return NULL;
  67. }
  68. static bool IsMemoryFileView(void* possibleMemoryFile)
  69. {
  70. os::FastAutoLock lock(&s_Mutex);
  71. return std::find(s_MemoryFileViews.begin(), s_MemoryFileViews.end(), possibleMemoryFile) != s_MemoryFileViews.end();
  72. }
  73. FileHandle* MemoryMappedFile::Create(FileHandle* file, const char* mapName, int32_t mode, int64_t *capacity, MemoryMappedFileAccess access, int32_t options, MemoryMappedFileError* error)
  74. {
  75. if (file == NULL)
  76. {
  77. if (mapName != NULL)
  78. {
  79. os::FastAutoLock lock(&s_Mutex);
  80. const MemoryFileData* memoryFileData = FindMemoryFile(mapName);
  81. if (memoryFileData != NULL)
  82. {
  83. file = (FileHandle*)memoryFileData->handle;
  84. }
  85. else
  86. {
  87. MemoryFileData* memoryFileData = new MemoryFileData(mapName, *capacity);
  88. s_MemoryFiles.push_back(memoryFileData);
  89. file = (FileHandle*)memoryFileData->handle;
  90. }
  91. }
  92. }
  93. return file;
  94. }
  95. static MemoryMappedFile::MemoryMappedFileHandle ViewRealFile(FileHandle* mappedFileHandle, int64_t* length, int64_t offset, MemoryMappedFileAccess access, MemoryMappedFileError* error)
  96. {
  97. int fileError = 0;
  98. if (*length == 0)
  99. {
  100. *length = os::File::GetLength(mappedFileHandle, &fileError);
  101. if (fileError != 0)
  102. {
  103. if (error != NULL)
  104. *error = COULD_NOT_MAP_MEMORY;
  105. return NULL;
  106. }
  107. }
  108. void* buffer = IL2CPP_MALLOC_ZERO((size_t)*length);
  109. os::File::Seek(mappedFileHandle, offset, 0, &fileError);
  110. if (fileError != 0)
  111. {
  112. IL2CPP_FREE(buffer);
  113. if (error != NULL)
  114. *error = COULD_NOT_MAP_MEMORY;
  115. return NULL;
  116. }
  117. int bytesRead = File::Read(mappedFileHandle, (char*)buffer, (int)*length, &fileError);
  118. if (bytesRead != *length || fileError != 0)
  119. {
  120. IL2CPP_FREE(buffer);
  121. if (error != NULL)
  122. *error = COULD_NOT_MAP_MEMORY;
  123. return NULL;
  124. }
  125. return buffer;
  126. }
  127. static MemoryMappedFile::MemoryMappedFileHandle ViewMemoryFile(void* mappedFileHandle, int64_t* length, int64_t offset, MemoryMappedFileAccess access, MemoryMappedFileError* error)
  128. {
  129. os::FastAutoLock lock(&s_Mutex);
  130. const MemoryFileData* memoryFileData = FindMemoryFile(mappedFileHandle);
  131. IL2CPP_ASSERT(memoryFileData != NULL);
  132. if (*length == 0)
  133. *length = memoryFileData->size;
  134. if (*length > memoryFileData->size)
  135. {
  136. if (error != NULL)
  137. *error = COULD_NOT_MAP_MEMORY;
  138. return NULL;
  139. }
  140. void* memoryMappedFileView = (uint8_t*)memoryFileData->handle + offset;
  141. s_MemoryFileViews.push_back(memoryMappedFileView);
  142. return memoryMappedFileView;
  143. }
  144. MemoryMappedFile::MemoryMappedFileHandle MemoryMappedFile::View(FileHandle* mappedFileHandle, int64_t* length, int64_t offset, MemoryMappedFileAccess access, int64_t* actualOffset, MemoryMappedFileError* error)
  145. {
  146. IL2CPP_ASSERT(actualOffset != NULL);
  147. *actualOffset = offset;
  148. if (IsMemoryFile((void*)mappedFileHandle))
  149. return ViewMemoryFile((void*)mappedFileHandle, length, offset, access, error);
  150. else
  151. return ViewRealFile(mappedFileHandle, length, offset, access, error);
  152. }
  153. void MemoryMappedFile::Flush(MemoryMappedFileHandle memoryMappedFileData, int64_t length)
  154. {
  155. }
  156. bool MemoryMappedFile::UnmapView(MemoryMappedFileHandle memoryMappedFileData, int64_t length)
  157. {
  158. if (IsMemoryFileView(memoryMappedFileData))
  159. s_MemoryFileViews.erase_swap_back(&memoryMappedFileData);
  160. else if (!IsMemoryFile(memoryMappedFileData))
  161. IL2CPP_FREE(memoryMappedFileData);
  162. return true;
  163. }
  164. bool MemoryMappedFile::Close(FileHandle* file)
  165. {
  166. os::FastAutoLock lock(&s_Mutex);
  167. MemoryFileData* memoryFileData = FindMemoryFile((void*)file);
  168. if (memoryFileData != NULL)
  169. {
  170. delete memoryFileData;
  171. s_MemoryFiles.erase_swap_back(&memoryFileData);
  172. }
  173. return true;
  174. }
  175. void MemoryMappedFile::ConfigureHandleInheritability(FileHandle* file, bool inheritability)
  176. {
  177. }
  178. bool MemoryMappedFile::OwnsDuplicatedFileHandle(FileHandle* file)
  179. {
  180. return true;
  181. }
  182. }
  183. }
  184. #endif