Micro & pluggable web framework for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
820B

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "reflect"
  7. "sync"
  8. )
  9. type pool struct {
  10. size int
  11. tp reflect.Type
  12. stp reflect.Type
  13. pool reflect.Value
  14. cur int
  15. lock sync.Mutex
  16. }
  17. func newPool(size int, tp reflect.Type) *pool {
  18. return &pool{
  19. size: size,
  20. cur: 0,
  21. pool: reflect.MakeSlice(reflect.SliceOf(tp), 0, 0), // init for don't allocate memory
  22. tp: reflect.SliceOf(tp),
  23. stp: tp,
  24. }
  25. }
  26. func (p *pool) New() reflect.Value {
  27. if p.size == 0 {
  28. return reflect.New(p.stp)
  29. }
  30. p.lock.Lock()
  31. if p.cur == p.pool.Len() {
  32. p.pool = reflect.MakeSlice(p.tp, p.size, p.size)
  33. p.cur = 0
  34. }
  35. res := p.pool.Index(p.cur).Addr()
  36. p.cur++
  37. p.lock.Unlock()
  38. return res
  39. }