slot_pool.go 935 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. newVal := atomic.AddInt64(&total, 1)
  37. glog.V(4).Infof("++> %d", newVal)
  38. slab := *getSlotPool(size).Get().(*[]byte)
  39. return slab[:size]
  40. }
  41. func Free(buf []byte) {
  42. newVal := atomic.AddInt64(&total, -1)
  43. glog.V(4).Infof("--> %d", newVal)
  44. getSlotPool(cap(buf)).Put(&buf)
  45. }