Commit 81f79a50 authored by osku's avatar osku

Fix comments for memory allocation functions and add some extra checks.

Adapt callers.
parent 1a77f1c5
...@@ -40,9 +40,13 @@ ha_create( ...@@ -40,9 +40,13 @@ ha_create(
table->adaptive = FALSE; table->adaptive = FALSE;
} }
/* Creating MEM_HEAP_BTR_SEARCH type heaps can potentially fail,
but in practise it never should in this case, hence the asserts. */
if (n_mutexes == 0) { if (n_mutexes == 0) {
if (in_btr_search) { if (in_btr_search) {
table->heap = mem_heap_create_in_btr_search(4096); table->heap = mem_heap_create_in_btr_search(4096);
ut_a(table->heap);
} else { } else {
table->heap = mem_heap_create_in_buffer(4096); table->heap = mem_heap_create_in_buffer(4096);
} }
...@@ -57,6 +61,7 @@ ha_create( ...@@ -57,6 +61,7 @@ ha_create(
for (i = 0; i < n_mutexes; i++) { for (i = 0; i < n_mutexes; i++) {
if (in_btr_search) { if (in_btr_search) {
table->heaps[i] = mem_heap_create_in_btr_search(4096); table->heaps[i] = mem_heap_create_in_btr_search(4096);
ut_a(table->heaps[i]);
} else { } else {
table->heaps[i] = mem_heap_create_in_buffer(4096); table->heaps[i] = mem_heap_create_in_buffer(4096);
} }
......
...@@ -68,21 +68,6 @@ ha_insert_for_fold( ...@@ -68,21 +68,6 @@ ha_insert_for_fold(
node is created! */ node is created! */
void* data); /* in: data, must not be NULL */ void* data); /* in: data, must not be NULL */
/***************************************************************** /*****************************************************************
Reserves the necessary hash table mutex and inserts an entry into the hash
table. */
UNIV_INLINE
ibool
ha_insert_for_fold_mutex(
/*=====================*/
/* out: TRUE if succeed, FALSE if no more
memory could be allocated */
hash_table_t* table, /* in: hash table */
ulint fold, /* in: folded value of data; if a node with
the same fold value already exists, it is
updated to point to the same data, and no new
node is created! */
void* data); /* in: data, must not be NULL */
/*****************************************************************
Deletes an entry from a hash table. */ Deletes an entry from a hash table. */
void void
......
...@@ -191,30 +191,3 @@ ha_search_and_delete_if_found( ...@@ -191,30 +191,3 @@ ha_search_and_delete_if_found(
return(FALSE); return(FALSE);
} }
/*****************************************************************
Reserves the necessary hash table mutex and inserts an entry into the hash
table. */
UNIV_INLINE
ibool
ha_insert_for_fold_mutex(
/*=====================*/
/* out: TRUE if succeed, FALSE if no more
memory could be allocated */
hash_table_t* table, /* in: hash table */
ulint fold, /* in: folded value of data; if a node with
the same fold value already exists, it is
updated to point to the same data, and no new
node is created! */
void* data) /* in: data, must not be NULL */
{
ibool ret;
hash_mutex_enter(table, fold);
ret = ha_insert_for_fold(table, fold, data);
hash_mutex_exit(table, fold);
return(ret);
}
...@@ -31,13 +31,18 @@ typedef mem_block_info_t mem_block_t; ...@@ -31,13 +31,18 @@ typedef mem_block_info_t mem_block_t;
typedef mem_block_t mem_heap_t; typedef mem_block_t mem_heap_t;
/* Types of allocation for memory heaps: DYNAMIC means allocation from the /* Types of allocation for memory heaps: DYNAMIC means allocation from the
dynamic memory pool of the C compiler, BUFFER means allocation from the index dynamic memory pool of the C compiler, BUFFER means allocation from the
page buffer pool; the latter method is used for very big heaps */ buffer pool; the latter method is used for very big heaps */
#define MEM_HEAP_DYNAMIC 0 /* the most common type */ #define MEM_HEAP_DYNAMIC 0 /* the most common type */
#define MEM_HEAP_BUFFER 1 #define MEM_HEAP_BUFFER 1
#define MEM_HEAP_BTR_SEARCH 2 /* this flag can be ORed to the #define MEM_HEAP_BTR_SEARCH 2 /* this flag can optionally be
previous */ 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. */
/* The following start size is used for the first block in the memory heap if /* 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 the size is not specified, i.e., 0 is given as the parameter in the call of
...@@ -98,13 +103,15 @@ heap freeing. */ ...@@ -98,13 +103,15 @@ heap freeing. */
(heap), __FILE__, __LINE__) (heap), __FILE__, __LINE__)
/********************************************************************* /*********************************************************************
NOTE: Use the corresponding macros instead of this function. Creates a NOTE: Use the corresponding macros instead of this function. Creates a
memory heap which allocates memory from dynamic space. For debugging memory heap. For debugging purposes, takes also the file name and line as
purposes, takes also the file name and line as argument. */ arguments. */
UNIV_INLINE UNIV_INLINE
mem_heap_t* mem_heap_t*
mem_heap_create_func( mem_heap_create_func(
/*=================*/ /*=================*/
/* out, own: memory heap */ /* 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 this means that a single user buffer
of size n will fit in the block, of size n will fit in the block,
...@@ -121,11 +128,9 @@ mem_heap_create_func( ...@@ -121,11 +128,9 @@ mem_heap_create_func(
block is not unintentionally erased block is not unintentionally erased
(if allocated in the stack), before (if allocated in the stack), before
the memory heap is explicitly freed. */ the memory heap is explicitly freed. */
ulint type, /* in: MEM_HEAP_DYNAMIC ulint type, /* in: heap type */
or MEM_HEAP_BUFFER */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line /* in: line where created */ ulint line); /* in: line where created */
);
/********************************************************************* /*********************************************************************
NOTE: Use the corresponding macro instead of this function. Frees the space 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 occupied by a memory heap. In the debug version erases the heap memory
...@@ -143,8 +148,9 @@ UNIV_INLINE ...@@ -143,8 +148,9 @@ UNIV_INLINE
void* void*
mem_heap_alloc( mem_heap_alloc(
/*===========*/ /*===========*/
/* out: allocated storage, NULL if /* out: allocated storage, NULL if did not
did not succeed */ succeed (only possible for
MEM_HEAP_BTR_SEARCH type heaps) */
mem_heap_t* heap, /* in: memory heap */ mem_heap_t* heap, /* in: memory heap */
ulint n); /* in: number of bytes; if the heap is allowed ulint n); /* in: number of bytes; if the heap is allowed
to grow into the buffer pool, this must be to grow into the buffer pool, this must be
...@@ -220,8 +226,7 @@ UNIV_INLINE ...@@ -220,8 +226,7 @@ UNIV_INLINE
void* void*
mem_alloc_func( mem_alloc_func(
/*===========*/ /*===========*/
/* out, own: free storage, NULL /* out, own: free storage */
if did not succeed */
ulint n, /* in: desired number of bytes */ ulint n, /* in: desired number of bytes */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line /* in: line where created */ ulint line /* in: line where created */
...@@ -235,8 +240,7 @@ with mem_free. */ ...@@ -235,8 +240,7 @@ with mem_free. */
void* void*
mem_alloc_func_noninline( mem_alloc_func_noninline(
/*=====================*/ /*=====================*/
/* out, own: free storage, /* out, own: free storage */
NULL if did not succeed */
ulint n, /* in: desired number of bytes */ ulint n, /* in: desired number of bytes */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line /* in: line where created */ ulint line /* in: line where created */
......
...@@ -16,8 +16,9 @@ Creates a memory heap block where data can be allocated. */ ...@@ -16,8 +16,9 @@ Creates a memory heap block where data can be allocated. */
mem_block_t* mem_block_t*
mem_heap_create_block( mem_heap_create_block(
/*==================*/ /*==================*/
/* out, own: memory heap block, /* out, own: memory heap block, NULL if
NULL if did not succeed */ 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 */ should be created */
ulint n, /* in: number of bytes needed for user data, or ulint n, /* in: number of bytes needed for user data, or
...@@ -50,7 +51,8 @@ mem_block_t* ...@@ -50,7 +51,8 @@ mem_block_t*
mem_heap_add_block( mem_heap_add_block(
/*===============*/ /*===============*/
/* out: created block, NULL if did not /* out: created block, NULL if did not
succeed */ succeed (only possible for
MEM_HEAP_BTR_SEARCH type heaps)*/
mem_heap_t* heap, /* in: memory heap */ mem_heap_t* heap, /* in: memory heap */
ulint n); /* in: number of bytes user needs */ ulint n); /* in: number of bytes user needs */
...@@ -126,7 +128,9 @@ UNIV_INLINE ...@@ -126,7 +128,9 @@ UNIV_INLINE
void* void*
mem_heap_alloc( mem_heap_alloc(
/*===========*/ /*===========*/
/* out: allocated storage */ /* out: allocated storage, NULL if did not
succeed (only possible for
MEM_HEAP_BTR_SEARCH type heaps) */
mem_heap_t* heap, /* in: memory heap */ mem_heap_t* heap, /* in: memory heap */
ulint n) /* in: number of bytes; if the heap is allowed ulint n) /* in: number of bytes; if the heap is allowed
to grow into the buffer pool, this must be to grow into the buffer pool, this must be
...@@ -370,13 +374,15 @@ mem_heap_free_top( ...@@ -370,13 +374,15 @@ mem_heap_free_top(
/********************************************************************* /*********************************************************************
NOTE: Use the corresponding macros instead of this function. Creates a NOTE: Use the corresponding macros instead of this function. Creates a
memory heap which allocates memory from dynamic space. For debugging memory heap. For debugging purposes, takes also the file name and line as
purposes, takes also the file name and line as argument. */ argument. */
UNIV_INLINE UNIV_INLINE
mem_heap_t* mem_heap_t*
mem_heap_create_func( mem_heap_create_func(
/*=================*/ /*=================*/
/* out, own: memory heap */ /* 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 this means that a single user buffer
of size n will fit in the block, of size n will fit in the block,
...@@ -393,11 +399,9 @@ mem_heap_create_func( ...@@ -393,11 +399,9 @@ mem_heap_create_func(
block is not unintentionally erased block is not unintentionally erased
(if allocated in the stack), before (if allocated in the stack), before
the memory heap is explicitly freed. */ the memory heap is explicitly freed. */
ulint type, /* in: MEM_HEAP_DYNAMIC ulint type, /* in: heap type */
or MEM_HEAP_BUFFER */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line /* in: line where created */ ulint line) /* in: line where created */
)
{ {
mem_block_t* block; mem_block_t* block;
...@@ -409,8 +413,11 @@ mem_heap_create_func( ...@@ -409,8 +413,11 @@ mem_heap_create_func(
init_block, type, file_name, line); init_block, type, file_name, line);
} }
ut_ad(block); if (block == NULL) {
return(NULL);
}
UT_LIST_INIT(block->base); UT_LIST_INIT(block->base);
/* Add the created block itself as the first block in the list */ /* Add the created block itself as the first block in the list */
...@@ -418,11 +425,6 @@ mem_heap_create_func( ...@@ -418,11 +425,6 @@ mem_heap_create_func(
#ifdef UNIV_MEM_DEBUG #ifdef UNIV_MEM_DEBUG
if (block == NULL) {
return(block);
}
mem_hash_insert(block, file_name, line); mem_hash_insert(block, file_name, line);
#endif #endif
...@@ -484,8 +486,7 @@ UNIV_INLINE ...@@ -484,8 +486,7 @@ UNIV_INLINE
void* void*
mem_alloc_func( mem_alloc_func(
/*===========*/ /*===========*/
/* out, own: free storage, NULL /* out, own: free storage */
if did not succeed */
ulint n, /* in: desired number of bytes */ ulint n, /* in: desired number of bytes */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line /* in: line where created */ ulint line /* in: line where created */
...@@ -496,11 +497,7 @@ mem_alloc_func( ...@@ -496,11 +497,7 @@ mem_alloc_func(
heap = mem_heap_create_func(n, NULL, MEM_HEAP_DYNAMIC, file_name, heap = mem_heap_create_func(n, NULL, MEM_HEAP_DYNAMIC, file_name,
line); line);
if (heap == NULL) {
return(NULL);
}
/* Note that as we created the first block in the heap big enough /* 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 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 first block and thus we can calculate the pointer to the heap from
......
...@@ -1705,7 +1705,7 @@ static ...@@ -1705,7 +1705,7 @@ static
lock_t* lock_t*
lock_rec_create( lock_rec_create(
/*============*/ /*============*/
/* out: created lock, NULL if out of memory */ /* out: created lock */
ulint type_mode,/* in: lock mode and wait flag, type is ulint type_mode,/* in: lock mode and wait flag, type is
ignored and replaced by LOCK_REC */ ignored and replaced by LOCK_REC */
rec_t* rec, /* in: record on page */ rec_t* rec, /* in: record on page */
...@@ -1746,11 +1746,6 @@ lock_rec_create( ...@@ -1746,11 +1746,6 @@ lock_rec_create(
n_bytes = 1 + n_bits / 8; n_bytes = 1 + n_bits / 8;
lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t) + n_bytes); lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t) + n_bytes);
if (UNIV_UNLIKELY(lock == NULL)) {
return(NULL);
}
UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock); UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock);
...@@ -1886,8 +1881,7 @@ static ...@@ -1886,8 +1881,7 @@ static
lock_t* lock_t*
lock_rec_add_to_queue( lock_rec_add_to_queue(
/*==================*/ /*==================*/
/* out: lock where the bit was set, NULL if out /* out: lock where the bit was set */
of memory */
ulint type_mode,/* in: lock mode, wait, gap etc. flags; ulint type_mode,/* in: lock mode, wait, gap etc. flags;
type is ignored and replaced by LOCK_REC */ type is ignored and replaced by LOCK_REC */
rec_t* rec, /* in: record on page */ rec_t* rec, /* in: record on page */
...@@ -3405,8 +3399,7 @@ UNIV_INLINE ...@@ -3405,8 +3399,7 @@ UNIV_INLINE
lock_t* lock_t*
lock_table_create( lock_table_create(
/*==============*/ /*==============*/
/* out, own: new lock object, or NULL if /* out, own: new lock object */
out of memory */
dict_table_t* table, /* in: database table in dictionary cache */ dict_table_t* table, /* in: database table in dictionary cache */
ulint type_mode,/* in: lock mode possibly ORed with ulint type_mode,/* in: lock mode possibly ORed with
LOCK_WAIT */ LOCK_WAIT */
...@@ -3432,11 +3425,6 @@ lock_table_create( ...@@ -3432,11 +3425,6 @@ lock_table_create(
lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t)); lock = mem_heap_alloc(trx->lock_heap, sizeof(lock_t));
} }
if (lock == NULL) {
return(NULL);
}
UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock); UT_LIST_ADD_LAST(trx_locks, trx->trx_locks, lock);
lock->type_mode = type_mode | LOCK_TABLE; lock->type_mode = type_mode | LOCK_TABLE;
......
...@@ -92,12 +92,10 @@ with mem_free. */ ...@@ -92,12 +92,10 @@ with mem_free. */
void* void*
mem_alloc_func_noninline( mem_alloc_func_noninline(
/*=====================*/ /*=====================*/
/* out, own: free storage, /* out, own: free storage */
NULL if did not succeed */
ulint n, /* in: desired number of bytes */ ulint n, /* in: desired number of bytes */
const char* file_name, /* in: file name where created */ const char* file_name, /* in: file name where created */
ulint line /* in: line where created */ ulint line) /* in: line where created */
)
{ {
return(mem_alloc_func(n, file_name, line)); return(mem_alloc_func(n, file_name, line));
} }
...@@ -122,8 +120,9 @@ Creates a memory heap block where data can be allocated. */ ...@@ -122,8 +120,9 @@ Creates a memory heap block where data can be allocated. */
mem_block_t* mem_block_t*
mem_heap_create_block( mem_heap_create_block(
/*==================*/ /*==================*/
/* out, own: memory heap block, /* out, own: memory heap block, NULL if
NULL if did not succeed */ 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 */ should be created */
ulint n, /* in: number of bytes needed for user data, or ulint n, /* in: number of bytes needed for user data, or
...@@ -182,6 +181,8 @@ mem_heap_create_block( ...@@ -182,6 +181,8 @@ mem_heap_create_block(
} }
if (block == NULL) { if (block == NULL) {
/* Only MEM_HEAP_BTR_SEARCH allocation should ever fail. */
ut_a(type & MEM_HEAP_BTR_SEARCH);
return(NULL); return(NULL);
} }
...@@ -222,7 +223,8 @@ mem_block_t* ...@@ -222,7 +223,8 @@ mem_block_t*
mem_heap_add_block( mem_heap_add_block(
/*===============*/ /*===============*/
/* out: created block, NULL if did not /* out: created block, NULL if did not
succeed */ succeed (only possible for
MEM_HEAP_BTR_SEARCH type heaps)*/
mem_heap_t* heap, /* in: memory heap */ mem_heap_t* heap, /* in: memory heap */
ulint n) /* in: number of bytes user needs */ ulint n) /* in: number of bytes user needs */
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment