Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/jvmti/hprof/hprof_blocks.c
38829 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/383940/* Allocations from large blocks, no individual free's */4142#include "hprof.h"4344/*45* This file contains some allocation code that allows you46* to have space allocated via larger blocks of space.47* The only free allowed is of all the blocks and all the elements.48* Elements can be of different alignments and fixed or variable sized.49* The space allocated never moves.50*51*/5253/* Get the real size allocated based on alignment and bytes needed */54static int55real_size(int alignment, int nbytes)56{57if ( alignment > 1 ) {58int wasted;5960wasted = alignment - ( nbytes % alignment );61if ( wasted != alignment ) {62nbytes += wasted;63}64}65return nbytes;66}6768/* Add a new current_block to the Blocks* chain, adjust size if nbytes big. */69static void70add_block(Blocks *blocks, int nbytes)71{72int header_size;73int block_size;74BlockHeader *block_header;7576HPROF_ASSERT(blocks!=NULL);77HPROF_ASSERT(nbytes>0);7879header_size = real_size(blocks->alignment, sizeof(BlockHeader));80block_size = blocks->elem_size*blocks->population;81if ( nbytes > block_size ) {82block_size = real_size(blocks->alignment, nbytes);83}84block_header = (BlockHeader*)HPROF_MALLOC(block_size+header_size);85block_header->next = NULL;86block_header->bytes_left = block_size;87block_header->next_pos = header_size;8889/* Link in new block */90if ( blocks->current_block != NULL ) {91blocks->current_block->next = block_header;92}93blocks->current_block = block_header;94if ( blocks->first_block == NULL ) {95blocks->first_block = block_header;96}97}9899/* Initialize a new Blocks */100Blocks *101blocks_init(int alignment, int elem_size, int population)102{103Blocks *blocks;104105HPROF_ASSERT(alignment>0);106HPROF_ASSERT(elem_size>0);107HPROF_ASSERT(population>0);108109blocks = (Blocks*)HPROF_MALLOC(sizeof(Blocks));110blocks->alignment = alignment;111blocks->elem_size = elem_size;112blocks->population = population;113blocks->first_block = NULL;114blocks->current_block = NULL;115return blocks;116}117118/* Allocate bytes from a Blocks area. */119void *120blocks_alloc(Blocks *blocks, int nbytes)121{122BlockHeader *block;123int pos;124void *ptr;125126HPROF_ASSERT(blocks!=NULL);127HPROF_ASSERT(nbytes>=0);128if ( nbytes == 0 ) {129return NULL;130}131132block = blocks->current_block;133nbytes = real_size(blocks->alignment, nbytes);134if ( block == NULL || block->bytes_left < nbytes ) {135add_block(blocks, nbytes);136block = blocks->current_block;137}138pos = block->next_pos;139ptr = (void*)(((char*)block)+pos);140block->next_pos += nbytes;141block->bytes_left -= nbytes;142return ptr;143}144145/* Terminate the Blocks */146void147blocks_term(Blocks *blocks)148{149BlockHeader *block;150151HPROF_ASSERT(blocks!=NULL);152153block = blocks->first_block;154while ( block != NULL ) {155BlockHeader *next_block;156157next_block = block->next;158HPROF_FREE(block);159block = next_block;160}161HPROF_FREE(blocks);162}163164165