Line data Source code
1 : /*
2 : * Copyright (c) 2013, Andrea Mazzoleni. All rights reserved.
3 : *
4 : * Redistribution and use in source and binary forms, with or without
5 : * modification, are permitted provided that the following conditions
6 : * are met:
7 : *
8 : * 1. Redistributions of source code must retain the above copyright
9 : * notice, this list of conditions and the following disclaimer.
10 : *
11 : * 2. Redistributions in binary form must reproduce the above copyright
12 : * notice, this list of conditions and the following disclaimer in the
13 : * documentation and/or other materials provided with the distribution.
14 : *
15 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 : * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 : * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 : * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 : * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 : * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 : * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 : * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 : * POSSIBILITY OF SUCH DAMAGE.
26 : */
27 :
28 : #include "tommyarrayof.h"
29 :
30 : /******************************************************************************/
31 : /* array */
32 :
33 1 : void tommy_arrayof_init(tommy_arrayof* array, tommy_size_t element_size)
34 : {
35 : tommy_uint_t i;
36 :
37 : /* fixed initial size */
38 1 : array->element_size = element_size;
39 1 : array->bucket_bit = TOMMY_ARRAYOF_BIT;
40 1 : array->bucket_max = 1 << array->bucket_bit;
41 1 : array->bucket[0] = tommy_calloc(array->bucket_max, array->element_size);
42 6 : for (i = 1; i < TOMMY_ARRAYOF_BIT; ++i)
43 5 : array->bucket[i] = array->bucket[0];
44 :
45 1 : array->count = 0;
46 1 : }
47 :
48 1 : void tommy_arrayof_done(tommy_arrayof* array)
49 : {
50 : tommy_uint_t i;
51 :
52 1 : tommy_free(array->bucket[0]);
53 21 : for (i = TOMMY_ARRAYOF_BIT; i < array->bucket_bit; ++i) {
54 20 : unsigned char* segment = tommy_cast(unsigned char*, array->bucket[i]);
55 20 : tommy_free(segment + (((tommy_ptrdiff_t)1) << i) * array->element_size);
56 : }
57 1 : }
58 :
59 50000001 : void tommy_arrayof_grow(tommy_arrayof* array, tommy_size_t count)
60 : {
61 50000001 : if (array->count >= count)
62 1 : return;
63 50000000 : array->count = count;
64 :
65 100000020 : while (count > array->bucket_max) {
66 : unsigned char* segment;
67 :
68 : /* allocate one more segment */
69 20 : segment = tommy_cast(unsigned char*, tommy_calloc(array->bucket_max, array->element_size));
70 :
71 : /* store it adjusting the offset */
72 : /* cast to ptrdiff_t to ensure to get a negative value */
73 20 : array->bucket[array->bucket_bit] = segment - (tommy_ptrdiff_t)array->bucket_max * array->element_size;
74 :
75 20 : ++array->bucket_bit;
76 20 : array->bucket_max = 1 << array->bucket_bit;
77 : }
78 : }
79 :
80 1 : tommy_size_t tommy_arrayof_memory_usage(tommy_arrayof* array)
81 : {
82 1 : return array->bucket_max * (tommy_size_t)array->element_size;
83 : }
84 :
|