github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/runtime/gocontpool.go (about)

     1  //go:build !nocontpool
     2  // +build !nocontpool
     3  
     4  package runtime
     5  
     6  // Size of the GoCont pool. The value of 10 was reached by trial-and-error but
     7  // is probably not optimal.
     8  const goContPoolSize = 10
     9  
    10  // Pool for reusing Go continuations. It is modelled exactly on luacontpool.go,
    11  // which has some documentation :)
    12  type goContPool struct {
    13  	conts [goContPoolSize]*GoCont
    14  	next  int
    15  }
    16  
    17  func (p *goContPool) get() *GoCont {
    18  	if p.next == 0 {
    19  		return new(GoCont)
    20  	}
    21  	p.next--
    22  	c := p.conts[p.next]
    23  	p.conts[p.next] = nil
    24  	return c
    25  }
    26  
    27  func (p *goContPool) release(c *GoCont) {
    28  	*c = GoCont{}
    29  	if p.next == goContPoolSize {
    30  		return
    31  	}
    32  	p.conts[p.next] = c
    33  	p.next++
    34  }