\doxysection{mmio.\+c}
\label{mmio_8c_source}\index{src/libsphinxbase/util/mmio.c@{src/libsphinxbase/util/mmio.c}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ \textcolor{comment}{/*\ -\/*-\/\ c-\/basic-\/offset:\ 4;\ indent-\/tabs-\/mode:\ nil\ -\/*-\/\ */}}
\DoxyCodeLine{00002\ \textcolor{comment}{/*\ ====================================================================}}
\DoxyCodeLine{00003\ \textcolor{comment}{\ *\ Copyright\ (c)\ 2005\ Carnegie\ Mellon\ University.\ \ All\ rights\ }}
\DoxyCodeLine{00004\ \textcolor{comment}{\ *\ reserved.}}
\DoxyCodeLine{00005\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00006\ \textcolor{comment}{\ *\ Redistribution\ and\ use\ in\ source\ and\ binary\ forms,\ with\ or\ without}}
\DoxyCodeLine{00007\ \textcolor{comment}{\ *\ modification,\ are\ permitted\ provided\ that\ the\ following\ conditions}}
\DoxyCodeLine{00008\ \textcolor{comment}{\ *\ are\ met:}}
\DoxyCodeLine{00009\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00010\ \textcolor{comment}{\ *\ 1.\ Redistributions\ of\ source\ code\ must\ retain\ the\ above\ copyright}}
\DoxyCodeLine{00011\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer.\ }}
\DoxyCodeLine{00012\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00013\ \textcolor{comment}{\ *\ 2.\ Redistributions\ in\ binary\ form\ must\ reproduce\ the\ above\ copyright}}
\DoxyCodeLine{00014\ \textcolor{comment}{\ *\ \ \ \ notice,\ this\ list\ of\ conditions\ and\ the\ following\ disclaimer\ in}}
\DoxyCodeLine{00015\ \textcolor{comment}{\ *\ \ \ \ the\ documentation\ and/or\ other\ materials\ provided\ with\ the}}
\DoxyCodeLine{00016\ \textcolor{comment}{\ *\ \ \ \ distribution.}}
\DoxyCodeLine{00017\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00018\ \textcolor{comment}{\ *\ This\ work\ was\ supported\ in\ part\ by\ funding\ from\ the\ Defense\ Advanced\ }}
\DoxyCodeLine{00019\ \textcolor{comment}{\ *\ Research\ Projects\ Agency\ and\ the\ National\ Science\ Foundation\ of\ the\ }}
\DoxyCodeLine{00020\ \textcolor{comment}{\ *\ United\ States\ of\ America,\ and\ the\ CMU\ Sphinx\ Speech\ Consortium.}}
\DoxyCodeLine{00021\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00022\ \textcolor{comment}{\ *\ THIS\ SOFTWARE\ IS\ PROVIDED\ BY\ CARNEGIE\ MELLON\ UNIVERSITY\ \`{}\`{}AS\ IS''\ AND\ }}
\DoxyCodeLine{00023\ \textcolor{comment}{\ *\ ANY\ EXPRESSED\ OR\ IMPLIED\ WARRANTIES,\ INCLUDING,\ BUT\ NOT\ LIMITED\ TO,\ }}
\DoxyCodeLine{00024\ \textcolor{comment}{\ *\ THE\ IMPLIED\ WARRANTIES\ OF\ MERCHANTABILITY\ AND\ FITNESS\ FOR\ A\ PARTICULAR}}
\DoxyCodeLine{00025\ \textcolor{comment}{\ *\ PURPOSE\ ARE\ DISCLAIMED.\ \ IN\ NO\ EVENT\ SHALL\ CARNEGIE\ MELLON\ UNIVERSITY}}
\DoxyCodeLine{00026\ \textcolor{comment}{\ *\ NOR\ ITS\ EMPLOYEES\ BE\ LIABLE\ FOR\ ANY\ DIRECT,\ INDIRECT,\ INCIDENTAL,}}
\DoxyCodeLine{00027\ \textcolor{comment}{\ *\ SPECIAL,\ EXEMPLARY,\ OR\ CONSEQUENTIAL\ DAMAGES\ (INCLUDING,\ BUT\ NOT\ }}
\DoxyCodeLine{00028\ \textcolor{comment}{\ *\ LIMITED\ TO,\ PROCUREMENT\ OF\ SUBSTITUTE\ GOODS\ OR\ SERVICES;\ LOSS\ OF\ USE,\ }}
\DoxyCodeLine{00029\ \textcolor{comment}{\ *\ DATA,\ OR\ PROFITS;\ OR\ BUSINESS\ INTERRUPTION)\ HOWEVER\ CAUSED\ AND\ ON\ ANY\ }}
\DoxyCodeLine{00030\ \textcolor{comment}{\ *\ THEORY\ OF\ LIABILITY,\ WHETHER\ IN\ CONTRACT,\ STRICT\ LIABILITY,\ OR\ TORT\ }}
\DoxyCodeLine{00031\ \textcolor{comment}{\ *\ (INCLUDING\ NEGLIGENCE\ OR\ OTHERWISE)\ ARISING\ IN\ ANY\ WAY\ OUT\ OF\ THE\ USE\ }}
\DoxyCodeLine{00032\ \textcolor{comment}{\ *\ OF\ THIS\ SOFTWARE,\ EVEN\ IF\ ADVISED\ OF\ THE\ POSSIBILITY\ OF\ SUCH\ DAMAGE.}}
\DoxyCodeLine{00033\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00034\ \textcolor{comment}{\ *\ ====================================================================}}
\DoxyCodeLine{00035\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00036\ \textcolor{comment}{\ */}}
\DoxyCodeLine{00037\ \textcolor{comment}{/*********************************************************************}}
\DoxyCodeLine{00038\ \textcolor{comment}{\ *}}
\DoxyCodeLine{00039\ \textcolor{comment}{\ *\ File:\ mmio.c}}
\DoxyCodeLine{00040\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00041\ \textcolor{comment}{\ *\ Description:\ mmap()\ wrappers\ for\ Unix/Windows}}
\DoxyCodeLine{00042\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00043\ \textcolor{comment}{\ *\ Author:\ David\ Huggins-\/Daines\ <dhuggins@cs.cmu.edu>}}
\DoxyCodeLine{00044\ \textcolor{comment}{\ *\ }}
\DoxyCodeLine{00045\ \textcolor{comment}{\ *********************************************************************/}}
\DoxyCodeLine{00046\ }
\DoxyCodeLine{00047\ \textcolor{preprocessor}{\#include\ <string.h>}}
\DoxyCodeLine{00048\ \textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{00049\ }
\DoxyCodeLine{00050\ \textcolor{preprocessor}{\#ifdef\ GNUWINCE}}
\DoxyCodeLine{00051\ \textcolor{preprocessor}{\#\ include\ <sys/wcebase.h>}}
\DoxyCodeLine{00052\ \textcolor{preprocessor}{\#\ include\ <sys/wcetypes.h>}}
\DoxyCodeLine{00053\ \textcolor{preprocessor}{\#\ include\ <sys/wcememory.h>}}
\DoxyCodeLine{00054\ \textcolor{preprocessor}{\#\ include\ <sys/wcefile.h>}}
\DoxyCodeLine{00055\ \textcolor{preprocessor}{\#elif\ defined(\_\_SYMBIAN32\_\_)\ }\textcolor{comment}{/*\ SYMBIAN32\ must\ be\ before\ WIN32\ since\ Symbian\ SDK\ defines\ WIN32\ as\ well\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00056\ \textcolor{preprocessor}{\#\ include\ <unistd.h>}}
\DoxyCodeLine{00057\ \textcolor{preprocessor}{\#\ include\ <fcntl.h>}}
\DoxyCodeLine{00058\ \textcolor{preprocessor}{\#\ include\ <sys/stat.h>}}
\DoxyCodeLine{00059\ \textcolor{preprocessor}{\#\ include\ <sys/mman.h>}}
\DoxyCodeLine{00060\ \textcolor{preprocessor}{\#elif\ defined(\_WIN32)}}
\DoxyCodeLine{00061\ \textcolor{preprocessor}{\#\ include\ <windows.h>}}
\DoxyCodeLine{00062\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00063\ \textcolor{preprocessor}{\#\ include\ <unistd.h>}}
\DoxyCodeLine{00064\ \textcolor{preprocessor}{\#\ include\ <fcntl.h>}}
\DoxyCodeLine{00065\ \textcolor{preprocessor}{\#\ include\ <sys/stat.h>}}
\DoxyCodeLine{00066\ \textcolor{preprocessor}{\#\ include\ <sys/file.h>}}
\DoxyCodeLine{00067\ \textcolor{preprocessor}{\#\ include\ <sys/mman.h>}}
\DoxyCodeLine{00068\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00069\ }
\DoxyCodeLine{00070\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/prim\_type.h"{}}}
\DoxyCodeLine{00071\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/err.h"{}}}
\DoxyCodeLine{00072\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/mmio.h"{}}}
\DoxyCodeLine{00073\ \textcolor{preprocessor}{\#include\ "{}sphinxbase/ckd\_alloc.h"{}}}
\DoxyCodeLine{00074\ }
\DoxyCodeLine{00076\ \textcolor{preprocessor}{\#if\ defined(\_WIN32\_WCE)\ ||\ defined(GNUWINCE)}}
\DoxyCodeLine{00077\ \textcolor{keyword}{struct\ }mmio\_file\_s\ \{}
\DoxyCodeLine{00078\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ dummy;}
\DoxyCodeLine{00079\ \};}
\DoxyCodeLine{00080\ }
\DoxyCodeLine{00081\ mmio\_file\_t\ *}
\DoxyCodeLine{00082\ mmio\_file\_read(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename)}
\DoxyCodeLine{00083\ \{}
\DoxyCodeLine{00084\ \ \ \ \ HANDLE\ ffm,\ fd;}
\DoxyCodeLine{00085\ \ \ \ \ WCHAR\ *wfilename;}
\DoxyCodeLine{00086\ \ \ \ \ \textcolor{keywordtype}{void}\ *rv;}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordtype}{int}\ len;}
\DoxyCodeLine{00088\ }
\DoxyCodeLine{00089\ \ \ \ \ len\ =\ mbstowcs(NULL,\ filename,\ 0)\ +\ 1;}
\DoxyCodeLine{00090\ \ \ \ \ wfilename\ =\ malloc(len\ *\ \textcolor{keyword}{sizeof}(WCHAR));}
\DoxyCodeLine{00091\ \ \ \ \ mbstowcs(wfilename,\ filename,\ len);}
\DoxyCodeLine{00092\ }
\DoxyCodeLine{00093\ \ \ \ \ \textcolor{keywordflow}{if}\ ((ffm\ =}
\DoxyCodeLine{00094\ \ \ \ \ \ \ \ \ \ CreateFileForMappingW(wfilename,\ GENERIC\_READ,\ FILE\_SHARE\_READ,}
\DoxyCodeLine{00095\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL,\ OPEN\_EXISTING,\ FILE\_ATTRIBUTE\_NORMAL,}
\DoxyCodeLine{00096\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL))\ ==\ INVALID\_HANDLE\_VALUE)\ \{}
\DoxyCodeLine{00097\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ create\ mapping\ for\ the\ file\ '\%s':\ \%08x\(\backslash\)n"{}},\ filename,}
\DoxyCodeLine{00098\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ GetLastError());}
\DoxyCodeLine{00099\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00100\ \ \ \ \ \}}
\DoxyCodeLine{00101\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fd\ =}
\DoxyCodeLine{00102\ \ \ \ \ \ \ \ \ \ CreateFileMappingW(ffm,\ NULL,\ PAGE\_READONLY,\ 0,\ 0,\ NULL))\ ==\ NULL)\ \{}
\DoxyCodeLine{00103\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ CreateFileMapping:\ \%08x\(\backslash\)n"{}},\ GetLastError());}
\DoxyCodeLine{00104\ \ \ \ \ \ \ \ \ CloseHandle(ffm);}
\DoxyCodeLine{00105\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00106\ \ \ \ \ \}}
\DoxyCodeLine{00107\ \ \ \ \ rv\ =\ MapViewOfFile(fd,\ FILE\_MAP\_READ,\ 0,\ 0,\ 0);}
\DoxyCodeLine{00108\ \ \ \ \ free(wfilename);}
\DoxyCodeLine{00109\ \ \ \ \ CloseHandle(ffm);}
\DoxyCodeLine{00110\ \ \ \ \ CloseHandle(fd);}
\DoxyCodeLine{00111\ }
\DoxyCodeLine{00112\ \ \ \ \ \textcolor{keywordflow}{return}\ (mmio\_file\_t\ *)\ rv;}
\DoxyCodeLine{00113\ \}}
\DoxyCodeLine{00114\ }
\DoxyCodeLine{00115\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00116\ mmio\_file\_unmap(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00117\ \{}
\DoxyCodeLine{00118\ \ \ \ \ \textcolor{keywordflow}{if}\ (!UnmapViewOfFile((\textcolor{keywordtype}{void}\ *)mf))\ \{}
\DoxyCodeLine{00119\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ UnmapViewOfFile:\ \%08x\(\backslash\)n"{}},\ GetLastError());}
\DoxyCodeLine{00120\ \ \ \ \ \}}
\DoxyCodeLine{00121\ \}}
\DoxyCodeLine{00122\ }
\DoxyCodeLine{00123\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00124\ mmio\_file\_ptr(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00125\ \{}
\DoxyCodeLine{00126\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{void}\ *)mf;}
\DoxyCodeLine{00127\ \}}
\DoxyCodeLine{00128\ }
\DoxyCodeLine{00129\ \textcolor{preprocessor}{\#elif\ defined(WIN32)\ }\textcolor{comment}{/*\ !WINCE\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00130\ \textcolor{keyword}{struct\ }mmio\_file\_s\ \{}
\DoxyCodeLine{00131\ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{int}\ dummy;}
\DoxyCodeLine{00132\ \};}
\DoxyCodeLine{00133\ }
\DoxyCodeLine{00134\ mmio\_file\_t\ *}
\DoxyCodeLine{00135\ mmio\_file\_read(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename)}
\DoxyCodeLine{00136\ \{}
\DoxyCodeLine{00137\ \ \ \ \ HANDLE\ ffm,\ fd;}
\DoxyCodeLine{00138\ \ \ \ \ \textcolor{keywordtype}{void}\ *rv;}
\DoxyCodeLine{00139\ }
\DoxyCodeLine{00140\ \ \ \ \ \textcolor{keywordflow}{if}\ ((ffm\ =\ CreateFile(filename,\ GENERIC\_READ,\ FILE\_SHARE\_READ,}
\DoxyCodeLine{00141\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL,\ OPEN\_EXISTING,\ FILE\_ATTRIBUTE\_NORMAL,}
\DoxyCodeLine{00142\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ NULL))\ ==\ INVALID\_HANDLE\_VALUE)\ \{}
\DoxyCodeLine{00143\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ create\ file\ '\%s':\ \%08x\(\backslash\)n"{}},}
\DoxyCodeLine{00144\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ filename,\ GetLastError());}
\DoxyCodeLine{00145\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00146\ \ \ \ \ \}}
\DoxyCodeLine{00147\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fd\ =\ CreateFileMapping(ffm,\ NULL,}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PAGE\_READONLY,\ 0,\ 0,\ NULL))\ ==\ NULL)\ \{}
\DoxyCodeLine{00149\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ CreateFileMapping:\ \%08x\(\backslash\)n"{}},\ GetLastError());}
\DoxyCodeLine{00150\ \ \ \ \ \ \ \ \ CloseHandle(ffm);}
\DoxyCodeLine{00151\ \ \ \ \ \}}
\DoxyCodeLine{00152\ \ \ \ \ rv\ =\ MapViewOfFile(fd,\ FILE\_MAP\_READ,\ 0,\ 0,\ 0);}
\DoxyCodeLine{00153\ \ \ \ \ CloseHandle(ffm);}
\DoxyCodeLine{00154\ \ \ \ \ CloseHandle(fd);}
\DoxyCodeLine{00155\ }
\DoxyCodeLine{00156\ \ \ \ \ \textcolor{keywordflow}{return}\ (mmio\_file\_t\ *)rv;}
\DoxyCodeLine{00157\ \}}
\DoxyCodeLine{00158\ }
\DoxyCodeLine{00159\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00160\ mmio\_file\_unmap(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00161\ \{}
\DoxyCodeLine{00162\ \ \ \ \ \textcolor{keywordflow}{if}\ (!UnmapViewOfFile((\textcolor{keywordtype}{void}\ *)mf))\ \{}
\DoxyCodeLine{00163\ \ \ \ \ \ \ \ \ E\_ERROR(\textcolor{stringliteral}{"{}Failed\ to\ UnmapViewOfFile:\ \%08x\(\backslash\)n"{}},\ GetLastError());}
\DoxyCodeLine{00164\ \ \ \ \ \}}
\DoxyCodeLine{00165\ \}}
\DoxyCodeLine{00166\ }
\DoxyCodeLine{00167\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00168\ mmio\_file\_ptr(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00169\ \{}
\DoxyCodeLine{00170\ \ \ \ \ \textcolor{keywordflow}{return}\ (\textcolor{keywordtype}{void}\ *)mf;}
\DoxyCodeLine{00171\ \}}
\DoxyCodeLine{00172\ }
\DoxyCodeLine{00173\ \textcolor{preprocessor}{\#else\ }\textcolor{comment}{/*\ !WIN32,\ !WINCE\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00174\ \textcolor{preprocessor}{\#if\ defined(\_\_ADSPBLACKFIN\_\_)\ }\textcolor{comment}{/*\ This\ is\ true\ for\ both\ uClinux\ and\ VisualDSP++,}}
\DoxyCodeLine{00175\ \textcolor{comment}{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ but\ actually\ we\ need\ a\ better\ way\ to\ detect\ it.\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00176\ \textcolor{keyword}{struct\ }mmio\_file\_s\ \{}
\DoxyCodeLine{00177\ \ \ \ \ \textcolor{keywordtype}{int}\ dummy;}
\DoxyCodeLine{00178\ \};}
\DoxyCodeLine{00179\ }
\DoxyCodeLine{00180\ mmio\_file\_t\ *}
\DoxyCodeLine{00181\ mmio\_file\_read(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename)}
\DoxyCodeLine{00182\ \{}
\DoxyCodeLine{00183\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}mmio\ is\ not\ implemented\ on\ this\ platform!"{}});}
\DoxyCodeLine{00184\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00185\ \}}
\DoxyCodeLine{00186\ }
\DoxyCodeLine{00187\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00188\ mmio\_file\_unmap(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00189\ \{}
\DoxyCodeLine{00190\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}mmio\ is\ not\ implemented\ on\ this\ platform!"{}});}
\DoxyCodeLine{00191\ \}}
\DoxyCodeLine{00192\ }
\DoxyCodeLine{00193\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00194\ mmio\_file\_ptr(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00195\ \{}
\DoxyCodeLine{00196\ \ \ \ \ \ \ \ \ E\_FATAL(\textcolor{stringliteral}{"{}mmio\ is\ not\ implemented\ on\ this\ platform!"{}});}
\DoxyCodeLine{00197\ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00198\ \}}
\DoxyCodeLine{00199\ \textcolor{preprocessor}{\#else\ }\textcolor{comment}{/*\ !\_\_ADSPBLACKFIN\_\_\ */}\textcolor{preprocessor}{}}
\DoxyCodeLine{00200\ \textcolor{keyword}{struct\ }mmio\_file\_s\ \{}
\DoxyCodeLine{00201\ \ \ \ \ \textcolor{keywordtype}{void}\ *ptr;}
\DoxyCodeLine{00202\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ mapsize;}
\DoxyCodeLine{00203\ \};}
\DoxyCodeLine{00204\ }
\DoxyCodeLine{00205\ mmio\_file\_t\ *}
\DoxyCodeLine{00206\ mmio\_file\_read(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *filename)}
\DoxyCodeLine{00207\ \{}
\DoxyCodeLine{00208\ \ \ \ \ mmio\_file\_t\ *mf;}
\DoxyCodeLine{00209\ \ \ \ \ \textcolor{keyword}{struct\ }stat\ buf;}
\DoxyCodeLine{00210\ \ \ \ \ \textcolor{keywordtype}{void}\ *ptr;}
\DoxyCodeLine{00211\ \ \ \ \ \textcolor{keywordtype}{int}\ fd;}
\DoxyCodeLine{00212\ \ \ \ \ \textcolor{keywordtype}{size\_t}\ pagesize;}
\DoxyCodeLine{00213\ }
\DoxyCodeLine{00214\ \ \ \ \ \textcolor{keywordflow}{if}\ ((fd\ =\ open(filename,\ O\_RDONLY))\ ==\ -\/1)\ \{}
\DoxyCodeLine{00215\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ open\ \%s"{}},\ filename);}
\DoxyCodeLine{00216\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00217\ \ \ \ \ \}}
\DoxyCodeLine{00218\ \ \ \ \ \textcolor{keywordflow}{if}\ (fstat(fd,\ \&buf)\ ==\ -\/1)\ \{}
\DoxyCodeLine{00219\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ stat\ \%s"{}},\ filename);}
\DoxyCodeLine{00220\ \ \ \ \ \ \ \ \ close(fd);}
\DoxyCodeLine{00221\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00222\ \ \ \ \ \}}
\DoxyCodeLine{00223\ \ \ \ \ ptr\ =\ mmap(NULL,\ buf.st\_size,\ PROT\_READ,\ MAP\_SHARED,\ fd,\ 0);}
\DoxyCodeLine{00224\ \ \ \ \ \textcolor{keywordflow}{if}\ (ptr\ ==\ (\textcolor{keywordtype}{void}\ *)-\/1)\ \{}
\DoxyCodeLine{00225\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ mmap\ \%lld\ bytes"{}},\ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long}\ \textcolor{keywordtype}{long})buf.st\_size);}
\DoxyCodeLine{00226\ \ \ \ \ \ \ \ \ close(fd);}
\DoxyCodeLine{00227\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ NULL;}
\DoxyCodeLine{00228\ \ \ \ \ \}}
\DoxyCodeLine{00229\ \ \ \ \ close(fd);}
\DoxyCodeLine{00230\ \ \ \ \ mf\ =\ ckd\_calloc(1,\ \textcolor{keyword}{sizeof}(*mf));}
\DoxyCodeLine{00231\ \ \ \ \ mf-\/>ptr\ =\ ptr;}
\DoxyCodeLine{00232\ \ \ \ \ \textcolor{comment}{/*\ Align\ map\ size\ to\ next\ page.\ */}}
\DoxyCodeLine{00233\ \ \ \ \ pagesize\ =\ getpagesize();}
\DoxyCodeLine{00234\ \ \ \ \ mf-\/>mapsize\ =\ (buf.st\_size\ +\ pagesize\ -\/\ 1)\ /\ pagesize\ *\ pagesize;}
\DoxyCodeLine{00235\ }
\DoxyCodeLine{00236\ \ \ \ \ \textcolor{keywordflow}{return}\ mf;}
\DoxyCodeLine{00237\ \}}
\DoxyCodeLine{00238\ }
\DoxyCodeLine{00239\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{00240\ mmio\_file\_unmap(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00241\ \{}
\DoxyCodeLine{00242\ \ \ \ \ \textcolor{keywordflow}{if}\ (mf\ ==\ NULL)}
\DoxyCodeLine{00243\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return};}
\DoxyCodeLine{00244\ \ \ \ \ \textcolor{keywordflow}{if}\ (munmap(mf-\/>ptr,\ mf-\/>mapsize)\ <\ 0)\ \{}
\DoxyCodeLine{00245\ \ \ \ \ \ \ \ \ E\_ERROR\_SYSTEM(\textcolor{stringliteral}{"{}Failed\ to\ unmap\ \%ld\ bytes\ at\ \%p"{}},\ mf-\/>mapsize,\ mf-\/>ptr);}
\DoxyCodeLine{00246\ \ \ \ \ \}}
\DoxyCodeLine{00247\ \ \ \ \ ckd\_free(mf);}
\DoxyCodeLine{00248\ \}}
\DoxyCodeLine{00249\ }
\DoxyCodeLine{00250\ \textcolor{keywordtype}{void}\ *}
\DoxyCodeLine{00251\ mmio\_file\_ptr(mmio\_file\_t\ *mf)}
\DoxyCodeLine{00252\ \{}
\DoxyCodeLine{00253\ \ \ \ \ \textcolor{keywordflow}{return}\ mf-\/>ptr;}
\DoxyCodeLine{00254\ \}}
\DoxyCodeLine{00255\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{/*\ !\_\_ADSPBLACKFIN\_\_\ */}\textcolor{preprocessor}{\ }}
\DoxyCodeLine{00256\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{/*\ !(WINCE\ ||\ WIN32)\ */}\textcolor{preprocessor}{}}

\end{DoxyCode}
