~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/vm/SimplePropertiesSection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include <SimpleProperties.hpp>
 
17
#include <TransporterDefinitions.hpp>
 
18
#include "LongSignal.hpp"
 
19
 
 
20
SimplePropertiesSectionReader::SimplePropertiesSectionReader
 
21
(struct SegmentedSectionPtr & ptr, class SectionSegmentPool & pool)
 
22
  : m_pool(pool)
 
23
{
 
24
  if(ptr.p == 0){
 
25
    m_pos = 0;
 
26
    m_len = 0;
 
27
    m_head = 0;
 
28
    m_currentSegment = 0;
 
29
  } else {
 
30
    m_pos = 0;
 
31
    m_len = ptr.p->m_sz;
 
32
    m_head = ptr.p;
 
33
    m_currentSegment = ptr.p;
 
34
  }
 
35
  first();
 
36
}
 
37
  
 
38
void
 
39
SimplePropertiesSectionReader::reset(){
 
40
  m_pos = 0;
 
41
  m_currentSegment = m_head;
 
42
}
 
43
 
 
44
bool
 
45
SimplePropertiesSectionReader::step(Uint32 len){
 
46
  if(m_pos + len >= m_len) {
 
47
    m_pos++;
 
48
    return false;
 
49
  }
 
50
  while(len > SectionSegment::DataLength){
 
51
    m_currentSegment = m_pool.getPtr(m_currentSegment->m_nextSegment);
 
52
 
 
53
    len -= SectionSegment::DataLength;
 
54
    m_pos += SectionSegment::DataLength;
 
55
  }
 
56
  
 
57
  Uint32 ind = m_pos % SectionSegment::DataLength;
 
58
  while(len > 0){
 
59
    len--;
 
60
    m_pos++;
 
61
    
 
62
    ind++;
 
63
    if(ind == SectionSegment::DataLength){
 
64
      ind = 0;
 
65
      m_currentSegment = m_pool.getPtr(m_currentSegment->m_nextSegment);
 
66
    }
 
67
  }
 
68
  return true;
 
69
}
 
70
 
 
71
bool
 
72
SimplePropertiesSectionReader::getWord(Uint32 * dst){
 
73
  if (peekWord(dst)) {
 
74
    step(1);
 
75
    return true;
 
76
  }
 
77
  return false;
 
78
}
 
79
 
 
80
bool
 
81
SimplePropertiesSectionReader::peekWord(Uint32 * dst) const {
 
82
  if(m_pos < m_len){
 
83
    Uint32 ind = m_pos % SectionSegment::DataLength; 
 
84
    * dst = m_currentSegment->theData[ind];
 
85
    return true;
 
86
  }
 
87
  return false;
 
88
}
 
89
 
 
90
bool
 
91
SimplePropertiesSectionReader::peekWords(Uint32 * dst, Uint32 len) const {
 
92
  if(m_pos + len > m_len){
 
93
    return false;
 
94
  }
 
95
  Uint32 ind = (m_pos % SectionSegment::DataLength);
 
96
  Uint32 left = (SectionSegment::DataLength - ind);
 
97
  SectionSegment * p = m_currentSegment;
 
98
 
 
99
  while(len > left){
 
100
    memcpy(dst, &p->theData[ind], 4 * left);
 
101
    dst += left;
 
102
    len -= left;
 
103
    ind = 0;
 
104
    left = SectionSegment::DataLength;
 
105
    p = m_pool.getPtr(p->m_nextSegment);
 
106
  }
 
107
  
 
108
  memcpy(dst, &p->theData[ind], 4 * len);      
 
109
  return true;
 
110
}
 
111
 
 
112
bool
 
113
SimplePropertiesSectionReader::getWords(Uint32 * dst, Uint32 len){
 
114
  if(peekWords(dst, len)){
 
115
    step(len);
 
116
    return true;
 
117
  }
 
118
  return false;
 
119
}
 
