-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add memory mapping and locking to file helpers
PiperOrigin-RevId: 703514385
- Loading branch information
1 parent
0c4adef
commit 0bd500e
Showing
8 changed files
with
337 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "mediapipe/framework/deps/mlock_helpers.h" | ||
|
||
#include <cstddef> | ||
|
||
#ifdef _WIN32 | ||
#include <memoryapi.h> | ||
#else | ||
#include <sys/mman.h> | ||
#endif | ||
|
||
#include "absl/status/status.h" | ||
#include "absl/strings/str_cat.h" | ||
#include "mediapipe/framework/deps/platform_strings.h" | ||
|
||
namespace mediapipe { | ||
#ifdef _WIN32 | ||
absl::Status LockMemory(const void* base_address, size_t length) { | ||
BOOL status = VirtualLock(const_cast<LPVOID>(base_address), length); | ||
if (!status) { | ||
return absl::UnavailableError( | ||
absl::StrCat("Failed to lock pages in memory: ", FormatLastError())); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
|
||
absl::Status UnlockMemory(const void* base_address, size_t length) { | ||
BOOL status = VirtualUnlock(const_cast<LPVOID>(base_address), length); | ||
if (!status) { | ||
return absl::UnavailableError( | ||
absl::StrCat("Failed to unlock memory pages: ", FormatLastError())); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
#else // _WIN32 | ||
absl::Status LockMemory(const void* base_address, size_t length) { | ||
int status = mlock(base_address, length); | ||
if (status < 0) { | ||
return absl::UnavailableError( | ||
absl::StrCat("Failed to lock pages in memory: ", FormatLastError())); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
|
||
absl::Status UnlockMemory(const void* base_address, size_t length) { | ||
int status = munlock(base_address, length); | ||
if (status < 0) { | ||
return absl::UnavailableError( | ||
absl::StrCat("Failed to unlock memory pages: ", FormatLastError())); | ||
} | ||
return absl::OkStatus(); | ||
} | ||
#endif // _WIN32 | ||
} // namespace mediapipe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef MEDIAPIPE_FRAMEWORK_DEPS_MLOCK_HELPERS_H_ | ||
#define MEDIAPIPE_FRAMEWORK_DEPS_MLOCK_HELPERS_H_ | ||
#include "absl/status/status.h" | ||
|
||
namespace mediapipe { | ||
// Uses `mlock`/`VirtualLock` to pin memory pages. | ||
absl::Status LockMemory(const void* base_address, size_t length); | ||
// Unlocks a previously locked memory region. | ||
absl::Status UnlockMemory(const void* base_address, size_t length); | ||
} // namespace mediapipe | ||
#endif // MEDIAPIPE_FRAMEWORK_DEPS_MLOCK_HELPERS_H_ |
Oops, something went wrong.