-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #301
- Loading branch information
Showing
25 changed files
with
502 additions
and
454 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using Unity.Extension; | ||
|
||
namespace Unity.Container | ||
{ | ||
public partial class Defaults : IPolicyObservable | ||
{ | ||
public object? Get(Type? target, Type type, PolicyChangeHandler handler) | ||
{ | ||
var hash = (uint)(((target?.GetHashCode() ?? 0) + 37) ^ type.GetHashCode()); | ||
|
||
ref var bucket = ref Meta[hash % Meta.Length]; | ||
var position = bucket.Position; | ||
|
||
while (position > 0) | ||
{ | ||
ref var candidate = ref Data[position]; | ||
if (ReferenceEquals(candidate.Target, target) && | ||
ReferenceEquals(candidate.Type, type)) | ||
{ | ||
// Found existing | ||
candidate.PolicyChanged += handler; | ||
return candidate.Value; | ||
} | ||
|
||
position = Meta[position].Location; | ||
} | ||
|
||
if (++Count >= Data.Length) | ||
{ | ||
Expand(); | ||
bucket = ref Meta[hash % Meta.Length]; | ||
} | ||
|
||
// Allocate placeholder | ||
ref var entry = ref Data[Count]; | ||
entry = new Policy(hash, target, type, default); | ||
entry.PolicyChanged += handler; | ||
Meta[Count].Location = bucket.Position; | ||
bucket.Position = Count; | ||
return default; | ||
} | ||
|
||
public void Set(Type? target, Type type, object policy, PolicyChangeHandler handler) | ||
{ | ||
var hash = (uint)(((target?.GetHashCode() ?? 0) + 37) ^ type.GetHashCode()); | ||
|
||
lock (_syncRoot) | ||
{ | ||
ref var bucket = ref Meta[hash % Meta.Length]; | ||
var position = bucket.Position; | ||
|
||
while (position > 0) | ||
{ | ||
ref var candidate = ref Data[position]; | ||
if (ReferenceEquals(candidate.Target, target) && | ||
ReferenceEquals(candidate.Type, type)) | ||
{ | ||
// Found existing | ||
if (candidate.Value is null) candidate.Value = policy; | ||
} | ||
|
||
position = Meta[position].Location; | ||
} | ||
|
||
if (++Count >= Data.Length) | ||
{ | ||
Expand(); | ||
bucket = ref Meta[hash % Meta.Length]; | ||
} | ||
|
||
// Add new registration | ||
Data[Count] = new Policy(hash, target, type, policy); | ||
Meta[Count].Location = bucket.Position; | ||
bucket.Position = Count; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.