MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ObjectMap.cpp
1 /*
2  Copyright (C) 2007, 2008 MySQL AB, 2008 Sun Microsystems, Inc.
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 #include "ObjectMap.hpp"
20 
21 NdbObjectIdMap::NdbObjectIdMap(Uint32 sz, Uint32 eSz):
22  m_expandSize(eSz),
23  m_size(0),
24  m_firstFree(InvalidId),
25  m_lastFree(InvalidId),
26  m_map(0)
27 {
28  expand(sz);
29 #ifdef DEBUG_OBJECTMAP
30  ndbout_c("NdbObjectIdMap:::NdbObjectIdMap(%u)", sz);
31 #endif
32 }
33 
34 NdbObjectIdMap::~NdbObjectIdMap()
35 {
36  assert(checkConsistency());
37  free(m_map);
38  m_map = NULL;
39 }
40 
41 int NdbObjectIdMap::expand(Uint32 incSize)
42 {
43  assert(checkConsistency());
44  Uint32 newSize = m_size + incSize;
45  MapEntry * tmp = (MapEntry*)realloc(m_map, newSize * sizeof(MapEntry));
46 
47  if (likely(tmp != 0))
48  {
49  m_map = tmp;
50 
51  for(Uint32 i = m_size; i < newSize-1; i++)
52  {
53  m_map[i].setNext(i+1);
54  }
55  m_firstFree = m_size;
56  m_lastFree = newSize - 1;
57  m_map[newSize-1].setNext(InvalidId);
58  m_size = newSize;
59  assert(checkConsistency());
60  }
61  else
62  {
63  g_eventLogger->error("NdbObjectIdMap::expand: realloc(%u*%lu) failed",
64  newSize, sizeof(MapEntry));
65  return -1;
66  }
67  return 0;
68 }
69 
70 bool NdbObjectIdMap::checkConsistency()
71 {
72  if (m_firstFree == InvalidId)
73  {
74  for (Uint32 i = 0; i<m_size; i++)
75  {
76  if (m_map[i].isFree())
77  {
78  assert(false);
79  return false;
80  }
81  }
82  return true;
83  }
84 
85  Uint32 i = m_firstFree;
86  while (m_map[i].getNext() != InvalidId)
87  {
88  i = m_map[i].getNext();
89  }
90  assert(i == m_lastFree);
91  return i == m_lastFree;
92 }