From 575e61059c5e2e4d7ea6ce4d3af7459d6bfb315f Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 7 Mar 2021 21:13:32 -0800 Subject: [PATCH] make dict alloc its own storage having it keep a ptr passed in is dumb --- xwords4/wasm/main.c | 7 ++----- xwords4/wasm/wasmdict.c | 17 +++++++---------- xwords4/wasm/wasmdutil.c | 10 +++++++--- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/xwords4/wasm/main.c b/xwords4/wasm/main.c index 158385b5d..fcfddfb42 100644 --- a/xwords4/wasm/main.c +++ b/xwords4/wasm/main.c @@ -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; } diff --git a/xwords4/wasm/wasmdict.c b/xwords4/wasm/wasmdict.c index 7809f190e..c97b4502c 100644 --- a/xwords4/wasm/wasmdict.c +++ b/xwords4/wasm/wasmdict.c @@ -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* diff --git a/xwords4/wasm/wasmdutil.c b/xwords4/wasm/wasmdutil.c index 68624fdcf..43b02692b 100644 --- a/xwords4/wasm/wasmdutil.c +++ b/xwords4/wasm/wasmdutil.c @@ -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 ); } }