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, version 2.0,
7 as published by the Free Software Foundation.
8
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "ObjectMap.hpp"
27
NdbObjectIdMap(Uint32 sz,Uint32 eSz)28 NdbObjectIdMap::NdbObjectIdMap(Uint32 sz, Uint32 eSz):
29 m_expandSize(eSz),
30 m_size(0),
31 m_firstFree(InvalidId),
32 m_lastFree(InvalidId),
33 m_map(0)
34 {
35 expand(sz);
36 #ifdef DEBUG_OBJECTMAP
37 ndbout_c("NdbObjectIdMap:::NdbObjectIdMap(%u)", sz);
38 #endif
39 }
40
~NdbObjectIdMap()41 NdbObjectIdMap::~NdbObjectIdMap()
42 {
43 assert(checkConsistency());
44 free(m_map);
45 m_map = NULL;
46 }
47
expand(Uint32 incSize)48 int NdbObjectIdMap::expand(Uint32 incSize)
49 {
50 assert(checkConsistency());
51 Uint32 newSize = m_size + incSize;
52 MapEntry * tmp = (MapEntry*)realloc(m_map, newSize * sizeof(MapEntry));
53
54 if (likely(tmp != 0))
55 {
56 m_map = tmp;
57
58 for(Uint32 i = m_size; i < newSize-1; i++)
59 {
60 m_map[i].setNext(i+1);
61 }
62 m_firstFree = m_size;
63 m_lastFree = newSize - 1;
64 m_map[newSize-1].setNext(InvalidId);
65 m_size = newSize;
66 assert(checkConsistency());
67 }
68 else
69 {
70 g_eventLogger->error("NdbObjectIdMap::expand: realloc(%u*%lu) failed",
71 newSize, sizeof(MapEntry));
72 return -1;
73 }
74 return 0;
75 }
76
checkConsistency()77 bool NdbObjectIdMap::checkConsistency()
78 {
79 if (m_firstFree == InvalidId)
80 {
81 for (Uint32 i = 0; i<m_size; i++)
82 {
83 if (m_map[i].isFree())
84 {
85 assert(false);
86 return false;
87 }
88 }
89 return true;
90 }
91
92 Uint32 i = m_firstFree;
93 while (m_map[i].getNext() != InvalidId)
94 {
95 i = m_map[i].getNext();
96 }
97 assert(i == m_lastFree);
98 return i == m_lastFree;
99 }
100