📄 mem0mem.ic
字号:
was given to the caller in the allocation*/ buf = (byte*)buf + MEM_FIELD_HEADER_SIZE; /* Check that the field lengths agree */ ut_ad(n == (ulint)mem_field_header_get_len(buf));#endif return(buf);} /*********************************************************************Frees the topmost element in a memory heap. The size of the element must begiven. */UNIV_INLINEvoidmem_heap_free_top(/*==============*/ mem_heap_t* heap, /* in: memory heap */ ulint n) /* in: size of the topmost element */{ mem_block_t* block; ut_ad(mem_heap_check(heap)); block = UT_LIST_GET_LAST(heap->base); /* Subtract the free field of block */ mem_block_set_free(block, mem_block_get_free(block) - MEM_SPACE_NEEDED(n));#ifdef UNIV_MEM_DEBUG ut_ad(mem_block_get_start(block) <= mem_block_get_free(block)); /* In the debug version check the consistency, and erase field */ mem_field_erase((byte*)block + mem_block_get_free(block), n);#endif /* If free == start, we may free the block if it is not the first one */ if ((heap != block) && (mem_block_get_free(block) == mem_block_get_start(block))) { mem_heap_block_free(heap, block); }}/*********************************************************************NOTE: Use the corresponding macros instead of this function. Creates amemory heap which allocates memory from dynamic space. For debuggingpurposes, takes also the file name and line as argument. */UNIV_INLINEmem_heap_t*mem_heap_create_func(/*=================*/ /* out, own: memory heap */ ulint n, /* in: desired start block size, this means that a single user buffer of size n will fit in the block, 0 creates a default size block; if init_block is not NULL, n tells its size in bytes */ void* init_block, /* in: if very fast creation is wanted, the caller can reserve some memory from its stack, for example, and pass it as the the initial block to the heap: then no OS call of malloc is needed at the creation. CAUTION: the caller must make sure the initial block is not unintentionally erased (if allocated in the stack), before the memory heap is explicitly freed. */ ulint type, /* in: MEM_HEAP_DYNAMIC or MEM_HEAP_BUFFER */ const char* file_name, /* in: file name where created */ ulint line /* in: line where created */ ){ mem_block_t* block; if (n > 0) { block = mem_heap_create_block(NULL, n, init_block, type, file_name, line); } else { block = mem_heap_create_block(NULL, MEM_BLOCK_START_SIZE, init_block, type, file_name, line); } ut_ad(block); UT_LIST_INIT(block->base); /* Add the created block itself as the first block in the list */ UT_LIST_ADD_FIRST(list, block->base, block);#ifdef UNIV_MEM_DEBUG if (block == NULL) { return(block); } mem_hash_insert(block, file_name, line);#endif return(block);}/*********************************************************************NOTE: Use the corresponding macro instead of this function. Frees the spaceoccupied by a memory heap. In the debug version erases the heap memoryblocks. */UNIV_INLINEvoidmem_heap_free_func(/*===============*/ mem_heap_t* heap, /* in, own: heap to be freed */ const char* file_name __attribute__((unused)), /* in: file name where freed */ ulint line __attribute__((unused))){ mem_block_t* block; mem_block_t* prev_block; ut_ad(mem_heap_check(heap)); block = UT_LIST_GET_LAST(heap->base);#ifdef UNIV_MEM_DEBUG /* In the debug version remove the heap from the hash table of heaps and check its consistency */ mem_hash_remove(heap, file_name, line); #endif if (heap->free_block) { mem_heap_free_block_free(heap); } while (block != NULL) { /* Store the contents of info before freeing current block (it is erased in freeing) */ prev_block = UT_LIST_GET_PREV(list, block); mem_heap_block_free(heap, block); block = prev_block; }}/*******************************************************************NOTE: Use the corresponding macro instead of this function.Allocates a single buffer of memory from the dynamic memory ofthe C compiler. Is like malloc of C. The buffer must be freed with mem_free. */UNIV_INLINEvoid*mem_alloc_func(/*===========*/ /* out, own: free storage, NULL if did not succeed */ ulint n, /* in: desired number of bytes */ const char* file_name, /* in: file name where created */ ulint line /* in: line where created */ ){ mem_heap_t* heap; void* buf; heap = mem_heap_create_func(n, NULL, MEM_HEAP_DYNAMIC, file_name, line); if (heap == NULL) { return(NULL); } /* Note that as we created the first block in the heap big enough for the buffer requested by the caller, the buffer will be in the first block and thus we can calculate the pointer to the heap from the pointer to the buffer when we free the memory buffer. */ buf = mem_heap_alloc(heap, n); ut_a((byte*)heap == (byte*)buf - MEM_BLOCK_HEADER_SIZE - MEM_FIELD_HEADER_SIZE); return(buf);}/*******************************************************************NOTE: Use the corresponding macro instead of this function. Frees a singlebuffer of storage from the dynamic memory of the C compiler. Similar to thefree of C. */UNIV_INLINEvoidmem_free_func(/*==========*/ void* ptr, /* in, own: buffer to be freed */ const char* file_name, /* in: file name where created */ ulint line /* in: line where created */ ){ mem_heap_t* heap; heap = (mem_heap_t*)((byte*)ptr - MEM_BLOCK_HEADER_SIZE - MEM_FIELD_HEADER_SIZE); mem_heap_free_func(heap, file_name, line);}/*********************************************************************Returns the space in bytes occupied by a memory heap. */UNIV_INLINEulintmem_heap_get_size(/*==============*/ mem_heap_t* heap) /* in: heap */{ mem_block_t* block; ulint size = 0; ut_ad(mem_heap_check(heap)); block = heap; while (block != NULL) { size += mem_block_get_len(block); block = UT_LIST_GET_NEXT(list, block); } if (heap->free_block) { size += UNIV_PAGE_SIZE; } return(size);}/**************************************************************************Duplicates a NUL-terminated string. */UNIV_INLINEchar*mem_strdup(/*=======*/ /* out, own: a copy of the string, must be deallocated with mem_free */ const char* str) /* in: string to be copied */{ ulint len = strlen(str) + 1; return(memcpy(mem_alloc(len), str, len));}/**************************************************************************Makes a NUL-terminated copy of a nonterminated string. */UNIV_INLINEchar*mem_strdupl(/*========*/ /* out, own: a copy of the string, must be deallocated with mem_free */ const char* str, /* in: string to be copied */ ulint len) /* in: length of str, in bytes */{ char* s = mem_alloc(len + 1); s[len] = 0; return(memcpy(s, str, len));}/**************************************************************************Makes a NUL-terminated quoted copy of a NUL-terminated string. */UNIV_INLINEchar*mem_strdupq(/*========*/ /* out, own: a quoted copy of the string, must be deallocated with mem_free */ const char* str, /* in: string to be copied */ char q) /* in: quote character */{ char* dst; char* d; const char* s = str; size_t len = strlen(str) + 3; /* calculate the number of quote characters in the string */ while((s = strchr(s, q)) != NULL) { s++; len++; } /* allocate the quoted string, and copy it */ d = dst = mem_alloc(len); *d++ = q; s = str; while(*s) { if ((*d++ = *s++) == q) { *d++ = q; } } *d++ = q; *d++ = '\0'; ut_ad((ssize_t) len == d - dst); return(dst);}/**************************************************************************Makes a NUL-terminated copy of a nonterminated string,allocated from a memory heap. */UNIV_INLINEchar*mem_heap_strdupl(/*=============*/ /* out, own: a copy of the string */ 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 */{ char* s = mem_heap_alloc(heap, len + 1); s[len] = 0; return(memcpy(s, str, len));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -