1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef MOZILLA_GFX_USERDATA_H_
#define MOZILLA_GFX_USERDATA_H_
#include <stdlib.h>
#include "Types.h"
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/Mutex.h"
namespace mozilla {
namespace gfx {
struct UserDataKey {
int unused;
};
/* this class is basically a clone of the user data concept from cairo */
class UserData {
public:
typedef void (*DestroyFunc)(void* data);
UserData() : count(0), entries(nullptr) {}
/* Attaches untyped userData associated with key. destroy is called on
* destruction */
void Add(UserDataKey* key, void* userData, DestroyFunc destroy) {
for (int i = 0; i < count; i++) {
if (key == entries[i].key) {
if (entries[i].destroy) {
entries[i].destroy(entries[i].userData);
}
entries[i].userData = userData;
entries[i].destroy = destroy;
return;
}
}
// We could keep entries in a std::vector instead of managing it by hand
// but that would propagate an stl dependency out which we'd rather not
// do (see bug 666609). Plus, the entries array is expect to stay small
// so doing a realloc everytime we add a new entry shouldn't be too costly
entries =
static_cast<Entry*>(realloc(entries, sizeof(Entry) * (count + 1)));
if (!entries) {
MOZ_CRASH("GFX: UserData::Add");
}
entries[count].key = key;
entries[count].userData = userData;
entries[count].destroy = destroy;
count++;
}
/* Remove and return user data associated with key, without destroying it */
void* Remove(UserDataKey* key) {
for (int i = 0; i < count; i++) {
if (key == entries[i].key) {
void* userData = entries[i].userData;
// decrement before looping so entries[i+1] doesn't read past the end:
--count;
for (; i < count; i++) {
entries[i] = entries[i + 1];
}
return userData;
}
}
return nullptr;
}
/* Remove and destroy a given key */
void RemoveAndDestroy(UserDataKey* key) {
for (int i = 0; i < count; i++) {
if (key == entries[i].key) {
if (entries[i].destroy) {
entries[i].destroy(entries[i].userData);
}
// decrement before looping so entries[i+1] doesn't read past the end:
--count;
for (; i < count; i++) {
entries[i] = entries[i + 1];
}
}
}
}
/* Retrives the userData for the associated key */
void* Get(UserDataKey* key) const {
for (int i = 0; i < count; i++) {
if (key == entries[i].key) {
return entries[i].userData;
}
}
return nullptr;
}
bool Has(UserDataKey* key) {
for (int i = 0; i < count; i++) {
if (key == entries[i].key) {
return true;
}
}
return false;
}
void Destroy() {
if (!entries) {
return;
}
for (int i = 0; i < count; i++) {
if (entries[i].destroy) {
entries[i].destroy(entries[i].userData);
}
}
free(entries);
entries = nullptr;
count = 0;
}
~UserData() { Destroy(); }
private:
struct Entry {
const UserDataKey* key;
void* userData;
DestroyFunc destroy;
};
int count;
Entry* entries;
};
class ThreadSafeUserData {
protected:
struct LockedUserData : public UserData {
Mutex mLock;
LockedUserData() : mLock("LockedUserData::mLock") {}
};
public:
~ThreadSafeUserData() {
if (LockedUserData* userData = mUserData.exchange(nullptr)) {
{
MutexAutoLock lock(userData->mLock);
userData->Destroy();
}
delete userData;
}
}
void Add(UserDataKey* key, void* value, UserData::DestroyFunc destroy) {
LockedUserData* userData = GetUserData();
MutexAutoLock lock(userData->mLock);
userData->Add(key, value, destroy);
}
void* Remove(UserDataKey* key) {
LockedUserData* userData = GetUserData();
MutexAutoLock lock(userData->mLock);
return userData->Remove(key);
}
void RemoveAndDestroy(UserDataKey* key) {
LockedUserData* userData = GetUserData();
MutexAutoLock lock(userData->mLock);
userData->RemoveAndDestroy(key);
}
void* Get(UserDataKey* key) const {
LockedUserData* userData = GetUserData();
MutexAutoLock lock(userData->mLock);
return userData->Get(key);
}
bool Has(UserDataKey* key) {
LockedUserData* userData = GetUserData();
MutexAutoLock lock(userData->mLock);
return userData->Has(key);
}
private:
LockedUserData* GetUserData() const {
LockedUserData* userData = mUserData;
if (!userData) {
userData = new LockedUserData;
if (!mUserData.compareExchange(nullptr, userData)) {
delete userData;
userData = mUserData;
MOZ_ASSERT(userData);
}
}
return userData;
}
// The Mutex class is quite large. For small, frequent classes (ScaledFont,
// SourceSurface, etc.) this can add a lot of memory overhead, especially if
// UserData is only infrequently used. To avoid this, we only allocate the
// LockedUserData if it is actually used. If unused, it only adds a single
// pointer as overhead.
mutable Atomic<LockedUserData*> mUserData;
};
} // namespace gfx
} // namespace mozilla
#endif /* MOZILLA_GFX_USERDATA_H_ */
|