| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2006, 2016, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify it under |
| 6 | the terms of the GNU General Public License as published by the Free Software |
| 7 | Foundation; version 2 of the License. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License along with |
| 14 | this program; if not, write to the Free Software Foundation, Inc., |
| 15 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 16 | |
| 17 | *****************************************************************************/ |
| 18 | |
| 19 | /*******************************************************************//** |
| 20 | @file ut/ut0vec.cc |
| 21 | A vector of pointers to data items |
| 22 | |
| 23 | Created 4/6/2006 Osku Salerma |
| 24 | ************************************************************************/ |
| 25 | |
| 26 | #include "ut0vec.h" |
| 27 | #include "mem0mem.h" |
| 28 | |
| 29 | /******************************************************************** |
| 30 | Create a new vector with the given initial size. */ |
| 31 | ib_vector_t* |
| 32 | ib_vector_create( |
| 33 | /*=============*/ |
| 34 | /* out: vector */ |
| 35 | ib_alloc_t* allocator, /* in: vector allocator */ |
| 36 | ulint sizeof_value, /* in: size of data item */ |
| 37 | ulint size) /* in: initial size */ |
| 38 | { |
| 39 | ib_vector_t* vec; |
| 40 | |
| 41 | ut_a(size > 0); |
| 42 | |
| 43 | vec = static_cast<ib_vector_t*>( |
| 44 | allocator->mem_malloc(allocator, sizeof(*vec))); |
| 45 | |
| 46 | vec->used = 0; |
| 47 | vec->total = size; |
| 48 | vec->allocator = allocator; |
| 49 | vec->sizeof_value = sizeof_value; |
| 50 | |
| 51 | vec->data = static_cast<void*>( |
| 52 | allocator->mem_malloc(allocator, vec->sizeof_value * size)); |
| 53 | |
| 54 | return(vec); |
| 55 | } |
| 56 | |
| 57 | /******************************************************************** |
| 58 | Resize the vector, currently the vector can only grow and we |
| 59 | expand the number of elements it can hold by 2 times. */ |
| 60 | void |
| 61 | ib_vector_resize( |
| 62 | /*=============*/ |
| 63 | ib_vector_t* vec) /* in: vector */ |
| 64 | { |
| 65 | ulint new_total = vec->total * 2; |
| 66 | ulint old_size = vec->used * vec->sizeof_value; |
| 67 | ulint new_size = new_total * vec->sizeof_value; |
| 68 | |
| 69 | vec->data = static_cast<void*>(vec->allocator->mem_resize( |
| 70 | vec->allocator, vec->data, old_size, new_size)); |
| 71 | |
| 72 | vec->total = new_total; |
| 73 | } |
| 74 | |