jdk/src/share/demo/jvmti/hprof/hprof_string.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10292 ed7db6a12c2a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2004, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/* Table of byte arrays (e.g. char* string + NULL byte) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Strings are unique by their own contents, since the string itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *   is the Key, and the hprof_table.c guarantees that keys don't move,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *   this works out perfect. Any key in this table can be used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   an char*.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This does mean that this table has dynamically sized keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Care needs to be taken to make sure the NULL byte is included, not for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   the sake of hprof_table.c, but so that the key can be used as a char*.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
string_init(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    HPROF_ASSERT(gdata->string_table==NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    gdata->string_table = table_initialize("Strings", 4096, 4096, 1024, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
StringIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
string_find_or_create(const char *str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    return table_find_or_create_entry(gdata->string_table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                (void*)str, (int)strlen(str)+1, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
list_item(TableIndex index, void *str, int len, void *info_ptr, void *arg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    debug_message( "0x%08x: String \"%s\"\n", index, (const char *)str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
string_list(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        "-------------------- String Table ------------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    table_walk_items(gdata->string_table, &list_item, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    debug_message(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "----------------------------------------------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
string_cleanup(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    table_cleanup(gdata->string_table, NULL, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    gdata->string_table = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
string_get(StringIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    void *key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    int   key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    table_get_key(gdata->string_table, index, &key, &key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    HPROF_ASSERT(key_len>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    return (char*)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
string_get_len(StringIndex index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    void *key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    int   key_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    table_get_key(gdata->string_table, index, &key, &key_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    HPROF_ASSERT(key_len>0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return key_len-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}