make dict alloc its own storage

having it keep a ptr passed in is dumb
This commit is contained in:
Eric House 2021-03-07 21:13:32 -08:00
parent 829242c9bb
commit 575e61059c
3 changed files with 16 additions and 18 deletions

View file

@ -813,18 +813,14 @@ storeAsDict(Globals* globals, const char* lc, const char* name,
/* First make a dict of it. If it doesn't work out, don't store the
data! */
uint8_t* poolCopy = XP_MALLOC( globals->mpool, len );
XP_MEMCPY( poolCopy, data, len );
DictionaryCtxt* dict =
wasm_dictionary_make( globals, NULL, shortName, poolCopy, len );
wasm_dictionary_make( globals, NULL, shortName, data, len );
bool success = !!dict;
if ( success ) {
dict_unref( dict, NULL );
const XP_UCHAR* keys[] = {KEY_DICTS, lc, shortName, NULL};
dutil_storePtr( globals->dutil, NULL, keys, data, len );
} else {
XP_FREE( globals->mpool, poolCopy );
}
LOG_RETURNF( "%d", success );
return success;
@ -1162,6 +1158,7 @@ onOneDict( void* closure, const XP_UCHAR* keys[] )
uint8_t* ptr = XP_MALLOC( fos->globals->mpool, len );
dutil_loadPtr( dutil, NULL, keys, ptr, &len );
fos->dict = wasm_dictionary_make( fos->globals, NULL, dictName, ptr, len );
XP_FREE( fos->globals->mpool, ptr );
return XP_FALSE;
}

View file

@ -51,24 +51,21 @@ getShortName( const DictionaryCtxt* dict )
static XP_Bool
initFromPtr( WasmDictionaryCtxt* dctx, const char* name,
uint8_t* dictBase, size_t len )
uint8_t* data, size_t len )
{
XP_Bool formatOk = XP_TRUE;
size_t dictLength;
XP_U32 topOffset;
char path[256];
dctx->dictLength = len;
dctx->dictBase = dictBase;
dctx->dictBase = XP_MALLOC( dctx->super.mpool, len );
XP_MEMCPY( dctx->dictBase, data, len );
const XP_U8* ptr = dctx->dictBase;
const XP_U8* end = ptr + dctx->dictLength;
formatOk = parseCommon( &dctx->super, NULL, &ptr, end );
XP_Bool formatOk = parseCommon( &dctx->super, NULL, &ptr, end );
/* && loadSpecialData( &dctx->super, &ptr, end ); */
if ( formatOk ) {
size_t curPos = ptr - dctx->dictBase;
dictLength = dctx->dictLength - curPos;
size_t dictLength = dctx->dictLength - curPos;
XP_U32 topOffset;
if ( dictLength > 0 ) {
memcpy( &topOffset, ptr, sizeof(topOffset) );
@ -171,7 +168,7 @@ wasm_dictionary_make_empty( Globals* globals )
dict_super_init( MPPARM(globals->mpool) (DictionaryCtxt*)wdict );
LOG_RETURNF( "%p", wdict );
return (DictionaryCtxt*)wdict;
return &wdict->super;
}
DictionaryCtxt*

View file

@ -429,7 +429,7 @@ gotForLang( void* closure, const XP_UCHAR* keys[] )
} else {
XP_LOGFF( "nothing for %s/%s", keys[1], keys[2] );
}
return NULL != fls->ptr;
return NULL == fls->ptr;
}
static const DictionaryCtxt*
@ -447,7 +447,9 @@ wasm_dutil_getDict( XW_DUtilCtxt* duc, XWEnv xwe,
const char* keys[] = {KEY_DICTS, lc, dictName, NULL };
dutil_loadPtr( duc, xwe, keys, NULL, &len );
if ( 0 < len ) {
uint8_t* ptr = XP_MALLOC( duc->mpool, len );
XP_LOGFF( "making stack alloc of %d bytes", len );
uint8_t ptr[len]; /* this should blow up. JS is special. */
XP_LOGFF( "MADE stack alloc of %d bytes!!!", len );
dutil_loadPtr( duc, xwe, keys, ptr, &len );
result = wasm_dictionary_make( globals, xwe, dictName, ptr, len );
dmgr_put( globals->dictMgr, xwe, dictName, result );
@ -460,7 +462,9 @@ wasm_dutil_getDict( XW_DUtilCtxt* duc, XWEnv xwe,
const char* langKeys[] = {KEY_DICTS, lc, KEY_WILDCARD, NULL};
dutil_forEach( duc, xwe, langKeys, gotForLang, &fls );
if ( !!fls.ptr ) {
result = wasm_dictionary_make( globals, xwe, dictName, fls.ptr, fls.len );
result = wasm_dictionary_make( globals, xwe, dictName,
fls.ptr, fls.len );
XP_FREE( globals->mpool, fls.ptr );
dmgr_put( globals->dictMgr, xwe, dictName, result );
}
}