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 270 : void tommy_arrayblkof_init(tommy_arrayblkof* array, tommy_size_t element_size) 34 : { 35 270 : tommy_array_init(&array->block); 36 : 37 270 : array->element_size = element_size; 38 270 : array->count = 0; 39 270 : } 40 : 41 259 : void tommy_arrayblkof_done(tommy_arrayblkof* array) 42 : { 43 : tommy_size_t i; 44 : 45 807 : for (i = 0; i < tommy_array_size(&array->block); ++i) 46 548 : tommy_free(tommy_array_get(&array->block, i)); 47 : 48 259 : tommy_array_done(&array->block); 49 259 : } 50 : 51 1697055 : void tommy_arrayblkof_grow(tommy_arrayblkof* array, tommy_size_t count) 52 : { 53 : tommy_size_t block_max; 54 : tommy_size_t block_mac; 55 : 56 1697055 : if (array->count >= count) 57 119632 : return; 58 1577423 : array->count = count; 59 : 60 1577423 : block_max = (count + TOMMY_ARRAYBLKOF_SIZE - 1) / TOMMY_ARRAYBLKOF_SIZE; 61 1577423 : block_mac = tommy_array_size(&array->block); 62 : 63 1577423 : if (block_mac < block_max) { 64 : /* grow the block array */ 65 556 : tommy_array_grow(&array->block, block_max); 66 : 67 : /* allocate new blocks */ 68 1112 : while (block_mac < block_max) { 69 556 : void** ptr = tommy_cast(void**, tommy_calloc(TOMMY_ARRAYBLKOF_SIZE, array->element_size)); 70 : 71 : /* set the new block */ 72 556 : tommy_array_set(&array->block, block_mac, ptr); 73 : 74 556 : ++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 :