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

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/transporter/buddy.hpp

  • 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
#ifndef BUDDY_H
 
17
#define BUDDY_H
 
18
 
 
19
#include <ndb_global.h>
 
20
 
 
21
typedef unsigned int Uint32;
 
22
typedef unsigned short Uint16;
 
23
typedef unsigned long long Uint64;
 
24
 
 
25
//
 
26
const int UNDEFINED_CHUNK = -2; // XXX Set to hex
 
27
 
 
28
//
 
29
const int END_OF_CHUNK_LIST = -1; // XXX Set to hex
 
30
 
 
31
// A timeout (no of seconds) for the memory segments in the TransporterRegistry 
 
32
// memory pool. If a segment has been occupied (free=false) for a longer period 
 
33
// than this timeout, it will be released.
 
34
const int ALLOCATION_TIMEOUT = 10000;
 
35
 
 
36
// Free segments should always be as large as possible
 
37
// and are only allowed to be in any of these sizes
 
38
enum FreeSegmentSize {
 
39
  sz_256  = 0,
 
40
  sz_512  = 1,
 
41
  sz_1024 = 2,
 
42
  sz_2048 = 3,
 
43
  sz_4096 = 4,
 
44
  sz_8192 = 5,
 
45
  sz_16384 = 6,
 
46
  sz_32768 = 7,
 
47
  sz_65536 = 8,
 
48
  sz_131072 = 9,
 
49
  sz_GET_MAX = 5,
 
50
  sz_MAX = 9
 
51
};
 
52
 
 
53
struct Segment;
 
54
 
 
55
class BuddyMemory {
 
56
public:
 
57
 
 
58
  // Return true if there is at least 8 kB memory available
 
59
  bool memoryAvailable();
 
60
 
 
61
  // 
 
62
  bool allocate(int nChunksToAllocate);
 
63
 
 
64
  // Remove the segment from the freeSegment list
 
65
  void removeFromFreeSegmentList(int sz, int index);
 
66
  
 
67
  // Release the segment of size
 
68
  void release(int releaseId, int size);
 
69
  
 
70
  // Add a segment to the freeSegment list
 
71
  void addToFreeSegmentList(int sz, int index);
 
72
 
 
73
  bool getSegment(Uint32 size, Segment * dst);
 
74
 
 
75
  void refreshTime(Uint32 time);
 
76
  
 
77
  //Calculate log2(arg) + 1
 
78
  Uint32 logTwoPlus(Uint32 arg);
 
79
  
 
80
  // The current time
 
81
  Uint32 currentTime;
 
82
  
 
83
  // Pointer to the first free segment of size FreeSegmentSize
 
84
  Uint32 freeSegment[sz_MAX];
 
85
  
 
86
  // Start address of the memory block allocated
 
87
  Uint32* startOfMemoryBlock;
 
88
  
 
89
  // Total number of 256 byte chunks.
 
90
  Uint32 totalNoOfChunks;
 
91
  
 
92
  // Array of 256-byte chunks
 
93
  struct Chunk256* chunk;
 
94
};
 
95
 
 
96
struct Segment {
 
97
  Uint32 segmentSize;    // Size of the segment in no of words
 
98
  Uint16 index;          // Index in the array of SegmentListElements
 
99
  Uint16 releaseId;      // Unique no used when releasing the segment
 
100
                         // Undefined if Long_signal.deallocIndicator==0
 
101
  union {
 
102
    Uint32* segmentAddress; // Address to the memory segment
 
103
    Uint64  _padding_NOT_TO_BE_USED_;
 
104
  };
 
105
};
 
106
 
 
107
struct Chunk256 {
 
108
  Uint32 allocationTimeStamp;   // Bit 0 represents if the segment is free or not
 
109
                                // Bit 1-31 is the allocation time for the segment
 
110
                                // Bit 1-31 are undefined if the segment is free 
 
111
  Uint32 nextSegmentOfSameSize; // Undefined if allocated. 
 
112
                                // The first chunk in a free segment has a valid 
 
113
                                // next-pointer. In the rest of the chunks 
 
114
                                // belonging to the segment it is UNDEFINED_CHUNK.
 
115
  Uint32 prevSegmentOfSameSize; // Undefined if allocated
 
116
                                // The first chunk in a free segment has a valid 
 
117
                                // prev-pointer. In the rest of the chunks 
 
118
                                // belonging to the segment it is UNDEFINED_CHUNK.
 
119
 
 
120
  void setFree(bool free);
 
121
 
 
122
  bool getFree();
 
123
 
 
124
  void setAllocationTimeStamp(Uint32 cTime);
 
125
 
 
126
  Uint32 getAllocationTimeStamp();  
 
127
};
 
128
 
 
129
// inline void Chunk256::setFree(bool free){
 
130
//   // Bit 0 of allocationTimeStamp represents if the segment is free or not
 
131
//   allocationTimeStamp = 0x0;
 
132
 
 
133
//   printf("\nSet free segment");
 
134
//   Uint32 offMask = 0x0; // A mask to set the 0 bit to 0
 
135
//   if(free) 
 
136
//     // Set this bit to 0, if segment should be free
 
137
//     allocationTimeStamp = allocationTimeStamp & offMask;
 
138
// }
 
139
 
 
140
// inline bool Chunk256::getFree(){
 
141
//   // Get free segment
 
142
 
 
143
//   allocationTimeStamp = 0x0;
 
144
//   Uint32 offMask = 0x0; 
 
145
 
 
146
//   printf("\nGet free segment"); 
 
147
//   return ((allocationTimeStamp | offMask) == offMask ? true : false);
 
148
// }
 
149
 
 
150
// inline void Chunk256::setAllocationTimeStamp(Uint32 cTime){
 
151
//   // Bits 1-31 of allocationTimeStamp represent the allocation time for segment
 
152
 
 
153
//   Uint32 onMask = 0x80000000; // A mask to set the 0 bit to 1
 
154
//   allocationTimeStamp = 0x0;
 
155
 
 
156
//   printf("\nSet allocation time");
 
157
  
 
158
//   allocationTimeStamp = onMask | cTime;
 
159
// }
 
160
 
 
161
// inline Uint32 Chunk256::getAllocationTimeStamp(){
 
162
 
 
163
//   Uint32 onMask = 0x80000000; // A mask to set the 0 bit to 1
 
164
//   allocationTimeStamp = 0x0;
 
165
 
 
166
//   printf("\nGet allocation time");
 
167
//   allocationTimeStamp = allocationTimeStamp ^ onMask;
 
168
//   return allocationTimeStamp;
 
169
// };
 
170
 
 
171
#endif