 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dennis Guest
|
Posted: Thu May 05, 2005 9:20 am Post subject: Re: Fastcode MM B&V 0.35 |
|
|
Hi
Could somebody review this new validation function?
//Grow dynamic array with STEPSIZE from MIN to MAX and validate that it is 4
byte aligned
function TMMValidation.Validate22 : Boolean;
var
SomeArray : array of Byte;
MemoryStatus : TMemoryStatus;
BytesToAllocate, StartAddress, I2, I3, OldBytesToAllocate : Cardinal;
const
BYTESTOALLOCATESTEPSIZE : Cardinal = 2;
BYTESTOALLOCATEMIN : Cardinal = 5;//5 byte
BYTESTOALLOCATEMAX : Cardinal = 50*1024*1024;//50 Mbyte
FILLBYTE : Byte = 255;
begin
try
Result := True;
OldBytesToAllocate := 1;
//Allocate
SetLength(SomeArray, 1);
SomeArray[0] := FILLBYTE;
BytesToAllocate := BYTESTOALLOCATEMIN;
while BytesToAllocate <= BYTESTOALLOCATEMAX do
begin
GlobalMemoryStatus(MemoryStatus);
if MemoryStatus.dwAvailVirtual >= BytesToAllocate then
begin
//Reallocate
SetLength(SomeArray, BytesToAllocate);
//Old data preserved?
for I3 := 0 to OldBytesToAllocate-1 do
begin
if SomeArray[I3] <> FILLBYTE then
begin
Result := False;
Exit;
end;
end;
//4 byte alignment?
//Add 8 byte offset to compensate for refcount and size data
StartAddress := Cardinal(@SomeArray[8]);
if StartAddress mod 4 <> 0 then
begin
Result := False;
Exit;
end;
//Fill newly allocated space
for I2 := OldBytesToAllocate to BytesToAllocate-1 do
SomeArray[I2] := FILLBYTE;
OldBytesToAllocate := BytesToAllocate;
BytesToAllocate := BytesToAllocate * BYTESTOALLOCATESTEPSIZE;
end;
end;
//Free
SetLength(SomeArray, 0);
except
Result := False;
end;
end;
Regards
Dennis
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|