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 "tommyarrayblkof.h"
29 :
30 : /******************************************************************************/
31 : /* array */
32 :
33 268 : void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size)
34 : {
35 268 : tommy_array_init(&array->block);
36 :
37 268 : array->element_size = element_size;
38 268 : array->count = 0;
39 268 : }
40 :
41 245 : void tommy_arrayblkof_done(tommy_arrayblkof* array)
42 : {
43 : tommy_count_t i;
44 :
45 770 : for (i = 0; i < tommy_array_size(&array->block); ++i)
46 525 : tommy_free(tommy_array_get(&array->block, i));
47 :
48 245 : tommy_array_done(&array->block);
49 245 : }
50 :
51 1698281 : void tommy_arrayblkof_grow(tommy_arrayblkof* array, tommy_count_t count)
52 : {
53 : tommy_count_t block_max;
54 : tommy_count_t block_mac;
55 :
56 1698281 : if (array->count >= count)
57 123274 : return;
58 1575007 : array->count = count;
59 :
60 1575007 : block_max = (count + TOMMY_ARRAYBLKOF_SIZE - 1) / TOMMY_ARRAYBLKOF_SIZE;
61 1575007 : block_mac = tommy_array_size(&array->block);
62 :
63 1575007 : if (block_mac < block_max) {
64 : /* grow the block array */
65 555 : tommy_array_grow(&array->block, block_max);
66 :
67 : /* allocate new blocks */
68 1665 : while (block_mac < block_max) {
69 555 : void** ptr = tommy_cast(void**, tommy_calloc(TOMMY_ARRAYBLKOF_SIZE, array->element_size));
70 :
71 : /* set the new block */
72 555 : tommy_array_set(&array->block, block_mac, ptr);
73 :
74 555 : ++block_mac;
75 : }
76 : }
77 : }
78 :
79 1 : tommy_size_t tommy_arrayblkof_memory_usage(tommy_arrayblkof* array)
80 : {
81 1 : return tommy_array_memory_usage(&array->block) + tommy_array_size(&array->block) * TOMMY_ARRAYBLKOF_SIZE * array->element_size;
82 : }
83 :
|