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 "tommyarrayblk.h"
29 :
30 : /******************************************************************************/
31 : /* array */
32 :
33 1 : void tommy_arrayblk_init(tommy_arrayblk* array)
34 : {
35 1 : tommy_array_init(&array->block);
36 :
37 1 : array->count = 0;
38 1 : }
39 :
40 1 : void tommy_arrayblk_done(tommy_arrayblk* array)
41 : {
42 : tommy_size_t i;
43 :
44 12209 : for (i = 0; i < tommy_array_size(&array->block); ++i)
45 12208 : tommy_free(tommy_array_get(&array->block, i));
46 :
47 1 : tommy_array_done(&array->block);
48 1 : }
49 :
50 50000001 : void tommy_arrayblk_grow(tommy_arrayblk* array, tommy_size_t count)
51 : {
52 : tommy_size_t block_max;
53 : tommy_size_t block_mac;
54 :
55 50000001 : if (array->count >= count)
56 1 : return;
57 50000000 : array->count = count;
58 :
59 50000000 : block_max = (count + TOMMY_ARRAYBLK_SIZE - 1) / TOMMY_ARRAYBLK_SIZE;
60 50000000 : block_mac = tommy_array_size(&array->block);
61 :
62 50000000 : if (block_mac < block_max) {
63 : /* grow the block array */
64 12208 : tommy_array_grow(&array->block, block_max);
65 :
66 : /* allocate new blocks */
67 36624 : while (block_mac < block_max) {
68 12208 : void** ptr = tommy_cast(void**, tommy_calloc(TOMMY_ARRAYBLK_SIZE, sizeof(void*)));
69 :
70 : /* set the new block */
71 12208 : tommy_array_set(&array->block, block_mac, ptr);
72 :
73 12208 : ++block_mac;
74 : }
75 : }
76 : }
77 :
78 1 : tommy_size_t tommy_arrayblk_memory_usage(tommy_arrayblk* array)
79 : {
80 1 : return tommy_array_memory_usage(&array->block) + tommy_array_size(&array->block) * TOMMY_ARRAYBLK_SIZE * sizeof(void*);
81 : }
82 :
|