MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buddy.hpp
1 /*
2  Copyright (C) 2003-2006 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #ifndef BUDDY_H
20 #define BUDDY_H
21 
22 #include <ndb_global.h>
23 
24 typedef unsigned int Uint32;
25 typedef unsigned short Uint16;
26 typedef unsigned long long Uint64;
27 
28 //
29 const int UNDEFINED_CHUNK = -2; // XXX Set to hex
30 
31 //
32 const int END_OF_CHUNK_LIST = -1; // XXX Set to hex
33 
34 // A timeout (no of seconds) for the memory segments in the TransporterRegistry
35 // memory pool. If a segment has been occupied (free=false) for a longer period
36 // than this timeout, it will be released.
37 const int ALLOCATION_TIMEOUT = 10000;
38 
39 // Free segments should always be as large as possible
40 // and are only allowed to be in any of these sizes
41 enum FreeSegmentSize {
42  sz_256 = 0,
43  sz_512 = 1,
44  sz_1024 = 2,
45  sz_2048 = 3,
46  sz_4096 = 4,
47  sz_8192 = 5,
48  sz_16384 = 6,
49  sz_32768 = 7,
50  sz_65536 = 8,
51  sz_131072 = 9,
52  sz_GET_MAX = 5,
53  sz_MAX = 9
54 };
55 
56 struct Segment;
57 
58 class BuddyMemory {
59 public:
60 
61  // Return true if there is at least 8 kB memory available
62  bool memoryAvailable();
63 
64  //
65  bool allocate(int nChunksToAllocate);
66 
67  // Remove the segment from the freeSegment list
68  void removeFromFreeSegmentList(int sz, int index);
69 
70  // Release the segment of size
71  void release(int releaseId, int size);
72 
73  // Add a segment to the freeSegment list
74  void addToFreeSegmentList(int sz, int index);
75 
76  bool getSegment(Uint32 size, Segment * dst);
77 
78  void refreshTime(Uint32 time);
79 
80  //Calculate log2(arg) + 1
81  Uint32 logTwoPlus(Uint32 arg);
82 
83  // The current time
84  Uint32 currentTime;
85 
86  // Pointer to the first free segment of size FreeSegmentSize
87  Uint32 freeSegment[sz_MAX];
88 
89  // Start address of the memory block allocated
90  Uint32* startOfMemoryBlock;
91 
92  // Total number of 256 byte chunks.
93  Uint32 totalNoOfChunks;
94 
95  // Array of 256-byte chunks
96  struct Chunk256* chunk;
97 };
98 
99 struct Segment {
100  Uint32 segmentSize; // Size of the segment in no of words
101  Uint16 index; // Index in the array of SegmentListElements
102  Uint16 releaseId; // Unique no used when releasing the segment
103  // Undefined if Long_signal.deallocIndicator==0
104  union {
105  Uint32* segmentAddress; // Address to the memory segment
106  Uint64 _padding_NOT_TO_BE_USED_;
107  };
108 };
109 
110 struct Chunk256 {
111  Uint32 allocationTimeStamp; // Bit 0 represents if the segment is free or not
112  // Bit 1-31 is the allocation time for the segment
113  // Bit 1-31 are undefined if the segment is free
114  Uint32 nextSegmentOfSameSize; // Undefined if allocated.
115  // The first chunk in a free segment has a valid
116  // next-pointer. In the rest of the chunks
117  // belonging to the segment it is UNDEFINED_CHUNK.
118  Uint32 prevSegmentOfSameSize; // Undefined if allocated
119  // The first chunk in a free segment has a valid
120  // prev-pointer. In the rest of the chunks
121  // belonging to the segment it is UNDEFINED_CHUNK.
122 
123  void setFree(bool free);
124 
125  bool getFree();
126 
127  void setAllocationTimeStamp(Uint32 cTime);
128 
129  Uint32 getAllocationTimeStamp();
130 };
131 
132 // inline void Chunk256::setFree(bool free){
133 // // Bit 0 of allocationTimeStamp represents if the segment is free or not
134 // allocationTimeStamp = 0x0;
135 
136 // printf("\nSet free segment");
137 // Uint32 offMask = 0x0; // A mask to set the 0 bit to 0
138 // if(free)
139 // // Set this bit to 0, if segment should be free
140 // allocationTimeStamp = allocationTimeStamp & offMask;
141 // }
142 
143 // inline bool Chunk256::getFree(){
144 // // Get free segment
145 
146 // allocationTimeStamp = 0x0;
147 // Uint32 offMask = 0x0;
148 
149 // printf("\nGet free segment");
150 // return ((allocationTimeStamp | offMask) == offMask ? true : false);
151 // }
152 
153 // inline void Chunk256::setAllocationTimeStamp(Uint32 cTime){
154 // // Bits 1-31 of allocationTimeStamp represent the allocation time for segment
155 
156 // Uint32 onMask = 0x80000000; // A mask to set the 0 bit to 1
157 // allocationTimeStamp = 0x0;
158 
159 // printf("\nSet allocation time");
160 
161 // allocationTimeStamp = onMask | cTime;
162 // }
163 
164 // inline Uint32 Chunk256::getAllocationTimeStamp(){
165 
166 // Uint32 onMask = 0x80000000; // A mask to set the 0 bit to 1
167 // allocationTimeStamp = 0x0;
168 
169 // printf("\nGet allocation time");
170 // allocationTimeStamp = allocationTimeStamp ^ onMask;
171 // return allocationTimeStamp;
172 // };
173 
174 #endif