2019-08-08 22:04:05 +02:00
|
|
|
/* Copyright 2019 MariaDB corporation AB
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _my_stack_alloc_h
|
|
|
|
#define _my_stack_alloc_h
|
|
|
|
|
|
|
|
/*
|
2019-08-22 20:24:15 +02:00
|
|
|
Do allocation through alloca if there is enough stack available.
|
2019-08-08 22:04:05 +02:00
|
|
|
If not, use my_malloc() instead.
|
|
|
|
|
2019-08-22 20:24:15 +02:00
|
|
|
The idea is that to be able to alloc as much as possible through the
|
2019-08-08 22:04:05 +02:00
|
|
|
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
|
|
|
|
available.
|
|
|
|
This is for example used by Aria when traversing the b-tree and the code
|
|
|
|
needs to allocate one b-tree page and a few keys for each recursion. Even
|
|
|
|
if there is not space to allocate the b-tree pages on stack we can still
|
|
|
|
continue to allocate the keys.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Default suggested allocations
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Allocate big blocks as long as there is this much left */
|
|
|
|
#define STACK_ALLOC_BIG_BLOCK 1024*64
|
|
|
|
|
|
|
|
/* Allocate small blocks as long as there is this much left */
|
|
|
|
#define STACK_ALLOC_SMALL_BLOCK 1024*32
|
|
|
|
|
2019-08-22 20:24:15 +02:00
|
|
|
/* Allocate small blocks as long as there is this much left */
|
|
|
|
#define STACK_ALLOC_SMALL_BLOCK_SIZE 4096
|
2019-08-08 22:04:05 +02:00
|
|
|
|
|
|
|
/*
|
2019-08-22 20:24:15 +02:00
|
|
|
Allocate a block on stack or through malloc.
|
2019-08-08 22:04:05 +02:00
|
|
|
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
|
2019-08-22 20:24:15 +02:00
|
|
|
#define alloc_on_stack(stack_end, res, must_be_freed, size) \
|
|
|
|
do \
|
2019-08-08 22:04:05 +02:00
|
|
|
{ \
|
2019-08-22 20:24:15 +02:00
|
|
|
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)))) \
|
2019-08-08 22:04:05 +02:00
|
|
|
{ \
|
|
|
|
(must_be_freed)= 0; \
|
|
|
|
(res)= alloca(size); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
(must_be_freed)= 1; \
|
2020-01-29 13:50:26 +01:00
|
|
|
(res)= my_malloc(PSI_INSTRUMENT_ME, size, MYF(MY_THREAD_SPECIFIC | MY_WME)); \
|
2019-08-08 22:04:05 +02:00
|
|
|
} \
|
2019-08-22 20:24:15 +02:00
|
|
|
} while(0)
|
2019-08-08 22:04:05 +02:00
|
|
|
#else
|
2019-08-22 20:24:15 +02:00
|
|
|
#define alloc_on_stack(stack_end, res, must_be_freed, size) \
|
|
|
|
do { \
|
2019-08-08 22:04:05 +02:00
|
|
|
(must_be_freed)= 1; \
|
2020-01-29 13:50:26 +01:00
|
|
|
(res)= my_malloc(PSI_INSTRUMENT_ME, size, MYF(MY_THREAD_SPECIFIC | MY_WME)); \
|
2019-08-22 20:24:15 +02:00
|
|
|
} while(0)
|
2019-08-08 22:04:05 +02:00
|
|
|
#endif /* HAVE_ALLOCA */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Free memory allocated by stack_alloc
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void stack_alloc_free(void *res, my_bool must_be_freed)
|
|
|
|
{
|
|
|
|
if (must_be_freed)
|
|
|
|
my_free(res);
|
|
|
|
}
|
|
|
|
#endif /* _my_stack_alloc_h */
|