内存池

内存池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/// <author>cxg 2020-9-8</author>
/// 支持D7,更低版本没有测试,支持跨OS
unit MemPool;
 
interface
 
uses
  Math, Classes, SysUtils, SyncObjs;
 
type
  {$if CompilerVersion < 18} //before delphi 2007
  TBytes = array of Byte;
  {$ifend}
  TMemBlock = record
    buf: Pointer;
    size: Cardinal;
  end;
 
  pMemBlock = ^TMemBlock;
 
  TMemPool = class
  private
    FList: TList;
    FBlockSize: Cardinal;
    FCS: TCriticalSection;
  private
    procedure Lock;
    procedure UnLock;
    procedure newBlocks(const BlockNum, blockSize: Cardinal);
  public
    constructor Create(const blockNum: Cardinal; const blockSize: Cardinal);
    destructor Destroy; override;
  public
    function GetBlock: Pointer;
    procedure backBlock(const block: Pointer);
  end;
 
type
  TMemList = class
  private
    FPool: TMemPool;
    FList: TList;
  private
    function GetSize: Int64;
  public
    constructor Create(pool: TMemPool);
    destructor Destroy; override;
  public
    procedure addBuf(const buf: Pointer; const len: Cardinal);
    procedure backList;
    procedure fromStream(ms: TMemoryStream);
    procedure toStream(ms: TMemoryStream);
    function toBytes: tbytes;
    function toBuf: Pointer;
  public
    property list: TList read FList;
    property size: Int64 read GetSize;
  end;
 
implementation
 
{ TMemPool }
constructor TMemPool.Create(const BlockNum, BlockSize: Cardinal);
begin
  FCS := TCriticalSection.Create;
  FList := TList.Create;
  FBlockSize := BlockSize;
  newBlocks(BlockNum, FBlockSize);
end;
 
destructor TMemPool.Destroy;
begin
  FreeAndNil(FList);
  FreeAndNil(FCS);
  inherited;
end;
 
procedure TMemPool.backBlock(const block: Pointer);
begin
  Lock;
  try
    FList.Add(block);
  finally
    UnLock;
  end;
end;
 
function TMemPool.GetBlock: Pointer;
begin
  Lock;
  try
    if FList.Count = 0 then
      newBlocks(1, FBlockSize);
    Result := FList.Last;
    FList.Delete(FList.Count - 1);
  finally
    UnLock;
  end;
end;
 
procedure TMemPool.newBlocks(const BlockNum, blockSize: Cardinal);
var
  i: Integer;
  p: pMemBlock;
begin
  for i := 1 to BlockNum do
  begin
    New(p);
    GetMem(p.buf, BlockSize);
    FList.Add(p);
  end;
end;
 
procedure TMemPool.Lock;
begin
  FCS.Enter;
end;
 
procedure TMemPool.UnLock;
begin
  FCS.Leave;
end;
 
{ TMemList }
 
procedure TMemList.addBuf(const buf: Pointer; const len: Cardinal);
var
  p: pMemBlock;
begin
  p := FPool.GetBlock;
  p.buf := buf;
  p.size := len;
  FList.Add(p);
end;
 
constructor TMemList.Create(pool: TMemPool);
begin
  FPool := pool;
  FList := TList.Create;
end;
 
destructor TMemList.Destroy;
begin
  FreeAndNil(FList);
  inherited;
end;
 
procedure TMemList.backList;
var
  p: pMemBlock;
  i: integer;
begin
  for i := flist.Count - 1 downto 0 do
  begin
    p := pmemblock(flist[i]);
    FPool.backBlock(p);
    flist.Delete(i);
  end;
end;
 
function TMemList.GetSize: Int64;
var
  i: Integer;
begin
  Result := 0;
  for i := 0 to FList.Count - 1 do
    Result := Result + pmemblock(FList[i]).size;
  //i := FList.Count;
 // Result := FPool.FBlockSize * (i - 1) + pMemblock(FList[i - 1]).size;
end;
 
function TMemList.toBuf: Pointer;
var
  i: integer;
  p: pMemBlock;
begin
  New(Result);
  GetMem(Result, self.getsize);
  for i := 0 to flist.Count - 1 do
  begin
    p := pmemblock(list[i]);
    Move(p.buf^, Result^, p.size);
    if i < flist.Count - 1 then
      Result := Pointer(Cardinal(Result) + p.size);
  end;
end;
 
function TMemList.toBytes: tbytes;
var
  i: integer;
  p: pMemBlock;
begin
  SetLength(result, self.GetSize);
  for i := 0 to flist.Count - 1 do
  begin
    p := pmemblock(list[i]);
    Move(p.buf^, Result[i * p.size], p.size);
  end;
end;
 
procedure TMemList.toStream(ms: TMemoryStream);
var
  i: integer;
  p: pMemBlock;
begin
  ms.SetSize(Self.GetSize);
  for i := 0 to flist.Count - 1 do
  begin
    p := pmemblock(list[i]);
    ms.Write(p.buf^, p.size);
  end;
end;
 
procedure TMemList.fromStream(ms: TMemoryStream);
var
  p: pMemBlock;
  qty, remain, n, i: Integer;
begin
  backList;
  qty := Ceil(ms.Size / FPool.FBlockSize);
  n := qty - 1;
  remain := ms.Size - (n * FPool.FBlockSize);
  for i := 1 to qty do
  begin
    p := FPool.GetBlock;
    if i = qty then
    begin
      ms.Read(p.buf, remain);
      p.size := remain;
    end
    else
    begin
      ms.Read(p.buf, FPool.FBlockSize);
      p.size := FPool.FBlockSize;
    end;
    flist.Add(p);
  end;
end;
 
end.

  

posted @   delphi中间件  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2022-06-18 引入OAS3和Swagger全面提升开发者体验
2017-06-18 咏南CS开发框架新的界面风格
点击右上角即可分享
微信分享提示