120
 
 
121
SimplePropertiesSectionWriter::SimplePropertiesSectionWriter(class SectionSegmentPool & pool)
 
122
  : m_pool(pool)
 
123
{
 
124
  Ptr<SectionSegment> first;
 
125
  if(m_pool.seize(first)){
 
126
    ;
 
127
  } else {
 
128
    m_pos = -1;
 
129
    m_head = 0;
 
130
    m_currentSegment = 0;
 
131
    m_prevPtrI = RNIL;
 
132
    return;
 
133
  }
 
134
  m_sz = 0;
 
135
  m_pos = 0;
 
136
  m_head = first.p;
 
137
  m_head->m_lastSegment = first.i;
 
138
  m_currentSegment = first.p;
 
139
  m_prevPtrI = RNIL;
 
140
}
 
141
  
 
142
bool
 
143
SimplePropertiesSectionWriter::reset(){
 
144
  if(m_pos >= 0){
 
145
    m_pos = 0;
 
146
    return true;
 
147
  }
 
148
  return false;
 
149
}
 
150
 
 
151
bool
 
152
SimplePropertiesSectionWriter::putWord(Uint32 val){
 
153
  return putWords(&val, 1);
 
154
}
 
155
 
 
156
bool
 
157
SimplePropertiesSectionWriter::putWords(const Uint32 * src, Uint32 len){
 
158
  Uint32 left = SectionSegment::DataLength - m_pos;
 
159
  
 
160
  while(len >= left){
 
161
    memcpy(&m_currentSegment->theData[m_pos], src, 4 * left);
 
162
    Ptr<SectionSegment> next;    
 
163
    if(m_pool.seize(next)){
 
164
 
 
165
      m_prevPtrI = m_currentSegment->m_lastSegment;
 
166
      m_currentSegment->m_nextSegment = next.i;
 
167
      next.p->m_lastSegment = next.i;
 
168
      m_currentSegment = next.p;
 
169
      
 
170
      len -= left;
 
171
      src += left;
 
172
      m_sz += left;
 
173
      
 
174
      left = SectionSegment::DataLength;
 
175
      m_pos = 0;
 
176
    } else {
 
177
      abort();
 
178
      return false;
 
179
    }
 
180
  }
 
181
 
 
182
  memcpy(&m_currentSegment->theData[m_pos], src, 4 * len);
 
183
  m_sz += len;
 
184
  m_pos += len;
 
185
  
 
186
  assert(m_pos < (int)SectionSegment::DataLength);
 
187
 
 
188
  return true;
 
189
}
 
190
 
 
191
void
 
192
SimplePropertiesSectionWriter::getPtr(struct SegmentedSectionPtr & dst){
 
193
  // Set last ptr and size
 
194
  if(m_pos >= 0){
 
195
    dst.p = m_head;
 
196
    dst.i = m_head->m_lastSegment;
 
197
    dst.sz = m_sz;
 
198
    m_head->m_sz = m_sz;
 
199
    m_head->m_lastSegment = m_currentSegment->m_lastSegment;
 
200
 
 
201
    if((m_pos % SectionSegment::DataLength) == 0){
 
202
      m_pool.release(m_currentSegment->m_lastSegment);
 
203
      m_head->m_lastSegment = m_prevPtrI;
 
204
    }
 
205
 
 
206
    m_sz = 0;
 
207
    m_pos = -1;
 
208
    m_head = m_currentSegment = 0;
 
209
    m_prevPtrI = RNIL;
 
210
    return ;
 
211
  }
 
212
  dst.p = 0;
 
213
  dst.sz = 0;
 
214
  dst.i = RNIL;
 
215
 
 
216
  m_pool.release(m_head->m_lastSegment);
 
217
  
 
218
  m_sz = 0;
 
219
  m_pos = -1;
 
220
  m_head = m_currentSegment = 0;
 
221
  m_prevPtrI = RNIL;
 
222
}