00001 /****************************************************** 00002 The memory management 00003 00004 (c) 1994, 1995 Innobase Oy 00005 00006 Created 6/9/1994 Heikki Tuuri 00007 *******************************************************/ 00008 00009 #ifndef mem0mem_h 00010 #define mem0mem_h 00011 00012 #include "univ.i" 00013 #include "ut0mem.h" 00014 #include "ut0byte.h" 00015 #include "ut0ut.h" 00016 #include "ut0rnd.h" 00017 #include "sync0sync.h" 00018 #include "ut0lst.h" 00019 #include "mach0data.h" 00020 00021 /* -------------------- MEMORY HEAPS ----------------------------- */ 00022 00023 /* The info structure stored at the beginning of a heap block */ 00024 typedef struct mem_block_info_struct mem_block_info_t; 00025 00026 /* A block of a memory heap consists of the info structure 00027 followed by an area of memory */ 00028 typedef mem_block_info_t mem_block_t; 00029 00030 /* A memory heap is a nonempty linear list of memory blocks */ 00031 typedef mem_block_t mem_heap_t; 00032 00033 /* Types of allocation for memory heaps: DYNAMIC means allocation from the 00034 dynamic memory pool of the C compiler, BUFFER means allocation from the 00035 buffer pool; the latter method is used for very big heaps */ 00036 00037 #define MEM_HEAP_DYNAMIC 0 /* the most common type */ 00038 #define MEM_HEAP_BUFFER 1 00039 #define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be 00040 ORed to MEM_HEAP_BUFFER, in which 00041 case heap->free_block is used in 00042 some cases for memory allocations, 00043 and if it's NULL, the memory 00044 allocation functions can return 00045 NULL. */ 00046 00047 /* The following start size is used for the first block in the memory heap if 00048 the size is not specified, i.e., 0 is given as the parameter in the call of 00049 create. The standard size is the maximum (payload) size of the blocks used for 00050 allocations of small buffers. */ 00051 00052 #define MEM_BLOCK_START_SIZE 64 00053 #define MEM_BLOCK_STANDARD_SIZE 8000 00054 00055 /* If a memory heap is allowed to grow into the buffer pool, the following 00056 is the maximum size for a single allocated buffer: */ 00057 #define MEM_MAX_ALLOC_IN_BUF (UNIV_PAGE_SIZE - 200) 00058 00059 /********************************************************************** 00060 Initializes the memory system. */ 00061 00062 void 00063 mem_init( 00064 /*=====*/ 00065 ulint size); /* in: common pool size in bytes */ 00066 /****************************************************************** 00067 Use this macro instead of the corresponding function! Macro for memory 00068 heap creation. */ 00069 00070 #define mem_heap_create(N) mem_heap_create_func(\ 00071 (N), NULL, MEM_HEAP_DYNAMIC, __FILE__, __LINE__) 00072 /****************************************************************** 00073 Use this macro instead of the corresponding function! Macro for memory 00074 heap creation. */ 00075 00076 #define mem_heap_create_in_buffer(N) mem_heap_create_func(\ 00077 (N), NULL, MEM_HEAP_BUFFER, __FILE__, __LINE__) 00078 /****************************************************************** 00079 Use this macro instead of the corresponding function! Macro for memory 00080 heap creation. */ 00081 00082 #define mem_heap_create_in_btr_search(N) mem_heap_create_func(\ 00083 (N), NULL, MEM_HEAP_BTR_SEARCH | MEM_HEAP_BUFFER,\ 00084 __FILE__, __LINE__) 00085 /****************************************************************** 00086 Use this macro instead of the corresponding function! Macro for fast 00087 memory heap creation. An initial block of memory B is given by the 00088 caller, N is its size, and this memory block is not freed by 00089 mem_heap_free. See the parameter comment in mem_heap_create_func below. */ 00090 00091 #define mem_heap_fast_create(N, B) mem_heap_create_func(\ 00092 (N), (B), MEM_HEAP_DYNAMIC, __FILE__, __LINE__) 00093 00094 /****************************************************************** 00095 Use this macro instead of the corresponding function! Macro for memory 00096 heap freeing. */ 00097 00098 #define mem_heap_free(heap) mem_heap_free_func(\ 00099 (heap), __FILE__, __LINE__) 00100 /********************************************************************* 00101 NOTE: Use the corresponding macros instead of this function. Creates a 00102 memory heap. For debugging purposes, takes also the file name and line as 00103 arguments. */ 00104 UNIV_INLINE 00105 mem_heap_t* 00106 mem_heap_create_func( 00107 /*=================*/ 00108 /* out, own: memory heap, NULL if 00109 did not succeed (only possible for 00110 MEM_HEAP_BTR_SEARCH type heaps)*/ 00111 ulint n, /* in: desired start block size, 00112 this means that a single user buffer 00113 of size n will fit in the block, 00114 0 creates a default size block; 00115 if init_block is not NULL, n tells 00116 its size in bytes */ 00117 void* init_block, /* in: if very fast creation is 00118 wanted, the caller can reserve some 00119 memory from its stack, for example, 00120 and pass it as the the initial block 00121 to the heap: then no OS call of malloc 00122 is needed at the creation. CAUTION: 00123 the caller must make sure the initial 00124 block is not unintentionally erased 00125 (if allocated in the stack), before 00126 the memory heap is explicitly freed. */ 00127 ulint type, /* in: heap type */ 00128 const char* file_name, /* in: file name where created */ 00129 ulint line); /* in: line where created */ 00130 /********************************************************************* 00131 NOTE: Use the corresponding macro instead of this function. Frees the space 00132 occupied by a memory heap. In the debug version erases the heap memory 00133 blocks. */ 00134 UNIV_INLINE 00135 void 00136 mem_heap_free_func( 00137 /*===============*/ 00138 mem_heap_t* heap, /* in, own: heap to be freed */ 00139 const char* file_name, /* in: file name where freed */ 00140 ulint line); /* in: line where freed */ 00141 /******************************************************************* 00142 Allocates n bytes of memory from a memory heap. */ 00143 UNIV_INLINE 00144 void* 00145 mem_heap_alloc( 00146 /*===========*/ 00147 /* out: allocated storage, NULL if did not 00148 succeed (only possible for 00149 MEM_HEAP_BTR_SEARCH type heaps) */ 00150 mem_heap_t* heap, /* in: memory heap */ 00151 ulint n); /* in: number of bytes; if the heap is allowed 00152 to grow into the buffer pool, this must be 00153 <= MEM_MAX_ALLOC_IN_BUF */ 00154 /********************************************************************* 00155 Returns a pointer to the heap top. */ 00156 UNIV_INLINE 00157 byte* 00158 mem_heap_get_heap_top( 00159 /*==================*/ 00160 /* out: pointer to the heap top */ 00161 mem_heap_t* heap); /* in: memory heap */ 00162 /********************************************************************* 00163 Frees the space in a memory heap exceeding the pointer given. The 00164 pointer must have been acquired from mem_heap_get_heap_top. The first 00165 memory block of the heap is not freed. */ 00166 UNIV_INLINE 00167 void 00168 mem_heap_free_heap_top( 00169 /*===================*/ 00170 mem_heap_t* heap, /* in: heap from which to free */ 00171 byte* old_top);/* in: pointer to old top of heap */ 00172 /********************************************************************* 00173 Empties a memory heap. The first memory block of the heap is not freed. */ 00174 UNIV_INLINE 00175 void 00176 mem_heap_empty( 00177 /*===========*/ 00178 mem_heap_t* heap); /* in: heap to empty */ 00179 /********************************************************************* 00180 Returns a pointer to the topmost element in a memory heap. 00181 The size of the element must be given. */ 00182 UNIV_INLINE 00183 void* 00184 mem_heap_get_top( 00185 /*=============*/ 00186 /* out: pointer to the topmost element */ 00187 mem_heap_t* heap, /* in: memory heap */ 00188 ulint n); /* in: size of the topmost element */ 00189 /********************************************************************* 00190 Frees the topmost element in a memory heap. 00191 The size of the element must be given. */ 00192 UNIV_INLINE 00193 void 00194 mem_heap_free_top( 00195 /*==============*/ 00196 mem_heap_t* heap, /* in: memory heap */ 00197 ulint n); /* in: size of the topmost element */ 00198 /********************************************************************* 00199 Returns the space in bytes occupied by a memory heap. */ 00200 UNIV_INLINE 00201 ulint 00202 mem_heap_get_size( 00203 /*==============*/ 00204 mem_heap_t* heap); /* in: heap */ 00205 /****************************************************************** 00206 Use this macro instead of the corresponding function! 00207 Macro for memory buffer allocation */ 00208 00209 #define mem_alloc(N) mem_alloc_func((N), __FILE__, __LINE__) 00210 /****************************************************************** 00211 Use this macro instead of the corresponding function! 00212 Macro for memory buffer allocation */ 00213 00214 #define mem_alloc_noninline(N) mem_alloc_func_noninline(\ 00215 (N), __FILE__, __LINE__) 00216 /******************************************************************* 00217 NOTE: Use the corresponding macro instead of this function. 00218 Allocates a single buffer of memory from the dynamic memory of 00219 the C compiler. Is like malloc of C. The buffer must be freed 00220 with mem_free. */ 00221 UNIV_INLINE 00222 void* 00223 mem_alloc_func( 00224 /*===========*/ 00225 /* out, own: free storage */ 00226 ulint n, /* in: desired number of bytes */ 00227 const char* file_name, /* in: file name where created */ 00228 ulint line /* in: line where created */ 00229 ); 00230 /******************************************************************* 00231 NOTE: Use the corresponding macro instead of this function. 00232 Allocates a single buffer of memory from the dynamic memory of 00233 the C compiler. Is like malloc of C. The buffer must be freed 00234 with mem_free. */ 00235 00236 void* 00237 mem_alloc_func_noninline( 00238 /*=====================*/ 00239 /* out, own: free storage */ 00240 ulint n, /* in: desired number of bytes */ 00241 const char* file_name, /* in: file name where created */ 00242 ulint line /* in: line where created */ 00243 ); 00244 /****************************************************************** 00245 Use this macro instead of the corresponding function! 00246 Macro for memory buffer freeing */ 00247 00248 #define mem_free(PTR) mem_free_func((PTR), __FILE__, __LINE__) 00249 /******************************************************************* 00250 NOTE: Use the corresponding macro instead of this function. 00251 Frees a single buffer of storage from 00252 the dynamic memory of C compiler. Similar to free of C. */ 00253 UNIV_INLINE 00254 void 00255 mem_free_func( 00256 /*==========*/ 00257 void* ptr, /* in, own: buffer to be freed */ 00258 const char* file_name, /* in: file name where created */ 00259 ulint line /* in: line where created */ 00260 ); 00261 00262 /************************************************************************** 00263 Duplicates a NUL-terminated string. */ 00264 UNIV_INLINE 00265 char* 00266 mem_strdup( 00267 /*=======*/ 00268 /* out, own: a copy of the string, 00269 must be deallocated with mem_free */ 00270 const char* str); /* in: string to be copied */ 00271 /************************************************************************** 00272 Makes a NUL-terminated copy of a nonterminated string. */ 00273 UNIV_INLINE 00274 char* 00275 mem_strdupl( 00276 /*========*/ 00277 /* out, own: a copy of the string, 00278 must be deallocated with mem_free */ 00279 const char* str, /* in: string to be copied */ 00280 ulint len); /* in: length of str, in bytes */ 00281 00282 /************************************************************************** 00283 Makes a NUL-terminated quoted copy of a NUL-terminated string. */ 00284 UNIV_INLINE 00285 char* 00286 mem_strdupq( 00287 /*========*/ 00288 /* out, own: a quoted copy of the string, 00289 must be deallocated with mem_free */ 00290 const char* str, /* in: string to be copied */ 00291 char q); /* in: quote character */ 00292 00293 /************************************************************************** 00294 Duplicates a NUL-terminated string, allocated from a memory heap. */ 00295 00296 char* 00297 mem_heap_strdup( 00298 /*============*/ 00299 /* out, own: a copy of the string */ 00300 mem_heap_t* heap, /* in: memory heap where string is allocated */ 00301 const char* str); /* in: string to be copied */ 00302 /************************************************************************** 00303 Makes a NUL-terminated copy of a nonterminated string, 00304 allocated from a memory heap. */ 00305 UNIV_INLINE 00306 char* 00307 mem_heap_strdupl( 00308 /*=============*/ 00309 /* out, own: a copy of the string */ 00310 mem_heap_t* heap, /* in: memory heap where string is allocated */ 00311 const char* str, /* in: string to be copied */ 00312 ulint len); /* in: length of str, in bytes */ 00313 00314 /************************************************************************** 00315 Concatenate two strings and return the result, using a memory heap. */ 00316 00317 char* 00318 mem_heap_strcat( 00319 /*============*/ 00320 /* out, own: the result */ 00321 mem_heap_t* heap, /* in: memory heap where string is allocated */ 00322 const char* s1, /* in: string 1 */ 00323 const char* s2); /* in: string 2 */ 00324 00325 /************************************************************************** 00326 Duplicate a block of data, allocated from a memory heap. */ 00327 00328 void* 00329 mem_heap_dup( 00330 /*=========*/ 00331 /* out, own: a copy of the data */ 00332 mem_heap_t* heap, /* in: memory heap where copy is allocated */ 00333 const void* data, /* in: data to be copied */ 00334 ulint len); /* in: length of data, in bytes */ 00335 00336 /************************************************************************** 00337 Concatenate two memory blocks and return the result, using a memory heap. */ 00338 00339 void* 00340 mem_heap_cat( 00341 /*=========*/ 00342 /* out, own: the result */ 00343 mem_heap_t* heap, /* in: memory heap where result is allocated */ 00344 const void* b1, /* in: block 1 */ 00345 ulint len1, /* in: length of b1, in bytes */ 00346 const void* b2, /* in: block 2 */ 00347 ulint len2); /* in: length of b2, in bytes */ 00348 00349 /******************************************************************** 00350 A simple (s)printf replacement that dynamically allocates the space for the 00351 formatted string from the given heap. This supports a very limited set of 00352 the printf syntax: types 's' and 'u' and length modifier 'l' (which is 00353 required for the 'u' type). */ 00354 00355 char* 00356 mem_heap_printf( 00357 /*============*/ 00358 /* out: heap-allocated formatted string */ 00359 mem_heap_t* heap, /* in: memory heap */ 00360 const char* format, /* in: format string */ 00361 ...) __attribute__ ((format (printf, 2, 3))); 00362 00363 #ifdef MEM_PERIODIC_CHECK 00364 /********************************************************************** 00365 Goes through the list of all allocated mem blocks, checks their magic 00366 numbers, and reports possible corruption. */ 00367 00368 void 00369 mem_validate_all_blocks(void); 00370 /*=========================*/ 00371 #endif 00372 00373 /*#######################################################################*/ 00374 00375 /* The info header of a block in a memory heap */ 00376 00377 struct mem_block_info_struct { 00378 ulint magic_n;/* magic number for debugging */ 00379 char file_name[8];/* file name where the mem heap was created */ 00380 ulint line; /* line number where the mem heap was created */ 00381 UT_LIST_BASE_NODE_T(mem_block_t) base; /* In the first block in the 00382 the list this is the base node of the list of blocks; 00383 in subsequent blocks this is undefined */ 00384 UT_LIST_NODE_T(mem_block_t) list; /* This contains pointers to next 00385 and prev in the list. The first block allocated 00386 to the heap is also the first block in this list, 00387 though it also contains the base node of the list. */ 00388 ulint len; /* physical length of this block in bytes */ 00389 ulint type; /* type of heap: MEM_HEAP_DYNAMIC, or 00390 MEM_HEAP_BUF possibly ORed to MEM_HEAP_BTR_SEARCH */ 00391 ibool init_block; /* TRUE if this is the first block used in fast 00392 creation of a heap: the memory will be freed 00393 by the creator, not by mem_heap_free */ 00394 ulint free; /* offset in bytes of the first free position for 00395 user data in the block */ 00396 ulint start; /* the value of the struct field 'free' at the 00397 creation of the block */ 00398 byte* free_block; 00399 /* if the MEM_HEAP_BTR_SEARCH bit is set in type, 00400 and this is the heap root, this can contain an 00401 allocated buffer frame, which can be appended as a 00402 free block to the heap, if we need more space; 00403 otherwise, this is NULL */ 00404 #ifdef MEM_PERIODIC_CHECK 00405 UT_LIST_NODE_T(mem_block_t) mem_block_list; 00406 /* List of all mem blocks allocated; protected 00407 by the mem_comm_pool mutex */ 00408 #endif 00409 }; 00410 00411 #define MEM_BLOCK_MAGIC_N 764741555 00412 #define MEM_FREED_BLOCK_MAGIC_N 547711122 00413 00414 /* Header size for a memory heap block */ 00415 #define MEM_BLOCK_HEADER_SIZE ut_calc_align(sizeof(mem_block_info_t),\ 00416 UNIV_MEM_ALIGNMENT) 00417 #include "mem0dbg.h" 00418 00419 #ifndef UNIV_NONINL 00420 #include "mem0mem.ic" 00421 #endif 00422 00423 #endif
1.4.7

