Skip to content

Commit 7d0e71d

Browse files
committed
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.
1 parent bc0dae9 commit 7d0e71d

File tree

4 files changed

+137
-15
lines changed

4 files changed

+137
-15
lines changed

sources/form3.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ template<typename T> struct calc {
438438
*/
439439
#define WITHSORTBOTS
440440

441+
#define SPLITALLOC
442+
441443
#include <stdio.h>
442444
#include <stdlib.h>
443445
#include <string.h>

sources/fsizes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@
126126
#define MAXFPATCHES 256
127127
#define SORTIOSIZE 200000L
128128

129-
#define SSMALLBUFFER 500000L
130-
#define SSMALLOVERFLOW 800000L
129+
#define SSMALLBUFFER 2560016L
130+
#define SSMALLOVERFLOW 3840032L
131131
#define STERMSSMALL 10000L
132-
#define SLARGEBUFFER 4000000L
132+
#define SLARGEBUFFER 26880512L
133133
#define SMAXPATCHES 64
134134
#define SMAXFPATCHES 64
135135
#define SSORTIOSIZE 32768L

sources/setfile.c

Lines changed: 112 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ int AllocSetups(VOID)
574574
AM.S0 = 0;
575575
AM.S0 = AllocSort(LargeSize,SmallSize,SmallEsize,TermsInSmall
576576
,MaxPatches,MaxFpatches,IOsize);
577+
/* AM.S0->file.ziosize was already set to a (larger) value by AllocSort, here it is re-set. */
577578
#ifdef WITHZLIB
578579
AM.S0->file.ziosize = IOsize;
579580
#ifndef WITHPTHREADS
@@ -848,20 +849,30 @@ VOID WriteSetup(VOID)
848849
To be used for the main allocation of the sort buffers, and
849850
in a later stage for the function and subroutine sort buffers.
850851
*/
851-
852-
SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsInSmall,
853-
int MaxPatches, int MaxFpatches, LONG IOsize)
852+
SORTING *AllocSort(LONG inLargeSize, LONG inSmallSize, LONG inSmallEsize, LONG inTermsInSmall,
853+
int inMaxPatches, int inMaxFpatches, LONG inIOsize)
854854
{
855-
LONG allocation,longer,terms2insmall,sortsize,longerp;
855+
LONG LargeSize = inLargeSize;
856+
LONG SmallSize = inSmallSize;
857+
LONG SmallEsize = inSmallEsize;
858+
LONG TermsInSmall = inTermsInSmall;
859+
int MaxPatches = inMaxPatches;
860+
int MaxFpatches = inMaxFpatches;
861+
LONG IOsize = inIOsize;
862+
863+
#ifndef SPLITALLOC
864+
LONG allocation;
865+
#endif
866+
LONG longer,terms2insmall,sortsize,longerp;
856867
LONG IObuffersize = IOsize;
857868
LONG IOtry;
858869
SORTING *sort;
859-
int i = 0, j = 0;
870+
int fname2Size = 0, j = 0;
860871
char *s;
861872
if ( AM.S0 != 0 ) {
862-
s = FG.fname2; i = 0;
863-
while ( *s ) { s++; i++; }
864-
i += 16;
873+
s = FG.fname2; fname2Size = 0;
874+
while ( *s ) { s++; fname2Size++; }
875+
fname2Size += 16;
865876
}
866877
if ( MaxFpatches < 4 ) MaxFpatches = 4;
867878
longer = MaxPatches > MaxFpatches ? MaxPatches : MaxFpatches;
@@ -876,7 +887,6 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
876887
terms2insmall = 2*TermsInSmall; /* Used to be just + 100 rather than *2 */
877888
if ( SmallEsize < (SmallSize*3)/2 ) SmallEsize = (SmallSize*3)/2;
878889
if ( LargeSize > 0 && LargeSize < 2*SmallSize ) LargeSize = 2*SmallSize;
879-
/* if ( SmallEsize < 3*AM.MaxTer ) SmallEsize = 3*AM.MaxTer; */
880890
SmallEsize = (SmallEsize+15) & (-16L);
881891
if ( LargeSize < 0 ) LargeSize = 0;
882892
sortsize = sizeof(SORTING);
@@ -898,9 +908,23 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
898908

899909
IOtry = ((LargeSize+SmallEsize)/MaxFpatches-2*AM.MaxTer)/sizeof(WORD)-COMPINC;
900910

901-
if ( (LONG)(IObuffersize*sizeof(WORD)) < IOtry )
902-
IObuffersize = (IOtry+sizeof(WORD)-1)/sizeof(WORD);
911+
/* Here both IObuffersize and IOtry are in units of sizeof(WORD). */
912+
if ( IObuffersize < IOtry ) {
913+
IObuffersize = IOtry;
914+
}
915+
916+
#if DEBUGGING
917+
if ( LargeSize != inLargeSize ) { MesPrint("Warning: LargeSize adjusted: %l -> %l", inLargeSize, LargeSize); }
918+
if ( SmallSize != inSmallSize ) { MesPrint("Warning: SmallSize adjusted: %l -> %l", inSmallSize, SmallSize); }
919+
if ( SmallEsize != inSmallEsize ) {MesPrint("Warning: SmallEsize adjusted: %l -> %l", inSmallEsize, SmallEsize); }
920+
if ( TermsInSmall != inTermsInSmall ) { MesPrint("Warning: TermsInSmall adjusted: %l -> %l", inTermsInSmall, TermsInSmall); }
921+
if ( MaxPatches != inMaxPatches ) { MesPrint("Warning: MaxPatches adjusted: %d -> %d", inMaxPatches, MaxPatches); }
922+
if ( MaxFpatches != inMaxFpatches ) {MesPrint("Warning: MaxFPatches adjusted: %d -> %d", inMaxFpatches, MaxFpatches); }
923+
/* This one is always changed if the LargeSize has not been... */
924+
/* if ( IObuffersize != inIOsize/(LONG)sizeof(WORD) ) { MesPrint("Warning: IOsize adjusted: %l -> %l", inIOsize/sizeof(WORD), IObuffersize); } */
925+
#endif
903926

927+
#ifndef SPLITALLOC
904928
allocation =
905929
3*sizeof(POSITION)*(LONG)longer /* Filepositions!! */
906930
+2*sizeof(WORD *)*longer
@@ -914,7 +938,7 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
914938
+LargeSize
915939
+SmallEsize
916940
+sortsize
917-
+IObuffersize*sizeof(WORD) + i + 16;
941+
+IObuffersize*sizeof(WORD) + fname2Size + 16;
918942
sort = (SORTING *)Malloc1(allocation,"sort buffers");
919943

920944
sort->LargeSize = LargeSize/sizeof(WORD);
@@ -931,11 +955,13 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
931955
sort->pStop = sort->Patches+longer;
932956
sort->poina = sort->pStop+longer;
933957
sort->poin2a = sort->poina + longerp;
958+
934959
sort->fPatches = (POSITION *)(sort->poin2a+longerp);
935960
sort->fPatchesStop = sort->fPatches + longer;
936961
sort->inPatches = sort->fPatchesStop + longer;
937962
sort->tree = (WORD *)(sort->inPatches + longer);
938963
sort->used = sort->tree+longerp;
964+
939965
#ifdef WITHZLIB
940966
sort->fpcompressed = sort->used+longerp;
941967
sort->fpincompressed = sort->fpcompressed+longerp+2;
@@ -944,13 +970,16 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
944970
#else
945971
sort->ktoi = sort->used + longerp;
946972
#endif
973+
947974
sort->lBuffer = (WORD *)(sort->ktoi + longerp + 2);
948975
sort->lTop = sort->lBuffer+sort->LargeSize;
976+
949977
sort->sBuffer = sort->lTop;
950978
if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; }
951979
sort->sTop = sort->sBuffer + sort->SmallSize;
952980
sort->sTop2 = sort->sBuffer + sort->SmallEsize;
953981
sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1);
982+
954983
sort->file.PObuffer = (WORD *)(sort->sTop2);
955984
sort->file.POstop = sort->file.PObuffer+IObuffersize;
956985
sort->file.POsize = IObuffersize * sizeof(WORD);
@@ -975,6 +1004,77 @@ SORTING *AllocSort(LONG LargeSize, LONG SmallSize, LONG SmallEsize, LONG TermsIn
9751004
sort->cBufferSize = 0;
9761005
sort->f = 0;
9771006
sort->PolyWise = 0;
1007+
#endif
1008+
1009+
1010+
#ifdef SPLITALLOC
1011+
// Separate buffers.
1012+
sort = Malloc1(sizeof(*sort), "SPLITALLOC sorting struct");
1013+
1014+
sort->LargeSize = LargeSize/sizeof(WORD);
1015+
sort->SmallSize = SmallSize/sizeof(WORD);
1016+
sort->SmallEsize = SmallEsize/sizeof(WORD);
1017+
sort->MaxPatches = MaxPatches;
1018+
sort->MaxFpatches = MaxFpatches;
1019+
sort->TermsInSmall = TermsInSmall;
1020+
sort->Terms2InSmall = terms2insmall;
1021+
1022+
sort->sPointer = Malloc1(sizeof(*(sort->sPointer ))*terms2insmall, "SPLITALLOC sPointer");
1023+
sort->SplitScratch = Malloc1(sizeof(*(sort->SplitScratch))*terms2insmall/2, "SPLITALLOC SplitScratch");
1024+
sort->Patches = Malloc1(sizeof(*(sort->Patches ))*longer, "SPLITALLOC Patches");
1025+
sort->pStop = Malloc1(sizeof(*(sort->pStop ))*longer, "SPLITALLOC pStop");
1026+
sort->poina = Malloc1(sizeof(*(sort->poina ))*longerp, "SPLITALLOC poina");
1027+
sort->poin2a = Malloc1(sizeof(*(sort->poin2a ))*longerp, "SPLITALLOC poin2a");
1028+
1029+
sort->fPatches = Malloc1(sizeof(*(sort->fPatches ))*longer, "SPLITALLOC fPatches");
1030+
sort->fPatchesStop = Malloc1(sizeof(*(sort->fPatchesStop))*longer, "SPLITALLOC fPatchesStop");
1031+
sort->inPatches = Malloc1(sizeof(*(sort->inPatches ))*longer, "SPLITALLOC inPatches");
1032+
sort->tree = Malloc1(sizeof(*(sort->tree ))*longerp, "SPLITALLOC tree");
1033+
sort->used = Malloc1(sizeof(*(sort->used ))*longerp, "SPLITALLOC used");
1034+
1035+
#ifdef WITHZLIB
1036+
sort->fpcompressed = Malloc1(sizeof(*(sort->fpcompressed ))*(longerp+2), "SPLITALLOC fpcompressed");
1037+
sort->fpincompressed = Malloc1(sizeof(*(sort->fpincompressed))*(longerp+2), "SPLITALLOC fpincompressed");
1038+
sort->zsparray = 0;
1039+
#endif
1040+
1041+
sort->ktoi = Malloc1(sizeof(*(sort->ktoi))*(longerp+2), "SPLITALLOC ktoi");
1042+
1043+
// The combined Large buffer and Small buffer (+ extension) are used.
1044+
// They must be allocated together.
1045+
sort->lBuffer = Malloc1(sizeof(*(sort->lBuffer))*(sort->LargeSize+sort->SmallEsize), "SPLITALLOC lBuffer+sBuffer");
1046+
sort->lTop = sort->lBuffer+sort->LargeSize;
1047+
1048+
sort->sBuffer = sort->lTop;
1049+
if ( sort->LargeSize == 0 ) { sort->lBuffer = 0; sort->lTop = 0; }
1050+
sort->sTop = sort->sBuffer + sort->SmallSize;
1051+
sort->sTop2 = sort->sBuffer + sort->SmallEsize;
1052+
sort->sHalf = sort->sBuffer + (LONG)((sort->SmallSize+sort->SmallEsize)>>1);
1053+
1054+
sort->file.PObuffer = Malloc1(IObuffersize*sizeof(*(sort->file.PObuffer))+fname2Size+16, "SPLITALLOC PObuffer");
1055+
sort->file.POstop = sort->file.PObuffer+IObuffersize;
1056+
sort->file.POsize = IObuffersize * sizeof(WORD);
1057+
sort->file.POfill = sort->file.POfull = sort->file.PObuffer;
1058+
sort->file.active = 0;
1059+
sort->file.handle = -1;
1060+
PUTZERO(sort->file.POposition);
1061+
#ifdef WITHPTHREADS
1062+
sort->file.pthreadslock = dummylock;
1063+
#endif
1064+
#ifdef WITHZLIB
1065+
sort->file.ziosize = IObuffersize*sizeof(WORD);
1066+
sort->file.ziobuffer = 0;
1067+
#endif
1068+
if ( AM.S0 != 0 ) {
1069+
sort->file.name = (char *)(sort->file.PObuffer + IObuffersize);
1070+
AllocSortFileName(sort);
1071+
}
1072+
else sort->file.name = 0;
1073+
sort->cBuffer = 0;
1074+
sort->cBufferSize = 0;
1075+
sort->f = 0;
1076+
sort->PolyWise = 0;
1077+
#endif
9781078

9791079
return(sort);
9801080
}

