mem0mem.h 14.4 KB
Newer Older
1 2
/*****************************************************************************

3
Copyright (c) 1994, 2010, Innobase Oy. All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

*****************************************************************************/

19 20
/**************************************************//**
@file include/mem0mem.h
osku's avatar
osku committed
21 22 23 24 25 26 27 28 29 30 31 32
The memory management

Created 6/9/1994 Heikki Tuuri
*******************************************************/

#ifndef mem0mem_h
#define mem0mem_h

#include "univ.i"
#include "ut0mem.h"
#include "ut0byte.h"
#include "ut0rnd.h"
33 34 35
#ifndef UNIV_HOTBACKUP
# include "sync0sync.h"
#endif /* UNIV_HOTBACKUP */
osku's avatar
osku committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "ut0lst.h"
#include "mach0data.h"

/* -------------------- MEMORY HEAPS ----------------------------- */

/* The info structure stored at the beginning of a heap block */
typedef struct mem_block_info_struct mem_block_info_t;

/* A block of a memory heap consists of the info structure
followed by an area of memory */
typedef mem_block_info_t	mem_block_t;

/* A memory heap is a nonempty linear list of memory blocks */
typedef mem_block_t	mem_heap_t;

/* Types of allocation for memory heaps: DYNAMIC means allocation from the
52 53
dynamic memory pool of the C compiler, BUFFER means allocation from the
buffer pool; the latter method is used for very big heaps */
osku's avatar
osku committed
54 55 56

#define MEM_HEAP_DYNAMIC	0	/* the most common type */
#define MEM_HEAP_BUFFER		1
57 58 59 60 61 62 63
#define MEM_HEAP_BTR_SEARCH	2	/* this flag can optionally be
					ORed to MEM_HEAP_BUFFER, in which
					case heap->free_block is used in
					some cases for memory allocations,
					and if it's NULL, the memory
					allocation functions can return
					NULL. */
osku's avatar
osku committed
64 65 66 67 68 69

/* The following start size is used for the first block in the memory heap if
the size is not specified, i.e., 0 is given as the parameter in the call of
create. The standard size is the maximum (payload) size of the blocks used for
allocations of small buffers. */

70
#define MEM_BLOCK_START_SIZE		64
71 72
#define MEM_BLOCK_STANDARD_SIZE		\
	(UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF)
osku's avatar
osku committed
73 74 75 76 77

/* If a memory heap is allowed to grow into the buffer pool, the following
is the maximum size for a single allocated buffer: */
#define MEM_MAX_ALLOC_IN_BUF		(UNIV_PAGE_SIZE - 200)

78
/******************************************************************//**
osku's avatar
osku committed
79
Initializes the memory system. */
80
UNIV_INTERN
osku's avatar
osku committed
81 82 83
void
mem_init(
/*=====*/
84
	ulint	size);	/*!< in: common pool size in bytes */
85 86 87 88 89 90 91
/******************************************************************//**
Closes the memory system. */
UNIV_INTERN
void
mem_close(void);
/*===========*/

92
/**************************************************************//**
osku's avatar
osku committed
93 94 95
Use this macro instead of the corresponding function! Macro for memory
heap creation. */

96
#define mem_heap_create(N)	mem_heap_create_func(\
97
		(N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__)
98
/**************************************************************//**
osku's avatar
osku committed
99 100 101 102
Use this macro instead of the corresponding function! Macro for memory
heap creation. */

#define mem_heap_create_in_buffer(N)	mem_heap_create_func(\
103
		(N), MEM_HEAP_BUFFER, __FILE__, __LINE__)
104
/**************************************************************//**
osku's avatar
osku committed
105 106 107
Use this macro instead of the corresponding function! Macro for memory
heap creation. */

108
#define mem_heap_create_in_btr_search(N)	mem_heap_create_func(\
109
		(N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\
110
		__FILE__, __LINE__)
osku's avatar
osku committed
111

112
/**************************************************************//**
osku's avatar
osku committed
113 114 115 116 117
Use this macro instead of the corresponding function! Macro for memory
heap freeing. */

#define mem_heap_free(heap) mem_heap_free_func(\
					  (heap), __FILE__, __LINE__)
