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 |
11 static void malloc_freeproc(void* context) { | 11 static void malloc_freeproc(void* context) { |
12 sk_free(context); | 12 sk_free(context); |
13 } | 13 } |
14 | 14 |
15 // Makes empty table | 15 // Makes empty table |
16 SkDataTable::SkDataTable() { | 16 SkDataTable::SkDataTable() { |
17 fCount = 0; | 17 fCount = 0; |
18 fElemSize = 0; // 0 signals that we use fDir instead of fElems | 18 fElemSize = 0; // 0 signals that we use fDir instead of fElems |
19 fU.fDir = NULL; | 19 fU.fDir = nullptr; |
20 fFreeProc = NULL; | 20 fFreeProc = nullptr; |
21 fFreeProcContext = NULL; | 21 fFreeProcContext = nullptr; |
22 } | 22 } |
23 | 23 |
24 SkDataTable::SkDataTable(const void* array, size_t elemSize, int count, | 24 SkDataTable::SkDataTable(const void* array, size_t elemSize, int count, |
25 FreeProc proc, void* context) { | 25 FreeProc proc, void* context) { |
26 SkASSERT(count > 0); | 26 SkASSERT(count > 0); |
27 | 27 |
28 fCount = count; | 28 fCount = count; |
29 fElemSize = elemSize; // non-zero signals we use fElems instead of fDir | 29 fElemSize = elemSize; // non-zero signals we use fElems instead of fDir |
30 fU.fElems = (const char*)array; | 30 fU.fElems = (const char*)array; |
31 fFreeProc = proc; | 31 fFreeProc = proc; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 *size = fU.fDir[index].fSize; | 71 *size = fU.fDir[index].fSize; |
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 (nullptr == gEmpty) { |
82 gEmpty = new 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(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return SkDataTable::NewEmpty(); | 130 return SkDataTable::NewEmpty(); |
131 } | 131 } |
132 return new SkDataTable(array, elemSize, count, proc, ctx); | 132 return new SkDataTable(array, elemSize, count, proc, ctx); |
133 } | 133 } |
134 | 134 |
135 /////////////////////////////////////////////////////////////////////////////// | 135 /////////////////////////////////////////////////////////////////////////////// |
136 | 136 |
137 static void chunkalloc_freeproc(void* context) { delete (SkChunkAlloc*)context;
} | 137 static void chunkalloc_freeproc(void* context) { delete (SkChunkAlloc*)context;
} |
138 | 138 |
139 SkDataTableBuilder::SkDataTableBuilder(size_t minChunkSize) | 139 SkDataTableBuilder::SkDataTableBuilder(size_t minChunkSize) |
140 : fHeap(NULL) | 140 : fHeap(nullptr) |
141 , fMinChunkSize(minChunkSize) {} | 141 , fMinChunkSize(minChunkSize) {} |
142 | 142 |
143 SkDataTableBuilder::~SkDataTableBuilder() { this->reset(); } | 143 SkDataTableBuilder::~SkDataTableBuilder() { this->reset(); } |
144 | 144 |
145 void SkDataTableBuilder::reset(size_t minChunkSize) { | 145 void SkDataTableBuilder::reset(size_t minChunkSize) { |
146 fMinChunkSize = minChunkSize; | 146 fMinChunkSize = minChunkSize; |
147 fDir.reset(); | 147 fDir.reset(); |
148 if (fHeap) { | 148 if (fHeap) { |
149 delete fHeap; | 149 delete fHeap; |
150 fHeap = NULL; | 150 fHeap = nullptr; |
151 } | 151 } |
152 } | 152 } |
153 | 153 |
154 void SkDataTableBuilder::append(const void* src, size_t size) { | 154 void SkDataTableBuilder::append(const void* src, size_t size) { |
155 if (NULL == fHeap) { | 155 if (nullptr == fHeap) { |
156 fHeap = new SkChunkAlloc(fMinChunkSize); | 156 fHeap = new SkChunkAlloc(fMinChunkSize); |
157 } | 157 } |
158 | 158 |
159 void* dst = fHeap->alloc(size, SkChunkAlloc::kThrow_AllocFailType); | 159 void* dst = fHeap->alloc(size, SkChunkAlloc::kThrow_AllocFailType); |
160 memcpy(dst, src, size); | 160 memcpy(dst, src, size); |
161 | 161 |
162 SkDataTable::Dir* dir = fDir.append(); | 162 SkDataTable::Dir* dir = fDir.append(); |
163 dir->fPtr = dst; | 163 dir->fPtr = dst; |
164 dir->fSize = size; | 164 dir->fSize = size; |
165 } | 165 } |
166 | 166 |
167 SkDataTable* SkDataTableBuilder::detachDataTable() { | 167 SkDataTable* SkDataTableBuilder::detachDataTable() { |
168 const int count = fDir.count(); | 168 const int count = fDir.count(); |
169 if (0 == count) { | 169 if (0 == count) { |
170 return SkDataTable::NewEmpty(); | 170 return SkDataTable::NewEmpty(); |
171 } | 171 } |
172 | 172 |
173 // Copy the dir into the heap; | 173 // Copy the dir into the heap; |
174 void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir), | 174 void* dir = fHeap->alloc(count * sizeof(SkDataTable::Dir), |
175 SkChunkAlloc::kThrow_AllocFailType); | 175 SkChunkAlloc::kThrow_AllocFailType); |
176 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir)); | 176 memcpy(dir, fDir.begin(), count * sizeof(SkDataTable::Dir)); |
177 | 177 |
178 SkDataTable* table = new SkDataTable((SkDataTable::Dir*)dir, count, chunkall
oc_freeproc, fHeap); | 178 SkDataTable* table = new SkDataTable((SkDataTable::Dir*)dir, count, chunkall
oc_freeproc, fHeap); |
179 // 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 |
180 fHeap = NULL; | 180 fHeap = nullptr; |
181 fDir.reset(); | 181 fDir.reset(); |
182 return table; | 182 return table; |
183 } | 183 } |
OLD | NEW |