Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XNN_HAS_MMAP to common.h, as a way to indicate that a given platform doesn't support mmap() at all and must fall back to malloc() instead. #6816

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#ifdef _WIN32
#include <windows.h>
#else
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>
#include <unistd.h>
#endif

Expand Down
20 changes: 16 additions & 4 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#endif

#include <errno.h>
#if XNN_HAS_MMAP
#include <sys/mman.h>
#endif
#include <unistd.h>
#endif // XNN_PLATFORM_WINDOWS

Expand Down Expand Up @@ -60,7 +62,7 @@ static size_t get_page_size() {
GetSystemInfo(&sysinfo);
assert(sysinfo.dwPageSize != 0);
system_page_size = (size_t) sysinfo.dwPageSize;
#elif XNN_PLATFORM_QURT
#elif XNN_PLATFORM_QURT || !XNN_HAS_MMAP
// sysconf(_SC_PAGESIZE) will fail, but we're just using malloc anyway.
system_page_size = 4096;
#else
Expand Down Expand Up @@ -94,6 +96,13 @@ static void* allocate_buffer(size_t size) {
xnn_log_error("failed to allocate %zu bytes for code/weights buffer, error code: %d", size, errno);
return NULL;
}
#elif !XNN_HAS_MMAP
// Emulate through malloc.
void* p = malloc(size);
if (p == NULL) {
xnn_log_error("failed to allocate %zu bytes for code/weights buffer, error code: %d", size, errno);
return NULL;
}
#else
void* p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (p == MAP_FAILED) {
Expand All @@ -115,6 +124,9 @@ static enum xnn_status release_memory(void* start, size_t capacity) {
#elif XNN_PLATFORM_QURT
// Emulate through qurt_free.
qurt_free(start);
#elif !XNN_HAS_MMAP
// Emulate through free.
free(start);
#else
if (munmap(start, capacity) == -1) {
xnn_log_error("failed to release code/weights buffer, error code: %d", errno);
Expand Down Expand Up @@ -192,7 +204,7 @@ static enum xnn_status release_unused_memory(size_t size, void* start, size_t* c
return xnn_status_invalid_state;
}
*capacity = page_aligned_size;
#elif XNN_PLATFORM_QURT
#elif XNN_PLATFORM_QURT || !XNN_HAS_MMAP
// We can't selectively release parts of this memory --
// we *could* release the entire block if unused_capacity == *capacity,
// but that would invalidate the start pointer. Just do nothing.
Expand Down Expand Up @@ -242,8 +254,8 @@ static enum xnn_status set_memory_permission(void* start, size_t size, enum xnn_
"failed to set memory permission (%d), error code: %" PRIu32, permission, (uint32_t) GetLastError());
return xnn_status_invalid_state;
}
#elif XNN_PLATFORM_WEB || XNN_PLATFORM_QURT
// Memory protection not supported on Web or QuRT.
#elif XNN_PLATFORM_WEB || XNN_PLATFORM_QURT || !XNN_HAS_MMAP
// Memory protection not supported.
return xnn_status_success;
#else
int prot = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/xnnpack/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
#endif
#endif // XNN_PLATFORM_JIT

#if XNN_PLATFORM_WINDOWS
#define XNN_HAS_MMAP 0
#else
#define XNN_HAS_MMAP 1
#endif

// Define compile identification macros

#if defined(__clang__)
Expand Down
Loading