Jan Brittenson wrote:
return (vm_size_t) (1LL << 32) - total;
This assumes the full 4GB is actually available...
You should also be able to get the total number of allocated pages
with
task_info(TASK_BASIC_INFO) without an exhaustive survey of the
address space.
See http://www.gnu.org/software/hurd/gnumach-doc/mach_7.html#SEC64
for the
call.
Thanks for the suggestions. I have rewritten two separate functions
FreeVMem() and MaxVBlock(). FreeVMem uses task_info and is fast;
MaxVBlock is much slower. Both may be of assistance for the OP's
problem.
Skip Haughay wrote:
Now I need a way to be able to check if I have sufficient memory after
my allocations so that the Mac can actually keep running without
crashing in calls such as DrawPicture.
Call FreeVMem() to be sure you've left a safe amount of unallocated
VM. Trial and error should enable you to determine a suitable 'safe
amount' for your app, starting from, say, a guess of 20 MB.
/******************************************
Diagnostic aids for failure of malloc,
calloc, NewPtr and NewHandle.
FreeVMem() and MaxVBlock() are 'replacements'
for the old FreeMem and MaxBlock that return
meaningless constant values in OS X.
The return type is vm_size_t (which is
unsigned int as of 10.3, reflecting the
4GB virtual address space).
Robert P. 30 Jan 2005
******************************************/
#include <mach/mach.h>
#include <mach/task_info.h>
#include <mach/machine/vm_param.h>
// The total unallocated virtual address space.
// This usually represents the sum of several
// non-contiguous blocks.
vm_size_t FreeVMem( void )
{
struct task_basic_info info;
mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
if ( KERN_SUCCESS == task_info( mach_task_self(), TASK_BASIC_INFO,
(task_info_t)&info, &count ) )
return VM_MAX_ADDRESS - VM_MIN_ADDRESS - info.virtual_size; else
return 0;
}
kern_return_t VMAddressBlock( vm_map_t inTask, vm_address_t*
inOutAddress, vm_size_t* outSize )
{
struct vm_region_basic_info info;
kern_return_t err;
mach_port_t objectName = MACH_PORT_NULL;
mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT;
err = vm_region( inTask, inOutAddress, outSize, VM_REGION_BASIC_INFO,
(vm_region_info_t)&info, &count, &objectName );
if ( objectName != MACH_PORT_NULL ) mach_port_deallocate( inTask,
objectName );
return err;
}
// Iterate over all vm allocations for size
// of maximum unallocated block. This approximates
// the maximum malloc request that is likely to succeed.
vm_size_t MaxVBlock( void )
{
vm_map_t task = mach_task_self();
vm_size_t size, gap, maxGap = 0;
vm_address_t oldAddr = VM_MIN_ADDRESS, nextAddr = VM_MIN_ADDRESS;
while ( VMAddressBlock( task, &nextAddr, &size ) == KERN_SUCCESS )
{
if ( oldAddr != nextAddr )
{
gap = nextAddr - oldAddr;
if ( gap > maxGap ) maxGap = gap;
}
nextAddr += size;
oldAddr = nextAddr;
}
return maxGap;
}
int main( void )
{
void* p;
// as a demo, allocate 500 MB repeatedly until failure
do
{
printf( "FreeVMem %d MB MaxVBlock %d MB\n", FreeVMem() >> 20,
MaxVBlock() >> 20 );
p = malloc( 500 << 20 );
}
while ( p );
return 0;
}