MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DbtupPagMan.cpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #define DBTUP_C
19 #define DBTUP_PAG_MAN_CPP
20 #include "Dbtup.hpp"
21 #include <RefConvert.hpp>
22 #include <ndb_limits.h>
23 #include <pc.hpp>
24 
25 /* ---------------------------------------------------------------- */
26 // 4) Page Memory Manager (buddy algorithm)
27 //
28 // The following data structures in Dbtup is used by the Page Memory
29 // Manager.
30 //
31 // cfreepageList[16]
32 // Pages with a header
33 //
34 // The cfreepageList is 16 free lists. Free list 0 contains chunks of
35 // pages with 2^0 (=1) pages in each chunk. Free list 1 chunks of 2^1
36 // (=2) pages in each chunk and so forth upto free list 15 which
37 // contains chunks of 2^15 (=32768) pages in each chunk.
38 // The cfreepageList array contains the pointer to the first chunk
39 // in each of those lists. The lists are doubly linked where the
40 // first page in each chunk contains the next and previous references
41 // in position ZPAGE_NEXT_CLUST_POS and ZPAGE_PREV_CLUST_POS in the
42 // page header.
43 // In addition the leading page and the last page in each chunk is marked
44 // with a state (=ZFREE_COMMON) in position ZPAGE_STATE_POS in page
45 // header. This state indicates that the page is the leading or last page
46 // in a chunk of free pages. Furthermore the leading and last page is
47 // also marked with a reference to the leading (=ZPAGE_FIRST_CLUST_POS)
48 // and the last page (=ZPAGE_LAST_CLUST_POS) in the chunk.
49 //
50 // The aim of these data structures is to enable a free area handling of
51 // free pages based on a buddy algorithm. When allocating pages it is
52 // performed in chunks of pages and the algorithm tries to make the
53 // chunks as large as possible.
54 // This manager is invoked when fragments lack internal page space to
55 // accomodate all the data they are requested to store. It is also
56 // invoked when fragments deallocate page space back to the free area.
57 //
58 // The following routines are part of the external interface:
59 // void
60 // allocConsPages(Uint32 noOfPagesToAllocate, #In
61 // Uint32& noOfPagesAllocated, #Out
62 // Uint32& retPageRef) #Out
63 // void
64 // returnCommonArea(Uint32 retPageRef, #In
65 // Uint32 retNoPages) #In
66 //
67 // allocConsPages tries to allocate noOfPagesToAllocate pages in one chunk.
68 // If this fails it delivers a chunk as large as possible. It returns the
69 // i-value of the first page in the chunk delivered, if zero pages returned
70 // this i-value is undefined. It also returns the size of the chunk actually
71 // delivered.
72 //
73 // returnCommonArea is used when somebody is returning pages to the free area.
74 // It is used both from internal routines and external routines.
75 //
76 // The following routines are private routines used to support the
77 // above external interface:
78 // removeCommonArea()
79 // insertCommonArea()
80 // findFreeLeftNeighbours()
81 // findFreeRightNeighbours()
82 // Uint32
83 // nextHigherTwoLog(Uint32 input)
84 //
85 // nextHigherTwoLog is a support routine which is a mathematical function with
86 // an integer as input and an integer as output. It calculates the 2-log of
87 // (input + 1). If the 2-log of (input + 1) is larger than 15 then the routine
88 // will return 15. It is part of the external interface since it is also used
89 // by other similar memory management algorithms.
90 //
91 // External dependencies:
92 // None.
93 //
94 // Side Effects:
95 // Apart from the above mentioned data structures there are no more
96 // side effects other than through the subroutine parameters in the
97 // external interface.
98 //
99 /* ---------------------------------------------------------------- */
100 
101 /* ---------------------------------------------------------------- */
102 /* CALCULATE THE 2-LOG + 1 OF TMP AND PUT RESULT INTO TBITS */
103 /* ---------------------------------------------------------------- */
104 Uint32 Dbtup::nextHigherTwoLog(Uint32 input)
105 {
106  input = input | (input >> 8);
107  input = input | (input >> 4);
108  input = input | (input >> 2);
109  input = input | (input >> 1);
110  Uint32 output = (input & 0x5555) + ((input >> 1) & 0x5555);
111  output = (output & 0x3333) + ((output >> 2) & 0x3333);
112  output = output + (output >> 4);
113  output = (output & 0xf) + ((output >> 8) & 0xf);
114  return output;
115 }//nextHigherTwoLog()
116 
117 void Dbtup::initializePage()
118 {
119 }//Dbtup::initializePage()
120 
121 void Dbtup::allocConsPages(Uint32 noOfPagesToAllocate,
122  Uint32& noOfPagesAllocated,
123  Uint32& allocPageRef)
124 {
125  if (noOfPagesToAllocate == 0){
126  jam();
127  noOfPagesAllocated = 0;
128  return;
129  }//if
130 
131  m_ctx.m_mm.alloc_pages(RT_DBTUP_PAGE, &allocPageRef,
132  &noOfPagesToAllocate, 1);
133  noOfPagesAllocated = noOfPagesToAllocate;
134 
135  // Count number of allocated pages
136  m_pages_allocated += noOfPagesAllocated;
137  if (m_pages_allocated > m_pages_allocated_max)
138  m_pages_allocated_max = m_pages_allocated;
139 
140  return;
141 }//allocConsPages()
142 
143 void Dbtup::returnCommonArea(Uint32 retPageRef, Uint32 retNo)
144 {
145  m_ctx.m_mm.release_pages(RT_DBTUP_PAGE, retPageRef, retNo);
146 
147  // Count number of allocated pages
148  m_pages_allocated -= retNo;
149 
150 }//Dbtup::returnCommonArea()
151