| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkData.h" | 8 #include "SkData.h" |
| 9 #include "SkDataTable.h" | 9 #include "SkDataTable.h" |
| 10 | 10 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 } | 72 } |
| 73 return fU.fDir[index].fPtr; | 73 return fU.fDir[index].fPtr; |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 /////////////////////////////////////////////////////////////////////////////// | 77 /////////////////////////////////////////////////////////////////////////////// |
| 78 | 78 |
| 79 SkDataTable* SkDataTable::NewEmpty() { | 79 SkDataTable* SkDataTable::NewEmpty() { |
| 80 static SkDataTable* gEmpty; | 80 static SkDataTable* gEmpty; |
| 81 if (NULL == gEmpty) { | 81 if (NULL == gEmpty) { |
| 82 gEmpty = SkNEW(SkDataTable); | 82 gEmpty = new SkDataTable; |
| 83 } | 83 } |
| 84 gEmpty->ref(); | 84 gEmpty->ref(); |
| 85 return gEmpty; | 85 return gEmpty; |
| 86 } | 86 } |
| 87 | 87 |
| 88 SkDataTable* SkDataTable::NewCopyArrays(const void * const * ptrs, | 88 SkDataTable* SkDataTable::NewCopyArrays(const void * const * ptrs, |
| 89 const size_t sizes[], int count) { | 89 const size_t sizes[], int count) { |
| 90 if (count <= 0) { | 90 if (count <= 0) { |
| 91 return SkDataTable::NewEmpty(); | 91 return SkDataTable::NewEmpty(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 size_t dataSize = 0; | 94 size_t dataSize = 0; |
| 95 for (int i = 0; i < count; ++i) { | 95 for (int i = 0; i < count; ++i) { |
| 96 dataSize += sizes[i]; | 96 dataSize += sizes[i]; |
| 97 } | 97 } |
| 98 | 98 |
| 99 size_t bufferSize = count * sizeof(Dir) + dataSize; | 99 size_t bufferSize = count * sizeof(Dir) + dataSize; |
| 100 void* buffer = sk_malloc_throw(bufferSize); | 100 void* buffer = sk_malloc_throw(bufferSize); |
| 101 | 101 |
| 102 Dir* dir = (Dir*)buffer; | 102 Dir* dir = (Dir*)buffer; |
| 103 char* elem = (char*)(dir + count); | 103 char* elem = (char*)(dir + count); |
| 104 for (int i = 0; i < count; ++i) { | 104 for (int i = 0; i < count; ++i) { |
| 105 dir[i].fPtr = elem; | 105 dir[i].fPtr = elem; |
| 106 dir[i].fSize = sizes[i]; | 106 dir[i].fSize = sizes[i]; |
| 107 memcpy(elem, ptrs[i], sizes[i]); | 107 memcpy(elem, ptrs[i], sizes[i]); |
| 108 elem += sizes[i]; | 108 elem += sizes[i]; |
| 109 } | 109 } |
| 110 | 110 |
| 111 return SkNEW_ARGS(SkDataTable, (dir, count, malloc_freeproc, buffer)); | 111 return new SkDataTable(dir, count, malloc_freeproc, buffer); |
| 112 } | 112 } |
| 113 | 113 |
| 114 SkDataTable* SkDataTable::NewCopyArray(const void* array, size_t elemSize, | 114 SkDataTable* SkDataTable::NewCopyArray(const void* array, size_t elemSize, |
| 115 int count) { | 115 int count) { |
| 116 if (count <= 0) { | 116 if (count <= 0) { |
| 117 return SkDataTable::NewEmpty(); | 117 return SkDataTable::NewEmpty(); |
| 118 } | 118 } |
| 119 | 119 |
| 120 size_t bufferSize = elemSize * count; | 120 size_t bufferSize = elemSize * count; |
| 121 void* buffer = sk_malloc_throw(bufferSize); | 121 void* buffer = sk_malloc_throw(bufferSize); |
| 122 memcpy(buffer, array, bufferSize); | 122 memcpy(buffer, array, bufferSize); |
| 123 | 123 |
| 124 return SkNEW_ARGS(SkDataTable, | 124 return new SkDataTable(buffer, elemSize, count, malloc_freeproc, buffer); |
| 125 (buffer, elemSize, count, malloc_freeproc, buffer)); | |
| 126 } | 125 } |
| 127 | 126 |
| 128 SkDataTable* SkDataTable::NewArrayProc(const void* array, size_t elemSize, | 127 SkDataTable* SkDataTable::NewArrayProc(const void* array, size_t elemSize, |
| 129 int count, FreeProc proc, void* ctx) { | 128 int count, FreeProc proc, void* ctx) { |
| 130 if (count <= 0) { | 129 if (count <= 0) { |
| 131 return SkDataTable::NewEmpty(); | 130 return SkDataTable::NewEmpty(); |
| 132 } | 131 } |
| 133 return SkNEW_ARGS(SkDataTable, (array, elemSize, count, proc, ctx)); | 132 return new SkDataTable(array, elemSize, count, proc, ctx); |
| 134 } | 133 } |
| 135 | 134 |
| 136 /////////////////////////////////////////////////////////////////////////////// | 135 /////////////////////////////////////////////////////////////////////////////// |
| 137 | 136 |
| 138 static void chunkalloc_freeproc(void* context) { | 137 static void chunkalloc_freeproc(void* context) { delete (SkChunkAlloc*)context;
} |
| 139 SkDELETE((SkChunkAlloc*)context); | |
| 140 } | |
| 141 | 138 |
| 142 SkDataTableBuilder::SkDataTableBuilder(size_t minChunkSize) | 139 SkDataTableBuilder::SkDataTableBuilder(size_t minChunkSize) |
| 143 : fHeap(NULL) | 140 : fHeap(NULL) |
| 144 , fMinChunkSize(minChunkSize) {} | 141 , fMinChunkSize(minChunkSize) {} |
| 145 | 142 |
| 146 SkDataTableBuilder::~SkDataTableBuilder() { this->reset(); } | 143 SkDataTableBuilder::~SkDataTableBuilder() { this->reset(); } |
| 147 | 144 |
| 148 void SkDataTableBuilder::reset(size_t minChunkSize) { | 145 void SkDataTableBuilder::reset(size_t minChunkSize) { |
| 149 fMinChunkSize = minChunkSize; | 146 fMinChunkSize = minChunkSize; |
| 150 fDir.reset(); | 147 fDir.reset(); |
| 151 if (fHeap) { | 148 if (fHeap) { |
| 152 SkDELETE(fHeap); | 149 delete fHeap; |
| 153 fHeap = NULL; | 150 fHeap = NULL; |
| 154 } | 151 } |
| 155 } | 152 } |
| 156 | 153 |
| 157 void SkDataTableBuilder::append(const void* src, size_t size) { | 154 void SkDataTableBuilder::append(const void* src, size_t size) { |
| 158 if (NULL == fHeap) { | 155 if (NULL == fHeap) { |
| 159 fHeap = SkNEW_ARGS(SkChunkAlloc, (fMinChunkSize)); | 156 fHeap = new SkChunkAlloc(fMinChunkSize); |
| 160 } | 157 } |
| 161 | 158 |
| 162 void* dst = fHeap->alloc(size, SkChunkAlloc::kThrow_AllocFailType); | 159 void* dst = fHeap->alloc(size, SkChunkAlloc::kThrow_AllocFailType); |
| 163 memcpy(dst, src, size); | 160 memcpy(dst, src, size); |
| 164 | 161 |
| 165 SkDataTable::Dir* dir = fDir.append(); | 162 SkDataTable::Dir* dir = fDir.append(); |
| 166 dir->fPtr = dst; | 163 dir->fPtr = dst; |
| 167 dir->fSize = size; | 164 dir->fSize = size; |
| 168 } | 165 } |
| 169 | 166 |
| 170 SkDataTable* SkDataTableBuilder::detachDataTable() { | 167 SkDataTable* SkDataTableBuilder::detachDataTable() { |
| 171 const int count = fDir.count(); | 168 const int count = fDir.count(); |
| 172 if (0 == count) { | 169 if (0 == count) { |
| 173 return SkDataTable::NewEmpty(); | 170 return SkDataTable::NewEmpty(); |
| 174 } | 171 } |
| 175 | 172 |
| 176 // Copy the dir into the heap; | 173 // Copy the dir into the heap; |
| 177 void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir), | 174 void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir), |
| 178 SkChunkAlloc::kThrow_AllocFailType); | 175 SkChunkAlloc::kThrow_AllocFailType); |
| 179 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir)); | 176 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir)); |
| 180 | 177 |
| 181 SkDataTable* table = SkNEW_ARGS(SkDataTable, | 178 SkDataTable* table = new SkDataTable((SkDataTable::Dir*)dir, count, chunkall
oc_freeproc, fHeap); |
| 182 ((SkDataTable::Dir*)dir, count, | |
| 183 chunkalloc_freeproc, fHeap)); | |
| 184 // we have to detach our fHeap, since we are giving that to the table | 179 // we have to detach our fHeap, since we are giving that to the table |
| 185 fHeap = NULL; | 180 fHeap = NULL; |
| 186 fDir.reset(); | 181 fDir.reset(); |
| 187 return table; | 182 return table; |
| 188 } | 183 } |
| OLD | NEW |