MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ut0vec.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2006, 2011, 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 /*******************************************************************/
26 #include "ut0vec.h"
27 #ifdef UNIV_NONINL
28 #include "ut0vec.ic"
29 #endif
30 #include "mem0mem.h"
31 
32 /********************************************************************
33 Create a new vector with the given initial size. */
34 UNIV_INTERN
36 ib_vector_create(
37 /*=============*/
38  /* out: vector */
39  ib_alloc_t* allocator, /* in: vector allocator */
40  ulint sizeof_value, /* in: size of data item */
41  ulint size) /* in: initial size */
42 {
43  ib_vector_t* vec;
44 
45  ut_a(size > 0);
46 
47  vec = static_cast<ib_vector_t*>(
48  allocator->mem_malloc(allocator, sizeof(*vec)));
49 
50  vec->used = 0;
51  vec->total = size;
52  vec->allocator = allocator;
53  vec->sizeof_value = sizeof_value;
54 
55  vec->data = static_cast<void*>(
56  allocator->mem_malloc(allocator, vec->sizeof_value * size));
57 
58  return(vec);
59 }
60 
61 /********************************************************************
62 Resize the vector, currently the vector can only grow and we
63 expand the number of elements it can hold by 2 times. */
64 UNIV_INTERN
65 void
66 ib_vector_resize(
67 /*=============*/
68  ib_vector_t* vec) /* in: vector */
69 {
70  ulint new_total = vec->total * 2;
71  ulint old_size = vec->used * vec->sizeof_value;
72  ulint new_size = new_total * vec->sizeof_value;
73 
74  vec->data = static_cast<void*>(vec->allocator->mem_resize(
75  vec->allocator, vec->data, old_size, new_size));
76 
77  vec->total = new_total;
78 }