*/
#include "tests.h"
+#include <mach/mach.h>
extern char g_target_path[ PATH_MAX ];
char * my_test_page_p = NULL;
ssize_t my_result;
pid_t my_pid, my_wait_pid;
+ kern_return_t my_kr;
- my_pathp = (char *) malloc( PATH_MAX );
- if ( my_pathp == NULL ) {
- printf( "malloc failed with error %d - \"%s\" \n", errno, strerror( errno) );
- goto test_failed_exit;
- }
+ my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_pathp, PATH_MAX, VM_FLAGS_ANYWHERE);
+ if(my_kr != KERN_SUCCESS){
+ printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ goto test_failed_exit;
+ }
+
*my_pathp = 0x00;
strcat( my_pathp, &g_target_path[0] );
strcat( my_pathp, "/" );
}
my_page_size = getpagesize( );
- my_test_page_p = (char *) malloc( my_page_size );
- if ( my_test_page_p == NULL ) {
- printf( "malloc failed with error %d - \"%s\" \n", errno, strerror( errno) );
- goto test_failed_exit;
- }
+ my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_test_page_p, my_page_size, VM_FLAGS_ANYWHERE);
+ if(my_kr != KERN_SUCCESS){
+ printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ goto test_failed_exit;
+ }
+
*my_test_page_p = 0x00;
strcat( my_test_page_p, "parent data" );
strcat( my_test_page_p, " child data" );
/* create a test file in page size chunks */
- my_bufp = (char *) malloc( (my_page_size * 10) );
- if ( my_bufp == NULL ) {
- printf( "malloc failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ my_kr = vm_allocate((vm_map_t) mach_task_self(), (vm_address_t*)&my_bufp, (my_page_size * 10), VM_FLAGS_ANYWHERE);
+ if(my_kr != KERN_SUCCESS){
+ printf( "vm_allocate failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ my_err = -1;
+ goto exit_child;
+ }
+
+ /* test madvise on anonymous memory */
+ my_err = madvise(my_bufp, (my_page_size * 10), MADV_WILLNEED);
+ if ( my_err == -1 ) {
+ printf("madvise WILLNEED on anon memory failed with error %d - \"%s\" \n", errno, strerror( errno ) );
my_err = -1;
goto exit_child;
}
goto exit_child;
}
+ /* test madvise on anonymous memory */
+ my_err = madvise(my_bufp, (my_page_size * 10), MADV_DONTNEED);
+ if ( my_err == -1 ) {
+ printf("madvise DONTNEED on anon memory failed with error %d - \"%s\" \n", errno, strerror( errno ) );
+ my_err = -1;
+ goto exit_child;
+ }
+
my_result = write( my_fd, my_bufp, (my_page_size * 10) );
if ( my_result == -1 ) {
printf( "write call failed with error %d - \"%s\" \n", errno, strerror( errno) );
/* test madvise */
my_err = madvise( my_addr, (my_page_size * 2), MADV_WILLNEED );
if ( my_err == -1 ) {
- printf( "madvise call failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ printf( "madvise WILLNEED call failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ my_err = -1;
+ goto exit_child;
+ }
+
+ my_err = madvise( my_addr, (my_page_size * 2), MADV_DONTNEED );
+ if ( my_err == -1 ) {
+ printf( "madvise DONTNEED call failed with error %d - \"%s\" \n", errno, strerror( errno) );
my_err = -1;
goto exit_child;
}
goto exit_child;
}
+ /* mybufp is about to be reused, so test madvise on anonymous memory */
+ my_err = madvise(my_bufp, (my_page_size * 10), MADV_FREE);
+ if ( my_err == -1 ) {
+ printf("madvise FREE on anon memory failed with error %d - \"%s\" \n", errno, strerror( errno ) );
+ my_err = -1;
+ goto exit_child;
+ }
+
my_err = mincore( my_addr, 1, my_bufp );
if ( my_err == -1 ) {
printf( "mincore call failed with error %d - \"%s\" \n", errno, strerror( errno) );
goto exit_child;
}
+ /* test madvise */
+ my_err = madvise( my_addr, (my_page_size * 2), MADV_DONTNEED );
+ if ( my_err == -1 ) {
+ printf( "madvise DONTNEED call failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ my_err = -1;
+ goto exit_child;
+ }
+
+ /* test madvise */
+ my_err = madvise( my_addr, (my_page_size * 2), MADV_FREE );
+ if ( my_err == -1 ) {
+ printf( "madvise FREE call failed with error %d - \"%s\" \n", errno, strerror( errno) );
+ my_err = -1;
+ goto exit_child;
+ }
+
/* verify that the file was updated */
lseek( my_fd, 0, SEEK_SET );
bzero( (void *)my_bufp, my_page_size );
test_passed_exit:
if ( my_pathp != NULL ) {
remove( my_pathp );
- free( my_pathp );
+ vm_deallocate(mach_task_self(), (vm_address_t)my_pathp, PATH_MAX);
}
if ( my_test_page_p != NULL ) {
- free( my_test_page_p );
+ vm_deallocate(mach_task_self(), (vm_address_t)my_test_page_p, my_page_size);
}
return( my_err );
}