~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to storage/ndb/src/kernel/vm/CArray.hpp

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2003, 2005, 2006 MySQL AB
 
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
#ifndef CARRAY_HPP
 
20
#define CARRAY_HPP
 
21
 
 
22
#include "ndbd_malloc.hpp"
 
23
 
 
24
/**
 
25
 * Template class used for implementing an c - array
 
26
 */
 
27
template <class T>
 
28
class CArray {
 
29
public:
 
30
  CArray();
 
31
  ~CArray();
 
32
  
 
33
  /**
 
34
   * Set the size of the pool
 
35
   *
 
36
   * Note, can currently only be called once
 
37
   */
 
38
  bool setSize(Uint32 noOfElements, bool exit_on_error = true);
 
39
 
 
40
  /**
 
41
   * Get size
 
42
   */
 
43
  Uint32 getSize() const;
 
44
  
 
45
  /**
 
46
   * Update p value for ptr according to i value 
 
47
   */
 
48
  void getPtr(Ptr<T> &) const;
 
49
  
 
50
  /**
 
51
   * Get pointer for i value
 
52
   */
 
53
  T * getPtr(Uint32 i) const;
 
54
 
 
55
  /**
 
56
   * Update p & i value for ptr according to <b>i</b> value 
 
57
   */
 
58
  void getPtr(Ptr<T> &, Uint32 i) const;
 
59
 
 
60
private:
 
61
  Uint32 size;
 
62
  T * theArray;
 
63
};
 
64
 
 
65
template <class T>
 
66
inline
 
67
CArray<T>::CArray(){
 
68
  size = 0;
 
69
  theArray = 0;
 
70
}
 
71
 
 
72
template <class T>
 
73
inline
 
74
CArray<T>::~CArray(){
 
75
  if(theArray != 0){
 
76
    ndbd_free(theArray, size * sizeof(T));
 
77
    theArray = 0;
 
78
  }
 
79
}
 
80
 
 
81
/**
 
82
 * Set the size of the pool
 
83
 *
 
84
 * Note, can currently only be called once
 
85
 */
 
86
template <class T>
 
87
inline
 
88
bool
 
89
CArray<T>::setSize(Uint32 noOfElements, bool exit_on_error){
 
90
  if(size == noOfElements)
 
91
    return true;
 
92
  
 
93
  theArray = (T *)ndbd_malloc(noOfElements * sizeof(T));
 
94
  if(theArray == 0)
 
95
  {
 
96
    if (!exit_on_error)
 
97
      return false;
 
98
    ErrorReporter::handleAssert("CArray<T>::setSize malloc failed",
 
99
                                __FILE__, __LINE__, NDBD_EXIT_MEMALLOC);
 
100
    return false; // not reached
 
101
  }
 
102
  size = noOfElements;
 
103
  return true;
 
104
}
 
105
 
 
106
template<class T>
 
107
inline
 
108
Uint32
 
109
CArray<T>::getSize() const {
 
110
  return size;
 
111
}
 
112
 
 
113
template <class T>
 
114
inline
 
115
void
 
116
CArray<T>::getPtr(Ptr<T> & ptr) const {
 
117
  const Uint32 i = ptr.i;
 
118
  if(i < size){
 
119
    ptr.p = &theArray[i];
 
120
    return;
 
121
  } else {
 
122
    ErrorReporter::handleAssert("CArray<T>::getPtr", __FILE__, __LINE__);
 
123
  }
 
124
}
 
125
  
 
126
template <class T>
 
127
inline
 
128
T * 
 
129
CArray<T>::getPtr(Uint32 i) const {
 
130
  if(i < size){
 
131
    return &theArray[i];
 
132
  } else {
 
133
    ErrorReporter::handleAssert("CArray<T>::getPtr", __FILE__, __LINE__);
 
134
    return 0;
 
135
  }
 
136
}
 
137
 
 
138
template <class T>
 
139
inline
 
140
void
 
141
CArray<T>::getPtr(Ptr<T> & ptr, Uint32 i) const {
 
142
  ptr.i = i;
 
143
  if(i < size){
 
144
    ptr.p = &theArray[i];
 
145
    return;
 
146
  } else {
 
147
    ErrorReporter::handleAssert("CArray<T>::getPtr", __FILE__, __LINE__);
 
148
  }
 
149
}
 
150
 
 
151
#endif