sources/sort.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,7 +4757,27 @@ void CleanUpSort(int num)
47574757
MUNLOCK(ErrorMessageLock);
47584758
#endif
47594759
}
4760+
#ifdef SPLITALLOC
4761+
M_free(S->sPointer, "SPLITALLOC sPointer");
4762+
M_free(S->SplitScratch, "SPLITALLOC SplitScratch");
4763+
M_free(S->Patches, "SPLITALLOC Patches");
4764+
M_free(S->pStop, "SPLITALLOC pStop");
4765+
M_free(S->poina, "SPLITALLOC poina");
4766+
M_free(S->poin2a, "SPLITALLOC poin2a");
4767+
M_free(S->fPatches, "SPLITALLOC fPatches");
4768+
M_free(S->fPatchesStop, "SPLITALLOC fPatchesStop");
4769+
M_free(S->inPatches, "SPLITALLOC inPatches");
4770+
M_free(S->tree, "SPLITALLOC tree");
4771+
M_free(S->used, "SPLITALLOC used");
4772+
M_free(S->fpcompressed, "SPLITALLOC fpcompressed");
4773+
M_free(S->fpincompressed, "SPLITALLOC fpincompressed");
4774+
M_free(S->ktoi, "SPLITALLOC ktoi");
4775+
M_free(S->lBuffer, "SPLITALLOC lBuffer+sBuffer");
4776+
M_free(S->file.PObuffer, "SPLITALLOC PObuffer");
4777+
M_free(S, "SPLITALLOC sorting struct");
4778+
#else
47604779
M_free(S,"sorting struct");
4780+
#endif
47614781
}
47624782
AN.FunSorts[i] = 0;
47634783
}

0 commit comments

Comments
 (0)