1
/*****************************************************************************
3
Copyright (c) 1996, 2013, Oracle and/or its affiliates. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc.,
15
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/dyn0dyn.h
21
The dynamically allocated array
23
Created 2/5/1996 Heikki Tuuri
24
*******************************************************/
33
/** A block in a dynamically allocated array */
34
typedef struct dyn_block_struct dyn_block_t;
35
/** Dynamically allocated array */
36
typedef dyn_block_t dyn_array_t;
39
/** This is the initial 'payload' size of a dynamic array;
40
this must be > MLOG_BUF_MARGIN + 30! */
41
#define DYN_ARRAY_DATA_SIZE 512
43
/*********************************************************************//**
44
Initializes a dynamic array.
45
@return initialized dyn array */
50
dyn_array_t* arr) /*!< in/out memory buffer of
51
size sizeof(dyn_array_t) */
52
__attribute__((nonnull));
53
/************************************************************//**
54
Frees a dynamic array. */
59
dyn_array_t* arr) /*!< in,own: dyn array */
60
__attribute__((nonnull));
61
/*********************************************************************//**
62
Makes room on top of a dyn array and returns a pointer to a buffer in it.
63
After copying the elements, the caller must close the buffer using
65
@return pointer to the buffer */
70
dyn_array_t* arr, /*!< in: dynamic array */
71
ulint size) /*!< in: size in bytes of the buffer; MUST be
72
smaller than DYN_ARRAY_DATA_SIZE! */
73
__attribute__((nonnull, warn_unused_result));
74
/*********************************************************************//**
75
Closes the buffer returned by dyn_array_open. */
80
dyn_array_t* arr, /*!< in: dynamic array */
81
const byte* ptr) /*!< in: end of used space */
82
__attribute__((nonnull));
83
/*********************************************************************//**
84
Makes room on top of a dyn array and returns a pointer to
85
the added element. The caller must copy the element to
87
@return pointer to the element */
92
dyn_array_t* arr, /*!< in/out: dynamic array */
93
ulint size) /*!< in: size in bytes of the element */
94
__attribute__((nonnull, warn_unused_result));
95
/************************************************************//**
96
Returns pointer to an element in dyn array.
97
@return pointer to element */
100
dyn_array_get_element(
101
/*==================*/
102
const dyn_array_t* arr, /*!< in: dyn array */
103
ulint pos) /*!< in: position of element
104
in bytes from array start */
105
__attribute__((nonnull, warn_unused_result));
106
/************************************************************//**
107
Returns the size of stored data in a dyn array.
108
@return data size in bytes */
111
dyn_array_get_data_size(
112
/*====================*/
113
const dyn_array_t* arr) /*!< in: dyn array */
114
__attribute__((nonnull, warn_unused_result, pure));
115
/************************************************************//**
116
Gets the first block in a dyn array.
118
@return first block */
119
#define dyn_array_get_first_block(arr) (arr)
120
/************************************************************//**
121
Gets the last block in a dyn array.
123
@return last block */
124
#define dyn_array_get_last_block(arr) \
125
((arr)->heap ? UT_LIST_GET_LAST((arr)->base) : (arr))
126
/********************************************************************//**
127
Gets the next block in a dyn array.
129
@param block dyn array block
130
@return pointer to next, NULL if end of list */
131
#define dyn_array_get_next_block(arr, block) \
132
((arr)->heap ? UT_LIST_GET_NEXT(list, block) : NULL)
133
/********************************************************************//**
134
Gets the previous block in a dyn array.
136
@param block dyn array block
137
@return pointer to previous, NULL if end of list */
138
#define dyn_array_get_prev_block(arr, block) \
139
((arr)->heap ? UT_LIST_GET_PREV(list, block) : NULL)
140
/********************************************************************//**
141
Gets the number of used bytes in a dyn array block.
142
@return number of bytes used */
147
const dyn_block_t* block) /*!< in: dyn array block */
148
__attribute__((nonnull, warn_unused_result, pure));
149
/********************************************************************//**
150
Gets pointer to the start of data in a dyn array block.
151
@return pointer to data */
156
const dyn_block_t* block) /*!< in: dyn array block */
157
__attribute__((nonnull, warn_unused_result, pure));
158
/********************************************************//**
159
Pushes n bytes to a dyn array. */
164
dyn_array_t* arr, /*!< in/out: dyn array */
165
const byte* str, /*!< in: string to write */
166
ulint len) /*!< in: string length */
167
__attribute__((nonnull));
169
/*#################################################################*/
171
/** @brief A block in a dynamically allocated array.
172
NOTE! Do not access the fields of the struct directly: the definition
173
appears here only for the compiler to know its size! */
174
struct dyn_block_struct{
175
mem_heap_t* heap; /*!< in the first block this is != NULL
176
if dynamic allocation has been needed */
177
ulint used; /*!< number of data bytes used in this block;
178
DYN_BLOCK_FULL_FLAG is set when the block
180
byte data[DYN_ARRAY_DATA_SIZE];
181
/*!< storage for array elements */
182
UT_LIST_BASE_NODE_T(dyn_block_t) base;
183
/*!< linear list of dyn blocks: this node is
184
used only in the first block */
185
UT_LIST_NODE_T(dyn_block_t) list;
186
/*!< linear list node: used in all blocks */
188
ulint buf_end;/*!< only in the debug version: if dyn
189
array is opened, this is the buffer
190
end offset, else this is 0 */
191
ulint magic_n;/*!< magic number (DYN_BLOCK_MAGIC_N) */
197
#include "dyn0dyn.ic"