| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SCRATCH_BUFFER_H_ | |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SCRATCH_BUFFER_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 | |
| 10 #include "mojo/public/cpp/bindings/buffer.h" | |
| 11 #include "mojo/public/cpp/system/macros.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 namespace internal { | |
| 15 | |
| 16 // The following class is designed to be allocated on the stack. If necessary, | |
| 17 // it will failover to allocating objects on the heap. | |
| 18 class ScratchBuffer : public Buffer { | |
| 19 public: | |
| 20 ScratchBuffer(); | |
| 21 virtual ~ScratchBuffer(); | |
| 22 | |
| 23 virtual void* Allocate(size_t num_bytes, Destructor func = NULL) | |
| 24 MOJO_OVERRIDE; | |
| 25 | |
| 26 private: | |
| 27 enum { | |
| 28 kMinSegmentSize = 512, | |
| 29 kMaxSegmentSize = 1024 * 1024 * 1024, | |
| 30 }; | |
| 31 | |
| 32 struct Segment { | |
| 33 Segment* next; | |
| 34 char* cursor; | |
| 35 char* end; | |
| 36 }; | |
| 37 | |
| 38 void* AllocateInSegment(Segment* segment, size_t num_bytes); | |
| 39 bool AddOverflowSegment(size_t delta); | |
| 40 | |
| 41 char fixed_data_[kMinSegmentSize]; | |
| 42 Segment fixed_; | |
| 43 Segment* overflow_; | |
| 44 | |
| 45 struct PendingDestructor { | |
| 46 Destructor func; | |
| 47 void* address; | |
| 48 }; | |
| 49 std::deque<PendingDestructor> pending_dtors_; | |
| 50 | |
| 51 MOJO_DISALLOW_COPY_AND_ASSIGN(ScratchBuffer); | |
| 52 }; | |
| 53 | |
| 54 } // namespace internal | |
| 55 } // namespace mojo | |
| 56 | |
| 57 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SCRATCH_BUFFER_H_ | |
| OLD | NEW |