1 /*
2    Copyright (c) 2006, 2021, Oracle and/or its affiliates.
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, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef DYNARR256_HPP
26 #define DYNARR256_HPP
27 
28 #include "Pool.hpp"
29 #include <NdbMutex.h>
30 
31 #define JAM_FILE_ID 299
32 
33 
34 class DynArr256;
35 struct DA256Page;
36 
37 class DynArr256Pool
38 {
39   friend class DynArr256;
40 public:
41   DynArr256Pool();
42 
43   void init(Uint32 type_id, const Pool_context& pc);
44   void init(NdbMutex*, Uint32 type_id, const Pool_context& pc);
45 
46   /**
47     Memory usage data, used for populating Ndbinfo::pool_entry structures.
48    */
49   struct Info
50   {
51     // Number of pages (DA256Page) allocated.
52     Uint32 pg_count;
53     // Size of each page in bytes.
54     Uint32 pg_byte_sz;
55     // Number of nodes (DA256Node) in use.
56     Uint64 inuse_nodes;
57     // Size of each DA256Node in bytes.
58     Uint32 node_byte_sz;
59     // Number of nodes that fit in a page.
60     Uint32 nodes_per_page;
61   };
62 
63   const Info getInfo() const;
64 
getUsed()65   Uint32 getUsed()   { return m_used; }  // # entries currently seized
getUsedHi()66   Uint32 getUsedHi() { return m_usedHi;} // high water mark for getUsed()
67 
68 protected:
69   Uint32 m_type_id;
70   Uint32 m_first_free;
71   Uint32 m_last_free;
72   Pool_context m_ctx;
73   struct DA256Page* m_memroot;
74   NdbMutex * m_mutex;
75   // Number of nodes (DA256Node) in use.
76   Uint64 m_inuse_nodes;
77   // Number of pages (DA256Page) allocated.
78   Uint32 m_pg_count;
79   Uint32 m_used;
80   Uint32 m_usedHi;
81 
82 private:
83   Uint32 seize();
84   void release(Uint32);
85 };
86 
87 class DynArr256
88 {
89 public:
90   class Head
91   {
92     friend class DynArr256;
93   public:
94 #if defined VM_TRACE || defined ERROR_INSERT
Head()95     Head() { m_ptr_i = RNIL; m_sz = 0; m_no_of_nodes = 0; m_high_pos = 0; }
96 #else
97     Head() { m_ptr_i = RNIL; m_sz = 0; m_no_of_nodes = 0; }
98 #endif
~Head()99     ~Head()
100     {
101       assert(m_sz == 0);
102       assert(m_no_of_nodes == 0);
103     }
104 
isEmpty() const105     bool isEmpty() const { return m_sz == 0;}
106 
107     // Get allocated array size in bytes.
108     Uint32 getByteSize() const;
109 
110   private:
111     Uint32 m_ptr_i;
112     Uint32 m_sz;
113     // Number of DA256Nodes allocated.
114     Int32 m_no_of_nodes;
115 #if defined VM_TRACE || defined ERROR_INSERT
116     Uint32 m_high_pos;
117 #endif
118   };
119 
DynArr256(DynArr256Pool & pool,Head & head)120   DynArr256(DynArr256Pool & pool, Head& head) :
121     m_head(head), m_pool(pool){}
122 
123   Uint32* set(Uint32 pos);
124   Uint32* get(Uint32 pos) const ;
125   Uint32* get_dirty(Uint32 pos) const ;
126 
127   struct ReleaseIterator
128   {
129     Uint32 m_sz;
130     Uint32 m_pos;
131     Uint32 m_ptr_i[5];
132   };
133 
134   void init(ReleaseIterator&);
135   /**
136    * return 0 - done
137    *        1 - data (in retptr)
138    *        2 - nodata
139    */
140   Uint32 release(ReleaseIterator&, Uint32* retptr);
141   Uint32 trim(Uint32 trim_pos, ReleaseIterator&);
142   Uint32 truncate(Uint32 trunc_pos, ReleaseIterator&, Uint32* retptr);
143 protected:
144   Head & m_head;
145   DynArr256Pool & m_pool;
146 
147   bool expand(Uint32 pos);
148   void handle_invalid_ptr(Uint32 pos, Uint32 ptrI, Uint32 p0);
149 };
150 
151 inline
release(ReleaseIterator & iter,Uint32 * retptr)152 Uint32 DynArr256::release(ReleaseIterator& iter, Uint32* retptr)
153 {
154   return truncate(0, iter, retptr);
155 }
156 
157 inline
trim(Uint32 pos,ReleaseIterator & iter)158 Uint32 DynArr256::trim(Uint32 pos, ReleaseIterator& iter)
159 {
160   return truncate(pos, iter, NULL);
161 }
162 
163 inline
get(Uint32 pos) const164 Uint32 * DynArr256::get(Uint32 pos) const
165 {
166 #if defined VM_TRACE || defined ERROR_INSERT
167   // In debug this function will abort if used
168   // with pos not already mapped by call to set.
169   // Use get_dirty if return NULL is wanted instead.
170   require((m_head.m_sz > 0) && (pos <= m_head.m_high_pos));
171 #endif
172   return get_dirty(pos);
173 }
174 
175 #undef JAM_FILE_ID
176 
177 #endif
178