~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/blocks/backup/FsBuffer.hpp

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
 
15
 
 
16
#ifndef FS_BUFFER_HPP
 
17
#define FS_BUFFER_HPP
 
18
 
 
19
#include <ndb_global.h>
 
20
 
 
21
#define DEBUG(x)
 
22
 
 
23
/**
 
24
 * A circular data buffer to be used together with the FS
 
25
 * 
 
26
 * One writer - Typically your block
 
27
 *   getWritePtr()
 
28
 *   updateWritePtr()
 
29
 *
 
30
 * One reader - Typically "thread" in your block sending stuff to NDBFS
 
31
 *   getReadPtr()
 
32
 *   updateReadPtr()
 
33
 */
 
34
class FsBuffer {
 
35
public:  
 
36
  /**
 
37
   * Default constructor
 
38
   */
 
39
  FsBuffer();
 
40
 
 
41
  /**
 
42
   * setup FsBuffer
 
43
   *
 
44
   * @param Buffer    - Ptr to continuous memory
 
45
   * @param Size      - Buffer size in 32-bit words
 
46
   * @param BlockSize - Size of block in 32-bit words
 
47
   * @param MinRead   - Min read size in 32-bit words
 
48
   *                    Get rounded(down) to nearest multiple of block size.
 
49
   * @param MaxRead   - Max read size in 32-bit words
 
50
   *                    Get rounded(down) to nearest multiple of block size.
 
51
   * @param MaxWrite  - Maximum write (into buffer) in 32-bit words
 
52
   *
 
53
   * @return NULL if everything is OK
 
54
   *    else A string describing problem
 
55
   */
 
56
  const char * setup(Uint32 * Buffer,
 
57
                     Uint32 Size, 
 
58
                     Uint32 BlockSize = 128,   // 512 bytes
 
59
                     Uint32 MinRead   = 1024,  // 4k
 
60
                     Uint32 MaxRead   = 1024,  // 4k
 
61
                     Uint32 MaxWrite  = 1024); // 4k
 
62
  /*  
 
63
   * @return NULL if everything is OK
 
64
   *    else A string describing problem
 
65
   */
 
66
  const char * valid() const;
 
67
  
 
68
  Uint32 getBufferSize() const;
 
69
  Uint32 getUsableSize() const; 
 
70
  Uint32 * getStart() const;
 
71
  
 
72
  /**
 
73
   * getReadPtr - Get pointer and size of data to send to FS
 
74
   *
 
75
   * @param ptr - Where to fetch data
 
76
   * @param sz  - How much data in 32-bit words
 
77
   * @param eof - Is this the last fetch (only if return false)
 
78
   *                                             
 
79
   * @return true  - If there is data of size >= minread
 
80
   *         false - If there is can be data be if it is is < minread
 
81
   *               - else eof = true
 
82
   */
 
83
  bool getReadPtr(Uint32 ** ptr, Uint32 * sz, bool * eof);
 
84
  
 
85
  /**
 
86
   * @note: sz must be equal to sz returned by getReadPtr
 
87
   */
 
88
  void updateReadPtr(Uint32 sz);
 
89
 
 
90
  /**
 
91
   * 
 
92
   * @note Must be followed by a updateWritePtr(no of words used)
 
93
   */
 
94
  bool getWritePtr(Uint32 ** ptr, Uint32 sz);
 
95
  
 
96
  void updateWritePtr(Uint32 sz);
 
97
 
 
98
  /**
 
99
   * There will be no more writing to this buffer
 
100
   */
 
101
  void eof();
 
102
 
 
103
  /**
 
104
   * Getters for varibles
 
105
   */
 
106
  Uint32 getMaxWrite() const { return m_maxWrite;}
 
107
  Uint32 getMinRead() const { return m_minRead;}
 
108
  
 
109
  Uint32 getFreeSize() const { return m_free; }
 
110
 
 
111
  /**
 
112
   * reset
 
113
   */
 
114
  void reset();
 
115
 
 
116
private:
 
117
  
 
118
  Uint32 m_free;
 
119
  Uint32 m_readIndex;
 
120
  Uint32 m_writeIndex;
 
121
  Uint32 m_eof;
 
122
  Uint32 * m_start; 
 
123
  Uint32 m_minRead;
 
124
  Uint32 m_maxRead;
 
125
  Uint32 m_maxWrite;
 
126
  Uint32 m_size;
 
127
 
 
128
  Uint32 * m_buffer;
 
129
  Uint32 m_bufSize;
 
130
  Uint32 m_blockSize;
 
131
 
 
132
  void clear();
 
133
};
 
