J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. 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 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * - 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 | * - Neither the name of Sun Microsystems nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* Allocations from large blocks, no individual free's */ |
| 33 | |
| 34 | #include "hprof.h" |
| 35 | |
| 36 | /* |
| 37 | * This file contains some allocation code that allows you |
| 38 | * to have space allocated via larger blocks of space. |
| 39 | * The only free allowed is of all the blocks and all the elements. |
| 40 | * Elements can be of different alignments and fixed or variable sized. |
| 41 | * The space allocated never moves. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | /* Get the real size allocated based on alignment and bytes needed */ |
| 46 | static int |
| 47 | real_size(int alignment, int nbytes) |
| 48 | { |
| 49 | if ( alignment > 1 ) { |
| 50 | int wasted; |
| 51 | |
| 52 | wasted = alignment - ( nbytes % alignment ); |
| 53 | if ( wasted != alignment ) { |
| 54 | nbytes += wasted; |
| 55 | } |
| 56 | } |
| 57 | return nbytes; |
| 58 | } |
| 59 | |
| 60 | /* Add a new current_block to the Blocks* chain, adjust size if nbytes big. */ |
| 61 | static void |
| 62 | add_block(Blocks *blocks, int nbytes) |
| 63 | { |
| 64 | int header_size; |
| 65 | int block_size; |
| 66 | BlockHeader *block_header; |
| 67 | |
| 68 | HPROF_ASSERT(blocks!=NULL); |
| 69 | HPROF_ASSERT(nbytes>0); |
| 70 | |
| 71 | header_size = real_size(blocks->alignment, sizeof(BlockHeader)); |
| 72 | block_size = blocks->elem_size*blocks->population; |
| 73 | if ( nbytes > block_size ) { |
| 74 | block_size = real_size(blocks->alignment, nbytes); |
| 75 | } |
| 76 | block_header = (BlockHeader*)HPROF_MALLOC(block_size+header_size); |
| 77 | block_header->next = NULL; |
| 78 | block_header->bytes_left = block_size; |
| 79 | block_header->next_pos = header_size; |
| 80 | |
| 81 | /* Link in new block */ |
| 82 | if ( blocks->current_block != NULL ) { |
| 83 | blocks->current_block->next = block_header; |
| 84 | } |
| 85 | blocks->current_block = block_header; |
| 86 | if ( blocks->first_block == NULL ) { |
| 87 | blocks->first_block = block_header; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Initialize a new Blocks */ |
| 92 | Blocks * |
| 93 | blocks_init(int alignment, int elem_size, int population) |
| 94 | { |
| 95 | Blocks *blocks; |
| 96 | |
| 97 | HPROF_ASSERT(alignment>0); |
| 98 | HPROF_ASSERT(elem_size>0); |
| 99 | HPROF_ASSERT(population>0); |
| 100 | |
| 101 | blocks = (Blocks*)HPROF_MALLOC(sizeof(Blocks)); |
| 102 | blocks->alignment = alignment; |
| 103 | blocks->elem_size = elem_size; |
| 104 | blocks->population = population; |
| 105 | blocks->first_block = NULL; |
| 106 | blocks->current_block = NULL; |
| 107 | return blocks; |
| 108 | } |
| 109 | |
| 110 | /* Allocate bytes from a Blocks area. */ |
| 111 | void * |
| 112 | blocks_alloc(Blocks *blocks, int nbytes) |
| 113 | { |
| 114 | BlockHeader *block; |
| 115 | int pos; |
| 116 | void *ptr; |
| 117 | |
| 118 | HPROF_ASSERT(blocks!=NULL); |
| 119 | HPROF_ASSERT(nbytes>=0); |
| 120 | if ( nbytes == 0 ) { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | block = blocks->current_block; |
| 125 | nbytes = real_size(blocks->alignment, nbytes); |
| 126 | if ( block == NULL || block->bytes_left < nbytes ) { |
| 127 | add_block(blocks, nbytes); |
| 128 | block = blocks->current_block; |
| 129 | } |
| 130 | pos = block->next_pos; |
| 131 | ptr = (void*)(((char*)block)+pos); |
| 132 | block->next_pos += nbytes; |
| 133 | block->bytes_left -= nbytes; |
| 134 | return ptr; |
| 135 | } |
| 136 | |
| 137 | /* Terminate the Blocks */ |
| 138 | void |
| 139 | blocks_term(Blocks *blocks) |
| 140 | { |
| 141 | BlockHeader *block; |
| 142 | |
| 143 | HPROF_ASSERT(blocks!=NULL); |
| 144 | |
| 145 | block = blocks->first_block; |
| 146 | while ( block != NULL ) { |
| 147 | BlockHeader *next_block; |
| 148 | |
| 149 | next_block = block->next; |
| 150 | HPROF_FREE(block); |
| 151 | block = next_block; |
| 152 | } |
| 153 | HPROF_FREE(blocks); |
| 154 | } |