26 #define MIN_ALLOC_SIZE 8
27 #define MAX_ALLOC_SIZE 8192
46 #if defined( __BEOS__ )
53 #define MEMLOCK_HEADERSIZE roundUp( sizeof( MEMLOCK_INFO ), 4 )
54 #elif INT_MAX <= 0xFFFFFFFFUL
55 #define MEMLOCK_HEADERSIZE roundUp( sizeof( MEMLOCK_INFO ), 8 )
57 #define MEMLOCK_HEADERSIZE roundUp( sizeof( MEMLOCK_INFO ), 16 )
64 #define CANARY_STARTVALUE "\xC0\xED\xBA\xBE"
65 #define CANARY_ENDVALUE "\x36\xDD\x24\x36"
67 #define adjustMemCanary( size ) \
69 #define insertMemCanary( memBlockPtr, memPtr ) \
70 memcpy( memBlockPtr->canary, CANARY_STARTVALUE, CANARY_SIZE ); \
71 memcpy( memPtr + memBlockPtr->size - CANARY_SIZE, CANARY_ENDVALUE, \
73 #define checkMemCanary( memBlockPtr, memPtr ) \
74 REQUIRES( !memcmp( memBlockPtr->canary, CANARY_STARTVALUE, CANARY_SIZE ) ); \
75 REQUIRES( !memcmp( memPtr + memBlockPtr->size - CANARY_SIZE, \
76 CANARY_ENDVALUE, CANARY_SIZE ) );
87 static
int checkInitAlloc(
OUT void **allocPtrPtr,
92 "Memlock header size" );
104 static
int checkInitFree(
const void **freePtrPtr,
121 *memBlockPtrPtr = NULL;
134 *memBlockPtrPtr = memBlockPtr;
148 MEMLOCK_INFO *allocatedListHead = *allocatedListHeadPtr;
149 MEMLOCK_INFO *allocatedListTail = *allocatedListTailPtr;
152 assert( allocatedListHead == NULL || \
155 assert( allocatedListTail == NULL || \
160 allocatedListTail == NULL ) || \
161 ( allocatedListHead != NULL && \
162 allocatedListTail != NULL ) );
165 if( allocatedListHead == NULL )
167 *allocatedListHeadPtr = *allocatedListTailPtr = memBlockPtr;
172 allocatedListTail->
next = memBlockPtr;
173 memBlockPtr->prev = allocatedListTail;
174 *allocatedListTailPtr = memBlockPtr;
182 MEMLOCK_INFO *allocatedListHead = *allocatedListHeadPtr;
183 MEMLOCK_INFO *allocatedListTail = *allocatedListTailPtr;
188 assert( allocatedListHead == NULL || \
191 assert( allocatedListTail == NULL || \
197 if( memBlockPtr == allocatedListHead )
198 *allocatedListHeadPtr = nextBlockPtr;
204 prevBlockPtr->
next = nextBlockPtr;
206 if( nextBlockPtr != NULL )
207 nextBlockPtr->
prev = prevBlockPtr;
210 if( memBlockPtr == allocatedListTail )
211 *allocatedListTailPtr = prevBlockPtr;
214 memBlockPtr->
next = memBlockPtr->prev = NULL;
226 static void touchAllocatedPages(
void )
240 memBlockPtr = memBlockPtr->
next )
242 const int pageSize =
getSysVar( SYSVAR_PAGESIZE );
247 if( memBlockPtr->
size > pageSize )
249 BYTE *memPtr = (
BYTE * ) memBlockPtr + pageSize;
250 int memSize = memBlockPtr->
size;
256 while( memSize > pageSize )
286 krnlData = krnlDataPtr;
314 #if defined( __WIN32__ )
316 #if !defined( NDEBUG ) && !defined( NT_DRIVER ) && !defined( __BORLANDC__ )
317 #define USE_HEAP_CHECKING
320 #ifdef USE_HEAP_CHECKING
328 #if defined( _MSC_VER ) && ( _MSC_VER >= 1400 )
329 #define PTR_TYPE INT_PTR
331 #define PTR_TYPE long
334 #define getPageStartAddress( address ) \
335 ( ( PTR_TYPE ) ( address ) & ~( pageSize - 1 ) )
336 #define getPageEndAddress( address, size ) \
337 getPageStartAddress( ( PTR_TYPE ) address + ( size ) - 1 )
349 status = checkInitAlloc( pointer, size );
358 if( ( memPtr =
clAlloc(
"krnlMemAlloc", \
369 #if !defined( NT_DRIVER )
399 if( VirtualLock( memPtr, memBlockPtr->
size ) )
408 #ifdef USE_HEAP_CHECKING
410 assert( _CrtIsValidHeapPointer( memBlockPtr ) );
411 assert( memBlockPtr->
next == NULL );
413 _CrtIsValidHeapPointer( memBlockPtr->
prev ) );
427 int krnlMemfree(
INOUT_PTR void **pointer )
434 status = checkInitFree( pointer, &memPtr, &memBlockPtr );
441 #ifdef USE_HEAP_CHECKING
443 assert( _CrtIsValidHeapPointer( memBlockPtr ) );
444 assert( memBlockPtr->
next == NULL || \
445 _CrtIsValidHeapPointer( memBlockPtr->
next ) );
446 assert( memBlockPtr->
prev == NULL || \
447 _CrtIsValidHeapPointer( memBlockPtr->
prev ) );
452 #if !defined( NT_DRIVER )
468 PTR_TYPE block1PageAddress, block2PageAddress;
469 const int pageSize =
getSysVar( SYSVAR_PAGESIZE );
473 block1PageAddress = getPageStartAddress( memBlockPtr );
474 block2PageAddress = getPageEndAddress( memBlockPtr, memBlockPtr->
size );
475 if( block1PageAddress == block2PageAddress )
476 block2PageAddress = 0;
481 currentBlockPtr != NULL; currentBlockPtr = currentBlockPtr->
next )
483 const PTR_TYPE currentPage1Address = \
484 getPageStartAddress( currentBlockPtr );
485 PTR_TYPE currentPage2Address = \
486 getPageEndAddress( currentBlockPtr, currentBlockPtr->
size );
488 if( currentPage1Address == currentPage2Address )
489 currentPage2Address = 0;
493 if( block1PageAddress == currentPage1Address || \
494 block1PageAddress == currentPage2Address )
496 block1PageAddress = 0;
497 if( !block2PageAddress )
500 if( block2PageAddress == currentPage1Address || \
501 block2PageAddress == currentPage2Address )
503 block2PageAddress = 0;
504 if( !block1PageAddress )
511 if( block1PageAddress )
512 VirtualUnlock( (
void * ) block1PageAddress, 16 );
513 if( block2PageAddress )
514 VirtualUnlock( (
void * ) block2PageAddress, 16 );
522 clFree(
"krnlMemFree", memPtr );
534 #elif defined( __UNIX__ ) || defined( __BEOS__ )
540 #if defined( __osf__ ) || defined( __alpha__ )
541 #include <sys/mman.h>
543 #include <sys/mman.h>
544 #include <sys/types.h>
546 int mlock(
void *address,
size_t length );
547 int munlock(
void *address,
size_t length );
562 #if defined( _AIX ) || defined( __alpha__ ) || defined( __aux ) || \
563 defined( _CRAY ) || defined( __CYGWIN__ ) || defined( __hpux ) || \
564 ( defined( __linux__ ) && OSVERSION < 2 ) || \
565 defined( _M_XENIX ) || defined( __osf__ ) || \
566 ( defined( __QNX__ ) && OSVERSION <= 6 ) || \
567 defined( __TANDEM_NSK__ ) || defined( __TANDEM_OSS__ ) || \
569 #define mlock( a, b ) 1
570 #define munlock( a, b )
581 #if defined( __BEOS__ )
586 status = checkInitAlloc( pointer, size );
595 #if defined( __BEOS__ )
616 areaID = create_area(
"memory_block", (
void ** ) &memPtr, B_ANY_ADDRESS,
617 roundUp( size + MEMLOCK_HEADERSIZE, B_PAGE_SIZE ),
618 B_LAZY_LOCK, B_READ_AREA | B_WRITE_AREA );
619 if( areaID < B_NO_ERROR )
621 if( ( memPtr =
clAlloc(
"krnlMemAlloc", \
622 size + MEMLOCK_HEADERSIZE ) ) == NULL )
625 memset( memPtr, 0, size + MEMLOCK_HEADERSIZE );
629 #if defined( __BEOS__ )
630 memBlockPtr->areaID = areaID;
636 #if !defined( __BEOS__ )
637 if( !mlock( memPtr, memBlockPtr->
size ) )
658 int krnlMemfree(
INOUT_PTR void **pointer )
662 #if defined( __BEOS__ )
668 status = checkInitFree( (
const void ** ) pointer, &memPtr,
682 #if defined( __BEOS__ )
683 areaID = memBlockPtr->areaID;
685 delete_area( areaID );
688 munlock( memPtr, memBlockPtr->
size );
693 #if !defined( __BEOS__ )
695 clFree(
"krnlMemFree", memPtr );
708 #elif defined( __CHORUS__ )
714 #include <mem/chMem.h>
725 K_WRITEABLE | K_NODEMAND };
728 status = checkInitAlloc( pointer, size );
737 if( rgnAllocate( K_MYACTOR, &rgnDesc ) != K_OK )
739 memPtr = rgnDesc.startAddr;
740 memset( memPtr, 0, size + MEMLOCK_HEADERSIZE );
764 int krnlMemfree(
INOUT_PTR void **pointer )
768 KnRgnDesc rgnDesc = { K_ANYWHERE, 0, 0 };
772 status = checkInitFree( pointer, &memPtr, &memBlockPtr );
786 rgnDesc.size = memBlockPtr->
size;
787 rgnDesc.startAddr = memPtr;
789 rgnFree( K_MYACTOR, &rgnDesc );
801 #elif defined( __MAC__ )
815 status = checkInitAlloc( pointer, size );
824 if( ( memPtr =
clAlloc(
"krnlMemAlloc", \
825 size + MEMLOCK_HEADERSIZE ) ) == NULL )
827 memset( memPtr, 0, size + MEMLOCK_HEADERSIZE );
835 #if !defined( CALL_NOT_IN_CARBON ) || CALL_NOT_IN_CARBON
841 if( HoldMemory( memPtr, memBlockPtr->
size ) == noErr )
862 int krnlMemfree(
INOUT_PTR void **pointer )
869 status = checkInitFree( pointer, &memPtr, &memBlockPtr );
882 #if !defined( CALL_NOT_IN_CARBON ) || CALL_NOT_IN_CARBON
884 UnholdMemory( memPtr, memBlockPtr->
size );
890 clFree(
"krnlMemFree", memPtr );
904 #if defined( __MSDOS__ ) && defined( __DJGPP__ )
919 status = checkInitAlloc( pointer, size );
928 if( ( memPtr =
clAlloc(
"krnlMemAlloc", \
929 size + MEMLOCK_HEADERSIZE ) ) == NULL )
931 memset( memPtr, 0, size + MEMLOCK_HEADERSIZE );
939 #if defined( __MSDOS__ ) && defined( __DJGPP__ )
941 if( _go32_dpmi_lock_data( memPtr, memBlockPtr->
size ) == 0)
962 int krnlMemfree(
INOUT_PTR void **pointer )
969 status = checkInitFree( pointer, &memPtr, &memBlockPtr );
982 #if defined( __MSDOS__ ) && defined( __DJGPP__ )
993 clFree(
"krnlMemFree", memPtr );