~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/ndbapi/ObjectMap.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#ifndef NDB_OBJECT_ID_MAP_HPP
 
17
#define NDB_OBJECT_ID_MAP_HPP
 
18
 
 
19
#include <ndb_global.h>
 
20
//#include <NdbMutex.h>
 
21
#include <NdbOut.hpp>
 
22
 
 
23
#include <EventLogger.hpp>
 
24
extern EventLogger g_eventLogger;
 
25
 
 
26
//#define DEBUG_OBJECTMAP
 
27
 
 
28
/**
 
29
  * Global ObjectMap
 
30
  */
 
31
class NdbObjectIdMap //: NdbLockable
 
32
{
 
33
public:
 
34
  STATIC_CONST( InvalidId = ~(Uint32)0 );
 
35
  NdbObjectIdMap(NdbMutex*, Uint32 initalSize = 128, Uint32 expandSize = 10);
 
36
  ~NdbObjectIdMap();
 
37
 
 
38
  Uint32 map(void * object);
 
39
  void * unmap(Uint32 id, void *object);
 
40
  
 
41
  void * getObject(Uint32 id);
 
42
private:
 
43
  Uint32 m_size;
 
44
  Uint32 m_expandSize;
 
45
  Uint32 m_firstFree;
 
46
  union MapEntry {
 
47
     Uint32 m_next;
 
48
     void * m_obj;
 
49
  } * m_map;
 
50
 
 
51
  NdbMutex * m_mutex;
 
52
  int expand(Uint32 newSize);
 
53
};
 
54
 
 
55
inline
 
56
Uint32
 
57
NdbObjectIdMap::map(void * object){
 
58
  
 
59
  //  lock();
 
60
  
 
61
  if(m_firstFree == InvalidId && expand(m_expandSize))
 
62
    return InvalidId;
 
63
  
 
64
  Uint32 ff = m_firstFree;
 
65
  m_firstFree = m_map[ff].m_next;
 
66
  m_map[ff].m_obj = object;
 
67
  
 
68
  //  unlock();
 
69
  
 
70
  DBUG_PRINT("info",("NdbObjectIdMap::map(0x%lx) %u", (long) object, ff<<2));
 
71
 
 
72
  return ff<<2;
 
73
}
 
74
 
 
75
inline
 
76
void *
 
77
NdbObjectIdMap::unmap(Uint32 id, void *object){
 
78
 
 
79
  Uint32 i = id>>2;
 
80
 
 
81
  //  lock();
 
82
  if(i < m_size){
 
83
    void * obj = m_map[i].m_obj;
 
84
    if (object == obj) {
 
85
      m_map[i].m_next = m_firstFree;
 
86
      m_firstFree = i;
 
87
    } else {
 
88
      g_eventLogger.error("NdbObjectIdMap::unmap(%u, 0x%x) obj=0x%x",
 
89
                          id, (long) object, (long) obj);
 
90
      DBUG_PRINT("error",("NdbObjectIdMap::unmap(%u, 0x%lx) obj=0x%lx",
 
91
                          id, (long) object, (long) obj));
 
92
      return 0;
 
93
    }
 
94
    
 
95
    //  unlock();
 
96
    
 
97
    DBUG_PRINT("info",("NdbObjectIdMap::unmap(%u) obj=0x%lx", id, (long) obj));
 
98
    
 
99
    return obj;
 
100
  }
 
101
  return 0;
 
102
}
 
103
 
 
104
inline void *
 
105
NdbObjectIdMap::getObject(Uint32 id){
 
106
  // DBUG_PRINT("info",("NdbObjectIdMap::getObject(%u) obj=0x%x", id,  m_map[id>>2].m_obj));
 
107
  id >>= 2;
 
108
  if(id < m_size){
 
109
    return m_map[id].m_obj;
 
110
  }
 
111
  return 0;
 
112
}
 
113
#endif