gfx/skia/trunk/src/core/SkDataTable.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 "SkData.h"
michael@0 9 #include "SkDataTable.h"
michael@0 10
michael@0 11 static void malloc_freeproc(void* context) {
michael@0 12 sk_free(context);
michael@0 13 }
michael@0 14
michael@0 15 // Makes empty table
michael@0 16 SkDataTable::SkDataTable() {
michael@0 17 fCount = 0;
michael@0 18 fElemSize = 0; // 0 signals that we use fDir instead of fElems
michael@0 19 fU.fDir = NULL;
michael@0 20 fFreeProc = NULL;
michael@0 21 fFreeProcContext = NULL;
michael@0 22 }
michael@0 23
michael@0 24 SkDataTable::SkDataTable(const void* array, size_t elemSize, int count,
michael@0 25 FreeProc proc, void* context) {
michael@0 26 SkASSERT(count > 0);
michael@0 27
michael@0 28 fCount = count;
michael@0 29 fElemSize = elemSize; // non-zero signals we use fElems instead of fDir
michael@0 30 fU.fElems = (const char*)array;
michael@0 31 fFreeProc = proc;
michael@0 32 fFreeProcContext = context;
michael@0 33 }
michael@0 34
michael@0 35 SkDataTable::SkDataTable(const Dir* dir, int count, FreeProc proc, void* ctx) {
michael@0 36 SkASSERT(count > 0);
michael@0 37
michael@0 38 fCount = count;
michael@0 39 fElemSize = 0; // 0 signals that we use fDir instead of fElems
michael@0 40 fU.fDir = dir;
michael@0 41 fFreeProc = proc;
michael@0 42 fFreeProcContext = ctx;
michael@0 43 }
michael@0 44
michael@0 45 SkDataTable::~SkDataTable() {
michael@0 46 if (fFreeProc) {
michael@0 47 fFreeProc(fFreeProcContext);
michael@0 48 }
michael@0 49 }
michael@0 50
michael@0 51 size_t SkDataTable::atSize(int index) const {
michael@0 52 SkASSERT((unsigned)index < (unsigned)fCount);
michael@0 53
michael@0 54 if (fElemSize) {
michael@0 55 return fElemSize;
michael@0 56 } else {
michael@0 57 return fU.fDir[index].fSize;
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 const void* SkDataTable::at(int index, size_t* size) const {
michael@0 62 SkASSERT((unsigned)index < (unsigned)fCount);
michael@0 63
michael@0 64 if (fElemSize) {
michael@0 65 if (size) {
michael@0 66 *size = fElemSize;
michael@0 67 }
michael@0 68 return fU.fElems + index * fElemSize;
michael@0 69 } else {
michael@0 70 if (size) {
michael@0 71 *size = fU.fDir[index].fSize;
michael@0 72 }
michael@0 73 return fU.fDir[index].fPtr;
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77 ///////////////////////////////////////////////////////////////////////////////
michael@0 78
michael@0 79 SkDataTable* SkDataTable::NewEmpty() {
michael@0 80 static SkDataTable* gEmpty;
michael@0 81 if (NULL == gEmpty) {
michael@0 82 gEmpty = SkNEW(SkDataTable);
michael@0 83 }
michael@0 84 gEmpty->ref();
michael@0 85 return gEmpty;
michael@0 86 }
michael@0 87
michael@0 88 SkDataTable* SkDataTable::NewCopyArrays(const void * const * ptrs,
michael@0 89 const size_t sizes[], int count) {
michael@0 90 if (count <= 0) {
michael@0 91 return SkDataTable::NewEmpty();
michael@0 92 }
michael@0 93
michael@0 94 size_t dataSize = 0;
michael@0 95 for (int i = 0; i < count; ++i) {
michael@0 96 dataSize += sizes[i];
michael@0 97 }
michael@0 98
michael@0 99 size_t bufferSize = count * sizeof(Dir) + dataSize;
michael@0 100 void* buffer = sk_malloc_throw(bufferSize);
michael@0 101
michael@0 102 Dir* dir = (Dir*)buffer;
michael@0 103 char* elem = (char*)(dir + count);
michael@0 104 for (int i = 0; i < count; ++i) {
michael@0 105 dir[i].fPtr = elem;
michael@0 106 dir[i].fSize = sizes[i];
michael@0 107 memcpy(elem, ptrs[i], sizes[i]);
michael@0 108 elem += sizes[i];
michael@0 109 }
michael@0 110
michael@0 111 return SkNEW_ARGS(SkDataTable, (dir, count, malloc_freeproc, buffer));
michael@0 112 }
michael@0 113
michael@0 114 SkDataTable* SkDataTable::NewCopyArray(const void* array, size_t elemSize,
michael@0 115 int count) {
michael@0 116 if (count <= 0) {
michael@0 117 return SkDataTable::NewEmpty();
michael@0 118 }
michael@0 119
michael@0 120 size_t bufferSize = elemSize * count;
michael@0 121 void* buffer = sk_malloc_throw(bufferSize);
michael@0 122 memcpy(buffer, array, bufferSize);
michael@0 123
michael@0 124 return SkNEW_ARGS(SkDataTable,
michael@0 125 (buffer, elemSize, count, malloc_freeproc, buffer));
michael@0 126 }
michael@0 127
michael@0 128 SkDataTable* SkDataTable::NewArrayProc(const void* array, size_t elemSize,
michael@0 129 int count, FreeProc proc, void* ctx) {
michael@0 130 if (count <= 0) {
michael@0 131 return SkDataTable::NewEmpty();
michael@0 132 }
michael@0 133 return SkNEW_ARGS(SkDataTable, (array, elemSize, count, proc, ctx));
michael@0 134 }
michael@0 135
michael@0 136 ///////////////////////////////////////////////////////////////////////////////
michael@0 137
michael@0 138 static void chunkalloc_freeproc(void* context) {
michael@0 139 SkDELETE((SkChunkAlloc*)context);
michael@0 140 }
michael@0 141
michael@0 142 SkDataTableBuilder::SkDataTableBuilder(size_t minChunkSize)
michael@0 143 : fHeap(NULL)
michael@0 144 , fMinChunkSize(minChunkSize) {}
michael@0 145
michael@0 146 SkDataTableBuilder::~SkDataTableBuilder() { this->reset(); }
michael@0 147
michael@0 148 void SkDataTableBuilder::reset(size_t minChunkSize) {
michael@0 149 fMinChunkSize = minChunkSize;
michael@0 150 fDir.reset();
michael@0 151 if (fHeap) {
michael@0 152 SkDELETE(fHeap);
michael@0 153 fHeap = NULL;
michael@0 154 }
michael@0 155 }
michael@0 156
michael@0 157 void SkDataTableBuilder::append(const void* src, size_t size) {
michael@0 158 if (NULL == fHeap) {
michael@0 159 fHeap = SkNEW_ARGS(SkChunkAlloc, (fMinChunkSize));
michael@0 160 }
michael@0 161
michael@0 162 void* dst = fHeap->alloc(size, SkChunkAlloc::kThrow_AllocFailType);
michael@0 163 memcpy(dst, src, size);
michael@0 164
michael@0 165 SkDataTable::Dir* dir = fDir.append();
michael@0 166 dir->fPtr = dst;
michael@0 167 dir->fSize = size;
michael@0 168 }
michael@0 169
michael@0 170 SkDataTable* SkDataTableBuilder::detachDataTable() {
michael@0 171 const int count = fDir.count();
michael@0 172 if (0 == count) {
michael@0 173 return SkDataTable::NewEmpty();
michael@0 174 }
michael@0 175
michael@0 176 // Copy the dir into the heap;
michael@0 177 void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir),
michael@0 178 SkChunkAlloc::kThrow_AllocFailType);
michael@0 179 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir));
michael@0 180
michael@0 181 SkDataTable* table = SkNEW_ARGS(SkDataTable,
michael@0 182 ((SkDataTable::Dir*)dir, count,
michael@0 183 chunkalloc_freeproc, fHeap));
michael@0 184 // we have to detach our fHeap, since we are giving that to the table
michael@0 185 fHeap = NULL;
michael@0 186 fDir.reset();
michael@0 187 return table;
michael@0 188 }

mercurial