mirror of
				https://github.com/xjasonlyu/tun2socks.git
				synced 2025-11-01 04:22:44 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			36 lines
		
	
	
		
			441 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			36 lines
		
	
	
		
			441 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package core
 | |
| 
 | |
| import (
 | |
| 	"sync"
 | |
| )
 | |
| 
 | |
| var pool *sync.Pool
 | |
| 
 | |
| const BufSize = 32 * 1024
 | |
| 
 | |
| func SetBufferPool(p *sync.Pool) {
 | |
| 	pool = p
 | |
| }
 | |
| 
 | |
| func NewBytes(size int) []byte {
 | |
| 	if size <= BufSize {
 | |
| 		return pool.Get().([]byte)
 | |
| 	} else {
 | |
| 		return make([]byte, size)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func FreeBytes(b []byte) {
 | |
| 	if len(b) >= BufSize {
 | |
| 		pool.Put(b)
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func init() {
 | |
| 	SetBufferPool(&sync.Pool{
 | |
| 		New: func() interface{} {
 | |
| 			return make([]byte, BufSize)
 | |
| 		},
 | |
| 	})
 | |
| }
 | 
