Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / public / bindings / lib / scratch_buffer.h
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_BINDINGS_LIB_SCRATCH_BUFFER_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_SCRATCH_BUFFER_H_
7
8 #include <deque>
9
10 #include "mojo/public/bindings/buffer.h"
11 #include "mojo/public/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 { kMinSegmentSize = 512 };
28
29   struct Segment {
30     Segment* next;
31     char* cursor;
32     char* end;
33   };
34
35   void* AllocateInSegment(Segment* segment, size_t num_bytes);
36   void AddOverflowSegment(size_t delta);
37
38   char fixed_data_[kMinSegmentSize];
39   Segment fixed_;
40   Segment* overflow_;
41
42   struct PendingDestructor {
43     Destructor func;
44     void* address;
45   };
46   std::deque<PendingDestructor> pending_dtors_;
47
48   MOJO_DISALLOW_COPY_AND_ASSIGN(ScratchBuffer);
49 };
50
51 }  // namespace internal
52 }  // namespace mojo
53
54 #endif  // MOJO_PUBLIC_BINDINGS_LIB_SCRATCH_BUFFER_H_