134
 
 
135
inline
 
136
FsBuffer::FsBuffer() 
 
137
{
 
138
  clear();
 
139
}
 
140
 
 
141
inline
 
142
void
 
143
FsBuffer::clear(){
 
144
  m_minRead = m_maxRead = m_maxWrite = m_size = m_bufSize = m_free = 0;
 
145
  m_buffer = m_start = 0;
 
146
}
 
147
 
 
148
static 
 
149
Uint32 * 
 
150
align(Uint32 * ptr, Uint32 alignment, bool downwards){
 
151
  
 
152
  const UintPtr a = (UintPtr)ptr;
 
153
  const UintPtr b = a % alignment;
 
154
  
 
155
  if(downwards){
 
156
    return (Uint32 *)(a - b);
 
157
  } else {
 
158
    return (Uint32 *)(a + (b == 0 ? 0 : (alignment - b)));
 
159
  }
 
160
}
 
161
 
 
162
inline
 
163
const char * 
 
164
FsBuffer::setup(Uint32 * Buffer,
 
165
                Uint32 Size, 
 
166
                Uint32 Block,
 
167
                Uint32 MinRead,
 
168
                Uint32 MaxRead,
 
169
                Uint32 MaxWrite)
 
170
{
 
171
  clear();
 
172
  m_buffer    = Buffer;
 
173
  m_bufSize   = Size;
 
174
  m_blockSize = Block;
 
175
  if(Block == 0){
 
176
    return valid();
 
177
  }
 
178
  
 
179
  m_minRead  = (MinRead / Block) * Block;
 
180
  m_maxRead  = (MaxRead / Block) * Block;
 
181
  m_maxWrite = MaxWrite;
 
182
 
 
183
  m_start = align(Buffer, Block*4, false);
 
184
  Uint32 * stop = align(Buffer + Size - MaxWrite, Block*4, true);
 
185
  if(stop > m_start){
 
186
    m_size = stop - m_start;
 
187
  } else {
 
188
    m_size = 0;
 
189
  }
 
190
  
 
191
  if(m_minRead == 0)
 
192
    m_size = 0;
 
193
  else
 
194
    m_size = (m_size / m_minRead) * m_minRead;
 
195
  
 
196
#if 0
 
197
  ndbout_c("Block = %d MinRead = %d -> %d", Block*4, MinRead*4, m_minRead*4);
 
198
  ndbout_c("Block = %d MaxRead = %d -> %d", Block*4, MaxRead*4, m_maxRead*4);
 
199
  
 
200
  ndbout_c("Buffer = %d -> %d", Buffer, m_start);
 
201
  ndbout_c("Buffer = %d Size = %d MaxWrite = %d -> %d",
 
202
           Buffer, Size*4, MaxWrite*4, m_size*4);
 
203
#endif
 
204
 
 
205
  m_readIndex = m_writeIndex = m_eof = 0;
 
206
  m_free = m_size;
 
207
  return valid();
 
208
}
 
209
 
 
210
inline
 
211
void
 
212
FsBuffer::reset() 
 
213
{
 
214
  m_readIndex = m_writeIndex = 0;
 
215
  m_free = m_size;
 
216
  m_eof = 0;
 
217
}
 
218
 
 
219
inline
 
220
const char *
 
221
FsBuffer::valid() const {
 
222
  if(m_buffer  == 0) return "Null pointer buffer";
 
223
  if(m_bufSize == 0) return "Zero size buffer";
 
224
  if(m_blockSize == 0) return "Zero block size";
 
225
  if(m_minRead < m_blockSize) return "Min read less than block size";
 
226
  if(m_maxRead < m_blockSize) return "Max read less than block size";
 
227
  if(m_maxRead < m_minRead) return "Max read less than min read";
 
228
  if(m_size == 0) return "Zero usable space";
 
229
  return 0;
 
230
}
 
231
 
 
232
inline
 
233
Uint32 
 
234
FsBuffer::getBufferSize() const {
 
235
  return m_bufSize;
 
236
}
 
237
 
 
238
inline
 
239
Uint32
 
240
FsBuffer::getUsableSize() const {
 
241
  return m_size;
 
242
}
 
243
 
 
244
inline
 
245
Uint32 *
 
246
FsBuffer::getStart() const {
 
247
  return m_start;
 
248
}
 
249
 
 
250
inline
 
251
bool 
 
