socket hash
socket hash
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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | unit uSocketHash; interface uses SyncObjs; type {$IF CompilerVersion >= 22} //XE=22,2010=21 TSocket = NativeUInt; {$ELSE} TSocket = Integer; {$IFEND} PPSocketHashItem = ^PSocketHashItem; PSocketHashItem = ^TSocketHashItem; TSocketHashItem = record Next: PSocketHashItem; Key : TSocket; Value:Pointer; end; TSocketHash = class private FBucketPool:array of PSocketHashItem; Buckets: array of PSocketHashItem; function NewBucket():PSocketHashItem; procedure DisposeBucket(Value:PSocketHashItem); protected function Find( const Key: TSocket): PPSocketHashItem; function HashOf( const Key: TSocket): Cardinal; virtual ; public constructor Create(Size: Cardinal = 256); destructor Destroy; override ; function Add( const Key: TSocket; Value: Pointer):Integer; procedure Clear; procedure Remove( const Key: TSocket); function Modify( const Key: TSocket; Value: Pointer): Boolean; function ValueOf( const Key: TSocket): Pointer; end; TThreadSocketHash = class private FObj:TSocketHash; FCS:TCriticalSection; procedure Lock(); procedure UnLock(); public constructor Create(Size: Cardinal = 256); destructor Destroy; override ; function Add( const Key: TSocket; Value: Pointer):Integer; procedure Clear; procedure Remove( const Key: TSocket); function Modify( const Key: TSocket; Value: Pointer): Boolean; function ValueOf( const Key: TSocket): Pointer; function GetAndRemove( const Key:TSocket):Pointer; end; implementation function TSocketHash.Add( const Key: TSocket; Value: Pointer):Integer; var Hash: Integer; Bucket: PSocketHashItem; begin Bucket:= NewBucket(); if Bucket <> nil then begin Hash := HashOf(Key) mod Cardinal(Length(Buckets)); Bucket^.Key := Key; Bucket^.Value := Value; Bucket^.Next := Buckets[Hash]; Buckets[Hash] := Bucket; Result := Hash; end else Result := -1; //空间满 end; procedure TSocketHash.Clear; var I: Integer; P, N: PSocketHashItem; begin for I := 0 to Length(Buckets) - 1 do begin P := Buckets[I]; while P <> nil do begin N := P^.Next; DisposeBucket(P); P := N; end; Buckets[I] := nil; end; end; constructor TSocketHash.Create(Size: Cardinal); var Index:Integer; PH:PSocketHashItem; begin inherited Create; SetLength(Buckets, Size); SetLength(FBucketPool,Size); //:array of PSocketHashItem; for Index := Low(FBucketPool) to High(FBucketPool) do begin New(PH); PH^.Next := nil; PH^.Key := 0; PH^.Value := nil; FBucketPool[Index] := PH; end; end; destructor TSocketHash.Destroy; var Index:Integer; P:PSocketHashItem; begin Clear; for Index := Low(FBucketPool) to High(FBucketPool) do begin P := FBucketPool[Index]; if P <> nil then Dispose(P); end; inherited Destroy; end; procedure TSocketHash.DisposeBucket(Value: PSocketHashItem); var Index:Integer; begin for Index := Low(FBucketPool) to High(FBucketPool) do begin if FBucketPool[Index] = nil then begin FBucketPool[Index] := Value; Break; end; end; end; function TSocketHash.Find( const Key: TSocket): PPSocketHashItem; var Hash: Integer; begin Hash := HashOf(Key) mod Cardinal(Length(Buckets)); Result := @Buckets[Hash]; while Result^ <> nil do begin if Result^.Key = Key then Exit else Result := @Result^.Next; end; end; function TSocketHash.HashOf( const Key: TSocket): Cardinal; var I: Integer; P: PByte; begin Result := 0; P := @Key; for I := 1 to SizeOf(Key) do begin Result := ((Result shl 2) or (Result shr (SizeOf(Result) * 8 - 2))) xor P^; Inc(P); end; end; function TSocketHash.Modify( const Key: TSocket; Value: Pointer): Boolean; var P: PSocketHashItem; begin P := Find(Key)^; if P <> nil then begin Result := True; P^.Value := Value; end else Result := False; end; function TSocketHash.NewBucket: PSocketHashItem; var Index:Integer; begin Result := nil; for Index := Low(FBucketPool) to High(FBucketPool) do begin Result := FBucketPool[Index]; if Result <> nil then begin FBucketPool[Index] := nil; Break; end; end; end; procedure TSocketHash.Remove( const Key: TSocket); var P: PSocketHashItem; Prev: PPSocketHashItem; begin Prev := Find(Key); P := Prev^; if P <> nil then begin Prev^ := P^.Next; DisposeBucket(P); end; end; function TSocketHash.ValueOf( const Key: TSocket): Pointer; var P: PSocketHashItem; begin P := Find(Key)^; if P <> nil then Result := P^.Value else Result := nil; // -1; end; { TThreadSocketHash } function TThreadSocketHash.Add( const Key: TSocket; Value: Pointer):Integer; begin Lock(); try Result := FObj.Add(Key,Value); finally UnLock(); end; end; procedure TThreadSocketHash.Clear; begin Lock(); try FObj.Clear(); finally UnLock(); end; end; constructor TThreadSocketHash.Create(Size: Cardinal); begin FObj := TSocketHash.Create(Size); FCS := TCriticalSection.Create(); end; destructor TThreadSocketHash.Destroy; begin FCS.Free(); FObj.Free(); inherited; end; function TThreadSocketHash.GetAndRemove( const Key: TSocket): Pointer; begin Lock(); try Result := FObj.ValueOf(Key); FObj.Remove(Key); finally UnLock(); end; end; procedure TThreadSocketHash.Lock; begin FCS.Enter(); end; function TThreadSocketHash.Modify( const Key: TSocket; Value: Pointer): Boolean; begin Lock(); try Result := FObj.Modify(Key,Value); finally UnLock(); end; end; procedure TThreadSocketHash.Remove( const Key: TSocket); begin Lock(); try FObj.Remove(Key); finally UnLock(); end; end; procedure TThreadSocketHash.UnLock; begin FCS.Leave(); end; function TThreadSocketHash.ValueOf( const Key: TSocket): Pointer; begin Lock(); try Result := FObj.ValueOf(Key); finally UnLock(); end; end; end. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/14640889.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-04-10 mqtt应用场景
2017-04-10 从数据池中捞取的存储过程控件使用完以后必须unprepare