Home | History | Annotate | Download | only in common
      1 // Copyright 2013 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 CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
      6 #define CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/gtest_prod_util.h"
     10 
     11 // A wrapper around a memory buffer that allows circular read and write with a
     12 // selectable wrapping position. Buffer layout (after wrap; H is header):
     13 // -----------------------------------------------------------
     14 // | H | Beginning           | End           | Middle        |
     15 // -----------------------------------------------------------
     16 //  ^---- Non-wrapping -----^ ^--------- Wrapping ----------^
     17 // The non-wrapping part is never overwritten. The wrapping part will be
     18 // circular. The very first part is the header (see the BufferData struct
     19 // below). It consists of the following information:
     20 // - Length written to the buffer (not including header).
     21 // - Wrapping position.
     22 // - End position of buffer. (If the last byte is at x, this will be x + 1.)
     23 // Users of wrappers around the same underlying buffer must ensure that writing
     24 // is finished before reading is started.
     25 class PartialCircularBuffer {
     26  public:
     27   // Use for reading. |buffer_size| is in bytes and must be larger than the
     28   // header size (see above).
     29   PartialCircularBuffer(void* buffer, uint32 buffer_size);
     30 
     31   // Use for writing. |buffer_size| is in bytes and must be larger than the
     32   // header size (see above). If |append| is true, the header data is not reset
     33   // and writing will continue were left off, |wrap_position| is then ignored.
     34   PartialCircularBuffer(void* buffer,
     35                         uint32 buffer_size,
     36                         uint32 wrap_position,
     37                         bool append);
     38 
     39   uint32 Read(void* buffer, uint32 buffer_size);
     40   void Write(const void* buffer, uint32 buffer_size);
     41 
     42  private:
     43   friend class PartialCircularBufferTest;
     44 
     45 #pragma pack(push)
     46 #pragma pack(4)
     47   struct BufferData {
     48     uint32 total_written;
     49     uint32 wrap_position;
     50     uint32 end_position;
     51     uint8 data[1];
     52   };
     53 #pragma pack(pop)
     54 
     55   void DoWrite(void* dest, const void* src, uint32 num);
     56 
     57   // Used for reading and writing.
     58   BufferData* buffer_data_;
     59   uint32 memory_buffer_size_;
     60   uint32 data_size_;
     61   uint32 position_;
     62 
     63   // Used for reading.
     64   uint32 total_read_;
     65 };
     66 
     67 #endif  // CHROME_COMMON_PARTIAL_CIRCULAR_BUFFER_H_
     68