slot_pool.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package mem
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "sync"
  5. "sync/atomic"
  6. )
  7. var pools []*sync.Pool
  8. const (
  9. min_size = 1024
  10. )
  11. func bitCount(size int) (count int) {
  12. for ; size > min_size; count++ {
  13. size = (size + 1) >> 1
  14. }
  15. return
  16. }
  17. func init() {
  18. // 1KB ~ 256MB
  19. pools = make([]*sync.Pool, bitCount(1024*1024*256))
  20. for i := 0; i < len(pools); i++ {
  21. slotSize := 1024 << i
  22. pools[i] = &sync.Pool{
  23. New: func() interface{} {
  24. buffer := make([]byte, slotSize)
  25. return &buffer
  26. },
  27. }
  28. }
  29. }
  30. func getSlotPool(size int) *sync.Pool {
  31. index := bitCount(size)
  32. return pools[index]
  33. }
  34. var total int64
  35. func Allocate(size int) []byte {
  36. pool := getSlotPool(size)
  37. if pool != nil {
  38. newVal := atomic.AddInt64(&total, 1)
  39. glog.V(4).Infof("++> %d", newVal)
  40. slab := *pool.Get().(*[]byte)
  41. return slab[:size]
  42. }
  43. return make([]byte, size)
  44. }
  45. func Free(buf []byte) {
  46. pool := getSlotPool(cap(buf))
  47. if pool != nil {
  48. newVal := atomic.AddInt64(&total, -1)
  49. glog.V(4).Infof("--> %d", newVal)
  50. pool.Put(&buf)
  51. }
  52. }