00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 #ifndef BUDDY_H 00018 #define BUDDY_H 00019 00020 #include <ndb_global.h> 00021 00022 typedef unsigned int Uint32; 00023 typedef unsigned short Uint16; 00024 typedef unsigned long long Uint64; 00025 00026 // 00027 const int UNDEFINED_CHUNK = -2; // XXX Set to hex 00028 00029 // 00030 const int END_OF_CHUNK_LIST = -1; // XXX Set to hex 00031 00032 // A timeout (no of seconds) for the memory segments in the TransporterRegistry 00033 // memory pool. If a segment has been occupied (free=false) for a longer period 00034 // than this timeout, it will be released. 00035 const int ALLOCATION_TIMEOUT = 10000; 00036 00037 // Free segments should always be as large as possible 00038 // and are only allowed to be in any of these sizes 00039 enum FreeSegmentSize { 00040 sz_256 = 0, 00041 sz_512 = 1, 00042 sz_1024 = 2, 00043 sz_2048 = 3, 00044 sz_4096 = 4, 00045 sz_8192 = 5, 00046 sz_16384 = 6, 00047 sz_32768 = 7, 00048 sz_65536 = 8, 00049 sz_131072 = 9, 00050 sz_GET_MAX = 5, 00051 sz_MAX = 9 00052 }; 00053 00054 struct Segment; 00055 00056 class BuddyMemory { 00057 public: 00058 00059 // Return true if there is at least 8 kB memory available 00060 bool memoryAvailable(); 00061 00062 // 00063 bool allocate(int nChunksToAllocate); 00064 00065 // Remove the segment from the freeSegment list 00066 void removeFromFreeSegmentList(int sz, int index); 00067 00068 // Release the segment of size 00069 void release(int releaseId, int size); 00070 00071 // Add a segment to the freeSegment list 00072 void addToFreeSegmentList(int sz, int index); 00073 00074 bool getSegment(Uint32 size, Segment * dst); 00075 00076 void refreshTime(Uint32 time); 00077 00078 //Calculate log2(arg) + 1 00079 Uint32 logTwoPlus(Uint32 arg); 00080 00081 // The current time 00082 Uint32 currentTime; 00083 00084 // Pointer to the first free segment of size FreeSegmentSize 00085 Uint32 freeSegment[sz_MAX]; 00086 00087 // Start address of the memory block allocated 00088 Uint32* startOfMemoryBlock; 00089 00090 // Total number of 256 byte chunks. 00091 Uint32 totalNoOfChunks; 00092 00093 // Array of 256-byte chunks 00094 struct Chunk256* chunk; 00095 }; 00096 00097 struct Segment { 00098 Uint32 segmentSize; // Size of the segment in no of words 00099 Uint16 index; // Index in the array of SegmentListElements 00100 Uint16 releaseId; // Unique no used when releasing the segment 00101 // Undefined if Long_signal.deallocIndicator==0 00102 union { 00103 Uint32* segmentAddress; // Address to the memory segment 00104 Uint64 _padding_NOT_TO_BE_USED_; 00105 }; 00106 }; 00107 00108 struct Chunk256 { 00109 Uint32 allocationTimeStamp; // Bit 0 represents if the segment is free or not 00110 // Bit 1-31 is the allocation time for the segment 00111 // Bit 1-31 are undefined if the segment is free 00112 Uint32 nextSegmentOfSameSize; // Undefined if allocated. 00113 // The first chunk in a free segment has a valid 00114 // next-pointer. In the rest of the chunks 00115 // belonging to the segment it is UNDEFINED_CHUNK. 00116 Uint32 prevSegmentOfSameSize; // Undefined if allocated 00117 // The first chunk in a free segment has a valid 00118 // prev-pointer. In the rest of the chunks 00119 // belonging to the segment it is UNDEFINED_CHUNK. 00120 00121 void setFree(bool free); 00122 00123 bool getFree(); 00124 00125 void setAllocationTimeStamp(Uint32 cTime); 00126 00127 Uint32 getAllocationTimeStamp(); 00128 }; 00129 00130 // inline void Chunk256::setFree(bool free){ 00131 // // Bit 0 of allocationTimeStamp represents if the segment is free or not 00132 // allocationTimeStamp = 0x0; 00133 00134 // printf("\nSet free segment"); 00135 // Uint32 offMask = 0x0; // A mask to set the 0 bit to 0 00136 // if(free) 00137 // // Set this bit to 0, if segment should be free 00138 // allocationTimeStamp = allocationTimeStamp & offMask; 00139 // } 00140 00141 // inline bool Chunk256::getFree(){ 00142 // // Get free segment 00143 00144 // allocationTimeStamp = 0x0; 00145 // Uint32 offMask = 0x0; 00146 00147 // printf("\nGet free segment"); 00148 // return ((allocationTimeStamp | offMask) == offMask ? true : false); 00149 // } 00150 00151 // inline void Chunk256::setAllocationTimeStamp(Uint32 cTime){ 00152 // // Bits 1-31 of allocationTimeStamp represent the allocation time for segment 00153 00154 // Uint32 onMask = 0x80000000; // A mask to set the 0 bit to 1 00155 // allocationTimeStamp = 0x0; 00156 00157 // printf("\nSet allocation time"); 00158 00159 // allocationTimeStamp = onMask | cTime; 00160 // } 00161 00162 // inline Uint32 Chunk256::getAllocationTimeStamp(){ 00163 00164 // Uint32 onMask = 0x80000000; // A mask to set the 0 bit to 1 00165 // allocationTimeStamp = 0x0; 00166 00167 // printf("\nGet allocation time"); 00168 // allocationTimeStamp = allocationTimeStamp ^ onMask; 00169 // return allocationTimeStamp; 00170 // }; 00171 00172 #endif
1.4.7

