Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/back/bag.c
38765 views
/*1* Copyright (c) 1998, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* General routines for manipulating a bag data structure */2627#include "util.h"28#include "bag.h"2930struct bag {31void *items; /* hold items in bag, must align on itemSize */32int used; /* number of items in bag */33int allocated; /* space reserved */34int itemSize; /* size of each item, should init to sizeof item */35};3637struct bag *38bagCreateBag(int itemSize, int initialAllocation) {39struct bag *theBag = (struct bag *)jvmtiAllocate(sizeof(struct bag));40if (theBag == NULL) {41return NULL;42}43itemSize = (itemSize + 7) & ~7; /* fit 8 byte boundary */44theBag->items = jvmtiAllocate(initialAllocation * itemSize);45if (theBag->items == NULL) {46jvmtiDeallocate(theBag);47return NULL;48}49theBag->used = 0;50theBag->allocated = initialAllocation;51theBag->itemSize = itemSize;52return theBag;53}5455struct bag *56bagDup(struct bag *oldBag)57{58struct bag *newBag = bagCreateBag(oldBag->itemSize,59oldBag->allocated);60if (newBag != NULL) {61newBag->used = oldBag->used;62(void)memcpy(newBag->items, oldBag->items, newBag->used * newBag->itemSize);63}64return newBag;65}6667void68bagDestroyBag(struct bag *theBag)69{70if (theBag != NULL) {71jvmtiDeallocate(theBag->items);72jvmtiDeallocate(theBag);73}74}7576void *77bagFind(struct bag *theBag, void *key)78{79char *items = theBag->items;80int itemSize = theBag->itemSize;81char *itemsEnd = items + (itemSize * theBag->used);8283for (; items < itemsEnd; items += itemSize) {84/*LINTED*/85if (*((void**)items) == key) {86return items;87}88}89return NULL;90}9192void *93bagAdd(struct bag *theBag)94{95int allocated = theBag->allocated;96int itemSize = theBag->itemSize;97void *items = theBag->items;98void *ret;99100/* if there are no unused slots reallocate */101if (theBag->used >= allocated) {102void *new_items;103allocated *= 2;104new_items = jvmtiAllocate(allocated * itemSize);105if (new_items == NULL) {106return NULL;107}108(void)memcpy(new_items, items, (theBag->used) * itemSize);109jvmtiDeallocate(items);110items = new_items;111theBag->allocated = allocated;112theBag->items = items;113}114ret = ((char *)items) + (itemSize * (theBag->used)++);115(void)memset(ret, 0, itemSize);116return ret;117}118119void120bagDelete(struct bag *theBag, void *condemned)121{122int used = --(theBag->used);123int itemSize = theBag->itemSize;124void *items = theBag->items;125void *tailItem = ((char *)items) + (used * itemSize);126127if (condemned != tailItem) {128(void)memcpy(condemned, tailItem, itemSize);129}130}131132void133bagDeleteAll(struct bag *theBag)134{135theBag->used = 0;136}137138139int140bagSize(struct bag *theBag)141{142return theBag->used;143}144145jboolean146bagEnumerateOver(struct bag *theBag, bagEnumerateFunction func, void *arg)147{148char *items = theBag->items;149int itemSize = theBag->itemSize;150char *itemsEnd = items + (itemSize * theBag->used);151152for (; items < itemsEnd; items += itemSize) {153if (!(func)((void *)items, arg)) {154return JNI_FALSE;155}156}157return JNI_TRUE;158}159160161