jdk/src/share/demo/jvmti/hprof/hprof_table.c
author goetz
Thu, 21 Nov 2013 18:29:34 -0800
changeset 22852 1063026e8cee
parent 14342 8435a30053c1
permissions -rw-r--r--
8028471: PPC64 (part 215): opto: Extend ImplicitNullCheck optimization. Summary: Fixed Implicit NULL check optimization for AIX, where the page at address '0' is only write-protected. Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13014
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Lookup Table of generic elements. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Each table has a unique lock, all accesses are protected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Table elements are identified with a 32bit unsigned int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   (Also see HARE trick below, which makes the TableIndex unique per table).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Each element has a key (N bytes) and possible additional info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Two elements with the same key should be the same element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The storage for the Key and Info cannot move, the table itself can.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The hash table will only be allocated if we have keys, and will resize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    when the table needs to resize. The hash buckets just provide the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *    reference to the first TableIndex in the hash bucket, the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *    field of the TableElement takes you to the next item in the hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    bucket. Lookups will drift the looked up item to the head of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The full 32bit hashcode and key length is saved for comparisons, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *    last thing done is the actual comparison of the Key contents with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *    keys_equal().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Freed elements (not many tables actually free items) are managed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *    a bit vector and a low index where a freed element might be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *    Bytes are inspected until a non-zero byte indicates a freed bit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *    set. A count of freed elements is also kept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* Macros for bit vectors: unsigned char 2^3==8 OR  unsigned int 2^5==32 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#define BV_CHUNK_POWER_2         3  /* 2 to this power == BV_CHUNK_BITSIZE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#define BV_CHUNK_TYPE            unsigned char
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#define BV_CHUNK_BITSIZE         (((int)sizeof(BV_CHUNK_TYPE))<<3) /* x8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#define BV_CHUNK_INDEX_MASK      ( (1 << BV_CHUNK_POWER_2) - 1 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#define BV_ELEMENT_COUNT(nelems) ((((nelems+1)) >> BV_CHUNK_POWER_2) + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#define BV_CHUNK_ROUND(i) ((i) & ~(BV_CHUNK_INDEX_MASK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#define BV_CHUNK(ptr, i)          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                (((BV_CHUNK_TYPE*)(ptr))[(i) >> BV_CHUNK_POWER_2])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#define BV_CHUNK_MASK(i)          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                (1 << ((i) & BV_CHUNK_INDEX_MASK))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
/* Hash code value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
typedef unsigned HashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
/* Basic key for an element. What makes the element unique. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
typedef struct TableKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    void        *ptr;   /* Pointer to arbitrary data that forms the key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int          len;   /* Length in bytes of this key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
} TableKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
/* Basic TableElement (but only allocated if keys are used) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
typedef struct TableElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    TableKey     key;   /* The element key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    HashCode     hcode; /* The full 32bit hashcode for the key. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    TableIndex   next;  /* The next TableElement in the hash bucket chain. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    void        *info;  /* Info pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
} TableElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
/* Generic Lookup Table structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
typedef struct LookupTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    char           name[48];            /* Name of table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    void          *table;               /* Pointer to array of elements. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    TableIndex    *hash_buckets;        /* Pointer to hash bucket chains. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    Blocks        *info_blocks;         /* Blocks space for info */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    Blocks        *key_blocks;          /* Blocks space for keys */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    TableIndex     next_index;          /* Next element available. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    TableIndex     table_size;          /* Current size of table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    TableIndex     table_incr;          /* Suggested increment size. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    TableIndex     hash_bucket_count;   /* Number of hash buckets. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    int            elem_size;           /* Size of element. */
13014
e2fe1611bb04 7176138: Fixes for missing close() calls and possible null pointer reference instead of fatal error
ohair
parents: 10292
diff changeset
   123
    int            info_size;           /* Size of info structure (can be 0). */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    void          *freed_bv;            /* Freed element bit vector */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    int            freed_count;         /* Count of freed'd elements */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    TableIndex     freed_start;         /* First freed in table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int            resizes;             /* Count of table resizes done. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    unsigned       bucket_walks;        /* Count of bucket walks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jrawMonitorID  lock;                /* Lock for table access. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    SerialNumber   serial_num;          /* Table serial number. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    TableIndex     hare;                /* Rabbit (HARE) trick. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
} LookupTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
/* To get a pointer to an element, regardless of element size. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
#define ELEMENT_PTR(ltable, i) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        ((void*)(((char*)(ltable)->table) + (ltable)->elem_size * (i)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
/* Sanity, check all the time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#define SANITY_CHECK(condition) ( (condition) ? (void)0 : \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                HPROF_ERROR(JNI_FALSE, "SANITY IN QUESTION: " #condition))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
/* To see if an index is valid. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
#define SANITY_CHECK_INDEX(ltable,i) SANITY_CHECK((i) < ltable->next_index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
/* Small rabbits (hares) can be hidden in the index value returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *   Only the right rabbits are allowed in certain pens (LookupTables).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *   When herding rabbits it's important to keep them separate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *   there are lots of rabbits, all different kinds and sizes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *   keeping them all separate is important to avoid cross breeding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
#define _SANITY_USE_HARE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
#ifdef _SANITY_USE_HARE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    #define SANITY_ADD_HARE(i,hare)    (SANITY_REMOVE_HARE(i) | (hare))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    #define SANITY_REMOVE_HARE(i)      ((i)  & 0x0FFFFFFF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    #define SANITY_CHECK_HARE(i,hare)  SANITY_CHECK(SANITY_ADD_HARE(i,hare)==(i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    #define SANITY_ADD_HARE(i,hare)    (i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    #define SANITY_REMOVE_HARE(i)      (i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    #define SANITY_CHECK_HARE(i,hare)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
static jrawMonitorID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
lock_create(char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    jrawMonitorID stanley;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    stanley = createRawMonitor(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    return stanley;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
lock_destroy(jrawMonitorID stanley)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    if ( stanley != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        destroyRawMonitor(stanley);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
lock_enter(jrawMonitorID stanley)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    if ( stanley != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        rawMonitorEnter(stanley);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
lock_exit(jrawMonitorID stanley)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if ( stanley != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        rawMonitorExit(stanley);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
get_key(LookupTable *ltable, TableIndex index, void **pkey_ptr, int *pkey_len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    *pkey_ptr = ((TableElement*)ELEMENT_PTR(ltable,index))->key.ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    *pkey_len = ((TableElement*)ELEMENT_PTR(ltable,index))->key.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
static void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
get_info(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    TableElement *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    element = (TableElement*)ELEMENT_PTR(ltable,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    return element->info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
hash_out(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        TableElement *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        TableElement *prev_e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        TableIndex    bucket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        TableIndex    i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        element = (TableElement*)ELEMENT_PTR(ltable,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        bucket = (element->hcode % ltable->hash_bucket_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        i = ltable->hash_buckets[bucket];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        HPROF_ASSERT(i!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        prev_e = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while ( i != 0 && i != index ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            prev_e = (TableElement*)ELEMENT_PTR(ltable,i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            i = prev_e->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        HPROF_ASSERT(i==index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if ( prev_e == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            ltable->hash_buckets[bucket] = element->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            prev_e->next = element->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        element->next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        element->hcode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
is_freed_entry(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    if ( ltable->freed_bv == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    if ( ( BV_CHUNK(ltable->freed_bv, index) & BV_CHUNK_MASK(index) ) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
set_freed_bit(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    void *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    HPROF_ASSERT(!is_freed_entry(ltable, index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    p = ltable->freed_bv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    if ( p == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        /* First time for a free */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        HPROF_ASSERT(ltable->freed_start==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        HPROF_ASSERT(ltable->freed_start==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        size             = BV_ELEMENT_COUNT(ltable->table_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        p                = HPROF_MALLOC(size*(int)sizeof(BV_CHUNK_TYPE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        ltable->freed_bv = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        (void)memset(p, 0, size*(int)sizeof(BV_CHUNK_TYPE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    BV_CHUNK(p, index) |= BV_CHUNK_MASK(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    ltable->freed_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    if ( ltable->freed_count == 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        /* Set freed_start for first time. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        HPROF_ASSERT(ltable->freed_start==0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        ltable->freed_start = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    } else if ( index < ltable->freed_start ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /* Set freed_start to smaller value so we can be smart about search */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        HPROF_ASSERT(ltable->freed_start!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        ltable->freed_start = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    HPROF_ASSERT(ltable->freed_start!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    HPROF_ASSERT(ltable->freed_start < ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    HPROF_ASSERT(is_freed_entry(ltable, index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
static TableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
find_freed_entry(LookupTable *ltable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if ( ltable->freed_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        TableIndex i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        TableIndex istart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        void *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        BV_CHUNK_TYPE chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        HPROF_ASSERT(BV_CHUNK_BITSIZE==(1<<BV_CHUNK_POWER_2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        p = ltable->freed_bv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        HPROF_ASSERT(p!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /* Go to beginning of chunk */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        HPROF_ASSERT(ltable->freed_start!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        HPROF_ASSERT(ltable->freed_start < ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        istart = BV_CHUNK_ROUND(ltable->freed_start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        /* Find chunk with any bit set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        chunk = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        for( ; istart < ltable->next_index ; istart += BV_CHUNK_BITSIZE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            chunk = BV_CHUNK(p, istart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if ( chunk != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        HPROF_ASSERT(chunk!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        HPROF_ASSERT(chunk==BV_CHUNK(p,istart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        HPROF_ASSERT(istart < ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        /* Find bit in chunk and return index of freed item */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        for( i = istart ; i < (istart+BV_CHUNK_BITSIZE) ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            BV_CHUNK_TYPE mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            mask = BV_CHUNK_MASK(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if ( (chunk & mask) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                HPROF_ASSERT(chunk==BV_CHUNK(p,i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                chunk &= ~mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                BV_CHUNK(p, i) = chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                ltable->freed_count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                HPROF_ASSERT(i < ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                if ( ltable->freed_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    /* Set freed_start so we can be smart about search */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    HPROF_ASSERT((i+1) < ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    ltable->freed_start = i+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    /* Clear freed_start because there are no freed entries */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    ltable->freed_start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                HPROF_ASSERT(!is_freed_entry(ltable, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        HPROF_ASSERT(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
free_entry(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    set_freed_bit(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    hash_out(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
/* Fairly generic hash code generator (not a hash table index) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
static HashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
hashcode(void *key_ptr, int key_len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    unsigned char *     p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    HashCode            hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    int                 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    hcode       = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    if ( key_ptr == NULL || key_len == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    i           = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    p           = (unsigned char*)key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    for ( ; i < key_len-3 ; i += 4 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        /* Do a little loop unrolling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        hcode += (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                ( (unsigned)(p[i])   << 24 ) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                ( (unsigned)(p[i+1]) << 16 ) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                ( (unsigned)(p[i+2]) <<  8 ) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                ( (unsigned)(p[i+3])       )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    for ( ; i < key_len ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        hcode += (unsigned)(p[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    return hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
hash_in(LookupTable *ltable, TableIndex index, HashCode hcode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        TableElement *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        TableIndex    bucket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        bucket                        = (hcode % ltable->hash_bucket_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        element                       = (TableElement*)ELEMENT_PTR(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        element->hcode                = hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        element->next                 = ltable->hash_buckets[bucket];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        ltable->hash_buckets[bucket]  = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
resize_hash_buckets(LookupTable *ltable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /*    Don't want to do this too often. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /* Hash table needs resizing when it's smaller than 1/16 the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *   elements used in the table. This is just a guess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    if (    ( ltable->hash_bucket_count < (ltable->next_index >> 4) )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         && ( ltable->hash_bucket_count > 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         && ( ( ltable->resizes % 10 ) == 0 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         && ( ltable->bucket_walks > 1000*ltable->hash_bucket_count )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int         old_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int         new_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        TableIndex *new_buckets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        TableIndex *old_buckets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int         bucket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        /* Increase size of hash_buckets array, and rehash all elements */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        LOG3("Table resize", ltable->name, ltable->resizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        old_size    = ltable->hash_bucket_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        old_buckets = ltable->hash_buckets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        new_size    = (ltable->next_index >> 3); /* 1/8 current used count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        SANITY_CHECK(new_size > old_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        new_buckets = HPROF_MALLOC(new_size*(int)sizeof(TableIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        (void)memset(new_buckets, 0, new_size*(int)sizeof(TableIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        ltable->hash_bucket_count = new_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        ltable->hash_buckets      = new_buckets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        for ( bucket = 0 ; bucket < old_size ; bucket++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            TableIndex    index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            index = old_buckets[bucket];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            while ( index != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                TableElement *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                TableIndex    next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                element       = (TableElement*)ELEMENT_PTR(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                next          = element->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                element->next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                hash_in(ltable, index, element->hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                index         = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        HPROF_FREE(old_buckets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        ltable->bucket_walks = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
resize(LookupTable *ltable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    int   old_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    int   new_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    void *old_table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    void *new_table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    int   nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    int   obytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    LOG3("Table resize", ltable->name, ltable->resizes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /* Adjust increment on every resize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *    Minimum is 1/4 the size of the current table or 512.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    old_size = ltable->table_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if ( ltable->table_incr < (unsigned)(old_size >> 2) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        ltable->table_incr = (old_size >> 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    if ( ltable->table_incr < 512 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        ltable->table_incr = 512;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    new_size  = old_size + ltable->table_incr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /* Basic table element array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    obytes    = old_size * ltable->elem_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    nbytes    = new_size * ltable->elem_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    old_table = ltable->table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    new_table = HPROF_MALLOC(nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    (void)memcpy(new_table, old_table, obytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    (void)memset(((char*)new_table)+obytes, 0, nbytes-obytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    ltable->table      = new_table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    ltable->table_size = new_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    HPROF_FREE(old_table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /* Then bit vector for freed entries */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    if ( ltable->freed_bv != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        void *old_bv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        void *new_bv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        obytes = BV_ELEMENT_COUNT(old_size)*(int)sizeof(BV_CHUNK_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        nbytes = BV_ELEMENT_COUNT(new_size)*(int)sizeof(BV_CHUNK_TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        old_bv = ltable->freed_bv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        new_bv = HPROF_MALLOC(nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        (void)memcpy(new_bv, old_bv, obytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        (void)memset(((char*)new_bv)+obytes, 0, nbytes-obytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        ltable->freed_bv = new_bv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        HPROF_FREE(old_bv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /* Check to see if the hash table needs resizing */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    resize_hash_buckets(ltable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    ltable->resizes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
keys_equal(void *key_ptr1, void *key_ptr2, int key_len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    unsigned char *     p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    unsigned char *     p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    int                 i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    if ( key_len == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /* We know these are aligned because we malloc'd them. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /* Compare word by word, then byte by byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    p1 = (unsigned char*)key_ptr1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    p2 = (unsigned char*)key_ptr2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    for ( i = 0 ; i < key_len-3 ; i += 4 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if ( *(unsigned*)(p1+i) != *(unsigned*)(p2+i) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    for ( ; i < key_len ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if ( p1[i] != p2[i] ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
static TableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
find_entry(LookupTable *ltable, void *key_ptr, int key_len, HashCode hcode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    TableIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    HPROF_ASSERT(ltable!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        TableIndex bucket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        TableIndex prev_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        HPROF_ASSERT(key_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        HPROF_ASSERT(key_len>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        prev_index  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        bucket      = (hcode % ltable->hash_bucket_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        index       = ltable->hash_buckets[bucket];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        while ( index != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            TableElement *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            TableElement *prev_element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            element = (TableElement*)ELEMENT_PTR(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if ( hcode == element->hcode &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                 key_len == element->key.len &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                 keys_equal(key_ptr, element->key.ptr, key_len) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                /* Place this guy at the head of the bucket list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                if ( prev_index != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    prev_element = (TableElement*)ELEMENT_PTR(ltable, prev_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    prev_element->next  = element->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    element->next       = ltable->hash_buckets[bucket];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    ltable->hash_buckets[bucket]    = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            prev_index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            index      = element->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            ltable->bucket_walks++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
static TableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
setup_new_entry(LookupTable *ltable, void *key_ptr, int key_len, void *info_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    TableIndex    index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    TableElement *element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    void         *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    void         *dup_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /* Assume we need new allocations for key and info */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    dup_key  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    info     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /* Look for a freed element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    if ( ltable->freed_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        index    = find_freed_entry(ltable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    if ( index != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        int old_key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        /* Found a freed element, re-use what we can but clean it up. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        element     = (TableElement*)ELEMENT_PTR(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        dup_key     = element->key.ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        old_key_len = element->key.len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        info        = element->info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        (void)memset(element, 0, ltable->elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        /* Toss the key space if size is too small to hold new key */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        if ( key_ptr != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if ( old_key_len < key_len ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                /* This could leak space in the Blocks if keys are variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                 *    in size AND the table does frees of elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                dup_key = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        /* Brand new table element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if ( ltable->next_index >= ltable->table_size ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            resize(ltable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        index = ltable->next_index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        element = (TableElement*)ELEMENT_PTR(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    /* Setup info area */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    if ( ltable->info_size > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if ( info == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            info = blocks_alloc(ltable->info_blocks, ltable->info_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if ( info_ptr==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            (void)memset(info, 0, ltable->info_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            (void)memcpy(info, info_ptr, ltable->info_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /* Setup key area if one was provided */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    if ( key_ptr != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if ( dup_key == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            dup_key  = blocks_alloc(ltable->key_blocks, key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        (void)memcpy(dup_key, key_ptr, key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /* Fill in element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    element->key.ptr = dup_key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    element->key.len = key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    element->info    = info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
LookupTable *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
table_initialize(const char *name, int size, int incr, int bucket_count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                        int info_size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    LookupTable * ltable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    char          lock_name[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    int           elem_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    int           key_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    HPROF_ASSERT(name!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    HPROF_ASSERT(size>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    HPROF_ASSERT(incr>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    HPROF_ASSERT(bucket_count>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    HPROF_ASSERT(info_size>=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    key_size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    ltable = (LookupTable *)HPROF_MALLOC((int)sizeof(LookupTable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    (void)memset(ltable, 0, (int)sizeof(LookupTable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    (void)strncpy(ltable->name, name, sizeof(ltable->name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    elem_size = (int)sizeof(TableElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    ltable->next_index          = 1; /* Never use index 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    ltable->table_size          = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    ltable->table_incr          = incr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    ltable->hash_bucket_count   = bucket_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    ltable->elem_size           = elem_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    ltable->info_size           = info_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    if ( info_size > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        ltable->info_blocks     = blocks_init(8, info_size, incr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    if ( key_size > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        ltable->key_blocks      = blocks_init(8, key_size, incr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    ltable->table               = HPROF_MALLOC(size * elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    (void)memset(ltable->table, 0, size * elem_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    if ( bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        int nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        nbytes               = (int)(bucket_count*sizeof(TableIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        ltable->hash_buckets = (TableIndex*)HPROF_MALLOC(nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        (void)memset(ltable->hash_buckets, 0, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    (void)md_snprintf(lock_name, sizeof(lock_name),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                "HPROF %s table lock", name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    lock_name[sizeof(lock_name)-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    ltable->lock        = lock_create(lock_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    ltable->serial_num  = gdata->table_serial_number_counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    ltable->hare        = (ltable->serial_num << 28);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    LOG3("Table initialized", ltable->name, ltable->table_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    return ltable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
table_element_count(LookupTable *ltable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    int nelems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    HPROF_ASSERT(ltable!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        nelems = ltable->next_index-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    return nelems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
table_free_entry(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    HPROF_ASSERT(ltable!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    SANITY_CHECK_HARE(index, ltable->hare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    index = SANITY_REMOVE_HARE(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    SANITY_CHECK_INDEX(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        HPROF_ASSERT(!is_freed_entry(ltable, index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        free_entry(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
table_walk_items(LookupTable *ltable, LookupTableIterator func, void* arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    if ( ltable == NULL || ltable->next_index <= 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    HPROF_ASSERT(func!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        TableIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        int        fcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        LOG3("table_walk_items() count+free", ltable->name, ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        fcount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        for ( index = 1 ; index < ltable->next_index ; index++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if ( ! is_freed_entry(ltable, index) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                void *key_ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                int   key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                void *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                get_key(ltable, index, &key_ptr, &key_len);
13014
e2fe1611bb04 7176138: Fixes for missing close() calls and possible null pointer reference instead of fatal error
ohair
parents: 10292
diff changeset
   760
                if ( ltable->info_size == 0 ) {
e2fe1611bb04 7176138: Fixes for missing close() calls and possible null pointer reference instead of fatal error
ohair
parents: 10292
diff changeset
   761
                    info = NULL;
e2fe1611bb04 7176138: Fixes for missing close() calls and possible null pointer reference instead of fatal error
ohair
parents: 10292
diff changeset
   762
                } else {
e2fe1611bb04 7176138: Fixes for missing close() calls and possible null pointer reference instead of fatal error
ohair
parents: 10292
diff changeset
   763
                    info = get_info(ltable, index);
e2fe1611bb04 7176138: Fixes for missing close() calls and possible null pointer reference instead of fatal error
ohair
parents: 10292
diff changeset
   764
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                (*func)(SANITY_ADD_HARE(index, ltable->hare), key_ptr, key_len, info, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                if ( is_freed_entry(ltable, index) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    fcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                fcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        LOG3("table_walk_items() count-free", ltable->name, ltable->next_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        HPROF_ASSERT(fcount==ltable->freed_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
table_cleanup(LookupTable *ltable, LookupTableIterator func, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    if ( ltable == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    if ( func != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        table_walk_items(ltable, func, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        HPROF_FREE(ltable->table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if ( ltable->hash_buckets != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            HPROF_FREE(ltable->hash_buckets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        if ( ltable->freed_bv != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            HPROF_FREE(ltable->freed_bv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if ( ltable->info_blocks != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            blocks_term(ltable->info_blocks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            ltable->info_blocks = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if ( ltable->key_blocks != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            blocks_term(ltable->key_blocks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            ltable->key_blocks = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    lock_destroy(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    ltable->lock = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    HPROF_FREE(ltable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    ltable = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
TableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
table_create_entry(LookupTable *ltable, void *key_ptr, int key_len, void *info_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    TableIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    HashCode   hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    HPROF_ASSERT(ltable!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /* Create hash code if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    hcode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        hcode = hashcode(key_ptr, key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /* Create a new entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        /* Need to create a new entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        index = setup_new_entry(ltable, key_ptr, key_len, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /* Add to hash table if we have one */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            hash_in(ltable, index, hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    return SANITY_ADD_HARE(index, ltable->hare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
TableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
table_find_entry(LookupTable *ltable, void *key_ptr, int key_len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    TableIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    HashCode   hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /* Create hash code if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    hcode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        hcode = hashcode(key_ptr, key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /* Look for element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        index = find_entry(ltable, key_ptr, key_len, hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    return index==0 ? index : SANITY_ADD_HARE(index, ltable->hare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
TableIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
table_find_or_create_entry(LookupTable *ltable, void *key_ptr, int key_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                jboolean *pnew_entry, void *info_ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    TableIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    HashCode   hcode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /* Assume it is NOT a new entry for now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    if ( pnew_entry ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        *pnew_entry = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /* Create hash code if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    hcode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        hcode = hashcode(key_ptr, key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /* Look for element */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            index = find_entry(ltable, key_ptr, key_len, hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        if ( index == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            /* Need to create a new entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            index = setup_new_entry(ltable, key_ptr, key_len, info_ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            /* Add to hash table if we have one */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if ( ltable->hash_bucket_count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                hash_in(ltable, index, hcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            if ( pnew_entry ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                *pnew_entry = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    return SANITY_ADD_HARE(index, ltable->hare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
table_get_info(LookupTable *ltable, TableIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    void *info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    HPROF_ASSERT(ltable!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    HPROF_ASSERT(ltable->info_size > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    SANITY_CHECK_HARE(index, ltable->hare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    index = SANITY_REMOVE_HARE(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    SANITY_CHECK_INDEX(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        HPROF_ASSERT(!is_freed_entry(ltable, index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        info = get_info(ltable,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
table_get_key(LookupTable *ltable, TableIndex index, void **pkey_ptr, int *pkey_len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    HPROF_ASSERT(ltable!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    HPROF_ASSERT(pkey_ptr!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    HPROF_ASSERT(pkey_len!=NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    SANITY_CHECK_HARE(index, ltable->hare);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    HPROF_ASSERT(ltable->elem_size!=0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    index = SANITY_REMOVE_HARE(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    SANITY_CHECK_INDEX(ltable, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    lock_enter(ltable->lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        HPROF_ASSERT(!is_freed_entry(ltable, index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        get_key(ltable, index, pkey_ptr, pkey_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    } lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
table_lock_enter(LookupTable *ltable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    lock_enter(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
table_lock_exit(LookupTable *ltable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    lock_exit(ltable->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
}