diff options
author | Sergey Vojtovich <svoj@mariadb.org> | 2014-12-04 17:35:55 +0400 |
---|---|---|
committer | Sergey Vojtovich <svoj@mariadb.org> | 2014-12-05 11:01:50 +0400 |
commit | 9127784d5ce845c9d66a34eee371cbaf82468d89 (patch) | |
tree | ce1b583eb90d483665db97c1a13335ae4a326e0c /mysys/array.c | |
parent | 974808772b63563d031f687fd0fd158f83eb94aa (diff) | |
download | mariadb-git-9127784d5ce845c9d66a34eee371cbaf82468d89.tar.gz |
Cherry pick dynamic array changes from commit:
commit 85fd3d901311688e18ffce92ffc78129e5625791
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
Diffstat (limited to 'mysys/array.c')
-rw-r--r-- | mysys/array.c | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/mysys/array.c b/mysys/array.c index a8c5d181638..35a41f2222c 100644 --- a/mysys/array.c +++ b/mysys/array.c @@ -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 @@ -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 @@ -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+ @@ -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+ @@ -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, @@ -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 | @@ -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; } /* @@ -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 @@ -391,3 +393,4 @@ int get_index_dynamic(DYNAMIC_ARRAY *array, void* element) return ret; } +#endif |