From 4dfa351e8d953157450d26c3a84893991d9d8e52 Mon Sep 17 00:00:00 2001 From: Josh Davies Date: Mon, 26 Feb 2024 21:56:07 +0000 Subject: [PATCH 1/4] Split the large sort allocation into separate allocations. Buffer overruns in these allocations will be visible to valgrind. Fix units mismatch re: IObuffersize, IOtry. Print warnings in debug mode, if any buffer sizes are altered by AllocSort. Update "default" buffer sizes, so that we have no warnings. --- sources/form3.h | 2 + sources/fsizes.h | 6 +-- sources/setfile.c | 124 +++++++++++++++++++++++++++++++++++++++++----- sources/sort.c | 20 ++++++++ 4 files changed, 137 insertions(+), 15 deletions(-) diff --git a/sources/form3.h b/sources/form3.h index 52553b85..bb4ed819 100644 --- a/sources/form3.h +++ b/sources/form3.h @@ -438,6 +438,8 @@ template struct calc { */ #define WITHSORTBOTS +#define SPLITALLOC + #include #include #include diff --git a/sources/fsizes.h b/sources/fsizes.h index d0b6f6e1..6fa740ac 100644 --- a/sources/fsizes.h +++ b/sources/fsizes.h @@ -126,10 +126,10 @@ #define MAXFPATCHES 256 #define SORTIOSIZE 200000L -#define SSMALLBUFFER 500000L -#define SSMALLOVERFLOW 800000L +#define SSMALLBUFFER 2560016L +#define SSMALLOVERFLOW 3840032L #define STERMSSMALL 10000L -#define SLARGEBUFFER 4000000L +#define SLARGEBUFFER 26880512L #define SMAXPATCHES 64 #define SMAXFPATCHES 64 #define SSORTIOSIZE 32768L diff --git a/sources/setfile.c b/sources/setfile.c index ecf976e7..451073e4 100644 --- a/sources/setfile.c +++ b/sources/setfile.c @@ -574,6 +574,7 @@ int AllocSetups(VOID) AM.S0 = 0; AM.S0 = AllocSort(LargeSize,SmallSize,SmallEsize,TermsInSmall ,MaxPatches,MaxFpatches,IOsize); + /* AM.S0->file.ziosize was already set to a (larger) value by AllocSort, here it is re-set. */ #ifdef WITHZLIB AM.S0->file.ziosize = IOsize; #ifndef WITHPTHREADS @@ -848,20 +849,30 @@ VOID WriteSetup(VOID) To be used for the main allocation of the sort buffers, and in a later stage for the function and subroutine sort buffers. */ - -SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsInSmall, - int MaxPatches, int MaxFpatches, LONG IOsize) +SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG inTermsInSmall, + int inMaxPatches, int inMaxFpatches, LONG inIOsize) { - LONG allocation,longer,terms2insmall,sortsize,longerp; + LONG LargeSize = inLargeSize; + LONG SmallSize = inSmallSize; + LONG SmallEsize = inSmallEsize; + LONG TermsInSmall = inTermsInSmall; + int MaxPatches = inMaxPatches; + int MaxFpatches = inMaxFpatches; + LONG IOsize = inIOsize; + +#ifndef SPLITALLOC + LONG allocation; +#endif + LONG longer,terms2insmall,sortsize,longerp; LONG IObuffersize = IOsize; LONG IOtry; SORTING *sort; - int i = 0, j = 0; + int fname2Size = 0, j = 0; char *s; if ( AM.S0 != 0 ) { - s = FG.fname2; i = 0; - while ( *s ) { s++; i++; } - i += 16; + s = FG.fname2; fname2Size = 0; + while ( *s ) { s++; fname2Size++; } + fname2Size += 16; } if ( MaxFpatches < 4 ) MaxFpatches = 4; longer = MaxPatches > MaxFpatches ? MaxPatches : MaxFpatches; @@ -876,7 +887,6 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn terms2insmall = 2*TermsInSmall; /* Used to be just + 100 rather than *2 */ if ( SmallEsize < (SmallSize*3)/2 ) SmallEsize = (SmallSize*3)/2; if ( LargeSize > 0 && LargeSize < 2*SmallSize ) LargeSize = 2*SmallSize; -/* if ( SmallEsize < 3*AM.MaxTer ) SmallEsize = 3*AM.MaxTer; */ SmallEsize = (SmallEsize+15) & (-16L); if ( LargeSize < 0 ) LargeSize = 0; sortsize = sizeof(SORTING); @@ -898,9 +908,23 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn IOtry = ((LargeSize+SmallEsize)/MaxFpatches-2*AM.MaxTer)/sizeof(WORD)-COMPINC; - if ( (LONG)(IObuffersize*sizeof(WORD)) < IOtry ) - IObuffersize = (IOtry+sizeof(WORD)-1)/sizeof(WORD); + /* Here both IObuffersize and IOtry are in units of sizeof(WORD). */ + if ( IObuffersize < IOtry ) { + IObuffersize = IOtry; + } + +#if DEBUGGING + if ( LargeSize != inLargeSize ) { MesPrint("Warning: LargeSize adjusted: %l -> %l", inLargeSize, LargeSize); } + if ( SmallSize != inSmallSize ) { MesPrint("Warning: SmallSize adjusted: %l -> %l", inSmallSize, SmallSize); } + if ( SmallEsize != inSmallEsize ) {MesPrint("Warning: SmallEsize adjusted: %l -> %l", inSmallEsize, SmallEsize); } + if ( TermsInSmall != inTermsInSmall ) { MesPrint("Warning: TermsInSmall adjusted: %l -> %l", inTermsInSmall, TermsInSmall); } + if ( MaxPatches != inMaxPatches ) { MesPrint("Warning: MaxPatches adjusted: %d -> %d", inMaxPatches, MaxPatches); } + if ( MaxFpatches != inMaxFpatches ) {MesPrint("Warning: MaxFPatches adjusted: %d -> %d", inMaxFpatches, MaxFpatches); } + /* This one is always changed if the LargeSize has not been... */ + /* if ( IObuffersize != inIOsize/(LONG)sizeof(WORD) ) { MesPrint("Warning: IOsize adjusted: %l -> %l", inIOsize/sizeof(WORD), IObuffersize); } */ +#endif +#ifndef SPLITALLOC allocation = 3*sizeof(POSITION)*(LONG)longer /* Filepositions!! */ +2*sizeof(WORD *)*longer @@ -914,7 +938,7 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn +LargeSize +SmallEsize +sortsize - +IObuffersize*sizeof(WORD) + i + 16; + +IObuffersize*sizeof(WORD) + fname2Size + 16; sort = (SORTING *)Malloc1(allocation,"sort buffers"); sort->LargeSize = LargeSize/sizeof(WORD); @@ -931,11 +955,13 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn sort->pStop = sort->Patches+longer; sort->poina = sort->pStop+longer; sort->poin2a = sort->poina + longerp; + sort->fPatches = (POSITION *)(sort->poin2a+longerp); sort->fPatchesStop = sort->fPatches + longer; sort->inPatches = sort->fPatchesStop + longer; sort->tree = (WORD *)(sort->inPatches + longer); sort->used = sort->tree+longerp; + #ifdef WITHZLIB sort->fpcompressed = sort->used+longerp; sort->fpincompressed = sort->fpcompressed+longerp+2; @@ -944,13 +970,16 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn #else sort->ktoi = sort->used + longerp; #endif + sort->lBuffer = (WORD *)(sort->ktoi + longerp + 2); sort->lTop = sort->lBuffer+sort->LargeSize; + sort->sBuffer = sort->lTop; if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; } sort->sTop = sort->sBuffer + sort->SmallSize; sort->sTop2 = sort->sBuffer + sort->SmallEsize; sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1); + sort->file.PObuffer = (WORD *)(sort->sTop2); sort->file.POstop = sort->file.PObuffer+IObuffersize; sort->file.POsize = IObuffersize * sizeof(WORD); @@ -975,6 +1004,77 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn sort->cBufferSize = 0; sort->f = 0; sort->PolyWise = 0; +#endif + + +#ifdef SPLITALLOC + // Separate buffers. + sort = Malloc1(sizeof(*sort), "SPLITALLOC sorting struct"); + + sort->LargeSize = LargeSize/sizeof(WORD); + sort->SmallSize = SmallSize/sizeof(WORD); + sort->SmallEsize = SmallEsize/sizeof(WORD); + sort->MaxPatches = MaxPatches; + sort->MaxFpatches = MaxFpatches; + sort->TermsInSmall = TermsInSmall; + sort->Terms2InSmall = terms2insmall; + + sort->sPointer = Malloc1(sizeof(*(sort->sPointer ))*terms2insmall, "SPLITALLOC sPointer"); + sort->SplitScratch = Malloc1(sizeof(*(sort->SplitScratch))*terms2insmall/2, "SPLITALLOC SplitScratch"); + sort->Patches = Malloc1(sizeof(*(sort->Patches ))*longer, "SPLITALLOC Patches"); + sort->pStop = Malloc1(sizeof(*(sort->pStop ))*longer, "SPLITALLOC pStop"); + sort->poina = Malloc1(sizeof(*(sort->poina ))*longerp, "SPLITALLOC poina"); + sort->poin2a = Malloc1(sizeof(*(sort->poin2a ))*longerp, "SPLITALLOC poin2a"); + + sort->fPatches = Malloc1(sizeof(*(sort->fPatches ))*longer, "SPLITALLOC fPatches"); + sort->fPatchesStop = Malloc1(sizeof(*(sort->fPatchesStop))*longer, "SPLITALLOC fPatchesStop"); + sort->inPatches = Malloc1(sizeof(*(sort->inPatches ))*longer, "SPLITALLOC inPatches"); + sort->tree = Malloc1(sizeof(*(sort->tree ))*longerp, "SPLITALLOC tree"); + sort->used = Malloc1(sizeof(*(sort->used ))*longerp, "SPLITALLOC used"); + +#ifdef WITHZLIB + sort->fpcompressed = Malloc1(sizeof(*(sort->fpcompressed ))*(longerp+2), "SPLITALLOC fpcompressed"); + sort->fpincompressed = Malloc1(sizeof(*(sort->fpincompressed))*(longerp+2), "SPLITALLOC fpincompressed"); + sort->zsparray = 0; +#endif + + sort->ktoi = Malloc1(sizeof(*(sort->ktoi))*(longerp+2), "SPLITALLOC ktoi"); + + // The combined Large buffer and Small buffer (+ extension) are used. + // They must be allocated together. + sort->lBuffer = Malloc1(sizeof(*(sort->lBuffer))*(sort->LargeSize+sort->SmallEsize), "SPLITALLOC lBuffer+sBuffer"); + sort->lTop = sort->lBuffer+sort->LargeSize; + + sort->sBuffer = sort->lTop; + if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; } + sort->sTop = sort->sBuffer + sort->SmallSize; + sort->sTop2 = sort->sBuffer + sort->SmallEsize; + sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1); + + sort->file.PObuffer = Malloc1(IObuffersize*sizeof(*(sort->file.PObuffer))+fname2Size+16, "SPLITALLOC PObuffer"); + sort->file.POstop = sort->file.PObuffer+IObuffersize; + sort->file.POsize = IObuffersize * sizeof(WORD); + sort->file.POfill = sort->file.POfull = sort->file.PObuffer; + sort->file.active = 0; + sort->file.handle = -1; + PUTZERO(sort->file.POposition); +#ifdef WITHPTHREADS + sort->file.pthreadslock = dummylock; +#endif +#ifdef WITHZLIB + sort->file.ziosize = IObuffersize*sizeof(WORD); + sort->file.ziobuffer = 0; +#endif + if ( AM.S0 != 0 ) { + sort->file.name = (char *)(sort->file.PObuffer + IObuffersize); + AllocSortFileName(sort); + } + else sort->file.name = 0; + sort->cBuffer = 0; + sort->cBufferSize = 0; + sort->f = 0; + sort->PolyWise = 0; +#endif return(sort); } diff --git a/sources/sort.c b/sources/sort.c index a67c1642..ae5939ba 100644 --- a/sources/sort.c +++ b/sources/sort.c @@ -4757,7 +4757,27 @@ void CleanUpSort(int num) MUNLOCK(ErrorMessageLock); #endif } +#ifdef SPLITALLOC + M_free(S->sPointer, "SPLITALLOC sPointer"); + M_free(S->SplitScratch, "SPLITALLOC SplitScratch"); + M_free(S->Patches, "SPLITALLOC Patches"); + M_free(S->pStop, "SPLITALLOC pStop"); + M_free(S->poina, "SPLITALLOC poina"); + M_free(S->poin2a, "SPLITALLOC poin2a"); + M_free(S->fPatches, "SPLITALLOC fPatches"); + M_free(S->fPatchesStop, "SPLITALLOC fPatchesStop"); + M_free(S->inPatches, "SPLITALLOC inPatches"); + M_free(S->tree, "SPLITALLOC tree"); + M_free(S->used, "SPLITALLOC used"); + M_free(S->fpcompressed, "SPLITALLOC fpcompressed"); + M_free(S->fpincompressed, "SPLITALLOC fpincompressed"); + M_free(S->ktoi, "SPLITALLOC ktoi"); + M_free(S->lBuffer, "SPLITALLOC lBuffer+sBuffer"); + M_free(S->file.PObuffer, "SPLITALLOC PObuffer"); + M_free(S, "SPLITALLOC sorting struct"); +#else M_free(S,"sorting struct"); +#endif } AN.FunSorts[i] = 0; } From 81741a3899efb323b64d0c8960c66eefd0a9e761 Mon Sep 17 00:00:00 2001 From: Josh Davies Date: Thu, 13 Jun 2024 21:58:43 +0100 Subject: [PATCH 2/4] Remove unused SplitScratch from sort struct --- sources/setfile.c | 5 +---- sources/sort.c | 1 - sources/structs.h | 5 ++--- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/sources/setfile.c b/sources/setfile.c index 451073e4..d656e1f2 100644 --- a/sources/setfile.c +++ b/sources/setfile.c @@ -934,7 +934,6 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i +(2*longerp+4)*sizeof(WORD) #endif +terms2insmall*sizeof(WORD *) - +terms2insmall*sizeof(WORD *)/2 +LargeSize +SmallEsize +sortsize @@ -950,8 +949,7 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i sort->Terms2InSmall = terms2insmall; sort->sPointer = (WORD **)(sort+1); - sort->SplitScratch = sort->sPointer + terms2insmall; - sort->Patches = (WORD **)(sort->SplitScratch + terms2insmall/2); + sort->Patches = (WORD **)(sort->sPointer + terms2insmall); sort->pStop = sort->Patches+longer; sort->poina = sort->pStop+longer; sort->poin2a = sort->poina + longerp; @@ -1020,7 +1018,6 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i sort->Terms2InSmall = terms2insmall; sort->sPointer = Malloc1(sizeof(*(sort->sPointer ))*terms2insmall, "SPLITALLOC sPointer"); - sort->SplitScratch = Malloc1(sizeof(*(sort->SplitScratch))*terms2insmall/2, "SPLITALLOC SplitScratch"); sort->Patches = Malloc1(sizeof(*(sort->Patches ))*longer, "SPLITALLOC Patches"); sort->pStop = Malloc1(sizeof(*(sort->pStop ))*longer, "SPLITALLOC pStop"); sort->poina = Malloc1(sizeof(*(sort->poina ))*longerp, "SPLITALLOC poina"); diff --git a/sources/sort.c b/sources/sort.c index ae5939ba..154b1fd6 100644 --- a/sources/sort.c +++ b/sources/sort.c @@ -4759,7 +4759,6 @@ void CleanUpSort(int num) } #ifdef SPLITALLOC M_free(S->sPointer, "SPLITALLOC sPointer"); - M_free(S->SplitScratch, "SPLITALLOC SplitScratch"); M_free(S->Patches, "SPLITALLOC Patches"); M_free(S->pStop, "SPLITALLOC pStop"); M_free(S->poina, "SPLITALLOC poina"); diff --git a/sources/structs.h b/sources/structs.h index 5806d132..08c47398 100644 --- a/sources/structs.h +++ b/sources/structs.h @@ -1151,7 +1151,6 @@ typedef struct sOrT { WORD *sFill; /* Filling point in the small buffer */ WORD **sPointer; /* Pointers to terms in the small buffer */ WORD **PoinFill; /* Filling point for pointers to the sm.buf */ - WORD **SplitScratch; /* Excess pointer space for the merge sort */ WORD *cBuffer; /* Compress buffer (if it exists) */ WORD **Patches; /* Positions of patches in large buffer */ WORD **pStop; /* Ends of patches in the large buffer */ @@ -1198,9 +1197,9 @@ typedef struct sOrT { WORD inNum; /* Number of patches on file (input) */ WORD stage4; /* Are we using stage4? */ #ifdef WITHZLIB - PADPOSITION(28,12,12,3,0); + PADPOSITION(27,12,12,3,0); #else - PADPOSITION(25,12,12,3,0); + PADPOSITION(24,12,12,3,0); #endif } SORTING; From 5c0f5af87c7b36fa3f7a9f3244794fe289f4c33e Mon Sep 17 00:00:00 2001 From: Josh Davies Date: Mon, 11 Nov 2024 15:19:37 +0000 Subject: [PATCH 3/4] Update manual to quote actual sub-buffer sizes --- doc/manual/setup.tex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/manual/setup.tex b/doc/manual/setup.tex index 8576c038..a1bea7d0 100644 --- a/doc/manual/setup.tex +++ b/doc/manual/setup.tex @@ -478,9 +478,9 @@ \chapter{The setup} sorttype & lowfirst & lowfirst & lowfirst \\ subfilepatches & 64 & 64 & 64 \\ sublargepatches & 64 & 64 & 64 \\ -sublargesize & 4000000 & 4000000 & 4000000 \\ -subsmallextension & 800000 & 800000 & 800000 \\ -subsmallsize & 500000 & 500000 & 500000 \\ +sublargesize & 26880512 & 26880512 & 26880512 \\ +subsmallextension & 3840032 & 3840032 & 3840032 \\ +subsmallsize & 2560016 & 2560016 & 2560016 \\ subsortiosize & 32768 & 32768 & 32768 \\ subtermsinsmall & 10000 & 10000 & 10000 \\ tempdir & . & . & . \\ From fd2410bba42aecb91a3c8961f782d1069d20b458 Mon Sep 17 00:00:00 2001 From: Josh Davies Date: Thu, 14 Nov 2024 09:26:09 +0000 Subject: [PATCH 4/4] Remove the old sort allocation code --- sources/form3.h | 2 - sources/setfile.c | 120 +++++++--------------------------------------- sources/sort.c | 36 +++++++------- 3 files changed, 33 insertions(+), 125 deletions(-) diff --git a/sources/form3.h b/sources/form3.h index bb4ed819..52553b85 100644 --- a/sources/form3.h +++ b/sources/form3.h @@ -438,8 +438,6 @@ template struct calc { */ #define WITHSORTBOTS -#define SPLITALLOC - #include #include #include diff --git a/sources/setfile.c b/sources/setfile.c index d656e1f2..cbdcdda0 100644 --- a/sources/setfile.c +++ b/sources/setfile.c @@ -860,9 +860,6 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i int MaxFpatches = inMaxFpatches; LONG IOsize = inIOsize; -#ifndef SPLITALLOC - LONG allocation; -#endif LONG longer,terms2insmall,sortsize,longerp; LONG IObuffersize = IOsize; LONG IOtry; @@ -924,21 +921,8 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i /* if ( IObuffersize != inIOsize/(LONG)sizeof(WORD) ) { MesPrint("Warning: IOsize adjusted: %l -> %l", inIOsize/sizeof(WORD), IObuffersize); } */ #endif -#ifndef SPLITALLOC - allocation = - 3*sizeof(POSITION)*(LONG)longer /* Filepositions!! */ - +2*sizeof(WORD *)*longer - +2*(longerp*(sizeof(WORD *)+sizeof(WORD))) - +(3*longerp+2)*sizeof(WORD) -#ifdef WITHZLIB - +(2*longerp+4)*sizeof(WORD) -#endif - +terms2insmall*sizeof(WORD *) - +LargeSize - +SmallEsize - +sortsize - +IObuffersize*sizeof(WORD) + fname2Size + 16; - sort = (SORTING *)Malloc1(allocation,"sort buffers"); + /* Allocate separate buffers for most struct members, for better testing and debugging with valgrind. */ + sort = Malloc1(sizeof(*sort), "AllocSort: sorting struct"); sort->LargeSize = LargeSize/sizeof(WORD); sort->SmallSize = SmallSize/sizeof(WORD); @@ -948,98 +932,29 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i sort->TermsInSmall = TermsInSmall; sort->Terms2InSmall = terms2insmall; - sort->sPointer = (WORD **)(sort+1); - sort->Patches = (WORD **)(sort->sPointer + terms2insmall); - sort->pStop = sort->Patches+longer; - sort->poina = sort->pStop+longer; - sort->poin2a = sort->poina + longerp; + sort->sPointer = Malloc1(sizeof(*(sort->sPointer ))*terms2insmall, "AllocSort: sPointer"); + sort->Patches = Malloc1(sizeof(*(sort->Patches ))*longer, "AllocSort: Patches"); + sort->pStop = Malloc1(sizeof(*(sort->pStop ))*longer, "AllocSort: pStop"); + sort->poina = Malloc1(sizeof(*(sort->poina ))*longerp, "AllocSort: poina"); + sort->poin2a = Malloc1(sizeof(*(sort->poin2a ))*longerp, "AllocSort: poin2a"); - sort->fPatches = (POSITION *)(sort->poin2a+longerp); - sort->fPatchesStop = sort->fPatches + longer; - sort->inPatches = sort->fPatchesStop + longer; - sort->tree = (WORD *)(sort->inPatches + longer); - sort->used = sort->tree+longerp; + sort->fPatches = Malloc1(sizeof(*(sort->fPatches ))*longer, "AllocSort: fPatches"); + sort->fPatchesStop = Malloc1(sizeof(*(sort->fPatchesStop))*longer, "AllocSort: fPatchesStop"); + sort->inPatches = Malloc1(sizeof(*(sort->inPatches ))*longer, "AllocSort: inPatches"); + sort->tree = Malloc1(sizeof(*(sort->tree ))*longerp, "AllocSort: tree"); + sort->used = Malloc1(sizeof(*(sort->used ))*longerp, "AllocSort: used"); #ifdef WITHZLIB - sort->fpcompressed = sort->used+longerp; - sort->fpincompressed = sort->fpcompressed+longerp+2; - sort->ktoi = sort->fpincompressed+longerp+2; + sort->fpcompressed = Malloc1(sizeof(*(sort->fpcompressed ))*(longerp+2), "AllocSort: fpcompressed"); + sort->fpincompressed = Malloc1(sizeof(*(sort->fpincompressed))*(longerp+2), "AllocSort: fpincompressed"); sort->zsparray = 0; -#else - sort->ktoi = sort->used + longerp; #endif - sort->lBuffer = (WORD *)(sort->ktoi + longerp + 2); - sort->lTop = sort->lBuffer+sort->LargeSize; - - sort->sBuffer = sort->lTop; - if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; } - sort->sTop = sort->sBuffer + sort->SmallSize; - sort->sTop2 = sort->sBuffer + sort->SmallEsize; - sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1); - - sort->file.PObuffer = (WORD *)(sort->sTop2); - sort->file.POstop = sort->file.PObuffer+IObuffersize; - sort->file.POsize = IObuffersize * sizeof(WORD); - sort->file.POfill = sort->file.POfull = sort->file.PObuffer; - sort->file.active = 0; - sort->file.handle = -1; - PUTZERO(sort->file.POposition); -#ifdef WITHPTHREADS - sort->file.pthreadslock = dummylock; -#endif -#ifdef WITHZLIB -/* sort->file.ziosize = IOsize; */ - sort->file.ziosize = IObuffersize*sizeof(WORD); - sort->file.ziobuffer = 0; -#endif - if ( AM.S0 != 0 ) { - sort->file.name = (char *)(sort->file.PObuffer + IObuffersize); - AllocSortFileName(sort); - } - else sort->file.name = 0; - sort->cBuffer = 0; - sort->cBufferSize = 0; - sort->f = 0; - sort->PolyWise = 0; -#endif - - -#ifdef SPLITALLOC - // Separate buffers. - sort = Malloc1(sizeof(*sort), "SPLITALLOC sorting struct"); - - sort->LargeSize = LargeSize/sizeof(WORD); - sort->SmallSize = SmallSize/sizeof(WORD); - sort->SmallEsize = SmallEsize/sizeof(WORD); - sort->MaxPatches = MaxPatches; - sort->MaxFpatches = MaxFpatches; - sort->TermsInSmall = TermsInSmall; - sort->Terms2InSmall = terms2insmall; - - sort->sPointer = Malloc1(sizeof(*(sort->sPointer ))*terms2insmall, "SPLITALLOC sPointer"); - sort->Patches = Malloc1(sizeof(*(sort->Patches ))*longer, "SPLITALLOC Patches"); - sort->pStop = Malloc1(sizeof(*(sort->pStop ))*longer, "SPLITALLOC pStop"); - sort->poina = Malloc1(sizeof(*(sort->poina ))*longerp, "SPLITALLOC poina"); - sort->poin2a = Malloc1(sizeof(*(sort->poin2a ))*longerp, "SPLITALLOC poin2a"); - - sort->fPatches = Malloc1(sizeof(*(sort->fPatches ))*longer, "SPLITALLOC fPatches"); - sort->fPatchesStop = Malloc1(sizeof(*(sort->fPatchesStop))*longer, "SPLITALLOC fPatchesStop"); - sort->inPatches = Malloc1(sizeof(*(sort->inPatches ))*longer, "SPLITALLOC inPatches"); - sort->tree = Malloc1(sizeof(*(sort->tree ))*longerp, "SPLITALLOC tree"); - sort->used = Malloc1(sizeof(*(sort->used ))*longerp, "SPLITALLOC used"); - -#ifdef WITHZLIB - sort->fpcompressed = Malloc1(sizeof(*(sort->fpcompressed ))*(longerp+2), "SPLITALLOC fpcompressed"); - sort->fpincompressed = Malloc1(sizeof(*(sort->fpincompressed))*(longerp+2), "SPLITALLOC fpincompressed"); - sort->zsparray = 0; -#endif - - sort->ktoi = Malloc1(sizeof(*(sort->ktoi))*(longerp+2), "SPLITALLOC ktoi"); + sort->ktoi = Malloc1(sizeof(*(sort->ktoi))*(longerp+2), "AllocSort: ktoi"); // The combined Large buffer and Small buffer (+ extension) are used. // They must be allocated together. - sort->lBuffer = Malloc1(sizeof(*(sort->lBuffer))*(sort->LargeSize+sort->SmallEsize), "SPLITALLOC lBuffer+sBuffer"); + sort->lBuffer = Malloc1(sizeof(*(sort->lBuffer))*(sort->LargeSize+sort->SmallEsize), "AllocSort: lBuffer+sBuffer"); sort->lTop = sort->lBuffer+sort->LargeSize; sort->sBuffer = sort->lTop; @@ -1048,7 +963,7 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i sort->sTop2 = sort->sBuffer + sort->SmallEsize; sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1); - sort->file.PObuffer = Malloc1(IObuffersize*sizeof(*(sort->file.PObuffer))+fname2Size+16, "SPLITALLOC PObuffer"); + sort->file.PObuffer = Malloc1(IObuffersize*sizeof(*(sort->file.PObuffer))+fname2Size+16, "AllocSort: PObuffer"); sort->file.POstop = sort->file.PObuffer+IObuffersize; sort->file.POsize = IObuffersize * sizeof(WORD); sort->file.POfill = sort->file.POfull = sort->file.PObuffer; @@ -1071,7 +986,6 @@ SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG i sort->cBufferSize = 0; sort->f = 0; sort->PolyWise = 0; -#endif return(sort); } diff --git a/sources/sort.c b/sources/sort.c index 154b1fd6..3835d757 100644 --- a/sources/sort.c +++ b/sources/sort.c @@ -4757,26 +4757,22 @@ void CleanUpSort(int num) MUNLOCK(ErrorMessageLock); #endif } -#ifdef SPLITALLOC - M_free(S->sPointer, "SPLITALLOC sPointer"); - M_free(S->Patches, "SPLITALLOC Patches"); - M_free(S->pStop, "SPLITALLOC pStop"); - M_free(S->poina, "SPLITALLOC poina"); - M_free(S->poin2a, "SPLITALLOC poin2a"); - M_free(S->fPatches, "SPLITALLOC fPatches"); - M_free(S->fPatchesStop, "SPLITALLOC fPatchesStop"); - M_free(S->inPatches, "SPLITALLOC inPatches"); - M_free(S->tree, "SPLITALLOC tree"); - M_free(S->used, "SPLITALLOC used"); - M_free(S->fpcompressed, "SPLITALLOC fpcompressed"); - M_free(S->fpincompressed, "SPLITALLOC fpincompressed"); - M_free(S->ktoi, "SPLITALLOC ktoi"); - M_free(S->lBuffer, "SPLITALLOC lBuffer+sBuffer"); - M_free(S->file.PObuffer, "SPLITALLOC PObuffer"); - M_free(S, "SPLITALLOC sorting struct"); -#else - M_free(S,"sorting struct"); -#endif + M_free(S->sPointer, "CleanUpSort: sPointer"); + M_free(S->Patches, "CleanUpSort: Patches"); + M_free(S->pStop, "CleanUpSort: pStop"); + M_free(S->poina, "CleanUpSort: poina"); + M_free(S->poin2a, "CleanUpSort: poin2a"); + M_free(S->fPatches, "CleanUpSort: fPatches"); + M_free(S->fPatchesStop, "CleanUpSort: fPatchesStop"); + M_free(S->inPatches, "CleanUpSort: inPatches"); + M_free(S->tree, "CleanUpSort: tree"); + M_free(S->used, "CleanUpSort: used"); + M_free(S->fpcompressed, "CleanUpSort: fpcompressed"); + M_free(S->fpincompressed, "CleanUpSort: fpincompressed"); + M_free(S->ktoi, "CleanUpSort: ktoi"); + M_free(S->lBuffer, "CleanUpSort: lBuffer+sBuffer"); + M_free(S->file.PObuffer, "CleanUpSort: PObuffer"); + M_free(S, "CleanUpSort: sorting struct"); } AN.FunSorts[i] = 0; }