00001 /* 00002 * @(#)hprof_string.c 1.16 10/03/23 00003 * 00004 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * -Redistribution of source code must retain the above copyright notice, this 00010 * list of conditions and the following disclaimer. 00011 * 00012 * -Redistribution in binary form must reproduce the above copyright notice, 00013 * this list of conditions and the following disclaimer in the documentation 00014 * and/or other materials provided with the distribution. 00015 * 00016 * Neither the name of Oracle or the names of contributors may 00017 * be used to endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * This software is provided "AS IS," without a warranty of any kind. ALL 00021 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING 00022 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 00023 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") 00024 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE 00025 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS 00026 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST 00027 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, 00028 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY 00029 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, 00030 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 00031 * 00032 * You acknowledge that this software is not designed, licensed or intended 00033 * for use in the design, construction, operation or maintenance of any 00034 * nuclear facility. 00035 */ 00036 00037 /* Table of byte arrays (e.g. char* string + NULL byte) */ 00038 00039 /* 00040 * Strings are unique by their own contents, since the string itself 00041 * is the Key, and the hprof_table.c guarantees that keys don't move, 00042 * this works out perfect. Any key in this table can be used as 00043 * an char*. 00044 * 00045 * This does mean that this table has dynamically sized keys. 00046 * 00047 * Care needs to be taken to make sure the NULL byte is included, not for 00048 * the sake of hprof_table.c, but so that the key can be used as a char*. 00049 * 00050 */ 00051 00052 #include "hprof.h" 00053 00054 void 00055 string_init(void) 00056 { 00057 HPROF_ASSERT(gdata->string_table==NULL); 00058 gdata->string_table = table_initialize("Strings", 4096, 4096, 1024, 0); 00059 } 00060 00061 StringIndex 00062 string_find_or_create(const char *str) 00063 { 00064 return table_find_or_create_entry(gdata->string_table, 00065 (void*)str, (int)strlen(str)+1, NULL, NULL); 00066 } 00067 00068 static void 00069 list_item(TableIndex index, void *str, int len, void *info_ptr, void *arg) 00070 { 00071 debug_message( "0x%08x: String \"%s\"\n", index, (const char *)str); 00072 } 00073 00074 void 00075 string_list(void) 00076 { 00077 debug_message( 00078 "-------------------- String Table ------------------------\n"); 00079 table_walk_items(gdata->string_table, &list_item, NULL); 00080 debug_message( 00081 "----------------------------------------------------------\n"); 00082 } 00083 00084 void 00085 string_cleanup(void) 00086 { 00087 table_cleanup(gdata->string_table, NULL, NULL); 00088 gdata->string_table = NULL; 00089 } 00090 00091 char * 00092 string_get(StringIndex index) 00093 { 00094 void *key; 00095 int key_len; 00096 00097 table_get_key(gdata->string_table, index, &key, &key_len); 00098 HPROF_ASSERT(key_len>0); 00099 return (char*)key; 00100 } 00101 00102 int 00103 string_get_len(StringIndex index) 00104 { 00105 void *key; 00106 int key_len; 00107 00108 table_get_key(gdata->string_table, index, &key, &key_len); 00109 HPROF_ASSERT(key_len>0); 00110 return key_len-1; 00111 } 00112
1.6.1