Skip to content
Permalink
Browse files

generalize emscripten directives to have_mlock

  • Loading branch information
woodser committed Feb 11, 2020
1 parent ec33b50 commit fdc96f77f48c64dd36e9da517bb04291ecaa8518
Showing with 24 additions and 36 deletions.
  1. +24 −36 contrib/epee/src/mlocker.cpp
@@ -106,33 +106,29 @@ namespace epee

size_t mlocker::get_page_size()
{
#if defined __EMSCRIPTEN__
return 4096;
#endif

#if defined HAVE_MLOCK
CRITICAL_REGION_LOCAL(mutex());
if (page_size == 0)
page_size = query_page_size();
return page_size;
#else
return 4096;
#endif
}

mlocker::mlocker(void *ptr, size_t len): ptr(ptr), len(len)
{
#if defined __EMSCRIPTEN__
return;
#endif

#if defined HAVE_MLOCK
lock(ptr, len);
#endif
}

mlocker::~mlocker()
{
#if defined __EMSCRIPTEN__
return;
#endif

#if defined HAVE_MLOCK
try { unlock(ptr, len); }
catch (...) { /* ignore and do not propagate through the dtor */ }
#endif
}

void mlocker::lock(void *ptr, size_t len)
@@ -143,16 +139,14 @@ namespace epee
if (page_size == 0)
return;

#if defined __EMSCRIPTEN__
return;
#endif

#if defined HAVE_MLOCK
CRITICAL_REGION_LOCAL(mutex());
const size_t first = ((uintptr_t)ptr) / page_size;
const size_t last = (((uintptr_t)ptr) + len - 1) / page_size;
for (size_t page = first; page <= last; ++page)
lock_page(page);
++num_locked_objects;
#endif

CATCH_ENTRY_L1("mlocker::lock", void());
}
@@ -165,46 +159,41 @@ namespace epee
if (page_size == 0)
return;

#if defined __EMSCRIPTEN__
return;
#endif

#if defined HAVE_MLOCK
CRITICAL_REGION_LOCAL(mutex());
const size_t first = ((uintptr_t)ptr) / page_size;
const size_t last = (((uintptr_t)ptr) + len - 1) / page_size;
for (size_t page = first; page <= last; ++page)
unlock_page(page);
--num_locked_objects;
#endif

CATCH_ENTRY_L1("mlocker::lock", void());
}

size_t mlocker::get_num_locked_pages()
{
#if defined __EMSCRIPTEN__
return 0;
#endif

#if defined HAVE_MLOCK
CRITICAL_REGION_LOCAL(mutex());
return map().size();
#else
return 0;
#endif
}

size_t mlocker::get_num_locked_objects()
{
#if defined __EMSCRIPTEN__
return 0;
#endif

#if defined HAVE_MLOCK
CRITICAL_REGION_LOCAL(mutex());
return num_locked_objects;
#else
return 0;
#endif
}

void mlocker::lock_page(size_t page)
{
#if defined __EMSCRIPTEN__
return;
#endif

#if defined HAVE_MLOCK
std::pair<std::map<size_t, unsigned int>::iterator, bool> p = map().insert(std::make_pair(page, 1));
if (p.second)
{
@@ -214,14 +203,12 @@ namespace epee
{
++p.first->second;
}
#endif
}

void mlocker::unlock_page(size_t page)
{
#if defined __EMSCRIPTEN__
return;
#endif

#if defined HAVE_MLOCK
std::map<size_t, unsigned int>::iterator i = map().find(page);
if (i == map().end())
{
@@ -235,5 +222,6 @@ namespace epee
do_unlock((void*)(page * page_size), page_size);
}
}
#endif
}
}

0 comments on commit fdc96f7

Please sign in to comment.
You can’t perform that action at this time.