• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libsphinxbase/util/mmio.c

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

Generated on Mon Aug 29 2011 for SphinxBase by  doxygen 1.7.1