From fe4db155361649999aa67f23ceb2bdee565cf6aa Mon Sep 17 00:00:00 2001 From: helixhorned Date: Mon, 25 May 2015 18:58:31 +0000 Subject: [PATCH] C-CON: fix 'resizearray' after r4735: copy over old values, zero-init new ones. Make lunatic/test/gamearray.con run in C-CON, too. git-svn-id: https://svn.eduke32.com/eduke32@5238 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/gameexec.c | 23 +++++++++++------- .../eduke32/source/lunatic/test/gamearray.con | 24 ++++++++++++------- polymer/eduke32/source/sdlmusic.c | 2 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/polymer/eduke32/source/gameexec.c b/polymer/eduke32/source/gameexec.c index a0c4b7408..1f36449af 100644 --- a/polymer/eduke32/source/gameexec.c +++ b/polymer/eduke32/source/gameexec.c @@ -4590,11 +4590,9 @@ finish_qsprintf: // elements, resize the array to size zero. if (numelts > 0) { - /*OSD_Printf(OSDTEXT_GREEN "CON_RESIZEARRAY: resizing array %s from %d to %d\n", - aGameArrays[j].szLabel, aGameArrays[j].size, numelts);*/ int32_t numbytes = numelts * sizeof(int32_t); #ifdef BITNESS64 - int32_t *tmpar = (int32_t *)Xmalloc(numbytes); + int32_t *tmpar = (int32_t *)Xcalloc(numelts, sizeof(int32_t)); kread(fil, tmpar, numbytes); #endif Baligned_free(aGameArrays[j].plValues); @@ -4650,14 +4648,23 @@ finish_qsprintf: insptr++; { tw=*insptr++; - int32_t asize = Gv_GetVarX(*insptr++); + const int32_t newSize = Gv_GetVarX(*insptr++); + const int32_t oldSize = aGameArrays[tw].size; - if (asize > 0) + if (newSize > 0 && newSize != oldSize) { - /*OSD_Printf(OSDTEXT_GREEN "CON_RESIZEARRAY: resizing array %s from %d to %d\n", aGameArrays[j].szLabel, aGameArrays[j].size, asize);*/ +// OSD_Printf(OSDTEXT_GREEN "CON_RESIZEARRAY: resizing array %s from %d to %d\n", +// aGameArrays[j].szLabel, aGameArrays[j].size, newSize); + intptr_t *const tmpar = Xmalloc(GAR_ELTSZ * oldSize); + memcpy(tmpar, aGameArrays[tw].plValues, GAR_ELTSZ * oldSize); + Baligned_free(aGameArrays[tw].plValues); - aGameArrays[tw].plValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, GAR_ELTSZ * asize); - aGameArrays[tw].size = asize; + aGameArrays[tw].plValues = (intptr_t *)Xaligned_alloc(ACTOR_VAR_ALIGNMENT, GAR_ELTSZ * newSize); + aGameArrays[tw].size = newSize; + + memcpy(aGameArrays[tw].plValues, tmpar, GAR_ELTSZ * min(oldSize, newSize)); + if (newSize > oldSize) + memset(&aGameArrays[tw].plValues[oldSize], 0, GAR_ELTSZ * (newSize - oldSize)); } continue; } diff --git a/polymer/eduke32/source/lunatic/test/gamearray.con b/polymer/eduke32/source/lunatic/test/gamearray.con index 88a2313f5..075f489a9 100644 --- a/polymer/eduke32/source/lunatic/test/gamearray.con +++ b/polymer/eduke32/source/lunatic/test/gamearray.con @@ -1,6 +1,4 @@ // Test for gamearrays / their persistence. -// Doesn't run in C-CON because redefinequote/qsprintf error out when passed a -// non-allocated quote as destination. define QWESZ 10 define ASDSZ 12 @@ -25,8 +23,15 @@ gamevar local 0 0 gamevar i 0 0 gamevar tmp 0 0 -definequote 400 LTEST_QWE_ARRAY.bin -definequote 401 LTEST_ASD_ARRAY.bin +define Q_qwe_fn 400 +define Q_asd_fn 401 +definequote Q_qwe_fn LTEST_QWE_ARRAY.bin +definequote Q_asd_fn LTEST_ASD_ARRAY.bin + +// Wouldn't run in C-CON otherwise; redefinequote/qsprintf errors out when +// passed a non-allocated quote as destination. +definequote 500 TEMP +definequote 501 TEMP definequote 505 tile y sizes: 0:%d 1:%d 2:%d 3:%d 4:%d 5:%d 6:%d 7:%d 8:%d definequote 506 1: 0x0a0a0d0d==168430861 ? %d @@ -80,11 +85,14 @@ onevent EVENT_ENTERLEVEL resizearray qwe NEWQWESZ // shrink resizearray asd NEWASDSZ // grow - writearraytofile qwe 400 - writearraytofile asd 401 + ifvarn qwe[4] 4 state error + ifvarn asd[4] 0x0a0a0d0d state error - readarrayfromfile qwe1 400 - readarrayfromfile asd1 401 + writearraytofile qwe Q_qwe_fn + writearraytofile asd Q_asd_fn + + readarrayfromfile qwe1 Q_qwe_fn + readarrayfromfile asd1 Q_asd_fn qsprintf 507 507 asd[4] userquote 507 diff --git a/polymer/eduke32/source/sdlmusic.c b/polymer/eduke32/source/sdlmusic.c index 74e3165be..477de1454 100644 --- a/polymer/eduke32/source/sdlmusic.c +++ b/polymer/eduke32/source/sdlmusic.c @@ -491,7 +491,7 @@ int32_t MUSIC_PlaySong(char *song, int32_t loopflag) else initprintf("%s: fopen: %s\n", __func__, strerror(errno)); } else - music_musicchunk = Mix_LoadMUS_RW(SDL_RWFromMem((char *) song, g_musicSize) + music_musicchunk = Mix_LoadMUS_RW(SDL_RWFromMem(song, g_musicSize) #if (SDL_MAJOR_VERSION > 1) , SDL_FALSE #endif