2005-09-20 21:26:39 +08:00
|
|
|
/** \file intern.c
|
|
|
|
|
|
|
|
Library for pooling common strings
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
2005-09-29 05:02:24 +08:00
|
|
|
#include <unistd.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2006-01-20 22:27:21 +08:00
|
|
|
#include "wutil.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "intern.h"
|
|
|
|
|
2006-01-24 04:40:14 +08:00
|
|
|
/**
|
|
|
|
Table of interned strings
|
|
|
|
*/
|
|
|
|
static hash_table_t *intern_table=0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Table of static interned strings
|
|
|
|
*/
|
|
|
|
static hash_table_t *intern_static_table=0;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
const wchar_t *intern( const wchar_t *in )
|
|
|
|
{
|
2012-01-01 07:57:30 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2005-09-20 21:26:39 +08:00
|
|
|
const wchar_t *res=0;
|
2006-11-11 18:54:52 +08:00
|
|
|
|
|
|
|
// debug( 0, L"intern %ls", in );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !in )
|
|
|
|
return 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !intern_table )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
intern_table = (hash_table_t *)malloc( sizeof( hash_table_t ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !intern_table )
|
|
|
|
{
|
2006-07-03 18:39:57 +08:00
|
|
|
DIE_MEM();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
hash_init( intern_table, &hash_wcs_func, &hash_wcs_cmp );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( intern_static_table )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
res = (const wchar_t *)hash_get( intern_static_table, in );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !res )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
res = (const wchar_t *)hash_get( intern_table, in );
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
res = wcsdup( in );
|
|
|
|
if( !res )
|
|
|
|
{
|
2006-07-03 18:39:57 +08:00
|
|
|
DIE_MEM();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
hash_put( intern_table, res, res );
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wchar_t *intern_static( const wchar_t *in )
|
|
|
|
{
|
|
|
|
const wchar_t *res=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !in )
|
|
|
|
return 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !intern_static_table )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
intern_static_table = (hash_table_t *)malloc( sizeof( hash_table_t ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !intern_static_table )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
DIE_MEM();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
hash_init( intern_static_table, &hash_wcs_func, &hash_wcs_cmp );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
res = (const wchar_t *)hash_get( intern_static_table, in );
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
res = in;
|
|
|
|
hash_put( intern_static_table, res, res );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
|
|
|
Free the specified key/value pair. Should only be called by intern_free_all at shutdown
|
|
|
|
*/
|
2006-06-13 05:47:42 +08:00
|
|
|
static void clear_value( void *key, void *data )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
debug( 3, L"interned string: '%ls'", data );
|
2005-09-20 21:26:39 +08:00
|
|
|
free( (void *)data );
|
|
|
|
}
|
|
|
|
|
|
|
|
void intern_free_all()
|
|
|
|
{
|
|
|
|
if( intern_table )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
hash_foreach( intern_table, &clear_value );
|
|
|
|
hash_destroy( intern_table );
|
2005-09-20 21:26:39 +08:00
|
|
|
free( intern_table );
|
2011-12-27 11:18:46 +08:00
|
|
|
intern_table=0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( intern_static_table )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
hash_destroy( intern_static_table );
|
2005-09-20 21:26:39 +08:00
|
|
|
free( intern_static_table );
|
2011-12-27 11:18:46 +08:00
|
|
|
intern_static_table=0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|