gfx/skia/trunk/src/ports/SkOSFile_posix.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/ports/SkOSFile_posix.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkOSFile.h"
    1.12 +
    1.13 +#include "SkTFitsIn.h"
    1.14 +
    1.15 +#include <stdio.h>
    1.16 +#include <sys/mman.h>
    1.17 +#include <sys/stat.h>
    1.18 +#include <sys/types.h>
    1.19 +
    1.20 +typedef struct {
    1.21 +    dev_t dev;
    1.22 +    ino_t ino;
    1.23 +} SkFILEID;
    1.24 +
    1.25 +static bool sk_ino(SkFILE* a, SkFILEID* id) {
    1.26 +    int fd = fileno((FILE*)a);
    1.27 +    if (fd < 0) {
    1.28 +        return 0;
    1.29 +    }
    1.30 +    struct stat status;
    1.31 +    if (0 != fstat(fd, &status)) {
    1.32 +        return 0;
    1.33 +    }
    1.34 +    id->dev = status.st_dev;
    1.35 +    id->ino = status.st_ino;
    1.36 +    return true;
    1.37 +}
    1.38 +
    1.39 +bool sk_fidentical(SkFILE* a, SkFILE* b) {
    1.40 +    SkFILEID aID, bID;
    1.41 +    return sk_ino(a, &aID) && sk_ino(b, &bID)
    1.42 +           && aID.ino == bID.ino
    1.43 +           && aID.dev == bID.dev;
    1.44 +}
    1.45 +
    1.46 +void sk_fmunmap(const void* addr, size_t length) {
    1.47 +    munmap(const_cast<void*>(addr), length);
    1.48 +}
    1.49 +
    1.50 +void* sk_fdmmap(int fd, size_t* size) {
    1.51 +    struct stat status;
    1.52 +    if (0 != fstat(fd, &status)) {
    1.53 +        return NULL;
    1.54 +    }
    1.55 +    if (!S_ISREG(status.st_mode)) {
    1.56 +        return NULL;
    1.57 +    }
    1.58 +    if (!SkTFitsIn<size_t>(status.st_size)) {
    1.59 +        return NULL;
    1.60 +    }
    1.61 +    size_t fileSize = static_cast<size_t>(status.st_size);
    1.62 +
    1.63 +    void* addr = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
    1.64 +    if (MAP_FAILED == addr) {
    1.65 +        return NULL;
    1.66 +    }
    1.67 +
    1.68 +    *size = fileSize;
    1.69 +    return addr;
    1.70 +}
    1.71 +
    1.72 +int sk_fileno(SkFILE* f) {
    1.73 +    return fileno((FILE*)f);
    1.74 +}
    1.75 +
    1.76 +void* sk_fmmap(SkFILE* f, size_t* size) {
    1.77 +    int fd = sk_fileno(f);
    1.78 +    if (fd < 0) {
    1.79 +        return NULL;
    1.80 +    }
    1.81 +
    1.82 +    return sk_fdmmap(fd, size);
    1.83 +}

mercurial