MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndbd_malloc_impl.hpp
1 /*
2  Copyright (c) 2006, 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 #ifndef NDBD_MALLOC_IMPL_H
19 #define NDBD_MALLOC_IMPL_H
20 
21 #include <kernel_types.h>
22 #include <Bitmask.hpp>
23 #include <assert.h>
24 #include "Pool.hpp"
25 #include <Vector.hpp>
26 
31 #define BMW_2LOG 13
32 #define BITMAP_WORDS (1 << BMW_2LOG)
33 
34 #define BPP_2LOG (BMW_2LOG + 5)
35 #define SPACE_PER_BMP_2LOG ((2 + BMW_2LOG) + BPP_2LOG)
36 
37 //#define BITMAP_WORDS GLOBAL_PAGE_SIZE_WORDS
38 
39 struct Alloc_page
40 {
41  Uint32 m_data[BITMAP_WORDS];
42 };
43 
44 struct InitChunk
45 {
46  Uint32 m_cnt;
47  Uint32 m_start;
48  Alloc_page* m_ptr;
49 };
50 
52 {
53  Uint32 m_list;
54  Uint32 m_next;
55  Uint32 m_prev;
56  Uint32 m_size;
57 };
58 
59 #define FPD_2LOG 2
60 
62 {
63 public:
65 
66  void set_resource_limit(const Resource_limit& rl);
67  bool get_resource_limit(Uint32 id, Resource_limit& rl) const;
68 
69  bool init(Uint32 *watchCounter, bool allow_alloc_less_than_requested = true);
70  void map(Uint32 * watchCounter, bool memlock = false, Uint32 resources[] = 0);
71  void* get_memroot() const;
72 
73  void dump() const ;
74 
75  enum AllocZone
76  {
77  NDB_ZONE_LO = 0, // Only allocate with page_id < (1 << 13)
78  NDB_ZONE_ANY = 1 // Allocate with any page_id
79  };
80 
81  void* alloc_page(Uint32 type, Uint32* i, enum AllocZone);
82  void release_page(Uint32 type, Uint32 i);
83 
84  void alloc_pages(Uint32 type, Uint32* i, Uint32 *cnt, Uint32 min = 1);
85  void release_pages(Uint32 type, Uint32 i, Uint32 cnt);
86 
92  static Uint32 ndb_log2(Uint32 size);
93 
94 private:
95  void grow(Uint32 start, Uint32 cnt);
96 
97 #define XX_RL_COUNT 9
98 
101  static Free_page_data* get_free_page_data(Alloc_page*, Uint32 idx);
102  Vector<Uint32> m_used_bitmap_pages;
103 
104  Uint32 m_buddy_lists[2][16];
105  Resource_limit m_resource_limit[XX_RL_COUNT]; // RG_COUNT in record_types.hpp
106  Alloc_page * m_base_page;
107 
108  void release_impl(Uint32 zone, Uint32 start, Uint32 cnt);
109  void insert_free_list(Uint32 zone, Uint32 start, Uint32 cnt);
110  Uint32 remove_free_list(Uint32 zone, Uint32 start, Uint32 list);
111 
112  void set(Uint32 first, Uint32 last);
113  void clear(Uint32 first, Uint32 last);
114  void clear_and_set(Uint32 first, Uint32 last);
115  Uint32 check(Uint32 first, Uint32 last);
116 
117  void alloc(AllocZone, Uint32* ret, Uint32 *pages, Uint32 min_requested);
118  void alloc_impl(Uint32 zone, Uint32* ret, Uint32 *pages, Uint32 min);
119  void release(Uint32 start, Uint32 cnt);
120 
125  Vector<InitChunk> m_unmapped_chunks;
126 };
127 
128 inline
130 Ndbd_mem_manager::get_free_page_data(Alloc_page* ptr, Uint32 idx)
131 {
132  assert(idx & ((1 << BPP_2LOG) - 1));
133  assert((idx & ((1 << BPP_2LOG) - 1)) != ((1 << BPP_2LOG) - 1));
134 
135  return (Free_page_data*)
136  (ptr->m_data + ((idx & ((BITMAP_WORDS >> FPD_2LOG) - 1)) << FPD_2LOG));
137 }
138 
139 inline
140 void
141 Ndbd_mem_manager::set(Uint32 first, Uint32 last)
142 {
143  Alloc_page * ptr = m_base_page;
144 #if ((SPACE_PER_BMP_2LOG < 32) && (SIZEOF_CHARP == 4)) || (SIZEOF_CHARP == 8)
145  Uint32 bmp = first & ~((1 << BPP_2LOG) - 1);
146  assert((first >> BPP_2LOG) == (last >> BPP_2LOG));
147  assert(bmp < m_resource_limit[0].m_resource_id);
148 
149  first -= bmp;
150  last -= bmp;
151  ptr += bmp;
152 #endif
153  BitmaskImpl::set(BITMAP_WORDS, ptr->m_data, first);
154  BitmaskImpl::set(BITMAP_WORDS, ptr->m_data, last);
155 }
156 
157 inline
158 void
159 Ndbd_mem_manager::clear(Uint32 first, Uint32 last)
160 {
161  Alloc_page * ptr = m_base_page;
162 #if ((SPACE_PER_BMP_2LOG < 32) && (SIZEOF_CHARP == 4)) || (SIZEOF_CHARP == 8)
163  Uint32 bmp = first & ~((1 << BPP_2LOG) - 1);
164  assert((first >> BPP_2LOG) == (last >> BPP_2LOG));
165  assert(bmp < m_resource_limit[0].m_resource_id);
166 
167  first -= bmp;
168  last -= bmp;
169  ptr += bmp;
170 #endif
171  BitmaskImpl::clear(BITMAP_WORDS, ptr->m_data, first);
172  BitmaskImpl::clear(BITMAP_WORDS, ptr->m_data, last);
173 }
174 
175 inline
176 void
177 Ndbd_mem_manager::clear_and_set(Uint32 first, Uint32 last)
178 {
179  Alloc_page * ptr = m_base_page;
180 #if ((SPACE_PER_BMP_2LOG < 32) && (SIZEOF_CHARP == 4)) || (SIZEOF_CHARP == 8)
181  Uint32 bmp = first & ~((1 << BPP_2LOG) - 1);
182  assert((first >> BPP_2LOG) == (last >> BPP_2LOG));
183  assert(bmp < m_resource_limit[0].m_resource_id);
184 
185  first -= bmp;
186  last -= bmp;
187  ptr += bmp;
188 #endif
189  BitmaskImpl::clear(BITMAP_WORDS, ptr->m_data, first);
190  BitmaskImpl::clear(BITMAP_WORDS, ptr->m_data, last);
191  BitmaskImpl::set(BITMAP_WORDS, ptr->m_data, last+1);
192 }
193 
194 inline
195 Uint32
196 Ndbd_mem_manager::check(Uint32 first, Uint32 last)
197 {
198  Uint32 ret = 0;
199  Alloc_page * ptr = m_base_page;
200 #if ((SPACE_PER_BMP_2LOG < 32) && (SIZEOF_CHARP == 4)) || (SIZEOF_CHARP == 8)
201  Uint32 bmp = first & ~((1 << BPP_2LOG) - 1);
202  assert((first >> BPP_2LOG) == (last >> BPP_2LOG));
203  assert(bmp < m_resource_limit[0].m_resource_id);
204 
205  first -= bmp;
206  last -= bmp;
207  ptr += bmp;
208 #endif
209  ret |= BitmaskImpl::get(BITMAP_WORDS, ptr->m_data, first) << 0;
210  ret |= BitmaskImpl::get(BITMAP_WORDS, ptr->m_data, last) << 1;
211  return ret;
212 }
213 
214 
215 #endif