JumpTableEntryAllocator.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using ARMeilleure.Common;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace ARMeilleure.Translation.Cache
  5. {
  6. class JumpTableEntryAllocator
  7. {
  8. private readonly BitMap _bitmap;
  9. private int _freeHint;
  10. public JumpTableEntryAllocator()
  11. {
  12. _bitmap = new BitMap();
  13. }
  14. public bool EntryIsValid(int entryIndex)
  15. {
  16. lock (_bitmap)
  17. {
  18. return _bitmap.IsSet(entryIndex);
  19. }
  20. }
  21. public void SetEntry(int entryIndex)
  22. {
  23. lock (_bitmap)
  24. {
  25. _bitmap.Set(entryIndex);
  26. }
  27. }
  28. public int AllocateEntry()
  29. {
  30. lock (_bitmap)
  31. {
  32. int entryIndex;
  33. if (!_bitmap.IsSet(_freeHint))
  34. {
  35. entryIndex = _freeHint;
  36. }
  37. else
  38. {
  39. entryIndex = _bitmap.FindFirstUnset();
  40. }
  41. _freeHint = entryIndex + 1;
  42. bool wasSet = _bitmap.Set(entryIndex);
  43. Debug.Assert(wasSet);
  44. return entryIndex;
  45. }
  46. }
  47. public void FreeEntry(int entryIndex)
  48. {
  49. lock (_bitmap)
  50. {
  51. _bitmap.Clear(entryIndex);
  52. _freeHint = entryIndex;
  53. }
  54. }
  55. public IEnumerable<int> GetEntries()
  56. {
  57. return _bitmap;
  58. }
  59. }
  60. }