mmap.c

Go to the documentation of this file.
00001 
00006 #include "ctl/ctldef.h"
00007 #include "ctl/dir.h"
00008 #ifdef WIN32
00009 #include <Windows.h>
00010 #include <io.h>
00011 #include <fcntl.h>
00012 #else
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <sys/mman.h>
00016 #include <unistd.h>
00017 #include <dirent.h>
00018 #endif
00019 #include <fcntl.h>
00020 #include <sys/stat.h>
00021 #include <stdio.h>
00022 #include "ctl/mmap.h"
00023 
00024 #ifndef CTL_UNIT
00025 #ifdef WIN32
00026 
00031 const void* ctl_mmap_open( ctl_mmap* map, const char* szPath )
00032 {
00033     assertobjptr(map);
00034     assertconst(szPath,1);
00035     map->base = NULL;
00036     map->size = 0;
00037     map->hMap = INVALID_HANDLE_VALUE;
00038     map->hFile = CreateFile( szPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
00039     if ( map->hFile != INVALID_HANDLE_VALUE ) 
00040     { 
00041         map->size = (size_t)GetFileSize( map->hFile, NULL );
00042         map->hMap = CreateFileMapping( map->hFile, NULL, PAGE_READONLY, 0, 0, NULL );
00043         if ( map->hMap != INVALID_HANDLE_VALUE ) 
00044         {
00045             map->base = MapViewOfFile( map->hMap, FILE_MAP_READ, 0, 0, 0 );
00046             if( map->base )
00047                 return map->base;
00048             if( map->hMap != INVALID_HANDLE_VALUE )
00049                 CloseHandle( map->hMap );
00050         }
00051         CloseHandle( map->hFile );
00052         map->hMap = INVALID_HANDLE_VALUE;
00053         map->hFile = INVALID_HANDLE_VALUE;
00054     }
00055     return NULL;
00056 }
00057 
00062 void ctl_mmap_close( ctl_mmap* map )
00063 {
00064     assertobjptr(map);
00065     if( map->base )
00066         UnmapViewOfFile( map->base );
00067     if( map->hMap != INVALID_HANDLE_VALUE )
00068         CloseHandle( map->hMap );
00069     if( map->hFile != INVALID_HANDLE_VALUE )
00070         CloseHandle( map->hFile );
00071     map->hMap = INVALID_HANDLE_VALUE;
00072     map->hFile = INVALID_HANDLE_VALUE;
00073     map->base = NULL;
00074     map->size = 0;
00075 }
00076 
00077 
00078 #else /* !WIN32 */
00079 
00085 const void* ctl_mmap_open( ctl_mmap* map, const char* szPath )
00086 {
00087     assertobjptr(map);
00088     assertconst(szPath,1);
00089     map->base = NULL;
00090     map->size = 0;
00091     map->fd = open(szPath, O_RDONLY);
00092     if(map->fd != -1) 
00093     { 
00094         struct stat buf;
00095         if ( !fstat(map->fd, &buf) )
00096         { 
00097             map->size = buf.st_size;
00098             map->base = mmap(0, map->size, PROT_READ, MAP_SHARED, map->fd, 0);
00099             if( map->base == MAP_FAILED )
00100             {
00101                 map->base = NULL;
00102                 map->size = 0;
00103                 close(map->fd);
00104                 map->fd = -1;
00105             }
00106             return map->base;
00107         }
00108     }
00109     return NULL;
00110 }
00111 
00116 void ctl_mmap_close( ctl_mmap* map )
00117 {
00118     assertobjptr(map);
00119     if( map->base )
00120         munmap( (void*)map->base, map->size );
00121     if( map->fd != -1 )
00122         close( map->fd );
00123     map->fd = -1;
00124     map->base = NULL;
00125     map->size = 0;
00126 }
00127 
00128 
00129 #endif /* IS_MSVC */
00130 
00131 #else /* CTL_UNIT */
00132 
00133 #include "unit/unit.h"
00134 
00141 void Test_MMAp(void)
00142 {
00143     const char szTesting[] = "Now is the time for all good men to come to the aid of their country.";
00144     FILE* fp = fopen( "Data.txt", "wb" );
00145     if( fp )
00146     {
00147         if( 1 != fwrite( szTesting, sizeof(szTesting), 1, fp ) )
00148         {
00149             fclose(fp);
00150             return;
00151         }
00152         else
00153         {
00154             fclose(fp);
00155         }
00156         {
00157             ctl_mmap map;
00158             ctl_mmap_open( &map, "Data.txt" );
00159             Test_Error( ctl_mmap_ready(&map) );
00160             Test_Error( !strcmp( (const char*)ctl_mmap_ptr(&map), szTesting ) );
00161             ctl_mmap_close( &map );
00162             unlink("Data.txt");
00163         }
00164     }
00165 }
00166 
00167 #endif /* CTL_UNIT */
00168 

Generated on Fri Jan 2 15:28:34 2009 for Squat by  doxygen 1.5.6