type {需要缓冲在完成端口关联的Socket发送列表中的对象} TSendObject = class public SrcSocket : TSocket; DstSocket : TSocket; Buf : array[0..DATABUF_MAXSIZE - 1] of Char; Len : Integer; end;
Const DEFAULTBLOCKCN = 100; BlockSize: Word = SizeOf(TBlock); implementation
constructor TMemoryBuffer.Create(); begin Create(DEFAULTBLOCKCN); end;
constructor TMemoryBuffer.Create(BlockCount: Integer); var I: Integer; P: PBlock; begin inherited Create; InitializeCriticalSection(FLock); FList := TList.Create; for I := 0 to BlockCount - 1 do begin New(P); FillChar(P^, BlockSize, 0); FList.Add(P); end; end;
destructor TMemoryBuffer.Destroy; var I: Integer; begin try EnterCriticalSection(FLock);
for I := FList.Count - 1 downto 0 do FreeMem(FList[I]); FList.Free; finally LeaveCriticalSection(FLock); DeleteCriticalSection(FLock); end; inherited Destroy; end;
function TMemoryBuffer.AllocBlock: PBlock; var I: Integer; begin EnterCriticalSection(FLock); try Result := nil; //FList.Pack; for I := FList.Count - 1 downto 0 do begin Result := FList[I]; if not Result.IsUse then break; end; if not Assigned(Result) or Result.IsUse then begin New(Result); FList.Add(Result); end; FillChar(Result^.Data, SizeOf(Result^.Data), 0); Result^.IsUse := True; finally LeaveCriticalSection(FLock); end; end;
function TMemoryBuffer.GetCount: Integer; begin Result := FList.Count; end;
function TMemoryBuffer.GetBlock(const Index: Integer): PBlock; begin if (Index >= Count) or (Index <= -1) then raise EMemoryBuffer.CreateFmt('需要不正确', [Index]) else Result := FList[Index]; end;