00001 /***************************************************************************** 00002 00003 Copyright (C) 1994, 2010, Innobase Oy. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it under 00006 the terms of the GNU General Public License as published by the Free Software 00007 Foundation; version 2 of the License. 00008 00009 This program is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License along with 00014 this program; if not, write to the Free Software Foundation, Inc., 51 Franklin 00015 St, Fifth Floor, Boston, MA 02110-1301 USA 00016 00017 *****************************************************************************/ 00018 00019 /**************************************************/ 00026 #pragma once 00027 #ifndef mem0mem_h 00028 #define mem0mem_h 00029 00030 #include "univ.i" 00031 #include "ut0mem.h" 00032 #include "ut0byte.h" 00033 #include "ut0rnd.h" 00034 #ifndef UNIV_HOTBACKUP 00035 # include "sync0sync.h" 00036 #endif /* UNIV_HOTBACKUP */ 00037 #include "ut0lst.h" 00038 #include "mach0data.h" 00039 00040 /* -------------------- MEMORY HEAPS ----------------------------- */ 00041 00042 /* The info structure stored at the beginning of a heap block */ 00043 typedef struct mem_block_info_struct mem_block_info_t; 00044 00045 /* A block of a memory heap consists of the info structure 00046 followed by an area of memory */ 00047 typedef mem_block_info_t mem_block_t; 00048 00049 /* A memory heap is a nonempty linear list of memory blocks */ 00050 typedef mem_block_t mem_heap_t; 00051 00052 /* Types of allocation for memory heaps: DYNAMIC means allocation from the 00053 dynamic memory pool of the C compiler, BUFFER means allocation from the 00054 buffer pool; the latter method is used for very big heaps */ 00055 00056 #define MEM_HEAP_DYNAMIC 0 /* the most common type */ 00057 #define MEM_HEAP_BUFFER 1 00058 #define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be 00059 ORed to MEM_HEAP_BUFFER, in which 00060 case heap->free_block is used in 00061 some cases for memory allocations, 00062 and if NULL, the memory 00063 allocation functions can return 00064 NULL. */ 00065 00066 /* The following start size is used for the first block in the memory heap if 00067 the size is not specified, i.e., 0 is given as the parameter in the call of 00068 create. The standard size is the maximum (payload) size of the blocks used for 00069 allocations of small buffers. */ 00070 00071 #define MEM_BLOCK_START_SIZE 64 00072 #define MEM_BLOCK_STANDARD_SIZE \ 00073 (UNIV_PAGE_SIZE >= 16384 ? 8000 : MEM_MAX_ALLOC_IN_BUF) 00074 00075 /* If a memory heap is allowed to grow into the buffer pool, the following 00076 is the maximum size for a single allocated buffer: */ 00077 #define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200) 00078 00079 /******************************************************************/ 00081 UNIV_INTERN 00082 void 00083 mem_init( 00084 /*=====*/ 00085 ulint size); 00086 /******************************************************************/ 00088 UNIV_INTERN 00089 void 00090 mem_close(void); 00091 /*===========*/ 00092 00093 /**************************************************************/ 00097 #define mem_heap_create(N) mem_heap_create_func(\ 00098 (N), MEM_HEAP_DYNAMIC, __FILE__, __LINE__) 00099 /**************************************************************/ 00103 #define mem_heap_create_in_buffer(N) mem_heap_create_func(\ 00104 (N), MEM_HEAP_BUFFER, __FILE__, __LINE__) 00105 /**************************************************************/ 00109 #define mem_heap_create_in_btr_search(N) mem_heap_create_func(\ 00110 (N), MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\ 00111 __FILE__, __LINE__) 00112 00113 /**************************************************************/ 00117 #define mem_heap_free(heap) mem_heap_free_func(\ 00118 (heap), __FILE__, __LINE__) 00119 /*****************************************************************/ 00125 UNIV_INLINE 00126 mem_heap_t* 00127 mem_heap_create_func( 00128 /*=================*/ 00129 ulint n, 00133 ulint type, 00134 const char* file_name, 00135 ulint line); 00136 /*****************************************************************/ 00140 UNIV_INLINE 00141 void 00142 mem_heap_free_func( 00143 /*===============*/ 00144 mem_heap_t* heap, 00145 const char* file_name, 00146 ulint line); 00147 /***************************************************************/ 00150 UNIV_INLINE 00151 void* 00152 mem_heap_zalloc( 00153 /*============*/ 00154 mem_heap_t* heap, 00155 ulint n); 00158 /***************************************************************/ 00162 UNIV_INLINE 00163 void* 00164 mem_heap_alloc( 00165 /*===========*/ 00166 mem_heap_t* heap, 00167 ulint n); 00170 /*****************************************************************/ 00173 UNIV_INLINE 00174 byte* 00175 mem_heap_get_heap_top( 00176 /*==================*/ 00177 mem_heap_t* heap); 00178 /*****************************************************************/ 00182 UNIV_INLINE 00183 void 00184 mem_heap_free_heap_top( 00185 /*===================*/ 00186 mem_heap_t* heap, 00187 byte* old_top); 00188 /*****************************************************************/ 00190 UNIV_INLINE 00191 void 00192 mem_heap_empty( 00193 /*===========*/ 00194 mem_heap_t* heap); 00195 /*****************************************************************/ 00199 UNIV_INLINE 00200 void* 00201 mem_heap_get_top( 00202 /*=============*/ 00203 mem_heap_t* heap, 00204 ulint n); 00205 /*****************************************************************/ 00208 UNIV_INLINE 00209 void 00210 mem_heap_free_top( 00211 /*==============*/ 00212 mem_heap_t* heap, 00213 ulint n); 00214 /*****************************************************************/ 00216 UNIV_INLINE 00217 ulint 00218 mem_heap_get_size( 00219 /*==============*/ 00220 mem_heap_t* heap); 00221 /**************************************************************/ 00225 #define mem_zalloc(N) memset(mem_alloc(N), 0, (N)); 00226 00227 #define mem_alloc(N) mem_alloc_func((N), NULL, __FILE__, __LINE__) 00228 #define mem_alloc2(N,S) mem_alloc_func((N), (S), __FILE__, __LINE__) 00229 /***************************************************************/ 00235 UNIV_INLINE 00236 void * 00237 mem_alloc_func( 00238 /*===========*/ 00239 ulint n, 00240 ulint* size, 00242 const char* file_name, 00243 ulint line); 00245 /**************************************************************/ 00249 #define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__) 00250 /***************************************************************/ 00254 UNIV_INLINE 00255 void 00256 mem_free_func( 00257 /*==========*/ 00258 void* ptr, 00259 const char* file_name, 00260 ulint line); 00262 /**********************************************************************/ 00265 UNIV_INLINE 00266 char* 00267 mem_strdup( 00268 /*=======*/ 00269 const char* str); 00270 /**********************************************************************/ 00273 UNIV_INLINE 00274 char* 00275 mem_strdupl( 00276 /*========*/ 00277 const char* str, 00278 ulint len); 00280 /**********************************************************************/ 00283 UNIV_INTERN 00284 char* 00285 mem_heap_strdup( 00286 /*============*/ 00287 mem_heap_t* heap, 00288 const char* str); 00289 /**********************************************************************/ 00293 UNIV_INLINE 00294 char* 00295 mem_heap_strdupl( 00296 /*=============*/ 00297 mem_heap_t* heap, 00298 const char* str, 00299 ulint len); 00301 /**********************************************************************/ 00304 UNIV_INTERN 00305 char* 00306 mem_heap_strcat( 00307 /*============*/ 00308 mem_heap_t* heap, 00309 const char* s1, 00310 const char* s2); 00312 /**********************************************************************/ 00315 UNIV_INTERN 00316 void* 00317 mem_heap_dup( 00318 /*=========*/ 00319 mem_heap_t* heap, 00320 const void* data, 00321 ulint len); 00323 /****************************************************************/ 00329 UNIV_INTERN 00330 char* 00331 mem_heap_printf( 00332 /*============*/ 00333 mem_heap_t* heap, 00334 const char* format, 00335 ...) __attribute__ ((format (printf, 2, 3))); 00336 00337 #ifdef MEM_PERIODIC_CHECK 00338 /******************************************************************/ 00341 UNIV_INTERN 00342 void 00343 mem_validate_all_blocks(void); 00344 /*=========================*/ 00345 #endif 00346 00347 /*#######################################################################*/ 00348 00349 /* The info header of a block in a memory heap */ 00350 00351 struct mem_block_info_struct { 00352 ulint magic_n;/* magic number for debugging */ 00353 char file_name[8];/* file name where the mem heap was created */ 00354 ulint line; 00355 UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the 00356 the list this is the base node of the list of blocks; 00357 in subsequent blocks this is undefined */ 00358 UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next 00359 and prev in the list. The first block allocated 00360 to the heap is also the first block in this list, 00361 though it also contains the base node of the list. */ 00362 ulint len; 00363 ulint total_size; 00366 ulint type; 00368 ulint free; 00370 ulint start; 00372 #ifndef UNIV_HOTBACKUP 00373 void* free_block; 00374 /* if the MEM_HEAP_BTR_SEARCH bit is set in type, 00375 and this is the heap root, this can contain an 00376 allocated buffer frame, which can be appended as a 00377 free block to the heap, if we need more space; 00378 otherwise, this is NULL */ 00379 void* buf_block; 00380 /* if this block has been allocated from the buffer 00381 pool, this contains the buf_block_t handle; 00382 otherwise, this is NULL */ 00383 #endif /* !UNIV_HOTBACKUP */ 00384 #ifdef MEM_PERIODIC_CHECK 00385 UT_LIST_NODE_T(mem_block_t) mem_block_list; 00386 /* List of all mem blocks allocated; protected 00387 by the mem_comm_pool mutex */ 00388 #endif 00389 }; 00390 00391 #define MEM_BLOCK_MAGIC_N 764741555 00392 #define MEM_FREED_BLOCK_MAGIC_N 547711122 00393 00394 /* Header size for a memory heap block */ 00395 #define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\ 00396 UNIV_MEM_ALIGNMENT) 00397 #include "mem0dbg.h" 00398 00399 #ifndef UNIV_NONINL 00400 #include "mem0mem.ic" 00401 #endif 00402 00403 #endif