From 2e665fb294d2cbac2a5e2f4f9c910600c16da54f Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Thu, 22 Aug 2019 20:24:15 +0200 Subject: [PATCH] alloc_on_stack: simplify the API --- include/my_stack_alloc.h | 56 +++++++---------------- include/myisamchk.h | 2 +- storage/maria/ha_maria.cc | 2 +- storage/maria/ma_blockrec.c | 38 +++++++--------- storage/maria/ma_check.c | 31 +++++-------- storage/maria/ma_delete.c | 82 +++++++++++++--------------------- storage/maria/ma_dynrec.c | 43 +++++++----------- storage/maria/ma_open.c | 2 +- storage/maria/ma_rt_index.c | 89 ++++++++++++++----------------------- storage/maria/ma_rt_split.c | 30 +++++-------- storage/maria/ma_search.c | 11 ++--- storage/maria/ma_write.c | 23 ++++------ storage/maria/maria_chk.c | 19 ++++---- storage/maria/maria_def.h | 17 +------ 14 files changed, 162 insertions(+), 283 deletions(-) diff --git a/include/my_stack_alloc.h b/include/my_stack_alloc.h index 6f6305f248d..2d5a721856d 100644 --- a/include/my_stack_alloc.h +++ b/include/my_stack_alloc.h @@ -18,10 +18,10 @@ #define _my_stack_alloc_h /* - Do allocation trough alloca if there is enough stack available. + Do allocation through alloca if there is enough stack available. If not, use my_malloc() instead. - The idea is that to be able to alloc as much as possible trough the + The idea is that to be able to alloc as much as possible through the stack. To ensure this, we have two different limits, on for big blocks and one for small blocks. This will enable us to continue to do allocation for small blocks even when there is less stack space @@ -42,47 +42,25 @@ /* Allocate small blocks as long as there is this much left */ #define STACK_ALLOC_SMALL_BLOCK 1024*32 -struct st_stack_alloc -{ - void **stack_ends_here; /* Set on init */ - size_t stack_for_big_blocks; - size_t stack_for_small_blocks; - size_t small_block_size; -}; -typedef struct st_stack_alloc STACK_ALLOC; - +/* Allocate small blocks as long as there is this much left */ +#define STACK_ALLOC_SMALL_BLOCK_SIZE 4096 /* - Initialize STACK_ALLOC structure -*/ - -static inline void init_stack_alloc(STACK_ALLOC *alloc, - size_t stack_for_big_blocks, - size_t stack_for_small_blocks, - size_t small_block_size) -{ - alloc->stack_ends_here= &my_thread_var->stack_ends_here; - alloc->stack_for_big_blocks= stack_for_big_blocks; - alloc->stack_for_small_blocks= stack_for_small_blocks; - alloc->small_block_size= small_block_size; -} - - -/* - Allocate a block on stack or trough malloc. + Allocate a block on stack or through malloc. The 'must_be_freed' variable will be set to 1 if malloc was called. 'must_be_freed' must be a variable on the stack! */ #ifdef HAVE_ALLOCA -#define alloc_on_stack(alloc, res, must_be_freed, size) \ +#define alloc_on_stack(stack_end, res, must_be_freed, size) \ +do \ { \ - size_t stack_left= available_stack_size(&(must_be_freed), \ - *(alloc)->stack_ends_here); \ - if (stack_left > (size_t) (size) && \ - ((alloc)->stack_for_big_blocks < stack_left - (size) || \ - (((alloc)->stack_for_small_blocks < stack_left - (size)) && \ - ((alloc)->small_block_size <= (size_t) (size))))) \ + size_t alloc_size= (size); \ + size_t stack_left= available_stack_size(&alloc_size, (stack_end)); \ + if (stack_left > alloc_size && \ + (STACK_ALLOC_BIG_BLOCK < stack_left - alloc_size || \ + ((STACK_ALLOC_SMALL_BLOCK < stack_left - alloc_size) && \ + (STACK_ALLOC_SMALL_BLOCK_SIZE <= alloc_size)))) \ { \ (must_be_freed)= 0; \ (res)= alloca(size); \ @@ -92,13 +70,13 @@ static inline void init_stack_alloc(STACK_ALLOC *alloc, (must_be_freed)= 1; \ (res)= my_malloc(size, MYF(MY_THREAD_SPECIFIC | MY_WME)); \ } \ -} +} while(0) #else -#define alloc_on_stack(alloc, res, must_be_freed, size) \ - { \ +#define alloc_on_stack(stack_end, res, must_be_freed, size) \ + do { \ (must_be_freed)= 1; \ (res)= my_malloc(size, MYF(MY_THREAD_SPECIFIC | MY_WME)); \ - } + } while(0) #endif /* HAVE_ALLOCA */ diff --git a/include/myisamchk.h b/include/myisamchk.h index d734df505fb..f1a7348eea0 100644 --- a/include/myisamchk.h +++ b/include/myisamchk.h @@ -104,7 +104,7 @@ typedef struct st_handler_check_param my_bool retry_repair, force_sort, calc_checksum, static_row_size; char temp_filename[FN_REFLEN]; IO_CACHE read_cache; - STACK_ALLOC stack_alloc; + void **stack_end_ptr; enum_handler_stats_method stats_method; /* For reporting progress */ uint stage, max_stage; diff --git a/storage/maria/ha_maria.cc b/storage/maria/ha_maria.cc index df889aa9f71..a9a2b73a2e6 100644 --- a/storage/maria/ha_maria.cc +++ b/storage/maria/ha_maria.cc @@ -2804,7 +2804,7 @@ int ha_maria::external_lock(THD *thd, int lock_type) file->state= &file->s->state.state; // Restore state if clone /* Remember stack end for this thread */ - aria_init_stack_alloc(file); + file->stack_end_ptr= &ha_thd()->mysys_var->stack_ends_here; DBUG_RETURN(result); } diff --git a/storage/maria/ma_blockrec.c b/storage/maria/ma_blockrec.c index 05a0baacd07..406de66bc9f 100644 --- a/storage/maria/ma_blockrec.c +++ b/storage/maria/ma_blockrec.c @@ -2458,16 +2458,13 @@ static my_bool free_full_pages(MARIA_HA *info, MARIA_ROW *row) /* Compact events by removing filler and tail events */ uchar *new_block= 0; uchar *end, *to, *compact_extent_info; - my_bool result, buff_alloced; + my_bool res, buff_alloced; uint extents_count; - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - row->extents_count * ROW_EXTENT_SIZE); - if (!(compact_extent_info= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, compact_extent_info, buff_alloced, + row->extents_count * ROW_EXTENT_SIZE); + if (!compact_extent_info) + DBUG_RETURN(1); to= compact_extent_info; for (end= extents + row->extents_count * ROW_EXTENT_SIZE ; @@ -2514,14 +2511,14 @@ static my_bool free_full_pages(MARIA_HA *info, MARIA_ROW *row) log_array[TRANSLOG_INTERNAL_PARTS + 0].length= sizeof(log_data); log_array[TRANSLOG_INTERNAL_PARTS + 1].str= compact_extent_info; log_array[TRANSLOG_INTERNAL_PARTS + 1].length= extents_length; - result= translog_write_record(&lsn, LOGREC_REDO_FREE_BLOCKS, info->trn, - info, - (translog_size_t) (sizeof(log_data) + - extents_length), - TRANSLOG_INTERNAL_PARTS + 2, log_array, - log_data, NULL); + res= translog_write_record(&lsn, LOGREC_REDO_FREE_BLOCKS, info->trn, + info, + (translog_size_t) (sizeof(log_data) + + extents_length), + TRANSLOG_INTERNAL_PARTS + 2, log_array, + log_data, NULL); stack_alloc_free(compact_extent_info, buff_alloced); - if (result) + if (res) DBUG_RETURN(1); } @@ -5199,13 +5196,10 @@ my_bool _ma_cmp_block_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def, my_bool buff_alloced; DBUG_ENTER("_ma_cmp_block_unique"); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - info->s->base.reclength); - if (!(old_record= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, old_record, buff_alloced, + info->s->base.reclength); + if (!old_record) + DBUG_RETURN(1); /* Don't let the compare destroy blobs that may be in use */ org_rec_buff= info->rec_buff; diff --git a/storage/maria/ma_check.c b/storage/maria/ma_check.c index 719eb330801..28d5a2d0ee0 100644 --- a/storage/maria/ma_check.c +++ b/storage/maria/ma_check.c @@ -126,10 +126,7 @@ void maria_chk_init(HA_CHECK *param) param->pagecache_block_size= KEY_CACHE_BLOCK_SIZE; param->stats_method= MI_STATS_METHOD_NULLS_NOT_EQUAL; param->max_stage= 1; - init_stack_alloc(¶m->stack_alloc, - STACK_ALLOC_BIG_BLOCK, - STACK_ALLOC_SMALL_BLOCK, - 4096); + param->stack_end_ptr= &my_thread_var->stack_ends_here; } @@ -876,15 +873,12 @@ static int chk_index(HA_CHECK *param, MARIA_HA *info, MARIA_KEYDEF *keyinfo, if (keyinfo->flag & (HA_SPATIAL | HA_RTREE_INDEX)) DBUG_RETURN(0); + alloc_on_stack(*param->stack_end_ptr, temp_buff, temp_buff_alloced, + (keyinfo->block_length + keyinfo->max_store_length)); + if (!temp_buff) { - void *res; - alloc_on_stack(¶m->stack_alloc, res, temp_buff_alloced, - (keyinfo->block_length + keyinfo->max_store_length)); - if (!(temp_buff= res)) - { - _ma_check_print_error(param,"Not enough memory for keyblock"); - DBUG_RETURN(-1); - } + _ma_check_print_error(param,"Not enough memory for keyblock"); + DBUG_RETURN(-1); } tmp_key_buff= temp_buff+ keyinfo->block_length; @@ -3246,15 +3240,12 @@ static int sort_one_index(HA_CHECK *param, MARIA_HA *info, param->new_file_pos+=keyinfo->block_length; key.keyinfo= keyinfo; + alloc_on_stack(*param->stack_end_ptr, buff, buff_alloced, + keyinfo->block_length + keyinfo->max_store_length); + if (!buff) { - void *res; - alloc_on_stack(¶m->stack_alloc, res, buff_alloced, - keyinfo->block_length + keyinfo->max_store_length); - if (!(buff= res)) - { - _ma_check_print_error(param,"Not enough memory for keyblock"); - DBUG_RETURN(-1); - } + _ma_check_print_error(param,"Not enough memory for keyblock"); + DBUG_RETURN(-1); } key.data= buff + keyinfo->block_length; diff --git a/storage/maria/ma_delete.c b/storage/maria/ma_delete.c index 3e3689cfd35..cbba9d975dc 100644 --- a/storage/maria/ma_delete.c +++ b/storage/maria/ma_delete.c @@ -166,13 +166,11 @@ my_bool _ma_ck_delete(MARIA_HA *info, MARIA_KEY *key) DBUG_ENTER("_ma_ck_delete"); LINT_INIT_STRUCT(org_key); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - key->keyinfo->max_store_length); - if (!(key_buff= res)) - DBUG_RETURN(1); - } + + alloc_on_stack(*info->stack_end_ptr, key_buff, buff_alloced, + key->keyinfo->max_store_length); + if (!key_buff) + DBUG_RETURN(1); save_key_data= key->data; if (share->now_transactional) @@ -221,13 +219,10 @@ my_bool _ma_ck_real_delete(register MARIA_HA *info, MARIA_KEY *key, DBUG_RETURN(1); } - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - (keyinfo->block_length + keyinfo->max_store_length*2)); - if (!(root_buff= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, root_buff, buff_alloced, + (keyinfo->block_length + keyinfo->max_store_length*2)); + if (!root_buff) + DBUG_RETURN(1); DBUG_PRINT("info",("root_page: %lu", (ulong) (old_root / keyinfo->block_length))); @@ -305,13 +300,10 @@ static int d_search(MARIA_HA *info, MARIA_KEY *key, uint32 comp_flag, DBUG_ENTER("d_search"); DBUG_DUMP("page", anc_page->buff, anc_page->size); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, lastkey_alloced, - keyinfo->max_store_length); - if (!(lastkey= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, lastkey, lastkey_alloced, + keyinfo->max_store_length); + if (!lastkey) + DBUG_RETURN(1); flag=(*keyinfo->bin_search)(key, anc_page, comp_flag, &keypos, lastkey, &last_key); @@ -406,13 +398,11 @@ static int d_search(MARIA_HA *info, MARIA_KEY *key, uint32 comp_flag, /* Read left child page */ leaf_page.pos= _ma_kpos(nod_flag,keypos); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - (keyinfo->block_length + keyinfo->max_store_length*2)); - if (!(leaf_buff= res)) - goto err; - } + alloc_on_stack(*info->stack_end_ptr, leaf_buff, buff_alloced, + (keyinfo->block_length + keyinfo->max_store_length*2)); + if (!leaf_buff) + goto err; + if (_ma_fetch_keypage(&leaf_page, info,keyinfo, leaf_page.pos, PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, leaf_buff, 0)) @@ -590,13 +580,10 @@ static int del(MARIA_HA *info, MARIA_KEY *key, keypos)); DBUG_DUMP("leaf_buff", leaf_page->buff, leaf_page->size); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, keybuff_alloced, - keyinfo->max_store_length); - if (!(keybuff= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, keybuff, keybuff_alloced, + keyinfo->max_store_length); + if (!keybuff) + DBUG_RETURN(1); page_flag= leaf_page->flag; leaf_length= leaf_page->size; @@ -614,13 +601,10 @@ static int del(MARIA_HA *info, MARIA_KEY *key, { next_page.pos= _ma_kpos(nod_flag,endpos); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - (keyinfo->block_length + keyinfo->max_store_length*2)); - if (!(next_buff= res)) - goto err; - } + alloc_on_stack(*info->stack_end_ptr, next_buff, buff_alloced, + (keyinfo->block_length + keyinfo->max_store_length*2)); + if (!next_buff) + goto err; if (_ma_fetch_keypage(&next_page, info, keyinfo, next_page.pos, PAGECACHE_LOCK_WRITE, DFLT_INIT_HITS, next_buff, 0)) @@ -820,14 +804,12 @@ static int underflow(MARIA_HA *info, MARIA_KEYDEF *keyinfo, DBUG_DUMP("anc_buff", anc_page->buff, anc_page->size); DBUG_DUMP("leaf_buff", leaf_page->buff, leaf_page->size); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->max_store_length*2); - if (!(anc_key_buff= res)) - DBUG_RETURN(1); - leaf_key_buff= anc_key_buff+ keyinfo->max_store_length; - } + alloc_on_stack(*info->stack_end_ptr, anc_key_buff, buff_alloced, + keyinfo->max_store_length*2); + if (!anc_key_buff) + DBUG_RETURN(1); + + leaf_key_buff= anc_key_buff+ keyinfo->max_store_length; anc_page_flag= anc_page->flag; anc_buff= anc_page->buff; diff --git a/storage/maria/ma_dynrec.c b/storage/maria/ma_dynrec.c index a8b33ea9657..a6dee74e57b 100644 --- a/storage/maria/ma_dynrec.c +++ b/storage/maria/ma_dynrec.c @@ -256,14 +256,11 @@ my_bool _ma_write_blob_record(MARIA_HA *info, const uchar *record) reclength= (info->s->base.pack_reclength + _ma_calc_total_blob_length(info,record)+ extra); + alloc_on_stack(*info->stack_end_ptr, rec_buff, buff_alloced, reclength); + if (!rec_buff) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, reclength); - if (!(rec_buff= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */ - return(1); - } + my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */ + return(1); } reclength2= _ma_rec_pack(info, @@ -308,15 +305,13 @@ my_bool _ma_update_blob_record(MARIA_HA *info, MARIA_RECORD_POS pos, } #endif + alloc_on_stack(*info->stack_end_ptr, rec_buff, buff_alloced, reclength); + if (!rec_buff) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, reclength); - if (!(rec_buff= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */ - return(1); - } + my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */ + return(1); } + reclength2= _ma_rec_pack(info, rec_buff+ ALIGN_SIZE(MARIA_MAX_DYN_BLOCK_HEADER), record); @@ -1595,13 +1590,10 @@ my_bool _ma_cmp_dynamic_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def, my_bool error, buff_alloced; DBUG_ENTER("_ma_cmp_dynamic_unique"); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - info->s->base.reclength); - if (!(old_record= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, old_record, buff_alloced, + info->s->base.reclength); + if (!old_record) + DBUG_RETURN(1); /* Don't let the compare destroy blobs that may be in use */ old_rec_buff= info->rec_buff; @@ -1657,12 +1649,9 @@ my_bool _ma_cmp_dynamic_record(register MARIA_HA *info, buffer_length= (info->s->base.pack_reclength + _ma_calc_total_blob_length(info,record)); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, buffer_length); - if (!(buffer= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, buffer, buff_alloced, buffer_length); + if (!buffer) + DBUG_RETURN(1); } if (!(reclength= _ma_rec_pack(info,buffer,record))) goto err; diff --git a/storage/maria/ma_open.c b/storage/maria/ma_open.c index 4995d3832ea..c7080daa265 100644 --- a/storage/maria/ma_open.c +++ b/storage/maria/ma_open.c @@ -1176,7 +1176,7 @@ MARIA_HA *maria_open(const char *name, int mode, uint open_flags, mysql_mutex_unlock(&THR_LOCK_maria); m_info->open_flags= open_flags; - aria_init_stack_alloc(m_info); + m_info->stack_end_ptr= &my_thread_var->stack_ends_here; DBUG_PRINT("exit", ("table: %p name: %s",m_info, name)); DBUG_RETURN(m_info); diff --git a/storage/maria/ma_rt_index.c b/storage/maria/ma_rt_index.c index b7bbbdaf630..b2efe298daa 100644 --- a/storage/maria/ma_rt_index.c +++ b/storage/maria/ma_rt_index.c @@ -68,15 +68,12 @@ static int maria_rtree_find_req(MARIA_HA *info, MARIA_KEYDEF *keyinfo, MARIA_PAGE page; my_bool buff_alloced; + alloc_on_stack(*info->stack_end_ptr, page_buf, buff_alloced, + keyinfo->block_length); + if (!page_buf) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->block_length); - if (!(page_buf= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; - return(-1); - } + my_errno= HA_ERR_OUT_OF_MEM; + return(-1); } if (_ma_fetch_keypage(&page, info, keyinfo, page_pos, @@ -339,15 +336,12 @@ static int maria_rtree_get_req(MARIA_HA *info, MARIA_KEYDEF *keyinfo, my_bool buff_alloced; MARIA_PAGE page; + alloc_on_stack(*info->stack_end_ptr, page_buf, buff_alloced, + keyinfo->block_length); + if (!page_buf) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->block_length); - if (!(page_buf= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; - return(-1); - } + my_errno= HA_ERR_OUT_OF_MEM; + return(-1); } if (_ma_fetch_keypage(&page, info, keyinfo, page_pos, @@ -627,15 +621,12 @@ static int maria_rtree_insert_req(MARIA_HA *info, MARIA_KEY *key, MARIA_PAGE page; DBUG_ENTER("maria_rtree_insert_req"); + alloc_on_stack(*info->stack_end_ptr, page_buf, buff_alloced, + keyinfo->block_length + keyinfo->max_store_length); + if (!page_buf) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->block_length + keyinfo->max_store_length); - if (!(page_buf= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; - DBUG_RETURN(-1); /* purecov: inspected */ - } + my_errno= HA_ERR_OUT_OF_MEM; + DBUG_RETURN(-1); /* purecov: inspected */ } if (_ma_fetch_keypage(&page, info, keyinfo, page_pos, PAGECACHE_LOCK_WRITE, @@ -798,15 +789,12 @@ int maria_rtree_insert_level(MARIA_HA *info, MARIA_KEY *key, int ins_level, DBUG_PRINT("rtree", ("root was split, grow a new root")); + alloc_on_stack(*info->stack_end_ptr, new_root_buf, new_root_buf_alloced, + keyinfo->block_length + keyinfo->max_store_length); + if (!new_root_buf) { - void *res; - alloc_on_stack(&info->stack_alloc, res, new_root_buf_alloced, - keyinfo->block_length + keyinfo->max_store_length); - if (!(new_root_buf= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; - DBUG_RETURN(-1); /* purecov: inspected */ - } + my_errno= HA_ERR_OUT_OF_MEM; + DBUG_RETURN(-1); /* purecov: inspected */ } bzero(new_root_buf, keyinfo->block_length); @@ -956,15 +944,12 @@ static int maria_rtree_delete_req(MARIA_HA *info, const MARIA_KEY *key, MARIA_PAGE page; DBUG_ENTER("maria_rtree_delete_req"); + alloc_on_stack(*info->stack_end_ptr, page_buf, buff_alloced, + keyinfo->block_length); + if (!page_buf) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->block_length); - if (!(page_buf= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; - DBUG_RETURN(-1); - } + my_errno= HA_ERR_OUT_OF_MEM; + DBUG_RETURN(-1); } if (_ma_fetch_keypage(&page, info, keyinfo, page_pos, PAGECACHE_LOCK_WRITE, @@ -1193,15 +1178,12 @@ my_bool maria_rtree_real_delete(MARIA_HA *info, MARIA_KEY *key, if (ReinsertList.n_pages) { + alloc_on_stack(*info->stack_end_ptr, page_buf, buff_alloced, + keyinfo->block_length); + if (!page_buf) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->block_length); - if (!(page_buf= res)) - { - my_errno= HA_ERR_OUT_OF_MEM; - goto err; - } + my_errno= HA_ERR_OUT_OF_MEM; + goto err; } for (i= 0; i < ReinsertList.n_pages; ++i) @@ -1313,13 +1295,10 @@ ha_rows maria_rtree_estimate(MARIA_HA *info, MARIA_KEY *key, uint32 flag) if ((root= share->state.key_root[key->keyinfo->key_nr]) == HA_OFFSET_ERROR) return HA_POS_ERROR; - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->block_length); - if (!(page_buf= res)) - return(HA_POS_ERROR); - } + alloc_on_stack(*info->stack_end_ptr, page_buf, buff_alloced, + keyinfo->block_length); + if (!page_buf) + return(HA_POS_ERROR); if (_ma_fetch_keypage(&page, info, keyinfo, root, PAGECACHE_LOCK_LEFT_UNLOCKED, DFLT_INIT_HITS, page_buf, diff --git a/storage/maria/ma_rt_split.c b/storage/maria/ma_rt_split.c index 3387a9d82ab..a0acb9ce34d 100644 --- a/storage/maria/ma_rt_split.c +++ b/storage/maria/ma_rt_split.c @@ -396,15 +396,11 @@ int maria_rtree_split_page(const MARIA_KEY *key, MARIA_PAGE *page, n_dim= keyinfo->keysegs / 2; - { - void *res; - size_t length= (n_dim * 2 * sizeof(double) * - (max_keys + 1 + 4) + - sizeof(SplitStruct) * (max_keys + 1)); - alloc_on_stack(&info->stack_alloc, res, coord_buf_alloced, length); - if (!(coord_buf= res)) - DBUG_RETURN(-1); - } + alloc_on_stack(*info->stack_end_ptr, coord_buf, coord_buf_alloced, + (n_dim * 2 * sizeof(double) * (max_keys + 1 + 4) + + sizeof(SplitStruct) * (max_keys + 1))); + if (!coord_buf) + DBUG_RETURN(-1); task= (SplitStruct *)(coord_buf + n_dim * 2 * (max_keys + 1 + 4)); @@ -439,17 +435,13 @@ int maria_rtree_split_page(const MARIA_KEY *key, MARIA_PAGE *page, } /* Allocate buffer for new page and piece of log record */ + alloc_on_stack(*info->stack_end_ptr, new_page_buff, new_page_buff_alloced, + (keyinfo->block_length + + (transactional ? max_keys * (2 + 2) + 1 + 2 + 1 + 2 : 0))); + if (!new_page_buff) { - void *res; - size_t len= (keyinfo->block_length + - (transactional ? (max_keys * (2 + 2) + 1 + 2 + 1 + 2) : 0)); - - alloc_on_stack(&info->stack_alloc, res, new_page_buff_alloced, len); - if (!(new_page_buff= res)) - { - err_code= -1; - goto split_err; - } + err_code= -1; + goto split_err; } log_internal_copy= log_internal_copy_ptr= new_page_buff + diff --git a/storage/maria/ma_search.c b/storage/maria/ma_search.c index b5ed64a6843..a57db7d2a2d 100644 --- a/storage/maria/ma_search.c +++ b/storage/maria/ma_search.c @@ -138,13 +138,10 @@ static int _ma_search_no_save(register MARIA_HA *info, MARIA_KEY *key, DBUG_RETURN(1); /* Search at upper levels */ } - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->max_store_length); - if (!(lastkey= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, lastkey, buff_alloced, + keyinfo->max_store_length); + if (!lastkey) + DBUG_RETURN(1); if (_ma_fetch_keypage(&page, info, keyinfo, pos, PAGECACHE_LOCK_READ, DFLT_INIT_HITS, 0, 0)) diff --git a/storage/maria/ma_write.c b/storage/maria/ma_write.c index 1e43454454b..92d90d5c426 100644 --- a/storage/maria/ma_write.c +++ b/storage/maria/ma_write.c @@ -629,13 +629,11 @@ static int w_search(register MARIA_HA *info, uint32 comp_flag, MARIA_KEY *key, DBUG_ENTER("w_search"); DBUG_PRINT("enter", ("page: %lu", (ulong) (page_pos/keyinfo->block_length))); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - (keyinfo->block_length + keyinfo->max_store_length*3)); - if (!(temp_buff= res)) - DBUG_RETURN(1); - } + alloc_on_stack(*info->stack_end_ptr, temp_buff, buff_alloced, + (keyinfo->block_length + keyinfo->max_store_length*3)); + if (!temp_buff) + DBUG_RETURN(1); + keybuff= temp_buff + (keyinfo->block_length + keyinfo->max_store_length*2); if (_ma_fetch_keypage(&page, info, keyinfo, page_pos, PAGECACHE_LOCK_WRITE, @@ -1257,13 +1255,10 @@ static int _ma_balance_page(MARIA_HA *info, MARIA_KEYDEF *keyinfo, MARIA_PAGE next_page, extra_page, *left_page, *right_page; DBUG_ENTER("_ma_balance_page"); - { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - keyinfo->max_store_length); - if (!(tmp_part_key= res)) - DBUG_RETURN(-1); - } + alloc_on_stack(*info->stack_end_ptr, tmp_part_key, buff_alloced, + keyinfo->max_store_length); + if (!tmp_part_key) + DBUG_RETURN(-1); k_length= keyinfo->keylength; father_length= father_page->size; diff --git a/storage/maria/maria_chk.c b/storage/maria/maria_chk.c index 30ac9d5eca9..c85b35570cb 100644 --- a/storage/maria/maria_chk.c +++ b/storage/maria/maria_chk.c @@ -2028,19 +2028,16 @@ static int sort_record_index(MARIA_SORT_PARAM *sort_param, nod_flag= ma_page->node; tmp_key.keyinfo= (MARIA_KEYDEF*) keyinfo; + alloc_on_stack(*info->stack_end_ptr, lastkey, buff_alloced, + (nod_flag ? keyinfo->block_length : 0) + + ALIGN_SIZE(keyinfo->max_store_length)); + if (!lastkey) { - void *res; - alloc_on_stack(&info->stack_alloc, res, buff_alloced, - (nod_flag ? keyinfo->block_length : 0) + - ALIGN_SIZE(keyinfo->max_store_length)); - if (!(lastkey= res)) - { - _ma_check_print_error(param,"Not Enough memory"); - DBUG_RETURN(-1); - } - if (nod_flag) - temp_buff= lastkey + ALIGN_SIZE(keyinfo->max_store_length); + _ma_check_print_error(param,"Not Enough memory"); + DBUG_RETURN(-1); } + if (nod_flag) + temp_buff= lastkey + ALIGN_SIZE(keyinfo->max_store_length); tmp_key.data= lastkey; diff --git a/storage/maria/maria_def.h b/storage/maria/maria_def.h index 110339ba722..720f4e1c783 100644 --- a/storage/maria/maria_def.h +++ b/storage/maria/maria_def.h @@ -623,7 +623,7 @@ struct st_maria_handler MARIA_STATUS_INFO *state_start; /* State at start of transaction */ MARIA_USED_TABLES *used_tables; struct ms3_st *s3; - STACK_ALLOC stack_alloc; + void **stack_end_ptr; MARIA_ROW cur_row; /* The active row that we just read */ MARIA_ROW new_row; /* Storage for a row during update */ MARIA_KEY last_key; /* Last found key */ @@ -1468,18 +1468,3 @@ static inline void unmap_file(MARIA_HA *info __attribute__((unused))) _ma_unmap_file(info); #endif } - -static inline void aria_init_stack_alloc(MARIA_HA *info) -{ -#ifndef DEBUG_STACK_ALLOC - init_stack_alloc(&info->stack_alloc, - STACK_ALLOC_BIG_BLOCK, - STACK_ALLOC_SMALL_BLOCK, - 4096); -#else - /* - Force all allocation to go trough malloc to more easily find corruptions - */ - init_stack_alloc(&info->stack_alloc, 10000000, 10000000, 4096); -#endif /* DEBUG_STACK_ALLOC */ -}