Skip to content

Commit

Permalink
Cherry pick dynamic array changes from commit:
Browse files Browse the repository at this point in the history
commit 85fd3d9
Author: Monty <monty@mariadb.org>
Date:   Fri Aug 29 14:07:43 2014 +0300

    my_alloc.c
    - Changed 0x%lx -> %p
    array.c:
    - Static (preallocated) buffer can now be anywhere
    my_sys.h
    - Define MY_INIT_BUFFER_USED
    sql_delete.cc & sql_lex.cc
    - Use memroot when allocating classes (avoids call to current_thd)
    sql_explain.h:
    - Use preallocated buffers
    sql_explain.cc:
    - Use preallocated buffers and memroot
    sql_select.cc:
    - Use multi_alloc_root() instead of many alloc_root()
    - Update calls to Explain
  • Loading branch information
svoj committed Dec 5, 2014
1 parent 9748087 commit 9127784
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
5 changes: 5 additions & 0 deletions include/my_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ typedef struct my_aio_result {
#define MY_SHORT_WAIT 64 /* my_lock() don't wait if can't lock */
#define MY_FORCE_LOCK 128 /* use my_lock() even if disable_locking */
#define MY_NO_WAIT 256 /* my_lock() don't wait at all */
/*
init_dynamic_array() has init buffer; Internal flag, not to be used by
caller.
*/
#define MY_INIT_BUFFER_USED 256
#define MY_ZEROFILL 32 /* my_malloc(), fill array with zero */
#define MY_ALLOW_ZERO_PTR 64 /* my_realloc() ; zero ptr -> malloc */
#define MY_FREE_ON_ERROR 128 /* my_realloc() ; Free old ptr on error */
Expand Down
41 changes: 22 additions & 19 deletions mysys/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
init_alloc eilements.
Array is usable even if space allocation failed, hence, the
function never returns TRUE.
Static buffers must begin immediately after the array structure.
RETURN VALUE
FALSE Ok
Expand All @@ -57,8 +56,12 @@ my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size,
array->alloc_increment=alloc_increment;
array->size_of_element=element_size;
array->malloc_flags= my_flags;
DBUG_ASSERT((my_flags & MY_INIT_BUFFER_USED) == 0);
if ((array->buffer= init_buffer))
{
array->malloc_flags|= MY_INIT_BUFFER_USED;
DBUG_RETURN(FALSE);
}
/*
Since the dynamic array is usable even if allocation fails here malloc
should not throw an error
Expand Down Expand Up @@ -124,10 +127,10 @@ void *alloc_dynamic(DYNAMIC_ARRAY *array)
if (array->elements == array->max_element)
{
char *new_ptr;
if (array->buffer == (uchar *)(array + 1))
if (array->malloc_flags & MY_INIT_BUFFER_USED)
{
/*
In this senerio, the buffer is statically preallocated,
In this scenario, the buffer is statically preallocated,
so we have to create an all-new malloc since we overflowed
*/
if (!(new_ptr= (char *) my_malloc((array->max_element+
Expand All @@ -137,6 +140,7 @@ void *alloc_dynamic(DYNAMIC_ARRAY *array)
DBUG_RETURN(0);
memcpy(new_ptr, array->buffer,
array->elements * array->size_of_element);
array->malloc_flags&= ~MY_INIT_BUFFER_USED;
}
else if (!(new_ptr=(char*)
my_realloc(array->buffer,(array->max_element+
Expand Down Expand Up @@ -231,7 +235,7 @@ my_bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
uchar *new_ptr;
size= (max_elements + array->alloc_increment)/array->alloc_increment;
size*= array->alloc_increment;
if (array->buffer == (uchar *)(array + 1))
if (array->malloc_flags & MY_INIT_BUFFER_USED)
{
/*
In this senerio, the buffer is statically preallocated,
Expand All @@ -243,7 +247,8 @@ my_bool allocate_dynamic(DYNAMIC_ARRAY *array, uint max_elements)
DBUG_RETURN(0);
memcpy(new_ptr, array->buffer,
array->elements * array->size_of_element);
}
array->malloc_flags&= ~MY_INIT_BUFFER_USED;
}
else if (!(new_ptr= (uchar*) my_realloc(array->buffer,size*
array->size_of_element,
MYF(MY_WME | MY_ALLOW_ZERO_PTR |
Expand Down Expand Up @@ -293,15 +298,11 @@ void delete_dynamic(DYNAMIC_ARRAY *array)
/*
Just mark as empty if we are using a static buffer
*/
if (array->buffer == (uchar *)(array + 1))
array->elements= 0;
else
if (array->buffer)
{
if (!(array->malloc_flags & MY_INIT_BUFFER_USED) && array->buffer)
my_free(array->buffer);
array->buffer=0;
array->elements=array->max_element=0;
}

array->buffer= 0;
array->elements= array->max_element= 0;
}

/*
Expand Down Expand Up @@ -350,24 +351,25 @@ void delete_dynamic_with_callback(DYNAMIC_ARRAY *array, FREE_FUNC f) {

void freeze_size(DYNAMIC_ARRAY *array)
{
uint elements=MY_MAX(array->elements,1);
uint elements;

/*
Do nothing if we are using a static buffer
*/
if (array->buffer == (uchar *)(array + 1))
if (array->malloc_flags & MY_INIT_BUFFER_USED)
return;

if (array->buffer && array->max_element != elements)
elements= MY_MAX(array->elements, 1);
if (array->buffer && array->max_element > elements)
{
array->buffer=(uchar*) my_realloc(array->buffer,
elements*array->size_of_element,
elements*array->size_of_element,
MYF(MY_WME | array->malloc_flags));
array->max_element=elements;
array->max_element= elements;
}
}


#ifdef NOT_USED
/*
Get the index of a dynamic element
Expand All @@ -391,3 +393,4 @@ int get_index_dynamic(DYNAMIC_ARRAY *array, void* element)
return ret;

}
#endif
15 changes: 8 additions & 7 deletions mysys/my_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ void init_alloc_root(MEM_ROOT *mem_root, size_t block_size,
myf my_flags)
{
DBUG_ENTER("init_alloc_root");
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
DBUG_PRINT("enter",("root: %p prealloc: %zu", mem_root,
pre_alloc_size));

mem_root->free= mem_root->used= mem_root->pre_alloc= 0;
mem_root->min_malloc= 32;
Expand Down Expand Up @@ -164,7 +165,7 @@ void *alloc_root(MEM_ROOT *mem_root, size_t length)
#if defined(HAVE_valgrind) && defined(EXTRA_DEBUG)
reg1 USED_MEM *next;
DBUG_ENTER("alloc_root");
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
DBUG_PRINT("enter",("root: %p", mem_root));

DBUG_ASSERT(alloc_root_inited(mem_root));

Expand All @@ -188,16 +189,16 @@ void *alloc_root(MEM_ROOT *mem_root, size_t length)
next->next= mem_root->used;
next->size= length;
mem_root->used= next;
DBUG_PRINT("exit",("ptr: 0x%lx", (long) (((char*) next)+
ALIGN_SIZE(sizeof(USED_MEM)))));
DBUG_PRINT("exit",("ptr: %p", (((char*) next)+
ALIGN_SIZE(sizeof(USED_MEM)))));
DBUG_RETURN((uchar*) (((char*) next)+ALIGN_SIZE(sizeof(USED_MEM))));
#else
size_t get_size, block_size;
uchar* point;
reg1 USED_MEM *next= 0;
reg2 USED_MEM **prev;
DBUG_ENTER("alloc_root");
DBUG_PRINT("enter",("root: 0x%lx", (long) mem_root));
DBUG_PRINT("enter",("root: %p", mem_root));
DBUG_ASSERT(alloc_root_inited(mem_root));

DBUG_EXECUTE_IF("simulate_out_of_memory",
Expand Down Expand Up @@ -256,7 +257,7 @@ void *alloc_root(MEM_ROOT *mem_root, size_t length)
mem_root->first_block_usage= 0;
}
TRASH_ALLOC(point, length);
DBUG_PRINT("exit",("ptr: 0x%lx", (ulong) point));
DBUG_PRINT("exit",("ptr: %p", point));
DBUG_RETURN((void*) point);
#endif
}
Expand Down Expand Up @@ -368,7 +369,7 @@ void free_root(MEM_ROOT *root, myf MyFlags)
{
reg1 USED_MEM *next,*old;
DBUG_ENTER("free_root");
DBUG_PRINT("enter",("root: 0x%lx flags: %u", (long) root, (uint) MyFlags));
DBUG_PRINT("enter",("root: %p flags: %u", root, (uint) MyFlags));

if (MyFlags & MY_MARK_BLOCKS_FREE)
{
Expand Down
7 changes: 7 additions & 0 deletions sql/sql_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ template <class Elem> class Dynamic_array
init(prealloc, increment);
}

Dynamic_array(MEM_ROOT *root, uint prealloc=16, uint increment=16)
{
void *init_buffer= alloc_root(root, sizeof(Elem) * prealloc);
my_init_dynamic_array2(&array, sizeof(Elem), init_buffer,
prealloc, increment, MYF(0));
}

void init(uint prealloc=16, uint increment=16)
{
my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment,
Expand Down

0 comments on commit 9127784

Please sign in to comment.