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

Side by Side Diff: runtime/bin/filter.cc

Issue 1777643003: Uses a finalizer to delete zlib filter resources (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments. Fix more problems. Created 4 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 | « runtime/bin/filter.h ('k') | runtime/bin/filter_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/dartutils.h" 5 #include "bin/dartutils.h"
6 #include "bin/filter.h" 6 #include "bin/filter.h"
7 #include "bin/io_buffer.h" 7 #include "bin/io_buffer.h"
8 8
9 #include "include/dart_api.h" 9 #include "include/dart_api.h"
10 10
11 namespace dart { 11 namespace dart {
12 namespace bin { 12 namespace bin {
13 13
14 const int kZLibFlagUseGZipHeader = 16; 14 const int kZLibFlagUseGZipHeader = 16;
15 const int kZLibFlagAcceptAnyHeader = 32; 15 const int kZLibFlagAcceptAnyHeader = 32;
16 16
17 static const int kFilterPointerNativeField = 0; 17 static const int kFilterPointerNativeField = 0;
18 18
19 static Filter* GetFilter(Dart_Handle filter_obj) { 19 static Dart_Handle GetFilter(Dart_Handle filter_obj, Filter** filter) {
20 Filter* filter; 20 Filter* result;
21 Dart_Handle result = Filter::GetFilterPointerNativeField(filter_obj, &filter); 21 Dart_Handle err = Filter::GetFilterNativeField(filter_obj, &result);
22 if (Dart_IsError(result)) { 22 if (Dart_IsError(err)) {
23 Dart_PropagateError(result); 23 return err;
24 } 24 }
25 if (filter == NULL) { 25 if (result == NULL) {
26 Dart_ThrowException(DartUtils::NewInternalError("Filter destroyed")); 26 return Dart_NewApiError("Filter was destroyed");
27 } 27 }
28 return filter; 28
29 *filter = result;
30 return Dart_Null();
29 } 31 }
30 32
31 static void EndFilter(Dart_Handle filter_obj, Filter* filter) {
32 Filter::SetFilterPointerNativeField(filter_obj, NULL);
33 delete filter;
34 }
35 33
36 static uint8_t* copyDictionary(Dart_Handle dictionary_obj) { 34 static Dart_Handle CopyDictionary(Dart_Handle dictionary_obj,
35 uint8_t** dictionary) {
36 ASSERT(dictionary != NULL);
37 uint8_t* src = NULL; 37 uint8_t* src = NULL;
38 intptr_t size; 38 intptr_t size;
39 Dart_TypedData_Type type; 39 Dart_TypedData_Type type;
40 40
41 if (Dart_IsError(Dart_ListLength(dictionary_obj, &size))) { 41 Dart_Handle err = Dart_ListLength(dictionary_obj, &size);
42 Dart_ThrowException(DartUtils::NewInternalError( 42 if (Dart_IsError(err)) {
43 "Failed to get the zlib dictionary length")); 43 return err;
44 } 44 }
45 45
46 uint8_t* dictionary = new uint8_t[size]; 46 uint8_t* result = new uint8_t[size];
47 47 if (result == NULL) {
48 if (dictionary == NULL) { 48 return Dart_NewApiError("Could not allocate new dictionary");
49 Dart_ThrowException(DartUtils::NewInternalError(
50 "Failed to allocate buffer for the zlib dictionary"));
51 } 49 }
52 50
53 Dart_Handle result = Dart_TypedDataAcquireData( 51 err = Dart_TypedDataAcquireData(
54 dictionary_obj, &type, reinterpret_cast<void**>(&src), &size); 52 dictionary_obj, &type, reinterpret_cast<void**>(&src), &size);
55 if (!Dart_IsError(result)) { 53 if (!Dart_IsError(err)) {
56 memmove(dictionary, src, size); 54 memmove(result, src, size);
57 Dart_TypedDataReleaseData(dictionary_obj); 55 Dart_TypedDataReleaseData(dictionary_obj);
58 } else { 56 } else {
59 if (Dart_IsError(Dart_ListGetAsBytes(dictionary_obj, 0, dictionary, 57 err = Dart_ListGetAsBytes(dictionary_obj, 0, result, size);
60 size))) { 58 if (Dart_IsError(err)) {
61 Dart_ThrowException(DartUtils::NewInternalError( 59 delete[] result;
62 "Failed to get the zlib dictionary")); 60 return err;
63 } 61 }
64 } 62 }
65 63
66 return dictionary; 64 *dictionary = result;
65 return Dart_Null();
67 } 66 }
68 67
68
69 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) { 69 void FUNCTION_NAME(Filter_CreateZLibInflate)(Dart_NativeArguments args) {
70 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 70 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
71 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1); 71 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 1);
72 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); 72 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj);
73 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2); 73 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 2);
74 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3);
75 bool raw = DartUtils::GetBooleanValue(raw_obj);
76
77 Dart_Handle err;
74 uint8_t* dictionary = NULL; 78 uint8_t* dictionary = NULL;
75 intptr_t dictionary_length = 0; 79 intptr_t dictionary_length = 0;
76 if (!Dart_IsNull(dict_obj)) { 80 if (!Dart_IsNull(dict_obj)) {
77 dictionary = copyDictionary(dict_obj); 81 err = CopyDictionary(dict_obj, &dictionary);
78 if (dictionary != NULL) { 82 if (Dart_IsError(err)) {
79 dictionary_length = 0; 83 Dart_PropagateError(err);
80 Dart_ListLength(dict_obj, &dictionary_length); 84 }
85 ASSERT(dictionary != NULL);
86 dictionary_length = 0;
87 err = Dart_ListLength(dict_obj, &dictionary_length);
88 if (Dart_IsError(err)) {
89 delete[] dictionary;
90 Dart_PropagateError(err);
81 } 91 }
siva 2016/03/11 12:48:19 Why not change the signature of CopyDictionary to
82 } 92 }
83 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 3); 93
84 bool raw; 94 ZLibInflateFilter* filter = new ZLibInflateFilter(
85 if (Dart_IsError(Dart_BooleanValue(raw_obj, &raw))) { 95 static_cast<int32_t>(window_bits), dictionary, dictionary_length, raw);
86 Dart_ThrowException(DartUtils::NewInternalError( 96 if (filter == NULL) {
87 "Failed to get 'raw' parameter")); 97 delete[] dictionary;
98 Dart_PropagateError(Dart_NewApiError(
99 "Could not allocate ZLibInflateFilter"));
88 } 100 }
89 Filter* filter = new ZLibInflateFilter(static_cast<int32_t>(window_bits),
90 dictionary, dictionary_length, raw);
91 if (!filter->Init()) { 101 if (!filter->Init()) {
92 delete filter; 102 delete filter;
93 Dart_ThrowException(DartUtils::NewInternalError( 103 Dart_ThrowException(DartUtils::NewInternalError(
94 "Failed to create ZLibInflateFilter")); 104 "Failed to create ZLibInflateFilter"));
95 } 105 }
96 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); 106 err = Filter::SetFilterAndCreateFinalizer(
97 if (Dart_IsError(result)) { 107 filter_obj, filter, sizeof(*filter) + dictionary_length);
108 if (Dart_IsError(err)) {
98 delete filter; 109 delete filter;
99 Dart_PropagateError(result); 110 Dart_PropagateError(err);
100 } 111 }
101 } 112 }
102 113
114
103 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) { 115 void FUNCTION_NAME(Filter_CreateZLibDeflate)(Dart_NativeArguments args) {
104 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 116 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
105 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1); 117 Dart_Handle gzip_obj = Dart_GetNativeArgument(args, 1);
106 bool gzip = DartUtils::GetBooleanValue(gzip_obj); 118 bool gzip = DartUtils::GetBooleanValue(gzip_obj);
107 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2); 119 Dart_Handle level_obj = Dart_GetNativeArgument(args, 2);
108 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32, 120 int64_t level = DartUtils::GetInt64ValueCheckRange(level_obj, kMinInt32,
109 kMaxInt32); 121 kMaxInt32);
110 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3); 122 Dart_Handle window_bits_obj = Dart_GetNativeArgument(args, 3);
111 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj); 123 int64_t window_bits = DartUtils::GetIntegerValue(window_bits_obj);
112 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4); 124 Dart_Handle mLevel_obj = Dart_GetNativeArgument(args, 4);
113 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj); 125 int64_t mem_level = DartUtils::GetIntegerValue(mLevel_obj);
114 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5); 126 Dart_Handle strategy_obj = Dart_GetNativeArgument(args, 5);
115 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj); 127 int64_t strategy = DartUtils::GetIntegerValue(strategy_obj);
116 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6); 128 Dart_Handle dict_obj = Dart_GetNativeArgument(args, 6);
129 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7);
130 bool raw = DartUtils::GetBooleanValue(raw_obj);
siva 2016/03/11 12:48:19 Maybe just: bool raw; Dart_GetNativeBooleanArgumen
131
132 Dart_Handle err;
117 uint8_t* dictionary = NULL; 133 uint8_t* dictionary = NULL;
118 intptr_t dictionary_length = 0; 134 intptr_t dictionary_length = 0;
119 if (!Dart_IsNull(dict_obj)) { 135 if (!Dart_IsNull(dict_obj)) {
120 dictionary = copyDictionary(dict_obj); 136 err = CopyDictionary(dict_obj, &dictionary);
121 if (dictionary != NULL) { 137 if (Dart_IsError(err)) {
122 dictionary_length = 0; 138 Dart_PropagateError(err);
123 Dart_ListLength(dict_obj, &dictionary_length); 139 }
140 ASSERT(dictionary != NULL);
141 dictionary_length = 0;
142 err = Dart_ListLength(dict_obj, &dictionary_length);
143 if (Dart_IsError(err)) {
144 delete[] dictionary;
145 Dart_PropagateError(err);
124 } 146 }
125 } 147 }
126 Dart_Handle raw_obj = Dart_GetNativeArgument(args, 7); 148
127 bool raw = DartUtils::GetBooleanValue(raw_obj); 149 ZLibDeflateFilter* filter = new ZLibDeflateFilter(
128 Filter* filter = new ZLibDeflateFilter(gzip, static_cast<int32_t>(level), 150 gzip,
129 static_cast<int32_t>(window_bits), 151 static_cast<int32_t>(level),
130 static_cast<int32_t>(mem_level), 152 static_cast<int32_t>(window_bits),
131 static_cast<int32_t>(strategy), 153 static_cast<int32_t>(mem_level),
132 dictionary, dictionary_length, raw); 154 static_cast<int32_t>(strategy),
155 dictionary, dictionary_length, raw);
156 if (filter == NULL) {
157 delete[] dictionary;
158 Dart_PropagateError(Dart_NewApiError(
159 "Could not allocate ZLibDeflateFilter"));
160 }
133 if (!filter->Init()) { 161 if (!filter->Init()) {
134 delete filter; 162 delete filter;
135 Dart_ThrowException(DartUtils::NewInternalError( 163 Dart_ThrowException(DartUtils::NewInternalError(
136 "Failed to create ZLibDeflateFilter")); 164 "Failed to create ZLibDeflateFilter"));
137 } 165 }
138 Dart_Handle result = Filter::SetFilterPointerNativeField(filter_obj, filter); 166 Dart_Handle result = Filter::SetFilterAndCreateFinalizer(
167 filter_obj, filter, sizeof(*filter) + dictionary_length);
139 if (Dart_IsError(result)) { 168 if (Dart_IsError(result)) {
140 delete filter; 169 delete filter;
141 Dart_PropagateError(result); 170 Dart_PropagateError(result);
142 } 171 }
143 } 172 }
144 173
174
145 void FUNCTION_NAME(Filter_Process)(Dart_NativeArguments args) { 175 void FUNCTION_NAME(Filter_Process)(Dart_NativeArguments args) {
146 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 176 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
147 Filter* filter = GetFilter(filter_obj);
148 Dart_Handle data_obj = Dart_GetNativeArgument(args, 1); 177 Dart_Handle data_obj = Dart_GetNativeArgument(args, 1);
149 intptr_t start = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); 178 intptr_t start = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2));
150 intptr_t end = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); 179 intptr_t end = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
151 intptr_t chunk_length = end - start; 180 intptr_t chunk_length = end - start;
152 intptr_t length; 181 intptr_t length;
153 Dart_TypedData_Type type; 182 Dart_TypedData_Type type;
154 uint8_t* buffer = NULL; 183 uint8_t* buffer = NULL;
184
185 Filter* filter = NULL;
186 Dart_Handle err = GetFilter(filter_obj, &filter);
187 if (Dart_IsError(err)) {
188 Dart_PropagateError(err);
189 }
190
155 Dart_Handle result = Dart_TypedDataAcquireData( 191 Dart_Handle result = Dart_TypedDataAcquireData(
156 data_obj, &type, reinterpret_cast<void**>(&buffer), &length); 192 data_obj, &type, reinterpret_cast<void**>(&buffer), &length);
157
158 if (!Dart_IsError(result)) { 193 if (!Dart_IsError(result)) {
159 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8); 194 ASSERT(type == Dart_TypedData_kUint8 || type == Dart_TypedData_kInt8);
160 if (type != Dart_TypedData_kUint8 && type != Dart_TypedData_kInt8) { 195 if (type != Dart_TypedData_kUint8 && type != Dart_TypedData_kInt8) {
161 Dart_TypedDataReleaseData(data_obj); 196 Dart_TypedDataReleaseData(data_obj);
162 Dart_ThrowException(DartUtils::NewInternalError( 197 Dart_ThrowException(DartUtils::NewInternalError(
163 "Invalid argument passed to Filter_Process")); 198 "Invalid argument passed to Filter_Process"));
164 } 199 }
165 uint8_t* zlib_buffer = new uint8_t[chunk_length]; 200 uint8_t* zlib_buffer = new uint8_t[chunk_length];
166 if (zlib_buffer == NULL) { 201 if (zlib_buffer == NULL) {
167 Dart_TypedDataReleaseData(data_obj); 202 Dart_TypedDataReleaseData(data_obj);
168 Dart_ThrowException(DartUtils::NewInternalError( 203 Dart_PropagateError(Dart_NewApiError("Could not allocate zlib buffer"));
169 "Failed to allocate buffer for zlib"));
170 } 204 }
205
171 memmove(zlib_buffer, buffer + start, chunk_length); 206 memmove(zlib_buffer, buffer + start, chunk_length);
172 Dart_TypedDataReleaseData(data_obj); 207 Dart_TypedDataReleaseData(data_obj);
173 buffer = zlib_buffer; 208 buffer = zlib_buffer;
174 } else { 209 } else {
175 if (Dart_IsError(Dart_ListLength(data_obj, &length))) { 210 err = Dart_ListLength(data_obj, &length);
176 Dart_ThrowException(DartUtils::NewInternalError( 211 if (Dart_IsError(err)) {
177 "Failed to get list length")); 212 Dart_PropagateError(err);
178 } 213 }
179 buffer = new uint8_t[chunk_length]; 214 buffer = new uint8_t[chunk_length];
180 if (Dart_IsError(Dart_ListGetAsBytes( 215 if (buffer == NULL) {
181 data_obj, start, buffer, chunk_length))) { 216 Dart_PropagateError(Dart_NewApiError("Could not allocate buffer"));
217 }
218 err = Dart_ListGetAsBytes(data_obj, start, buffer, chunk_length);
219 if (Dart_IsError(err)) {
182 delete[] buffer; 220 delete[] buffer;
183 Dart_ThrowException(DartUtils::NewInternalError( 221 Dart_PropagateError(err);
184 "Failed to get list bytes"));
185 } 222 }
186 } 223 }
187 // Process will take ownership of buffer, if successful. 224 // Process will take ownership of buffer, if successful.
188 if (!filter->Process(buffer, chunk_length)) { 225 if (!filter->Process(buffer, chunk_length)) {
189 delete[] buffer; 226 delete[] buffer;
190 EndFilter(filter_obj, filter);
191 Dart_ThrowException(DartUtils::NewInternalError( 227 Dart_ThrowException(DartUtils::NewInternalError(
192 "Call to Process while still processing data")); 228 "Call to Process while still processing data"));
193 } 229 }
194 } 230 }
195 231
196 232
197 void FUNCTION_NAME(Filter_Processed)(Dart_NativeArguments args) { 233 void FUNCTION_NAME(Filter_Processed)(Dart_NativeArguments args) {
198 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 234 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0);
199 Filter* filter = GetFilter(filter_obj);
200 Dart_Handle flush_obj = Dart_GetNativeArgument(args, 1); 235 Dart_Handle flush_obj = Dart_GetNativeArgument(args, 1);
201 bool flush; 236 bool flush = DartUtils::GetBooleanValue(flush_obj);
202 if (Dart_IsError(Dart_BooleanValue(flush_obj, &flush))) { 237 Dart_Handle end_obj = Dart_GetNativeArgument(args, 2);
203 Dart_ThrowException(DartUtils::NewInternalError( 238 bool end = DartUtils::GetBooleanValue(end_obj);
204 "Failed to get 'flush' parameter")); 239
240 Filter* filter = NULL;
241 Dart_Handle err = GetFilter(filter_obj, &filter);
242 if (Dart_IsError(err)) {
243 Dart_PropagateError(err);
205 } 244 }
206 Dart_Handle end_obj = Dart_GetNativeArgument(args, 2); 245
207 bool end;
208 if (Dart_IsError(Dart_BooleanValue(end_obj, &end))) {
209 Dart_ThrowException(DartUtils::NewInternalError(
210 "Failed to get 'end' parameter"));
211 }
212 intptr_t read = filter->Processed(filter->processed_buffer(), 246 intptr_t read = filter->Processed(filter->processed_buffer(),
213 filter->processed_buffer_size(), 247 filter->processed_buffer_size(),
214 flush, 248 flush,
215 end); 249 end);
216 if (read < 0) { 250 if (read < 0) {
217 // Error, end filter.
218 EndFilter(filter_obj, filter);
219 Dart_ThrowException(DartUtils::NewInternalError( 251 Dart_ThrowException(DartUtils::NewInternalError(
220 "Filter error, bad data")); 252 "Filter error, bad data"));
221 } else if (read == 0) { 253 } else if (read == 0) {
222 Dart_SetReturnValue(args, Dart_Null()); 254 Dart_SetReturnValue(args, Dart_Null());
223 } else { 255 } else {
224 uint8_t* io_buffer; 256 uint8_t* io_buffer;
225 Dart_Handle result = IOBuffer::Allocate(read, &io_buffer); 257 Dart_Handle result = IOBuffer::Allocate(read, &io_buffer);
226 memmove(io_buffer, filter->processed_buffer(), read); 258 memmove(io_buffer, filter->processed_buffer(), read);
227 Dart_SetReturnValue(args, result); 259 Dart_SetReturnValue(args, result);
228 } 260 }
229 } 261 }
230 262
231 263
232 void FUNCTION_NAME(Filter_End)(Dart_NativeArguments args) { 264 static void DeleteFilter(
233 Dart_Handle filter_obj = Dart_GetNativeArgument(args, 0); 265 void* isolate_data,
234 Filter* filter = GetFilter(filter_obj); 266 Dart_WeakPersistentHandle handle,
235 EndFilter(filter_obj, filter); 267 void* filter_pointer) {
268 Filter* filter = reinterpret_cast<Filter*>(filter_pointer);
269 delete filter;
236 } 270 }
237 271
238 272
239 Dart_Handle Filter::SetFilterPointerNativeField(Dart_Handle filter, 273 Dart_Handle Filter::SetFilterAndCreateFinalizer(Dart_Handle filter,
240 Filter* filter_pointer) { 274 Filter* filter_pointer,
241 return Dart_SetNativeInstanceField( 275 intptr_t size) {
276 Dart_Handle err = Dart_SetNativeInstanceField(
242 filter, 277 filter,
243 kFilterPointerNativeField, 278 kFilterPointerNativeField,
244 reinterpret_cast<intptr_t>(filter_pointer)); 279 reinterpret_cast<intptr_t>(filter_pointer));
280 if (Dart_IsError(err)) {
281 return err;
282 }
283 Dart_NewWeakPersistentHandle(filter,
284 reinterpret_cast<void*>(filter_pointer),
285 size,
286 DeleteFilter);
287 return err;
245 } 288 }
246 289
247 290
248 Dart_Handle Filter::GetFilterPointerNativeField(Dart_Handle filter, 291 Dart_Handle Filter::GetFilterNativeField(Dart_Handle filter,
249 Filter** filter_pointer) { 292 Filter** filter_pointer) {
250 return Dart_GetNativeInstanceField( 293 return Dart_GetNativeInstanceField(
251 filter, 294 filter,
252 kFilterPointerNativeField, 295 kFilterPointerNativeField,
253 reinterpret_cast<intptr_t*>(filter_pointer)); 296 reinterpret_cast<intptr_t*>(filter_pointer));
254 } 297 }
255 298
256 299
257 ZLibDeflateFilter::~ZLibDeflateFilter() { 300 ZLibDeflateFilter::~ZLibDeflateFilter() {
258 delete[] dictionary_; 301 delete[] dictionary_;
259 delete[] current_buffer_; 302 delete[] current_buffer_;
260 if (initialized()) deflateEnd(&stream_); 303 if (initialized()) {
304 deflateEnd(&stream_);
305 }
261 } 306 }
262 307
263 308
264 bool ZLibDeflateFilter::Init() { 309 bool ZLibDeflateFilter::Init() {
265 int window_bits = window_bits_; 310 int window_bits = window_bits_;
266 if (raw_) { 311 if (raw_) {
267 window_bits = -window_bits; 312 window_bits = -window_bits;
268 } else if (gzip_) { 313 } else if (gzip_) {
269 window_bits += kZLibFlagUseGZipHeader; 314 window_bits += kZLibFlagUseGZipHeader;
270 } 315 }
271 stream_.next_in = Z_NULL; 316 stream_.next_in = Z_NULL;
272 stream_.zalloc = Z_NULL; 317 stream_.zalloc = Z_NULL;
273 stream_.zfree = Z_NULL; 318 stream_.zfree = Z_NULL;
274 stream_.opaque = Z_NULL; 319 stream_.opaque = Z_NULL;
275 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits, 320 int result = deflateInit2(&stream_, level_, Z_DEFLATED, window_bits,
276 mem_level_, strategy_); 321 mem_level_, strategy_);
277 if (result != Z_OK) { 322 if (result != Z_OK) {
278 return false; 323 return false;
279 } 324 }
280 if (dictionary_ != NULL && !gzip_ && !raw_) { 325 if ((dictionary_ != NULL) && !gzip_ && !raw_) {
281 result = deflateSetDictionary(&stream_, dictionary_, dictionary_length_); 326 result = deflateSetDictionary(&stream_, dictionary_, dictionary_length_);
282 delete[] dictionary_; 327 delete[] dictionary_;
283 dictionary_ = NULL; 328 dictionary_ = NULL;
284 if (result != Z_OK) { 329 if (result != Z_OK) {
285 return false; 330 return false;
286 } 331 }
287 } 332 }
288 set_initialized(true); 333 set_initialized(true);
289 return true; 334 return true;
290 } 335 }
291 336
292 337
293 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) { 338 bool ZLibDeflateFilter::Process(uint8_t* data, intptr_t length) {
294 if (current_buffer_ != NULL) return false; 339 if (current_buffer_ != NULL) {
340 return false;
341 }
295 stream_.avail_in = length; 342 stream_.avail_in = length;
296 stream_.next_in = current_buffer_ = data; 343 stream_.next_in = current_buffer_ = data;
297 return true; 344 return true;
298 } 345 }
299 346
300 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer, 347 intptr_t ZLibDeflateFilter::Processed(uint8_t* buffer,
301 intptr_t length, 348 intptr_t length,
302 bool flush, 349 bool flush,
303 bool end) { 350 bool end) {
304 stream_.avail_out = length; 351 stream_.avail_out = length;
(...skipping 19 matching lines...) Expand all
324 delete[] current_buffer_; 371 delete[] current_buffer_;
325 current_buffer_ = NULL; 372 current_buffer_ = NULL;
326 // Either 0 Byte processed or error 373 // Either 0 Byte processed or error
327 return error ? -1 : 0; 374 return error ? -1 : 0;
328 } 375 }
329 376
330 377
331 ZLibInflateFilter::~ZLibInflateFilter() { 378 ZLibInflateFilter::~ZLibInflateFilter() {
332 delete[] dictionary_; 379 delete[] dictionary_;
333 delete[] current_buffer_; 380 delete[] current_buffer_;
334 if (initialized()) inflateEnd(&stream_); 381 if (initialized()) {
382 inflateEnd(&stream_);
383 }
335 } 384 }
336 385
337 386
338 bool ZLibInflateFilter::Init() { 387 bool ZLibInflateFilter::Init() {
339 int window_bits = raw_ ? 388 int window_bits = raw_ ?
340 -window_bits_ : 389 -window_bits_ :
341 window_bits_ | kZLibFlagAcceptAnyHeader; 390 window_bits_ | kZLibFlagAcceptAnyHeader;
342 391
343 stream_.next_in = Z_NULL; 392 stream_.next_in = Z_NULL;
344 stream_.avail_in = 0; 393 stream_.avail_in = 0;
345 stream_.zalloc = Z_NULL; 394 stream_.zalloc = Z_NULL;
346 stream_.zfree = Z_NULL; 395 stream_.zfree = Z_NULL;
347 stream_.opaque = Z_NULL; 396 stream_.opaque = Z_NULL;
348 int result = inflateInit2(&stream_, window_bits); 397 int result = inflateInit2(&stream_, window_bits);
349 if (result != Z_OK) { 398 if (result != Z_OK) {
350 return false; 399 return false;
351 } 400 }
352 set_initialized(true); 401 set_initialized(true);
353 return true; 402 return true;
354 } 403 }
355 404
356 405
357 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) { 406 bool ZLibInflateFilter::Process(uint8_t* data, intptr_t length) {
358 if (current_buffer_ != NULL) return false; 407 if (current_buffer_ != NULL) {
408 return false;
409 }
359 stream_.avail_in = length; 410 stream_.avail_in = length;
360 stream_.next_in = current_buffer_ = data; 411 stream_.next_in = current_buffer_ = data;
361 return true; 412 return true;
362 } 413 }
363 414
364 415
365 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer, 416 intptr_t ZLibInflateFilter::Processed(uint8_t* buffer,
366 intptr_t length, 417 intptr_t length,
367 bool flush, 418 bool flush,
368 bool end) { 419 bool end) {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 } 457 }
407 458
408 delete[] current_buffer_; 459 delete[] current_buffer_;
409 current_buffer_ = NULL; 460 current_buffer_ = NULL;
410 // Either 0 Byte processed or error 461 // Either 0 Byte processed or error
411 return error ? -1 : 0; 462 return error ? -1 : 0;
412 } 463 }
413 464
414 } // namespace bin 465 } // namespace bin
415 } // namespace dart 466 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/bin/filter.h ('k') | runtime/bin/filter_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698