Some benighted systems pass a char * to mlock; there

may be a -D to switch behavior (-D_XDG_4_2 seems like a candidate, but
fails in other system include headers) which I can't find right now.
So check the type of the first argument of mlock(2) and #define
types and casts in the few cases where mlock(2) is used.

CCMAIL: bradh@frogmouth.net
CCMAIL: kde-solaris@kde.org

svn path=/trunk/kdesupport/qca/; revision=700540
This commit is contained in:
Adriaan de Groot 2007-08-15 20:01:32 +00:00
parent af40671759
commit 54a5a1cfba
3 changed files with 28 additions and 4 deletions

View File

@ -3,6 +3,17 @@ IF(HAVE_SYS_FILIO_H)
ADD_DEFINITIONS(-DHAVE_SYS_FILIO_H)
ENDIF(HAVE_SYS_FILIO_H)
INCLUDE(CheckCXXSourceCompiles)
CHECK_CXX_SOURCE_COMPILES("
# include <stdlib.h>
# include <sys/mman.h>
int main() { void *f = 0; return mlock(f,8); }
" MLOCK_TAKES_VOID)
if(NOT MLOCK_TAKES_VOID)
MESSAGE(STATUS "mlock(2) does not take a void *")
ADD_DEFINITIONS(-DMLOCK_NOT_VOID_PTR)
endif(NOT MLOCK_TAKES_VOID)
# base source files

View File

@ -145,6 +145,11 @@ void* MemoryMapping_Allocator::alloc_block(u32bit n)
void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n)
{
if(ptr == 0) return;
#ifdef MLOCK_NOT_VOID_PTR
# define MLOCK_TYPE_CAST (char *)
#else
# define MLOCK_TYPE_CAST
#endif
const u32bit OVERWRITE_PASSES = 12;
const byte PATTERNS[] = { 0x00, 0xFF, 0xAA, 0x55, 0x73, 0x8C, 0x5F, 0xA0,
@ -153,14 +158,14 @@ void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n)
for(u32bit j = 0; j != OVERWRITE_PASSES; j++)
{
std::memset(ptr, PATTERNS[j % sizeof(PATTERNS)], n);
if(msync(ptr, n, MS_SYNC))
if(msync(MLOCK_TYPE_CAST ptr, n, MS_SYNC))
throw MemoryMapping_Failed("Sync operation failed");
}
std::memset(ptr, 0, n);
if(msync(ptr, n, MS_SYNC))
if(msync(MLOCK_TYPE_CAST ptr, n, MS_SYNC))
throw MemoryMapping_Failed("Sync operation failed");
if(munmap(ptr, n))
if(munmap(MLOCK_TYPE_CAST ptr, n))
throw MemoryMapping_Failed("Could not unmap file");
}

View File

@ -34,7 +34,15 @@ static bool can_lock()
{
#ifdef Q_OS_UNIX
bool ok = false;
void *d = malloc(256);
#ifdef MLOCK_NOT_VOID_PTR
# define MLOCK_TYPE char *
# define MLOCK_TYPE_CAST (MLOCK_TYPE)
#else
# define MLOCK_TYPE void *
# define MLOCK_TYPE_CAST
#endif
MLOCK_TYPE d = MLOCK_TYPE_CAST malloc(256);
if(mlock(d, 256) == 0)
{
munlock(d, 256);