gfx/2d/UserData.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef MOZILLA_GFX_USERDATA_H_
michael@0 7 #define MOZILLA_GFX_USERDATA_H_
michael@0 8
michael@0 9 #include <stdlib.h>
michael@0 10 #include "Types.h"
michael@0 11 #include "mozilla/Assertions.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace gfx {
michael@0 15
michael@0 16 struct UserDataKey {
michael@0 17 int unused;
michael@0 18 };
michael@0 19
michael@0 20 /* this class is basically a clone of the user data concept from cairo */
michael@0 21 class UserData
michael@0 22 {
michael@0 23 typedef void (*destroyFunc)(void *data);
michael@0 24 public:
michael@0 25 UserData() : count(0), entries(nullptr) {}
michael@0 26
michael@0 27 /* Attaches untyped userData associated with key. destroy is called on destruction */
michael@0 28 void Add(UserDataKey *key, void *userData, destroyFunc destroy)
michael@0 29 {
michael@0 30 for (int i=0; i<count; i++) {
michael@0 31 if (key == entries[i].key) {
michael@0 32 if (entries[i].destroy) {
michael@0 33 entries[i].destroy(entries[i].userData);
michael@0 34 }
michael@0 35 entries[i].userData = userData;
michael@0 36 entries[i].destroy = destroy;
michael@0 37 return;
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 // We could keep entries in a std::vector instead of managing it by hand
michael@0 42 // but that would propagate an stl dependency out which we'd rather not
michael@0 43 // do (see bug 666609). Plus, the entries array is expect to stay small
michael@0 44 // so doing a realloc everytime we add a new entry shouldn't be too costly
michael@0 45 entries = static_cast<Entry*>(realloc(entries, sizeof(Entry)*(count+1)));
michael@0 46
michael@0 47 if (!entries) {
michael@0 48 MOZ_CRASH();
michael@0 49 }
michael@0 50
michael@0 51 entries[count].key = key;
michael@0 52 entries[count].userData = userData;
michael@0 53 entries[count].destroy = destroy;
michael@0 54
michael@0 55 count++;
michael@0 56 }
michael@0 57
michael@0 58 /* Remove and return user data associated with key, without destroying it */
michael@0 59 void* Remove(UserDataKey *key)
michael@0 60 {
michael@0 61 for (int i=0; i<count; i++) {
michael@0 62 if (key == entries[i].key) {
michael@0 63 void *userData = entries[i].userData;
michael@0 64 // decrement before looping so entries[i+1] doesn't read past the end:
michael@0 65 --count;
michael@0 66 for (;i<count; i++) {
michael@0 67 entries[i] = entries[i+1];
michael@0 68 }
michael@0 69 return userData;
michael@0 70 }
michael@0 71 }
michael@0 72 return nullptr;
michael@0 73 }
michael@0 74
michael@0 75 /* Retrives the userData for the associated key */
michael@0 76 void *Get(UserDataKey *key) const
michael@0 77 {
michael@0 78 for (int i=0; i<count; i++) {
michael@0 79 if (key == entries[i].key) {
michael@0 80 return entries[i].userData;
michael@0 81 }
michael@0 82 }
michael@0 83 return nullptr;
michael@0 84 }
michael@0 85
michael@0 86 bool Has(UserDataKey *key)
michael@0 87 {
michael@0 88 for (int i=0; i<count; i++) {
michael@0 89 if (key == entries[i].key) {
michael@0 90 return true;
michael@0 91 }
michael@0 92 }
michael@0 93 return false;
michael@0 94 }
michael@0 95
michael@0 96 void Destroy()
michael@0 97 {
michael@0 98 for (int i=0; i<count; i++) {
michael@0 99 if (entries[i].destroy) {
michael@0 100 entries[i].destroy(entries[i].userData);
michael@0 101 }
michael@0 102 }
michael@0 103 free(entries);
michael@0 104 entries = nullptr;
michael@0 105 count = 0;
michael@0 106 }
michael@0 107
michael@0 108 ~UserData()
michael@0 109 {
michael@0 110 Destroy();
michael@0 111 }
michael@0 112
michael@0 113 private:
michael@0 114 struct Entry {
michael@0 115 const UserDataKey *key;
michael@0 116 void *userData;
michael@0 117 destroyFunc destroy;
michael@0 118 };
michael@0 119
michael@0 120 int count;
michael@0 121 Entry *entries;
michael@0 122
michael@0 123 };
michael@0 124
michael@0 125 }
michael@0 126 }
michael@0 127
michael@0 128 #endif /* MOZILLA_GFX_USERDATA_H_ */

mercurial