OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_PICKLE_H__ | 5 #ifndef BASE_PICKLE_H__ |
6 #define BASE_PICKLE_H__ | 6 #define BASE_PICKLE_H__ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 // with BeginWriteData), the Pickle can | 266 // with BeginWriteData), the Pickle can |
267 // be 'trimmed' if the amount of data required is less than originally | 267 // be 'trimmed' if the amount of data required is less than originally |
268 // requested. For example, you may have created a buffer with 10K of data, | 268 // requested. For example, you may have created a buffer with 10K of data, |
269 // but decided to only fill 10 bytes of that data. Use this function | 269 // but decided to only fill 10 bytes of that data. Use this function |
270 // to trim the buffer so that we don't send 9990 bytes of unused data. | 270 // to trim the buffer so that we don't send 9990 bytes of unused data. |
271 // You cannot increase the size of the variable buffer; only shrink it. | 271 // You cannot increase the size of the variable buffer; only shrink it. |
272 // This function assumes that the length of the variable buffer has | 272 // This function assumes that the length of the variable buffer has |
273 // not been changed. | 273 // not been changed. |
274 void TrimWriteData(int length); | 274 void TrimWriteData(int length); |
275 | 275 |
276 // Reserves space for upcoming writes when multiple writes will be made and | |
277 // their sizes are computed in advance. It can be significantly faster to call | |
278 // Reserve() before calling WriteFoo() multiple times. | |
279 void Reserve(size_t size); | |
Tom Sepez
2013/10/24 17:48:06
Is |size| the additional amount of space to reserv
danakj
2013/10/24 17:51:38
Additional space, I will rename it to additional_c
| |
280 | |
276 // Payload follows after allocation of Header (header size is customizable). | 281 // Payload follows after allocation of Header (header size is customizable). |
277 struct Header { | 282 struct Header { |
278 uint32 payload_size; // Specifies the size of the payload. | 283 uint32 payload_size; // Specifies the size of the payload. |
279 }; | 284 }; |
280 | 285 |
281 // Returns the header, cast to a user-specified type T. The type T must be a | 286 // Returns the header, cast to a user-specified type T. The type T must be a |
282 // subclass of Header and its size must correspond to the header_size passed | 287 // subclass of Header and its size must correspond to the header_size passed |
283 // to the Pickle constructor. | 288 // to the Pickle constructor. |
284 template <class T> | 289 template <class T> |
285 T* headerT() { | 290 T* headerT() { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 // to pad out for the next write. | 326 // to pad out for the next write. |
322 char* BeginWrite(size_t length); | 327 char* BeginWrite(size_t length); |
323 | 328 |
324 // Completes the write operation by padding the data with NULL bytes until it | 329 // Completes the write operation by padding the data with NULL bytes until it |
325 // is padded. Should be paired with BeginWrite, but it does not necessarily | 330 // is padded. Should be paired with BeginWrite, but it does not necessarily |
326 // have to be called after the data is written. | 331 // have to be called after the data is written. |
327 void EndWrite(char* dest, int length); | 332 void EndWrite(char* dest, int length); |
328 | 333 |
329 // Resize the capacity, note that the input value should include the size of | 334 // Resize the capacity, note that the input value should include the size of |
330 // the header: new_capacity = sizeof(Header) + desired_payload_capacity. | 335 // the header: new_capacity = sizeof(Header) + desired_payload_capacity. |
331 // A realloc() failure will cause a Resize failure... and caller should check | 336 void Resize(size_t new_capacity); |
332 // the return result for true (i.e., successful resizing). | |
333 bool Resize(size_t new_capacity); | |
334 | 337 |
335 // Aligns 'i' by rounding it up to the next multiple of 'alignment' | 338 // Aligns 'i' by rounding it up to the next multiple of 'alignment' |
336 static size_t AlignInt(size_t i, int alignment) { | 339 static size_t AlignInt(size_t i, int alignment) { |
337 return i + (alignment - (i % alignment)) % alignment; | 340 return i + (alignment - (i % alignment)) % alignment; |
338 } | 341 } |
339 | 342 |
340 // Find the end of the pickled data that starts at range_start. Returns NULL | 343 // Find the end of the pickled data that starts at range_start. Returns NULL |
341 // if the entire Pickle is not found in the given data range. | 344 // if the entire Pickle is not found in the given data range. |
342 static const char* FindNext(size_t header_size, | 345 static const char* FindNext(size_t header_size, |
343 const char* range_start, | 346 const char* range_start, |
(...skipping 10 matching lines...) Expand all Loading... | |
354 // Allocation size of payload (or -1 if allocation is const). | 357 // Allocation size of payload (or -1 if allocation is const). |
355 size_t capacity_; | 358 size_t capacity_; |
356 size_t variable_buffer_offset_; // IF non-zero, then offset to a buffer. | 359 size_t variable_buffer_offset_; // IF non-zero, then offset to a buffer. |
357 | 360 |
358 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); | 361 FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
359 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); | 362 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
360 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); | 363 FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
361 }; | 364 }; |
362 | 365 |
363 #endif // BASE_PICKLE_H__ | 366 #endif // BASE_PICKLE_H__ |
OLD | NEW |