Changeset 2856 for trunk/kLdr/kLdrRdr.c


Ignore:
Timestamp:
Nov 4, 2006, 11:19:33 PM (19 years ago)
Author:
bird
Message:

More code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/kLdrRdr.c

    r2826 r2856  
    3131#include <kLdr.h>
    3232#include "kLdrInternal.h"
     33
     34
     35/*******************************************************************************
     36*   Defined Constants And Macros                                               *
     37*******************************************************************************/
     38/** @def KLDRRDR_STRICT
     39 * Define KLDRRDR_STRICT to enabled strict checks in KLDRMOD. */
     40#define KLDRRDR_STRICT 1
     41
     42/** @def KLDRRDR_ASSERT
     43 * Assert that an expression is true when KLDR_STRICT is defined.
     44 */
     45#ifdef KLDRRDR_STRICT
     46# define KLDRRDR_ASSERT(expr)  kldrHlpAssert(expr)
     47#else
     48# define KLDRRDR_ASSERT(expr)  do {} while (0)
     49#endif
     50
     51/** Return / crash validation of a reader argument. */
     52#define KLDRRDR_VALIDATE_EX(pRdr, rc) \
     53    do  { \
     54        if (    (pRdr)->u32Magic != KLDRRDR_MAGIC \
     55            ||  (pRdr)->pOps == NULL \
     56           )\
     57        { \
     58            return (rc); \
     59        } \
     60    } while (0)
     61
     62/** Return / crash validation of a reader argument. */
     63#define KLDRRDR_VALIDATE(pRdr) \
     64    KLDRRDR_VALIDATE_EX(pRdr, KLDR_ERR_INVALID_PARAMETER)
     65
     66/** Return / crash validation of a reader argument. */
     67#define KLDRRDR_VALIDATE_VOID(pRdr) \
     68    do  { \
     69        if (    (pRdr)->u32Magic != KLDRRDR_MAGIC \
     70            ||  (pRdr)->pOps == NULL \
     71           )\
     72        { \
     73            return; \
     74        } \
     75    } while (0)
     76
    3377
    3478
     
    82126int kLdrRdrClose(PKLDRRDR pRdr)
    83127{
     128    KLDRRDR_VALIDATE(pRdr);
    84129    return pRdr->pOps->pfnDestroy(pRdr);
    85130}
     
    96141int kLdrRdrRead(PKLDRRDR pRdr, void *pvBuf, size_t cb, off_t off)
    97142{
     143    KLDRRDR_VALIDATE(pRdr);
    98144    return pRdr->pOps->pfnRead(pRdr, pvBuf, cb, off);
    99145}
     
    109155int kLdrRdrAllMap(PKLDRRDR pRdr, const void **ppvBits)
    110156{
     157    KLDRRDR_VALIDATE(pRdr);
    111158    return pRdr->pOps->pfnAllMap(pRdr, ppvBits);
    112159}
     
    121168int kLdrRdrAllUnmap(PKLDRRDR pRdr, const void *pvBits)
    122169{
     170    KLDRRDR_VALIDATE(pRdr);
    123171    return pRdr->pOps->pfnAllUnmap(pRdr, pvBits);
    124172}
     
    132180off_t kLdrRdrSize(PKLDRRDR pRdr)
    133181{
     182    KLDRRDR_VALIDATE(pRdr);
    134183    return pRdr->pOps->pfnSize(pRdr);
    135184}
     
    143192off_t kLdrRdrTell(PKLDRRDR pRdr)
    144193{
     194    KLDRRDR_VALIDATE(pRdr);
    145195    return pRdr->pOps->pfnTell(pRdr);
    146196}
     
    149199/** Get the file name.
    150200 *
    151  * @returns The file size. Returns -1 on failure.
     201 * @returns The file name. Returns NULL on failure.
    152202 * @param   pRdr        The file provider instance.
    153203 */
    154204const char *kLdrRdrName(PKLDRRDR pRdr)
    155205{
     206    KLDRRDR_VALIDATE_EX(pRdr, NULL);
    156207    return pRdr->pOps->pfnName(pRdr);
    157208}
    158209
    159210
     211/**
     212 * Gets the page size used when mapping sections of the file.
     213 *
     214 * @returns The page size.
     215 * @param   pRdr        The file provider instance.
     216 */
     217size_t  kLdrRdrPageSize(PKLDRRDR pRdr)
     218{
     219    KLDRRDR_VALIDATE_EX(pRdr, 0x10000);
     220    return pRdr->pOps->pfnPageSize(pRdr);
     221}
     222
     223
     224/**
     225 * Prepares a memory region to map file sections into.
     226 *
     227 * @returns 0 on success, OS specific error code on failure.
     228 * @param   pRdr        The file provider instance.
     229 * @param   ppv         If fFixed is set, *ppv contains the memory location which
     230 *                      the region should be based at. If fFixed is clear the OS
     231 *                      is free to choose the location.
     232 *                      On successful return *ppv contains address of the prepared
     233 *                      memory region.
     234 * @param   cb          The size of the memory region to prepare.
     235 * @param   fFixed      When set *ppv will contain the desired region address.
     236 *
     237 */
     238int kLdrRdrPrepare(PKLDRRDR pRdr, void **ppv, size_t cb, unsigned fFixed)
     239{
     240    KLDRRDR_VALIDATE(pRdr);
     241    return pRdr->pOps->pfnPrepare(pRdr, ppv, cb, fFixed);
     242}
     243
     244
     245/**
     246 * Maps a section of the file into the memory region reserved by pfnPrepare.
     247 *
     248 * @returns 0 on success, OS specific error code on failure.
     249 * @param   pRdr        The file provider instance.
     250 * @param   pv          The address in the prepared region.
     251 * @param   cb          The size of the memory mapping.
     252 * @param   enmProt     The desired memory protection.
     253 * @param   offFile     The start of the raw file bytes.
     254 * @param   cbFile      The number of raw file bytes. This must be less or equal to cb.
     255 */
     256int kLdrRdrMap(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt, off_t offFile, size_t cbFile)
     257{
     258    KLDRRDR_VALIDATE(pRdr);
     259    return pRdr->pOps->pfnMap(pRdr, pv, cb, enmProt, offFile, cbFile);
     260}
     261
     262
     263/**
     264 * Reloads dirty pages in mapped section.
     265 *
     266 * @returns 0 on success, OS specific error code on failure.
     267 * @param   pRdr        The file provider instance.
     268 * @param   pv          The address in the prepared region.
     269 * @param   cb          The size of the memory mapping.
     270 * @param   enmProt     The desired memory protection.
     271 * @param   offFile     The start of the raw file bytes.
     272 * @param   cbFile      The number of raw file bytes. This must be less or equal to cb.
     273 */
     274int kLdrRdrRefreshMap(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt, off_t offFile, size_t cbFile)
     275{
     276    KLDRRDR_VALIDATE(pRdr);
     277    return pRdr->pOps->pfnRefreshMap(pRdr, pv, cb, enmProt, offFile, cbFile);
     278}
     279
     280
     281/**
     282 * Changes the page protection of a section mapped using pfnMap.
     283 *
     284 * This is typically used for applying fixups and similar.
     285 *
     286 * @returns 0 on success, OS specific error code on failure.
     287 * @param   pRdr        The file provider instance.
     288 * @param   pv          The address passed to pfnMap.
     289 * @param   cb          The size passed to pfnMap.
     290 * @param   enmProt     The desired memory protection.
     291 */
     292int kLdrRdrProtect(PKLDRRDR pRdr, void *pv, size_t cb, KLDRPROT enmProt)
     293{
     294    KLDRRDR_VALIDATE(pRdr);
     295    return pRdr->pOps->pfnProtect(pRdr, pv, cb, enmProt);
     296}
     297
     298
     299/**
     300 * Unmaps a section of the file previously mapped using pfnMap.
     301 *
     302 * @returns 0 on success, OS specific error code on failure.
     303 * @param   pRdr        The file provider instance.
     304 * @param   pv          The address passed to pfnMap.
     305 * @param   cb          The size passed to pfnMap.
     306 */
     307int kLdrRdrUnmap(PKLDRRDR pRdr, void *pv, size_t cb)
     308{
     309    KLDRRDR_VALIDATE(pRdr);
     310    return pRdr->pOps->pfnUnmap(pRdr, pv, cb);
     311}
     312
     313
     314/**
     315 * Releases the memory region prepared by pfnPrepare().
     316 *
     317 * Before calling this function, all sections mapped by pfnMap must first be unmapped by calling pfnUnmap.
     318 *
     319 * @returns 0 on success, OS specific error code on failure.
     320 * @param   pRdr        The file provider instance.
     321 * @param   pv          The address of the prepared region.
     322 * @param   cb          The size of the prepared region.
     323 */
     324int kLdrRdrUnprepare(PKLDRRDR pRdr, void *pv, size_t cb)
     325{
     326    KLDRRDR_VALIDATE(pRdr);
     327    return pRdr->pOps->pfnUnprepare(pRdr, pv, cb);
     328}
     329
     330
     331/**
     332 * We're done reading from the file but would like to keep file mappings.
     333 *
     334 * If the OS support closing the file handle while the file is mapped,
     335 * the reader should do so.
     336 *
     337 * @param   pRdr        The file provider instance.
     338 */
     339void kLdrRdrDone(PKLDRRDR pRdr)
     340{
     341    KLDRRDR_VALIDATE_VOID(pRdr);
     342    pRdr->pOps->pfnDone(pRdr);
     343}
     344
Note: See TracChangeset for help on using the changeset viewer.