/g/ - Technology

install openbsd

[Make a Post]
[X]





mlock() and munlock() on Linux Nanonymous No.9876 [D][S][L][A][C] >>9885
I am writing software in C++ that handles sensitive data that I do not want swapped out to permanent storage.
From what I can tell, mlock(const void *addr, size_t len) locks pages and munlock(const void *addr, size_t len) unlocks them.
The man page says:
munlock() unlocks pages in the address range starting at addr and continuing for len bytes. After this call, all pages that contain a part of the specified memory range can be moved to external swap space again by the kernel.

Suppose I call mlock() on two different buffers, and I want to unlock one of them later in the program.
Is there a chance that both buffers may be unlocked by one call to munlock() if they are both on the same page boundary?
My current design has class constructors calling mlock() with the destructors calling munlock(), but I'm not sure if this is safe.

Any suggestions Nanons?

Nanonymous No.9877 [D]
Not really sure. You could always get more RAM and turn off swap.

Nanonymous No.9878 [D] >>9879
A call to mmap is guaranteed to return a certain number of complete pages. So make sure to directly allocate your memory with a call to mmap, perhaps by writing a custom allocator.

Also, use the sticky next time.

Nanonymous No.9879 [D] >>9880
>>9878

Is there a solution that could be used cross-platform?
Right now this project supports both Linux and Windows.
On Windows I'm using VirtualLock() and VirtualUnlock().

Nanonymous No.9880 [D] >>9881
>>9879
mlock/munlock are in POSIX

Nanonymous No.9881 [D]
>>9880
Yes, On POSIX systems I use mlock and munlock, and on Windows I use VirtualLock and VirtualUnlock.

Nanonymous No.9885 [D] >>9908
>>9876
>C++
>sensitive data
Whatcha doin glownigger?

Nanonymous No.9908 [D] >>9909
>>9885
Schizoid

Nanonymous No.9909 [D] >>9914
>>9908
Shoo shoo jew

Nanonymous No.9914 [D]
>>9909
Use HolyC

Nanonymous No.9937 [D]
Just in case anyone else is interested in doing this in their own software, I simply redesiged all my classes so that their buffers are allocated and managed externally, and they can all be locked and unlocked from one buffer.