コード例 #1
0
ファイル: bytebuffer.go プロジェクト: w3-golang/ringbuffer
func New(slotSize, slotCount int) (ringbuffer.RingBuffer, error) {
	if slotSize < MinSlotSize {
		return nil, ErrSlotSizeTooSmall
	}

	if slotCount > MaxDataSlots {
		return nil, ErrMaxDataSlotsExceeded
	}

	if !ringbuffer.PowerOfTwo(slotCount) {
		return nil, ErrNotPowerOfTwo
	}

	slotSize += SlotOverhead

	d := &byteBuffer{
		slotSize:   slotSize,
		slotCount:  slotCount,
		slotMask:   slotCount - 1,
		bufferSize: int64(slotSize * slotCount),
		buffer:     make([]byte, slotSize*slotCount),
		producers:  make([]*producer, 0),
		consumers:  make([]*consumer, 0),
	}

	return d, nil
}
コード例 #2
0
ファイル: consumer.go プロジェクト: w3-golang/ringbuffer
func NewConsumer(bufferSize int) (ringbuffer.Sequencer, error) {
	if !ringbuffer.PowerOfTwo(int(bufferSize)) {
		return nil, ErrNotPowerOfTwo
	}

	s := &Consumer{}
	s.cursor = InitialSequenceValue
	s.cachedGate = InitialSequenceValue
	s.bufferSize = bufferSize

	return s, nil
}