252
FsBuffer::getReadPtr(Uint32 ** ptr, Uint32 * sz, bool * _eof){
 
253
 
 
254
  Uint32 * Tp = m_start;
 
255
  const Uint32 Tr = m_readIndex;
 
256
  const Uint32 Tm = m_minRead;
 
257
  const Uint32 Ts = m_size;
 
258
  const Uint32 Tmw = m_maxRead;
 
259
 
 
260
  Uint32 sz1 = m_size - m_free; // Used
 
261
  
 
262
  if(sz1 >= Tm){
 
263
    if(Tr + sz1 > Ts)
 
264
      sz1 = (Ts - Tr);
 
265
    
 
266
    if(sz1 > Tmw)
 
267
      * sz = Tmw;
 
268
    else
 
269
      * sz = sz1 - (sz1 % Tm);
 
270
    
 
271
    * ptr = &Tp[Tr];
 
272
 
 
273
    DEBUG(ndbout_c("getReadPtr() Tr: %d Tmw: %d Ts: %d Tm: %d sz1: %d -> %d",
 
274
                   Tr, Tmw, Ts, Tm, sz1, * sz));
 
275
 
 
276
    return true;
 
277
  }
 
278
  
 
279
  if(!m_eof){
 
280
    * _eof = false;
 
281
    
 
282
    DEBUG(ndbout_c("getReadPtr() Tr: %d Tmw: %d Ts: %d Tm: %d sz1: %d -> false",
 
283
                   Tr, Tmw, Ts, Tm, sz1));
 
284
    
 
285
    return false;
 
286
  }
 
287
  
 
288
  * sz = sz1;
 
289
  * _eof = true;
 
290
  * ptr = &Tp[Tr];
 
291
 
 
292
  DEBUG(ndbout_c("getReadPtr() Tr: %d Tmw: %d Ts: %d Tm: %d sz1: %d -> %d eof",
 
293
                 Tr, Tmw, Ts, Tm, sz1, * sz));
 
294
  
 
295
  return false;
 
296
}
 
297
 
 
298
inline
 
299
void
 
300
FsBuffer::updateReadPtr(Uint32 sz){
 
301
  const Uint32 Tr = m_readIndex;
 
302
  const Uint32 Ts = m_size;
 
303
  
 
304
  m_free += sz;
 
305
  m_readIndex = (Tr + sz) % Ts;
 
306
}
 
307
 
 
308
inline
 
309
bool
 
310
FsBuffer::getWritePtr(Uint32 ** ptr, Uint32 sz){
 
311
  assert(sz <= m_maxWrite);
 
312
  Uint32 * Tp = m_start;
 
313
  const Uint32 Tw = m_writeIndex;
 
314
  const Uint32 sz1 = m_free;
 
315
 
 
316
  if(sz1 > sz){ // Note at least 1 word of slack
 
317
    * ptr = &Tp[Tw];
 
318
 
 
319
    DEBUG(ndbout_c("getWritePtr(%d) Tw: %d sz1: %d -> true",
 
320
                   sz, Tw, sz1));
 
321
    return true;
 
322
  }
 
323
 
 
324
  DEBUG(ndbout_c("getWritePtr(%d) Tw: %d sz1: %d -> false",
 
325
                 sz, Tw, sz1));
 
326
 
 
327
  return false;
 
328
}
 
329
 
 
330
inline
 
331
void 
 
332
FsBuffer::updateWritePtr(Uint32 sz){
 
333
  assert(sz <= m_maxWrite);
 
334
  Uint32 * Tp = m_start;
 
335
  const Uint32 Tw = m_writeIndex;
 
336
  const Uint32 Ts = m_size;
 
337
  
 
338
  const Uint32 Tnew = (Tw + sz);
 
339
  m_free -= sz;
 
340
  if(Tnew < Ts){
 
341
    m_writeIndex = Tnew;
 
342
    DEBUG(ndbout_c("updateWritePtr(%d) m_writeIndex: %d",
 
343
                   sz, m_writeIndex));
 
344
    return;
 
345
  }
 
346
 
 
347
  memcpy(Tp, &Tp[Ts], (Tnew - Ts) << 2);
 
348
  m_writeIndex = Tnew - Ts;
 
349
  DEBUG(ndbout_c("updateWritePtr(%d) m_writeIndex: %d",
 
350
                 sz, m_writeIndex));
 
351
}
 
352
 
 
353
inline
 
354
void
 
355
FsBuffer::eof(){
 
356
  m_eof = 1;
 
357
}
 
358
 
 
359
#endif