blob: c9da4db6b8aa0e2a4c8001f312e500571edd2aa3 [file] [log] [blame]
bungeman@google.com6cab1a42013-05-29 13:43:31 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkOSFile.h"
9
10#include <stdio.h>
11#include <sys/mman.h>
12#include <sys/stat.h>
13#include <sys/types.h>
14
15typedef struct {
16 dev_t dev;
17 ino_t ino;
18} SkFILEID;
19
20static bool sk_ino(SkFILE* a, SkFILEID* id) {
21 int fd = fileno((FILE*)a);
22 if (fd < 0) {
23 return 0;
24 }
25 struct stat status;
26 if (0 != fstat(fd, &status)) {
27 return 0;
28 }
29 id->dev = status.st_dev;
30 id->ino = status.st_ino;
31 return true;
32}
33
34bool sk_fidentical(SkFILE* a, SkFILE* b) {
35 SkFILEID aID, bID;
36 return sk_ino(a, &aID) && sk_ino(b, &bID)
37 && aID.ino == bID.ino
38 && aID.dev == bID.dev;
39}
40
41void sk_fmunmap(const void* addr, size_t length) {
42 munmap(const_cast<void*>(addr), length);
43}
44
45void* sk_fmmap(SkFILE* f, size_t* size) {
46 size_t fileSize = sk_fgetsize(f);
47 if (0 == fileSize) {
48 return NULL;
49 }
50
51 int fd = fileno((FILE*)f);
52 if (fd < 0) {
53 return NULL;
54 }
55
56 void* addr = mmap(NULL, fileSize, PROT_READ, MAP_PRIVATE, fd, 0);
57 if (MAP_FAILED == addr) {
58 return NULL;
59 }
60
61 *size = fileSize;
62 return addr;
63}