From 8075b05b7dc03f67abc7c7a3d788dcf87b3483c9 Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Sun, 16 Jun 2013 21:26:40 +0300 Subject: More merge fixes: - mdl.cc and mdl.h merged completely - mysql_system_tables*.sql merged completely - Fixed wrong merge of lock_tables - Added some missing functions: - bool THD::notify_shared_lock() - Dynamic_array::pop, Dynamic_array::del - Added MDL_context_owner to THD - Added metadata_locks_hash_instances --- sql/sql_array.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'sql/sql_array.h') diff --git a/sql/sql_array.h b/sql/sql_array.h index f07126bc0ef..9ac27cca63b 100644 --- a/sql/sql_array.h +++ b/sql/sql_array.h @@ -124,6 +124,17 @@ public: return (insert_dynamic(&array, (uchar*)&el)); } + /// Pops the last element. Does nothing if array is empty. + Elem& pop() + { + return *((Elem*)pop_dynamic(&array)); + } + + void del(uint idx) + { + delete_dynamic_element(&array, idx); + } + int elements() { return array.elements; -- cgit v1.2.1 From dfcc502ab540b4d93fe3d40d0bac15fa3ae449dd Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 19 Jun 2013 14:32:14 +0300 Subject: Finished merging wl5986 started by Igor. --- sql/sql_array.h | 120 ++++++++++++++++++++++++-------------------------------- 1 file changed, 51 insertions(+), 69 deletions(-) (limited to 'sql/sql_array.h') diff --git a/sql/sql_array.h b/sql/sql_array.h index 9ac27cca63b..baed3687215 100644 --- a/sql/sql_array.h +++ b/sql/sql_array.h @@ -99,29 +99,65 @@ template class Dynamic_array DYNAMIC_ARRAY array; public: Dynamic_array(uint prealloc=16, uint increment=16) + { + init(prealloc, increment); + } + + void init(uint prealloc=16, uint increment=16) { my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment, MYF(MY_THREAD_SPECIFIC)); } + /** + @note Though formally this could be declared "const" it would be + misleading at it returns a non-const pointer to array's data. + */ Elem& at(int idx) { return *(((Elem*)array.buffer) + idx); } + /// Const variant of at(), which cannot change data + const Elem& at(int idx) const + { + return *(((Elem*)array.buffer) + idx); + } + /// @returns pointer to first element; undefined behaviour if array is empty Elem *front() { + DBUG_ASSERT(array.elements >= 1); return (Elem*)array.buffer; } + /// @returns pointer to first element; undefined behaviour if array is empty + const Elem *front() const + { + DBUG_ASSERT(array.elements >= 1); + return (const Elem*)array.buffer; + } + + /// @returns pointer to last element; undefined behaviour if array is empty. Elem *back() { - return ((Elem*)array.buffer) + array.elements; + DBUG_ASSERT(array.elements >= 1); + return ((Elem*)array.buffer) + (array.elements - 1); + } + + /// @returns pointer to last element; undefined behaviour if array is empty. + const Elem *back() const + { + DBUG_ASSERT(array.elements >= 1); + return ((const Elem*)array.buffer) + (array.elements - 1); } - bool append(Elem &el) + /** + @retval false ok + @retval true OOM, @c my_error() has been called. + */ + bool append(const Elem &el) { - return (insert_dynamic(&array, (uchar*)&el)); + return insert_dynamic(&array, &el); } /// Pops the last element. Does nothing if array is empty. @@ -135,91 +171,37 @@ public: delete_dynamic_element(&array, idx); } - int elements() + int elements() const { return array.elements; } - ~Dynamic_array() - { - delete_dynamic(&array); - } - - typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2); - - void sort(CMP_FUNC cmp_func) - { - my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func); - } -}; - -/* - Array of pointers to Elem that uses memory from MEM_ROOT - - MEM_ROOT has no realloc() so this is supposed to be used for cases when - reallocations are rare. -*/ - -template class Array -{ - enum {alloc_increment = 16}; - Elem **buffer; - uint n_elements, max_element; -public: - Array(MEM_ROOT *mem_root, uint prealloc=16) - { - buffer= (Elem**)alloc_root(mem_root, prealloc * sizeof(Elem**)); - max_element = buffer? prealloc : 0; - n_elements= 0; - } - - Elem& at(int idx) - { - return *(((Elem*)buffer) + idx); - } - - Elem **front() - { - return buffer; - } - - Elem **back() + void elements(uint num_elements) { - return buffer + n_elements; + DBUG_ASSERT(num_elements <= array.max_element); + array.elements= num_elements; } - bool append(MEM_ROOT *mem_root, Elem *el) + void clear() { - if (n_elements == max_element) - { - Elem **newbuf; - if (!(newbuf= (Elem**)alloc_root(mem_root, (n_elements + alloc_increment)* - sizeof(Elem**)))) - { - return FALSE; - } - memcpy(newbuf, buffer, n_elements*sizeof(Elem*)); - buffer= newbuf; - } - buffer[n_elements++]= el; - return FALSE; + elements(0); } - int elements() + void set(uint idx, const Elem &el) { - return n_elements; + set_dynamic(&array, &el, idx); } - void clear() + ~Dynamic_array() { - n_elements= 0; + delete_dynamic(&array); } - typedef int (*CMP_FUNC)(Elem * const *el1, Elem *const *el2); + typedef int (*CMP_FUNC)(const Elem *el1, const Elem *el2); void sort(CMP_FUNC cmp_func) { - my_qsort(buffer, n_elements, sizeof(Elem*), (qsort_cmp)cmp_func); + my_qsort(array.buffer, array.elements, sizeof(Elem), (qsort_cmp)cmp_func); } }; -- cgit v1.2.1 From c46ce32cfbec0b3c21cb0e5b6bca91bd437130cf Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Thu, 20 Jun 2013 14:49:25 +0300 Subject: Fixed memory leaks. alias.test now runs clean with valgrind --- sql/sql_array.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'sql/sql_array.h') diff --git a/sql/sql_array.h b/sql/sql_array.h index baed3687215..71377b91ef9 100644 --- a/sql/sql_array.h +++ b/sql/sql_array.h @@ -92,6 +92,8 @@ private: /* A typesafe wrapper around DYNAMIC_ARRAY + + TODO: Change creator to take a THREAD_SPECIFIC option. */ template class Dynamic_array @@ -106,7 +108,7 @@ public: void init(uint prealloc=16, uint increment=16) { my_init_dynamic_array(&array, sizeof(Elem), prealloc, increment, - MYF(MY_THREAD_SPECIFIC)); + MYF(0)); } /** -- cgit v1.2.1