118
/*****************************************************************//**
osku's avatar
osku committed
119
NOTE: Use the corresponding macros instead of this function. Creates a
120
memory heap. For debugging purposes, takes also the file name and line as
121
arguments.
122 123
@return own: memory heap, NULL if did not succeed (only possible for
MEM_HEAP_BTR_SEARCH type heaps) */
osku's avatar
osku committed
124 125 126 127
UNIV_INLINE
mem_heap_t*
mem_heap_create_func(
/*=================*/
128
	ulint		n,		/*!< in: desired start block size,
osku's avatar
osku committed
129
					this means that a single user buffer
130
					of size n will fit in the block,
131
					0 creates a default size block */
132 133 134
	ulint		type,		/*!< in: heap type */
	const char*	file_name,	/*!< in: file name where created */
	ulint		line);		/*!< in: line where created */
135
/*****************************************************************//**
osku's avatar
osku committed
136 137 138 139 140 141 142
NOTE: Use the corresponding macro instead of this function. Frees the space
occupied by a memory heap. In the debug version erases the heap memory
blocks. */
UNIV_INLINE
void
mem_heap_free_func(
/*===============*/
143 144 145
	mem_heap_t*	heap,		/*!< in, own: heap to be freed */
	const char*	file_name,	/*!< in: file name where freed */
	ulint		line);		/*!< in: line where freed */
146
/***************************************************************//**
147 148
Allocates and zero-fills n bytes of memory from a memory heap.
@return	allocated, zero-filled storage */
149 150
UNIV_INLINE
void*
151
mem_heap_zalloc(
152
/*============*/
153 154
	mem_heap_t*	heap,	/*!< in: memory heap */
	ulint		n);	/*!< in: number of bytes; if the heap is allowed
155 156
				to grow into the buffer pool, this must be
				<= MEM_MAX_ALLOC_IN_BUF */
157
/***************************************************************//**
158
Allocates n bytes of memory from a memory heap.
159 160
@return allocated storage, NULL if did not succeed (only possible for
MEM_HEAP_BTR_SEARCH type heaps) */
osku's avatar
osku committed
161 162 163 164
UNIV_INLINE
void*
mem_heap_alloc(
/*===========*/
165 166
	mem_heap_t*	heap,	/*!< in: memory heap */
	ulint		n);	/*!< in: number of bytes; if the heap is allowed
osku's avatar
osku committed
167 168
				to grow into the buffer pool, this must be
				<= MEM_MAX_ALLOC_IN_BUF */
169
/*****************************************************************//**
170 171
Returns a pointer to the heap top.
@return	pointer to the heap top */
osku's avatar
osku committed
172 173 174
UNIV_INLINE
byte*
mem_heap_get_heap_top(
175
/*==================*/
176
	mem_heap_t*	heap);	/*!< in: memory heap */
177
/*****************************************************************//**
osku's avatar
osku committed
178 179 180 181 182 183 184
Frees the space in a memory heap exceeding the pointer given. The
pointer must have been acquired from mem_heap_get_heap_top. The first
memory block of the heap is not freed. */
UNIV_INLINE
void
mem_heap_free_heap_top(
/*===================*/
185 186
	mem_heap_t*	heap,	/*!< in: heap from which to free */
	byte*		old_top);/*!< in: pointer to old top of heap */
187
/*****************************************************************//**
osku's avatar
osku committed
188 189 190 191 192
Empties a memory heap. The first memory block of the heap is not freed. */
UNIV_INLINE
void
mem_heap_empty(
/*===========*/
193
	mem_heap_t*	heap);	/*!< in: heap to empty */
194
/*****************************************************************//**
osku's avatar
osku committed
195
Returns a pointer to the topmost element in a memory heap.
196 197
The size of the element must be given.
@return	pointer to the topmost element */
osku's avatar
osku committed
198 199 200
UNIV_INLINE
void*
mem_heap_get_top(
201
/*=============*/
202 203
	mem_heap_t*	heap,	/*!< in: memory heap */
	ulint		n);	/*!< in: size of the topmost element */
204
/*****************************************************************//**
osku's avatar
osku committed
205 206 207 208 209
Frees the topmost element in a memory heap.
The size of the element must be given. */
UNIV_INLINE
void
mem_heap_free_top(
210
/*==============*/
211 212
	mem_heap_t*	heap,	/*!< in: memory heap */
	ulint		n);	/*!< in: size of the topmost element */
