1 /* Copyright (c) 2003-2007 MySQL AB
2    Use is subject to license terms
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 Street, Fifth Floor, Boston, MA  02110-1301, USA */
16 
17 #ifndef NDB_OBJECT_ID_MAP_HPP
18 #define NDB_OBJECT_ID_MAP_HPP
19 
20 #include <ndb_global.h>
21 //#include <NdbMutex.h>
22 #include <NdbOut.hpp>
23 
24 #include <EventLogger.hpp>
25 extern EventLogger g_eventLogger;
26 
27 //#define DEBUG_OBJECTMAP
28 
29 /**
30   * Global ObjectMap
31   */
32 class NdbObjectIdMap //: NdbLockable
33 {
34 public:
35   STATIC_CONST( InvalidId = ~(Uint32)0 );
36   NdbObjectIdMap(NdbMutex*, Uint32 initalSize = 128, Uint32 expandSize = 10);
37   ~NdbObjectIdMap();
38 
39   Uint32 map(void * object);
40   void * unmap(Uint32 id, void *object);
41 
42   void * getObject(Uint32 id);
43 private:
44   Uint32 m_size;
45   Uint32 m_expandSize;
46   Uint32 m_firstFree;
47   union MapEntry {
48      Uint32 m_next;
49      void * m_obj;
50   } * m_map;
51 
52   NdbMutex * m_mutex;
53   int expand(Uint32 newSize);
54 };
55 
56 inline
57 Uint32
map(void * object)58 NdbObjectIdMap::map(void * object){
59 
60   //  lock();
61 
62   if(m_firstFree == InvalidId && expand(m_expandSize))
63     return InvalidId;
64 
65   Uint32 ff = m_firstFree;
66   m_firstFree = m_map[ff].m_next;
67   m_map[ff].m_obj = object;
68 
69   //  unlock();
70 
71   DBUG_PRINT("info",("NdbObjectIdMap::map(0x%lx) %u", (long) object, ff<<2));
72 
73   return ff<<2;
74 }
75 
76 inline
77 void *
unmap(Uint32 id,void * object)78 NdbObjectIdMap::unmap(Uint32 id, void *object){
79 
80   Uint32 i = id>>2;
81 
82   //  lock();
83   if(i < m_size){
84     void * obj = m_map[i].m_obj;
85     if (object == obj) {
86       m_map[i].m_next = m_firstFree;
87       m_firstFree = i;
88     } else {
89       g_eventLogger.error("NdbObjectIdMap::unmap(%u, 0x%x) obj=0x%x",
90                           id, (long) object, (long) obj);
91       DBUG_PRINT("error",("NdbObjectIdMap::unmap(%u, 0x%lx) obj=0x%lx",
92                           id, (long) object, (long) obj));
93       return 0;
94     }
95 
96     //  unlock();
97 
98     DBUG_PRINT("info",("NdbObjectIdMap::unmap(%u) obj=0x%lx", id, (long) obj));
99 
100     return obj;
101   }
102   return 0;
103 }
104 
105 inline void *
getObject(Uint32 id)106 NdbObjectIdMap::getObject(Uint32 id){
107   // DBUG_PRINT("info",("NdbObjectIdMap::getObject(%u) obj=0x%x", id,  m_map[id>>2].m_obj));
108   id >>= 2;
109   if(id < m_size){
110     return m_map[id].m_obj;
111   }
112   return 0;
113 }
114 #endif
115