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