summaryrefslogtreecommitdiff
path: root/ndb/src/kernel/blocks/ndbfs/CircularIndex.hpp
blob: 668b0e1b70ee9c865b69799f94998f9af729a199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef CircularIndex_H
#define CircularIndex_H

//===========================================================================
//
// .DESCRIPTION
//              Building block for circular buffers. It increment as a normal index.
//              untill it it becomes the maximum size then it becomes zero. 
//
// .TYPICAL USE:
//              to implement a circular buffer.
//
// .EXAMPLE:
//              See MemoryChannel.C
//===========================================================================

///////////////////////////////////////////////////////////////////////////////
// CircularIndex(  int start= 0,int size=256 );
//   Constuctor
// Parameters:
//      start: where to start to index
//      size : range of the index, will be from 0 to size-1
///////////////////////////////////////////////////////////////////////////////
// operator int ();
//   returns the index
///////////////////////////////////////////////////////////////////////////////
// void operator ++ ();
//   increments the index with one, of size is reached it is set to zero
///////////////////////////////////////////////////////////////////////////////
// friend int full( const CircularIndex& write, const CircularIndex& read );
//   Taken the write index and the read index from a buffer it is calculated
//              if the buffer is full
// Parameters:
//      write: index used a write index for the buffer
//      read : index used a read index for the buffer
// return
//      0 : not full
//      1 : full
///////////////////////////////////////////////////////////////////////////////
// friend int empty( const CircularIndex& write, const CircularIndex& read );
//   Taken the write index and the read index from a buffer it is calculated
//              if the buffer is empty
// Parameters:
//      write: index used a write index for the buffer
//      read : index used a read index for the buffer
// return
//      0 : not empty
//      1 : empty
///////////////////////////////////////////////////////////////////////////////

class CircularIndex
{
public:
  inline CircularIndex(  int start= 0,int size=256 );
  operator int () const;
  CircularIndex& operator ++ ();
  friend int full( const CircularIndex& write, const CircularIndex& read );
  friend int empty( const CircularIndex& write, const CircularIndex& read );
private:
  int theSize;
  int theIndex;
};

inline CircularIndex::operator int () const
{
  return theIndex;
}

inline CircularIndex& CircularIndex::operator ++ ()
{
  ++theIndex;
  if( theIndex >= theSize ){
    theIndex= 0;
  }
  return *this;
}


inline int full( const CircularIndex& write, const CircularIndex& read )
{
  int readTmp= read.theIndex;

  if( read.theIndex < write.theIndex  )
    readTmp += read.theSize;

  return ( readTmp - write.theIndex) == 1;
}

inline int empty( const CircularIndex& write, const CircularIndex& read )
{
  return read.theIndex == write.theIndex;
}


inline CircularIndex::CircularIndex(  int start,int size ):
  theSize(size),
  theIndex(start)
{
}
#endif