Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: src/core/SkFlate.cpp

Issue 963853002: Flate: skia_alloc_func works on both miniz and zlib (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-02-27 (Friday) 14:47:34 EST Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2010 The Android Open Source Project 3 * Copyright 2010 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkData.h" 10 #include "SkData.h"
11 #include "SkFlate.h" 11 #include "SkFlate.h"
12 #include "SkStream.h" 12 #include "SkStream.h"
13 13
14 #ifndef SK_NO_FLATE 14 #ifndef SK_NO_FLATE
15 15
16 namespace { 16 namespace {
17 17
18 #ifdef ZLIB_INCLUDE 18 #ifdef ZLIB_INCLUDE
19 #include ZLIB_INCLUDE 19 #include ZLIB_INCLUDE
20 #else 20 #else
21 #include "zlib.h" 21 #include "zlib.h"
22 #endif 22 #endif
23 23
24 // static 24 // static
25 const size_t kBufferSize = 1024; 25 const size_t kBufferSize = 1024;
26 26
27 // Different zlib implementations use different T.
28 // We've seen size_t and unsigned.
29 template <typename T> void* skia_alloc_func(void*, T items, T size) {
30 return sk_calloc_throw(SkToSizeT(items) * SkToSizeT(size));
31 }
32
33 static void skia_free_func(void*, void* address) { sk_free(address); }
34
27 bool doFlate(bool compress, SkStream* src, SkWStream* dst) { 35 bool doFlate(bool compress, SkStream* src, SkWStream* dst) {
28 uint8_t inputBuffer[kBufferSize]; 36 uint8_t inputBuffer[kBufferSize];
29 uint8_t outputBuffer[kBufferSize]; 37 uint8_t outputBuffer[kBufferSize];
30 z_stream flateData; 38 z_stream flateData;
31 flateData.zalloc = NULL; 39 flateData.zalloc = &skia_alloc_func;
32 flateData.zfree = NULL; 40 flateData.zfree = &skia_free_func;
41 flateData.opaque = NULL;
33 flateData.next_in = NULL; 42 flateData.next_in = NULL;
34 flateData.avail_in = 0; 43 flateData.avail_in = 0;
35 flateData.next_out = outputBuffer; 44 flateData.next_out = outputBuffer;
36 flateData.avail_out = kBufferSize; 45 flateData.avail_out = kBufferSize;
37 int rc; 46 int rc;
38 if (compress) 47 if (compress)
39 rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION); 48 rc = deflateInit(&flateData, Z_DEFAULT_COMPRESSION);
40 else 49 else
41 rc = inflateInit(&flateData); 50 rc = inflateInit(&flateData);
42 if (rc != Z_OK) 51 if (rc != Z_OK)
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 z_stream fZStream; 172 z_stream fZStream;
164 }; 173 };
165 174
166 SkDeflateWStream::SkDeflateWStream(SkWStream* out) 175 SkDeflateWStream::SkDeflateWStream(SkWStream* out)
167 : fImpl(SkNEW(SkDeflateWStream::Impl)) { 176 : fImpl(SkNEW(SkDeflateWStream::Impl)) {
168 fImpl->fOut = out; 177 fImpl->fOut = out;
169 fImpl->fInBufferIndex = 0; 178 fImpl->fInBufferIndex = 0;
170 if (!fImpl->fOut) { 179 if (!fImpl->fOut) {
171 return; 180 return;
172 } 181 }
173 fImpl->fZStream.zalloc = Z_NULL; 182 fImpl->fZStream.zalloc = &skia_alloc_func;
174 fImpl->fZStream.zfree = Z_NULL; 183 fImpl->fZStream.zfree = &skia_free_func;
175 fImpl->fZStream.opaque = Z_NULL; 184 fImpl->fZStream.opaque = NULL;
176 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION); 185 SkDEBUGCODE(int r =) deflateInit(&fImpl->fZStream, Z_DEFAULT_COMPRESSION);
177 SkASSERT(Z_OK == r); 186 SkASSERT(Z_OK == r);
178 } 187 }
179 188
180 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); } 189 SkDeflateWStream::~SkDeflateWStream() { this->finalize(); }
181 190
182 void SkDeflateWStream::finalize() { 191 void SkDeflateWStream::finalize() {
183 if (!fImpl->fOut) { 192 if (!fImpl->fOut) {
184 return; 193 return;
185 } 194 }
(...skipping 27 matching lines...) Expand all
213 return true; 222 return true;
214 } 223 }
215 224
216 size_t SkDeflateWStream::bytesWritten() const { 225 size_t SkDeflateWStream::bytesWritten() const {
217 return fImpl->fZStream.total_in + fImpl->fInBufferIndex; 226 return fImpl->fZStream.total_in + fImpl->fInBufferIndex;
218 } 227 }
219 228
220 229
221 #endif // SK_NO_FLATE 230 #endif // SK_NO_FLATE
222 231
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698