jdk/src/share/back/bag.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/* General routines for manipulating a bag data structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "bag.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
struct bag {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    void *items;    /* hold items in bag, must align on itemSize */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    int used;       /* number of items in bag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    int allocated;  /* space reserved */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    int itemSize;   /* size of each item, should init to sizeof item */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
struct bag *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
bagCreateBag(int itemSize, int initialAllocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    struct bag *theBag = (struct bag *)jvmtiAllocate(sizeof(struct bag));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    if (theBag == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    itemSize = (itemSize + 7) & ~7;    /* fit 8 byte boundary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    theBag->items = jvmtiAllocate(initialAllocation * itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    if (theBag->items == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        jvmtiDeallocate(theBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    theBag->used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    theBag->allocated = initialAllocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    theBag->itemSize = itemSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    return theBag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
struct bag *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
bagDup(struct bag *oldBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    struct bag *newBag = bagCreateBag(oldBag->itemSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                      oldBag->allocated);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    if (newBag != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        newBag->used = oldBag->used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        (void)memcpy(newBag->items, oldBag->items, newBag->used * newBag->itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    return newBag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
bagDestroyBag(struct bag *theBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (theBag != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        jvmtiDeallocate(theBag->items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        jvmtiDeallocate(theBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
bagFind(struct bag *theBag, void *key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    char *items = theBag->items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int itemSize = theBag->itemSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    char *itemsEnd = items + (itemSize * theBag->used);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    for (; items < itemsEnd; items += itemSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (*((void**)items) == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
bagAdd(struct bag *theBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    int allocated = theBag->allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    int itemSize = theBag->itemSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    void *items = theBag->items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    void *ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /* if there are no unused slots reallocate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if (theBag->used >= allocated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        void *new_items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        allocated *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        new_items = jvmtiAllocate(allocated * itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (new_items == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        (void)memcpy(new_items, items, (theBag->used) * itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        jvmtiDeallocate(items);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        items = new_items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        theBag->allocated = allocated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        theBag->items = items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    ret = ((char *)items) + (itemSize * (theBag->used)++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    (void)memset(ret, 0, itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
bagDelete(struct bag *theBag, void *condemned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int used = --(theBag->used);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    int itemSize = theBag->itemSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    void *items = theBag->items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    void *tailItem = ((char *)items) + (used * itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    if (condemned != tailItem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        (void)memcpy(condemned, tailItem, itemSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
bagDeleteAll(struct bag *theBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    theBag->used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
bagSize(struct bag *theBag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    return theBag->used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
bagEnumerateOver(struct bag *theBag, bagEnumerateFunction func, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    char *items = theBag->items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    int itemSize = theBag->itemSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    char *itemsEnd = items + (itemSize * theBag->used);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    for (; items < itemsEnd; items += itemSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (!(func)((void *)items, arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
}