mmap.c File Reference

Map files into memory. More...

#include "ctl/ctldef.h"
#include "ctl/dir.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include "ctl/mmap.h"

Include dependency graph for mmap.c:

Go to the source code of this file.

Functions

const void * ctl_mmap_open (ctl_mmap *map, const char *szPath)
 Open a memory map.
void ctl_mmap_close (ctl_mmap *map)
 Close a memory map.


Detailed Description

Map files into memory.

Author:
David Mace

Definition in file mmap.c.


Function Documentation

void ctl_mmap_close ( ctl_mmap *  map  ) 

Close a memory map.

Parameters:
map Map instance to open

Definition at line 116 of file mmap.c.

References assertobjptr.

Referenced by ppCPP().

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 }

const void* ctl_mmap_open ( ctl_mmap *  map,
const char *  szPath 
)

Open a memory map.

Parameters:
map Map instance to open
szPath Path to file to map into memory

Definition at line 85 of file mmap.c.

References assertconst, and assertobjptr.

Referenced by BMI_Load(), and ppCPP().

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 }


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