batchcounteralloc.mx raw

   1  // Copyright 2022 The Go 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  
   5  package calloc
   6  
   7  // This package contains a simple "batch" allocator for allocating
   8  // coverage counters (slices of uint32 basically), for working with
   9  // coverage data files. Collections of counter arrays tend to all be
  10  // live/dead over the same time period, so a good fit for batch
  11  // allocation.
  12  
  13  type BatchCounterAlloc struct {
  14  	pool []uint32
  15  }
  16  
  17  func (ca *BatchCounterAlloc) AllocateCounters(n int) []uint32 {
  18  	const chunk = 8192
  19  	if n > cap(ca.pool) {
  20  		siz := chunk
  21  		if n > chunk {
  22  			siz = n
  23  		}
  24  		ca.pool = []uint32{:siz}
  25  	}
  26  	rv := ca.pool[:n]
  27  	ca.pool = ca.pool[n:]
  28  	return rv
  29  }
  30