pools.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package x86_64
  2. import (
  3. `sync`
  4. )
  5. var (
  6. labelPool sync.Pool
  7. programPool sync.Pool
  8. instructionPool sync.Pool
  9. memoryOperandPool sync.Pool
  10. )
  11. func freeLabel(v *Label) {
  12. labelPool.Put(v)
  13. }
  14. func clearLabel(p *Label) *Label {
  15. *p = Label{}
  16. return p
  17. }
  18. // CreateLabel creates a new Label, it may allocate a new one or grab one from a pool.
  19. func CreateLabel(name string) *Label {
  20. var p *Label
  21. var v interface{}
  22. /* attempt to grab from the pool */
  23. if v = labelPool.Get(); v == nil {
  24. p = new(Label)
  25. } else {
  26. p = clearLabel(v.(*Label))
  27. }
  28. /* initialize the label */
  29. p.refs = 1
  30. p.Name = name
  31. return p
  32. }
  33. func newProgram(arch *Arch) *Program {
  34. var p *Program
  35. var v interface{}
  36. /* attempt to grab from the pool */
  37. if v = programPool.Get(); v == nil {
  38. p = new(Program)
  39. } else {
  40. p = clearProgram(v.(*Program))
  41. }
  42. /* initialize the program */
  43. p.arch = arch
  44. return p
  45. }
  46. func freeProgram(p *Program) {
  47. programPool.Put(p)
  48. }
  49. func clearProgram(p *Program) *Program {
  50. *p = Program{}
  51. return p
  52. }
  53. func newInstruction(name string, argc int, argv Operands) *Instruction {
  54. var v interface{}
  55. var p *Instruction
  56. /* attempt to grab from the pool */
  57. if v = instructionPool.Get(); v == nil {
  58. p = new(Instruction)
  59. } else {
  60. p = clearInstruction(v.(*Instruction))
  61. }
  62. /* initialize the instruction */
  63. p.name = name
  64. p.argc = argc
  65. p.argv = argv
  66. return p
  67. }
  68. func freeInstruction(v *Instruction) {
  69. instructionPool.Put(v)
  70. }
  71. func clearInstruction(p *Instruction) *Instruction {
  72. *p = Instruction { prefix: p.prefix[:0] }
  73. return p
  74. }
  75. func freeMemoryOperand(m *MemoryOperand) {
  76. memoryOperandPool.Put(m)
  77. }
  78. func clearMemoryOperand(m *MemoryOperand) *MemoryOperand {
  79. *m = MemoryOperand{}
  80. return m
  81. }
  82. // CreateMemoryOperand creates a new MemoryOperand, it may allocate a new one or grab one from a pool.
  83. func CreateMemoryOperand() *MemoryOperand {
  84. var v interface{}
  85. var p *MemoryOperand
  86. /* attempt to grab from the pool */
  87. if v = memoryOperandPool.Get(); v == nil {
  88. p = new(MemoryOperand)
  89. } else {
  90. p = clearMemoryOperand(v.(*MemoryOperand))
  91. }
  92. /* initialize the memory operand */
  93. p.refs = 1
  94. return p
  95. }