SWDEV-232464 - Memory Map modules loaded via file from hipModuleLoad

Change-Id: I0e644a161c8000abe1b07fbec72de09f1c0a4b18
This commit is contained in:
kjayapra-amd
2020-05-03 23:01:18 -04:00
bovenliggende 6d0fa49c5d
commit 347e36e31b
6 gewijzigde bestanden met toevoegingen van 98 en 3 verwijderingen
Regular → Executable
Bestand weergeven
Regular → Executable
+3
Bestand weergeven
@@ -91,6 +91,9 @@ class Os : AllStatic {
#endif
};
static bool MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size);
static bool MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size);
private:
static const size_t FILE_PATH_MAX_LENGTH = 1024;
Regular → Executable
+39
Bestand weergeven
@@ -707,6 +707,45 @@ void Os::getAppPathAndFileName(std::string& appName, std::string& appPathAndName
return;
}
bool Os::MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size) {
if(munmap(const_cast<void*>(mmap_ptr), mmap_size) != 0) {
return false;
}
return true;
}
bool Os::MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size) {
if ((mmap_ptr == nullptr) || (mmap_size == nullptr)) {
return false;
}
FILE* fp = fopen(fname, "r");
if (fp == nullptr) {
return false;
}
int fd = fileno(fp);
if (fd < 0 ) {
fclose(fp);
return false;
}
fseek(fp, 0L, SEEK_END);
*mmap_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
*mmap_ptr = mmap(NULL, *mmap_size, PROT_READ, MAP_SHARED, fd, 0);
fclose(fp);
if (*mmap_ptr == nullptr) {
return false;
}
return true;
}
} // namespace amd
#endif // !defined(_WIN32) && !defined(__CYGWIN__)
Regular → Executable
+42
Bestand weergeven
@@ -820,6 +820,48 @@ void Os::getAppPathAndFileName(std::string& appName, std::string& appPathAndName
delete[] buff;
return;
}
bool Os::MemoryUnmapFile(const void* mmap_ptr, size_t mmap_size) {
if(!UnmapViewOfFile(mmap_ptr)) {
return false;
}
return true;
}
bool Os::MemoryMapFile(const char* fname, const void** mmap_ptr, size_t* mmap_size) {
if ((mmap_ptr == nullptr) || (mmap_size == nullptr)) {
return false;
}
HANDLE map_handle = INVALID_HANDLE_VALUE;
HANDLE file_handle = INVALID_HANDLE_VALUE;
file_handle = CreateFileA(fname, GENERIC_READ, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY, NULL);
if (file_handle == INVALID_HANDLE_VALUE) {
return false;
}
map_handle = CreateFileMappingA(file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
if (map_handle == INVALID_HANDLE_VALUE) {
CloseHandle(file_handle);
return false;
}
*mmap_size = GetFileSize(file_handle, NULL);
*mmap_ptr = MapViewOfFile(map_handle, FILE_MAP_READ, 0,0,0);
CloseHandle(file_handle);
CloseHandle(map_handle);
if (*mmap_ptr == nullptr) {
return false;
}
return true;
}
} // namespace amd
#endif // _WIN32 || __CYGWIN__
Regular → Executable
+4
Bestand weergeven
@@ -67,6 +67,10 @@ Program::~Program() {
}
}
if (std::get<1>(mmap_) > 0) {
amd::Os::MemoryUnmapFile(std::get<0>(mmap_), std::get<1>(mmap_));
}
delete symbolTable_;
//! @todo Make sure we have destroyed all CPU specific objects
}
Regular → Executable
+10 -3
Bestand weergeven
@@ -80,6 +80,7 @@ class Context;
//! A collection of binaries for devices in the associated context.
class Program : public RuntimeObject {
public:
typedef std::pair<const void* /* mmap_ptr */, const size_t /* mmap_size */> mmap_t;
typedef std::tuple<const uint8_t* /*image*/, size_t /*size*/, bool /*allocated*/> binary_t;
typedef std::set<Device const*> devicelist_t;
typedef std::unordered_map<Device const*, binary_t> devicebinary_t;
@@ -113,6 +114,7 @@ class Program : public RuntimeObject {
//! The device program objects included in this program
deviceprograms_t devicePrograms_;
devicelist_t deviceList_;
const mmap_t mmap_;
std::string programLog_; //!< Log for parsing options, etc.
@@ -133,11 +135,16 @@ class Program : public RuntimeObject {
sourceCode_(sourceCode),
language_(language),
symbolTable_(NULL),
programLog_() {}
mmap_({nullptr, 0}),
programLog_() {
}
//! Construct a new program associated with a context.
Program(Context& context, Language language = Binary)
: context_(context), language_(language), symbolTable_(NULL) {}
Program(Context& context, Language language = Binary,
const void* mmap_ptr = nullptr, const size_t mmap_size = 0)
: context_(context), language_(language),
mmap_({mmap_ptr, mmap_size}),
symbolTable_(NULL) {}
//! Returns context, associated with the current program.
const Context& context() const { return context_(); }