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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2013 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include <unistd.h>
michael@0 9 #include <sys/mman.h>
michael@0 10 #include "SkDiscardableMemory.h"
michael@0 11 #include "SkTypes.h"
michael@0 12 #include "android/ashmem.h"
michael@0 13
michael@0 14 ////////////////////////////////////////////////////////////////////////////////
michael@0 15 namespace {
michael@0 16 /**
michael@0 17 * DiscardableMemory implementation that uses the Android kernel's
michael@0 18 * ashmem (Android shared memory).
michael@0 19 */
michael@0 20 class SkAshmemDiscardableMemory : public SkDiscardableMemory {
michael@0 21 public:
michael@0 22 SkAshmemDiscardableMemory(int fd, void* address, size_t size);
michael@0 23 virtual ~SkAshmemDiscardableMemory();
michael@0 24 virtual bool lock() SK_OVERRIDE;
michael@0 25 virtual void* data() SK_OVERRIDE;
michael@0 26 virtual void unlock() SK_OVERRIDE;
michael@0 27 private:
michael@0 28 bool fLocked;
michael@0 29 int fFd;
michael@0 30 void* fMemory;
michael@0 31 const size_t fSize;
michael@0 32 };
michael@0 33
michael@0 34 SkAshmemDiscardableMemory::SkAshmemDiscardableMemory(int fd,
michael@0 35 void* address,
michael@0 36 size_t size)
michael@0 37 : fLocked(true) // Ashmem pages are pinned by default.
michael@0 38 , fFd(fd)
michael@0 39 , fMemory(address)
michael@0 40 , fSize(size) {
michael@0 41 SkASSERT(fFd >= 0);
michael@0 42 SkASSERT(fMemory != NULL);
michael@0 43 SkASSERT(fSize > 0);
michael@0 44 }
michael@0 45
michael@0 46 SkAshmemDiscardableMemory::~SkAshmemDiscardableMemory() {
michael@0 47 SkASSERT(!fLocked);
michael@0 48 if (NULL != fMemory) {
michael@0 49 munmap(fMemory, fSize);
michael@0 50 }
michael@0 51 if (fFd != -1) {
michael@0 52 close(fFd);
michael@0 53 }
michael@0 54 }
michael@0 55
michael@0 56 bool SkAshmemDiscardableMemory::lock() {
michael@0 57 SkASSERT(!fLocked);
michael@0 58 if (-1 == fFd) {
michael@0 59 fLocked = false;
michael@0 60 return false;
michael@0 61 }
michael@0 62 SkASSERT(fMemory != NULL);
michael@0 63 if (fLocked || (ASHMEM_NOT_PURGED == ashmem_pin_region(fFd, 0, 0))) {
michael@0 64 fLocked = true;
michael@0 65 return true;
michael@0 66 } else {
michael@0 67 munmap(fMemory, fSize);
michael@0 68 fMemory = NULL;
michael@0 69
michael@0 70 close(fFd);
michael@0 71 fFd = -1;
michael@0 72 fLocked = false;
michael@0 73 return false;
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 void* SkAshmemDiscardableMemory::data() {
michael@0 78 SkASSERT(fLocked);
michael@0 79 return fLocked ? fMemory : NULL;
michael@0 80 }
michael@0 81
michael@0 82 void SkAshmemDiscardableMemory::unlock() {
michael@0 83 SkASSERT(fLocked);
michael@0 84 if (fLocked && (fFd != -1)) {
michael@0 85 ashmem_unpin_region(fFd, 0, 0);
michael@0 86 }
michael@0 87 fLocked = false;
michael@0 88 }
michael@0 89 } // namespace
michael@0 90 ////////////////////////////////////////////////////////////////////////////////
michael@0 91
michael@0 92 SkDiscardableMemory* SkDiscardableMemory::Create(size_t bytes) {
michael@0 93 // ashmem likes lengths on page boundaries.
michael@0 94 const size_t mask = getpagesize() - 1;
michael@0 95 size_t size = (bytes + mask) & ~mask;
michael@0 96
michael@0 97 static const char name[] = "Skia_Ashmem_Discardable_Memory";
michael@0 98 int fd = ashmem_create_region(name, size);
michael@0 99 if (fd < 0) {
michael@0 100 return NULL;
michael@0 101 }
michael@0 102 if (0 != ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE)) {
michael@0 103 close(fd);
michael@0 104 return NULL;
michael@0 105 }
michael@0 106 void* addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
michael@0 107 if ((MAP_FAILED == addr) || (NULL == addr)) {
michael@0 108 close(fd);
michael@0 109 return NULL;
michael@0 110 }
michael@0 111
michael@0 112 return SkNEW_ARGS(SkAshmemDiscardableMemory, (fd, addr, size));
michael@0 113 }

mercurial