summaryrefslogtreecommitdiff
path: root/storage/innobase/include/mem0mem.ic
diff options
context:
space:
mode:
Diffstat (limited to 'storage/innobase/include/mem0mem.ic')
-rw-r--r--storage/innobase/include/mem0mem.ic291
1 files changed, 159 insertions, 132 deletions
diff --git a/storage/innobase/include/mem0mem.ic b/storage/innobase/include/mem0mem.ic
index 6227a27f277..e7080d8c508 100644
--- a/storage/innobase/include/mem0mem.ic
+++ b/storage/innobase/include/mem0mem.ic
@@ -1,60 +1,75 @@
-/************************************************************************
-The memory management
+/*****************************************************************************
-(c) 1994, 1995 Innobase Oy
+Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
-Created 6/8/1994 Heikki Tuuri
-*************************************************************************/
+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.
-#include "mem0dbg.ic"
+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.
-#include "mem0pool.h"
+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
-/*******************************************************************
-Creates a memory heap block where data can be allocated. */
+*****************************************************************************/
+/********************************************************************//**
+@file include/mem0mem.ic
+The memory management
+
+Created 6/8/1994 Heikki Tuuri
+*************************************************************************/
+
+#include "mem0dbg.ic"
+#ifndef UNIV_HOTBACKUP
+# include "mem0pool.h"
+#endif /* !UNIV_HOTBACKUP */
+
+/***************************************************************//**
+Creates a memory heap block where data can be allocated.
+@return own: memory heap block, NULL if did not succeed (only possible
+for MEM_HEAP_BTR_SEARCH type heaps) */
+UNIV_INTERN
mem_block_t*
mem_heap_create_block(
/*==================*/
- /* out, own: memory heap block, NULL if
- did not succeed (only possible for
- MEM_HEAP_BTR_SEARCH type heaps) */
- mem_heap_t* heap, /* in: memory heap or NULL if first block
+ mem_heap_t* heap, /*!< in: memory heap or NULL if first block
should be created */
- ulint n, /* in: number of bytes needed for user data, or
- if init_block is not NULL, its size in bytes */
- void* init_block, /* in: init block in fast create,
- type must be MEM_HEAP_DYNAMIC */
- ulint type, /* in: type of heap: MEM_HEAP_DYNAMIC or
+ ulint n, /*!< in: number of bytes needed for user data */
+ ulint type, /*!< in: type of heap: MEM_HEAP_DYNAMIC or
MEM_HEAP_BUFFER */
- const char* file_name,/* in: file name where created */
- ulint line); /* in: line where created */
-/**********************************************************************
+ const char* file_name,/*!< in: file name where created */
+ ulint line); /*!< in: line where created */
+/******************************************************************//**
Frees a block from a memory heap. */
-
+UNIV_INTERN
void
mem_heap_block_free(
/*================*/
- mem_heap_t* heap, /* in: heap */
- mem_block_t* block); /* in: block to free */
-/**********************************************************************
+ mem_heap_t* heap, /*!< in: heap */
+ mem_block_t* block); /*!< in: block to free */
+#ifndef UNIV_HOTBACKUP
+/******************************************************************//**
Frees the free_block field from a memory heap. */
-
+UNIV_INTERN
void
mem_heap_free_block_free(
/*=====================*/
- mem_heap_t* heap); /* in: heap */
-/*******************************************************************
-Adds a new block to a memory heap. */
-
+ mem_heap_t* heap); /*!< in: heap */
+#endif /* !UNIV_HOTBACKUP */
+/***************************************************************//**
+Adds a new block to a memory heap.
+@return created block, NULL if did not succeed (only possible for
+MEM_HEAP_BTR_SEARCH type heaps) */
+UNIV_INTERN
mem_block_t*
mem_heap_add_block(
/*===============*/
- /* out: created block, NULL if did not
- succeed (only possible for
- MEM_HEAP_BTR_SEARCH type heaps)*/
- mem_heap_t* heap, /* in: memory heap */
- ulint n); /* in: number of bytes user needs */
+ mem_heap_t* heap, /*!< in: memory heap */
+ ulint n); /*!< in: number of bytes user needs */
UNIV_INLINE
void
@@ -122,17 +137,33 @@ mem_block_get_start(mem_block_t* block)
return(block->start);
}
-/*******************************************************************
-Allocates n bytes of memory from a memory heap. */
+/***************************************************************//**
+Allocates and zero-fills n bytes of memory from a memory heap.
+@return allocated, zero-filled storage */
+UNIV_INLINE
+void*
+mem_heap_zalloc(
+/*============*/
+ mem_heap_t* heap, /*!< in: memory heap */
+ ulint n) /*!< in: number of bytes; if the heap is allowed
+ to grow into the buffer pool, this must be
+ <= MEM_MAX_ALLOC_IN_BUF */
+{
+ ut_ad(heap);
+ ut_ad(!(heap->type & MEM_HEAP_BTR_SEARCH));
+ return(memset(mem_heap_alloc(heap, n), 0, n));
+}
+
+/***************************************************************//**
+Allocates n bytes of memory from a memory heap.
+@return allocated storage, NULL if did not succeed (only possible for
+MEM_HEAP_BTR_SEARCH type heaps) */
UNIV_INLINE
void*
mem_heap_alloc(
/*===========*/
- /* out: allocated storage, NULL if did not
- succeed (only possible for
- MEM_HEAP_BTR_SEARCH type heaps) */
- mem_heap_t* heap, /* in: memory heap */
- ulint n) /* in: number of bytes; if the heap is allowed
+ mem_heap_t* heap, /*!< in: memory heap */
+ ulint n) /*!< in: number of bytes; if the heap is allowed
to grow into the buffer pool, this must be
<= MEM_MAX_ALLOC_IN_BUF */
{
@@ -186,14 +217,14 @@ mem_heap_alloc(
return(buf);
}
-/*********************************************************************
-Returns a pointer to the heap top. */
+/*****************************************************************//**
+Returns a pointer to the heap top.
+@return pointer to the heap top */
UNIV_INLINE
byte*
mem_heap_get_heap_top(
/*==================*/
- /* out: pointer to the heap top */
- mem_heap_t* heap) /* in: memory heap */
+ mem_heap_t* heap) /*!< in: memory heap */
{
mem_block_t* block;
byte* buf;
@@ -207,7 +238,7 @@ mem_heap_get_heap_top(
return(buf);
}
-/*********************************************************************
+/*****************************************************************//**
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. */
@@ -215,8 +246,8 @@ UNIV_INLINE
void
mem_heap_free_heap_top(
/*===================*/
- mem_heap_t* heap, /* in: heap from which to free */
- byte* old_top)/* in: pointer to old top of heap */
+ mem_heap_t* heap, /*!< in: heap from which to free */
+ byte* old_top)/*!< in: pointer to old top of heap */
{
mem_block_t* block;
mem_block_t* prev_block;
@@ -291,31 +322,32 @@ mem_heap_free_heap_top(
}
}
-/*********************************************************************
+/*****************************************************************//**
Empties a memory heap. The first memory block of the heap is not freed. */
UNIV_INLINE
void
mem_heap_empty(
/*===========*/
- mem_heap_t* heap) /* in: heap to empty */
+ mem_heap_t* heap) /*!< in: heap to empty */
{
mem_heap_free_heap_top(heap, (byte*)heap + mem_block_get_start(heap));
-
+#ifndef UNIV_HOTBACKUP
if (heap->free_block) {
mem_heap_free_block_free(heap);
}
+#endif /* !UNIV_HOTBACKUP */
}
-/*********************************************************************
+/*****************************************************************//**
Returns a pointer to the topmost element in a memory heap. The size of the
-element must be given. */
+element must be given.
+@return pointer to the topmost element */
UNIV_INLINE
void*
mem_heap_get_top(
/*=============*/
- /* out: pointer to the topmost element */
- mem_heap_t* heap, /* in: memory heap */
- ulint n) /* in: size of the topmost element */
+ mem_heap_t* heap, /*!< in: memory heap */
+ ulint n) /*!< in: size of the topmost element */
{
mem_block_t* block;
void* buf;
@@ -341,15 +373,15 @@ mem_heap_get_top(
return(buf);
}
-/*********************************************************************
+/*****************************************************************//**
Frees the topmost element in a memory heap. The size of the element must be
given. */
UNIV_INLINE
void
mem_heap_free_top(
/*==============*/
- mem_heap_t* heap, /* in: memory heap */
- ulint n) /* in: size of the topmost element */
+ mem_heap_t* heap, /*!< in: memory heap */
+ ulint n) /*!< in: size of the topmost element */
{
mem_block_t* block;
@@ -384,48 +416,32 @@ mem_heap_free_top(
}
}
-/*********************************************************************
+/*****************************************************************//**
NOTE: Use the corresponding macros instead of this function. Creates a
memory heap. For debugging purposes, takes also the file name and line as
-argument. */
+argument.
+@return own: memory heap, NULL if did not succeed (only possible for
+MEM_HEAP_BTR_SEARCH type heaps) */
UNIV_INLINE
mem_heap_t*
mem_heap_create_func(
/*=================*/
- /* out, own: memory heap, NULL if
- did not succeed (only possible for
- MEM_HEAP_BTR_SEARCH type heaps)*/
- ulint n, /* in: desired start block size,
+ 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: heap type */
- const char* file_name, /* in: file name where created */
- ulint line) /* in: line where created */
+ 0 creates a default size block */
+ ulint type, /*!< in: heap type */
+ 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);
+ if (!n) {
+ n = MEM_BLOCK_START_SIZE;
}
+ block = mem_heap_create_block(NULL, n, type, file_name, line);
+
if (block == NULL) {
return(NULL);
@@ -445,7 +461,7 @@ mem_heap_create_func(
return(block);
}
-/*********************************************************************
+/*****************************************************************//**
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. */
@@ -453,9 +469,9 @@ UNIV_INLINE
void
mem_heap_free_func(
/*===============*/
- mem_heap_t* heap, /* in, own: heap to be freed */
+ mem_heap_t* heap, /*!< in, own: heap to be freed */
const char* file_name __attribute__((unused)),
- /* in: file name where freed */
+ /*!< in: file name where freed */
ulint line __attribute__((unused)))
{
mem_block_t* block;
@@ -473,10 +489,11 @@ mem_heap_free_func(
mem_hash_remove(heap, file_name, line);
#endif
-
+#ifndef UNIV_HOTBACKUP
if (heap->free_block) {
mem_heap_free_block_free(heap);
}
+#endif /* !UNIV_HOTBACKUP */
while (block != NULL) {
/* Store the contents of info before freeing current block
@@ -490,32 +507,44 @@ mem_heap_free_func(
}
}
-/*******************************************************************
+/***************************************************************//**
NOTE: Use the corresponding macro instead of this function.
Allocates a single buffer of memory from the dynamic memory of
the C compiler. Is like malloc of C. The buffer must be freed
-with mem_free. */
+with mem_free.
+@return own: free storage */
UNIV_INLINE
void*
mem_alloc_func(
/*===========*/
- /* out, own: free storage */
- ulint n, /* in: desired number of bytes */
- const char* file_name, /* in: file name where created */
- ulint line /* in: line where created */
- )
+ ulint n, /*!< in: desired number of bytes */
+ ulint* size, /*!< out: allocated size in bytes,
+ or NULL */
+ 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);
+ heap = mem_heap_create_func(n, MEM_HEAP_DYNAMIC, file_name, line);
/* 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. */
+ if (UNIV_LIKELY_NULL(size)) {
+ /* Adjust the allocation to the actual size of the
+ memory block. */
+ ulint m = mem_block_get_len(heap)
+ - mem_block_get_free(heap);
+#ifdef UNIV_MEM_DEBUG
+ m -= MEM_FIELD_HEADER_SIZE + MEM_FIELD_TRAILER_SIZE;
+#endif /* UNIV_MEM_DEBUG */
+ ut_ad(m >= n);
+ *size = n = m;
+ }
+
buf = mem_heap_alloc(heap, n);
ut_a((byte*)heap == (byte*)buf - MEM_BLOCK_HEADER_SIZE
@@ -523,7 +552,7 @@ mem_alloc_func(
return(buf);
}
-/*******************************************************************
+/***************************************************************//**
NOTE: Use the corresponding macro instead of this function. Frees a single
buffer of storage from the dynamic memory of the C compiler. Similar to the
free of C. */
@@ -531,10 +560,9 @@ UNIV_INLINE
void
mem_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 */
- )
+ 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;
@@ -543,13 +571,13 @@ mem_free_func(
mem_heap_free_func(heap, file_name, line);
}
-/*********************************************************************
+/*****************************************************************//**
Returns the space in bytes occupied by a memory heap. */
UNIV_INLINE
ulint
mem_heap_get_size(
/*==============*/
- mem_heap_t* heap) /* in: heap */
+ mem_heap_t* heap) /*!< in: heap */
{
mem_block_t* block;
ulint size = 0;
@@ -563,57 +591,56 @@ mem_heap_get_size(
size += mem_block_get_len(block);
block = UT_LIST_GET_NEXT(list, block);
}
-
+#ifndef UNIV_HOTBACKUP
if (heap->free_block) {
size += UNIV_PAGE_SIZE;
}
+#endif /* !UNIV_HOTBACKUP */
return(size);
}
-/**************************************************************************
-Duplicates a NUL-terminated string. */
+/**********************************************************************//**
+Duplicates a NUL-terminated string.
+@return own: a copy of the string, must be deallocated with mem_free */
UNIV_INLINE
char*
mem_strdup(
/*=======*/
- /* out, own: a copy of the string,
- must be deallocated with mem_free */
- const char* str) /* in: string to be copied */
+ const char* str) /*!< in: string to be copied */
{
ulint len = strlen(str) + 1;
- return(memcpy(mem_alloc(len), str, len));
+ return((char*) memcpy(mem_alloc(len), str, len));
}
-/**************************************************************************
-Makes a NUL-terminated copy of a nonterminated string. */
+/**********************************************************************//**
+Makes a NUL-terminated copy of a nonterminated string.
+@return own: a copy of the string, must be deallocated with mem_free */
UNIV_INLINE
char*
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 */
+ const char* str, /*!< in: string to be copied */
+ ulint len) /*!< in: length of str, in bytes */
{
- char* s = mem_alloc(len + 1);
+ char* s = (char*) mem_alloc(len + 1);
s[len] = 0;
- return(memcpy(s, str, len));
+ return((char*) memcpy(s, str, len));
}
-/**************************************************************************
+/**********************************************************************//**
Makes a NUL-terminated copy of a nonterminated string,
-allocated from a memory heap. */
+allocated from a memory heap.
+@return own: a copy of the string */
UNIV_INLINE
char*
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 */
+ 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);
+ char* s = (char*) mem_heap_alloc(heap, len + 1);
s[len] = 0;
- return(memcpy(s, str, len));
+ return((char*) memcpy(s, str, len));
}