213
/*****************************************************************//**
osku's avatar
osku committed
214 215 216 217 218
Returns the space in bytes occupied by a memory heap. */
UNIV_INLINE
ulint
mem_heap_get_size(
/*==============*/
219
	mem_heap_t*	heap);		/*!< in: heap */
220
/**************************************************************//**
osku's avatar
osku committed
221 222 223
Use this macro instead of the corresponding function!
Macro for memory buffer allocation */

224
#define mem_zalloc(N)	memset(mem_alloc(N), 0, (N));
225

226 227
#define mem_alloc(N)	mem_alloc_func((N), NULL, __FILE__, __LINE__)
#define mem_alloc2(N,S)	mem_alloc_func((N), (S), __FILE__, __LINE__)
228
/***************************************************************//**
osku's avatar
osku committed
229 230
NOTE: Use the corresponding macro instead of this function.
Allocates a single buffer of memory from the dynamic memory of
231
the C compiler. Is like malloc of C. The buffer must be freed
232 233
with mem_free.
@return	own: free storage */
osku's avatar
osku committed
234 235 236 237
UNIV_INLINE
void*
mem_alloc_func(
/*===========*/
238 239
	ulint		n,		/*!< in: requested size in bytes */
	ulint*		size,		/*!< out: allocated size in bytes,
240
					or NULL */
241 242
	const char*	file_name,	/*!< in: file name where created */
	ulint		line);		/*!< in: line where created */
243

244
/**************************************************************//**
osku's avatar
osku committed
245 246 247
Use this macro instead of the corresponding function!
Macro for memory buffer freeing */

248
#define mem_free(PTR)	mem_free_func((PTR), __FILE__, __LINE__)
249
/***************************************************************//**
osku's avatar
osku committed
250 251 252 253 254 255 256
NOTE: Use the corresponding macro instead of this function.
Frees a single buffer of storage from
the dynamic memory of C compiler. Similar to free of C. */
UNIV_INLINE
void
mem_free_func(
/*==========*/
257 258 259
	void*		ptr,		/*!< in, own: buffer to be freed */
	const char*	file_name,	/*!< in: file name where created */
	ulint		line);		/*!< in: line where created */
osku's avatar
osku committed
260

261
/**********************************************************************//**
262 263
Duplicates a NUL-terminated string.
@return	own: a copy of the string, must be deallocated with mem_free */
osku's avatar
osku committed
264 265 266 267
UNIV_INLINE
char*
mem_strdup(
/*=======*/
268
	const char*	str);	/*!< in: string to be copied */
269
/**********************************************************************//**
270 271
Makes a NUL-terminated copy of a nonterminated string.
@return	own: a copy of the string, must be deallocated with mem_free */
osku's avatar
osku committed
272 273 274 275
UNIV_INLINE
char*
mem_strdupl(
/*========*/
276 277
	const char*	str,	/*!< in: string to be copied */
	ulint		len);	/*!< in: length of str, in bytes */
osku's avatar
osku committed
278

279
/**********************************************************************//**
280 281
Duplicates a NUL-terminated string, allocated from a memory heap.
@return	own: a copy of the string */
282
UNIV_INTERN
osku's avatar
osku committed
283 284 285
char*
mem_heap_strdup(
/*============*/
286 287
	mem_heap_t*	heap,	/*!< in: memory heap where string is allocated */
	const char*	str);	/*!< in: string to be copied */
288
/**********************************************************************//**
osku's avatar
osku committed
289
Makes a NUL-terminated copy of a nonterminated string,
290 291
allocated from a memory heap.
@return	own: a copy of the string */
osku's avatar
osku committed
292 293 294 295
UNIV_INLINE
char*
mem_heap_strdupl(
/*=============*/
296 297 298
	mem_heap_t*	heap,	/*!< in: memory heap where string is allocated */
	const char*	str,	/*!< in: string to be copied */
	ulint		len);	/*!< in: length of str, in bytes */
osku's avatar
osku committed
299

300
/**********************************************************************//**
301 302
Concatenate two strings and return the result, using a memory heap.
@return	own: the result */
303
UNIV_INTERN
304 305 306
char*
mem_heap_strcat(
/*============*/
307 308 309
	mem_heap_t*	heap,	/*!< in: memory heap where string is allocated */
	const char*	s1,	/*!< in: string 1 */
	const char*	s2);	/*!< in: string 2 */
310

311
/**********************************************************************//**
312 313
Duplicate a block of data, allocated from a memory heap.
@return	own: a copy of the data */
314
UNIV_INTERN
315 316 317
void*
mem_heap_dup(
/*=========*/
318 319 320
	mem_heap_t*	heap,	/*!< in: memory heap where copy is allocated */
	const void*	data,	/*!< in: data to be copied */
	ulint		len);	/*!< in: length of data, in bytes */
321

322
/****************************************************************//**
323 324 325
A simple (s)printf replacement that dynamically allocates the space for the
formatted string from the given heap. This supports a very limited set of
the printf syntax: types 's' and 'u' and length modifier 'l' (which is
326 327
required for the 'u' type).
@return	heap-allocated formatted string */
328
UNIV_INTERN
329 330 331
char*
mem_heap_printf(
/*============*/
332 333
	mem_heap_t*	heap,	/*!< in: memory heap */
	const char*	format,	/*!< in: format string */
334 335
	...) __attribute__ ((format (printf, 2, 3)));

osku's avatar
osku committed
336
#ifdef MEM_PERIODIC_CHECK
337
/******************************************************************//**
osku's avatar
osku committed
338 339
Goes through the list of all allocated mem blocks, checks their magic
numbers, and reports possible corruption. */
340
UNIV_INTERN
osku's avatar
osku committed
341 342 343 344 345 346
void
mem_validate_all_blocks(void);
/*=========================*/
#endif

/*#######################################################################*/
347

osku's avatar
osku committed
348 349 350
/* The info header of a block in a memory heap */

struct mem_block_info_struct {
351
	ulint	magic_n;/* magic number for debugging */
osku's avatar
osku committed
352
	char	file_name[8];/* file name where the mem heap was created */
353
	ulint	line;	/*!< line number where the mem heap was created */
osku's avatar
osku committed
354 355 356 357 358 359 360
	UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the
			the list this is the base node of the list of blocks;
			in subsequent blocks this is undefined */
	UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next
			and prev in the list. The first block allocated
			to the heap is also the first block in this list,
			though it also contains the base node of the list. */
361 362
	ulint	len;	/*!< physical length of this block in bytes */
	ulint	total_size; /*!< physical length in bytes of all blocks
inaam's avatar
inaam committed
363 364
			in the heap. This is defined only in the base
			node and is set to ULINT_UNDEFINED in others. */
365
	ulint	type;	/*!< type of heap: MEM_HEAP_DYNAMIC, or
osku's avatar
osku committed
366
			MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */
367
	ulint	free;	/*!< offset in bytes of the first free position for
osku's avatar
osku committed
368
			user data in the block */
369
	ulint	start;	/*!< the value of the struct field 'free' at the
osku's avatar
osku committed
370
			creation of the block */
371
#ifndef UNIV_HOTBACKUP
372
	void*	free_block;
osku's avatar
osku committed
373 374 375 376 377
			/* if the MEM_HEAP_BTR_SEARCH bit is set in type,
			and this is the heap root, this can contain an
			allocated buffer frame, which can be appended as a
			free block to the heap, if we need more space;
			otherwise, this is NULL */
378 379 380 381
	void*	buf_block;
			/* if this block has been allocated from the buffer
			pool, this contains the buf_block_t handle;
			otherwise, this is NULL */
382
#endif /* !UNIV_HOTBACKUP */
383
#ifdef MEM_PERIODIC_CHECK
osku's avatar
osku committed
384 385 386 387 388 389 390 391 392 393
	UT_LIST_NODE_T(mem_block_t) mem_block_list;
			/* List of all mem blocks allocated; protected
			by the mem_comm_pool mutex */
#endif
};

#define MEM_BLOCK_MAGIC_N	764741555
#define MEM_FREED_BLOCK_MAGIC_N	547711122

/* Header size for a memory heap block */
394
#define MEM_BLOCK_HEADER_SIZE	ut_calc_align(sizeof(mem_block_info_t),\
osku's avatar
osku committed
395 396 397 398 399 400 401
							UNIV_MEM_ALIGNMENT)
#include "mem0dbg.h"

#ifndef UNIV_NONINL
#include "mem0mem.ic"
#endif

402
#endif