// newBlock allocates a new block, from hc's free list if possible.
553590
func (hc *halfConn) newBlock() *block {
	b := hc.bfree
	if b == nil {
		return new(block)
	}
	hc.bfree = b.link
	b.link = nil
	b.resize(0)
	return b
}
379806
func (pq *BlockPriorityQueue) UpdateBlock(b *CachedBlock, summary *BlockDataBasic, stakeInfo *StakeInfoExtended) {
	if b != nil {
		heightAdded, hash, mismatch := checkBlockHeightHash(&cachedBlockData{
			blockSummary: summary,
			stakeInfo:    stakeInfo,
		})
		if mismatch {
			fmt.Println("Cannot UpdateBlock!")
			return
		}
		heightRemoved := b.height

		b.height = heightAdded
		b.hash = hash
		b.summary = summary
		b.stakeInfo = stakeInfo
		b.accesses = 0
		b.Access()
		heap.Fix(pq, b.heapIdx)
		pq.RescanMinMaxForUpdate(heightAdded, heightRemoved)
		pq.lastAccess = time.Unix(0, b.accessTime)
	}
}
563342
func Blocks(ctx context.Context, r io.Reader, blocksize int, sizehint int64, counter Counter, useWeakHashes bool) ([]protocol.BlockInfo, error) {
	if counter == nil {
		counter = &noopCounter{}
	}

	hf := sha256.New()
	hashLength := hf.Size()

	var weakHf hash.Hash32 = noopHash{}
	var multiHf io.Writer = hf
	if useWeakHashes {
		// Use an actual weak hash function, make the multiHf
		// write to both hash functions.
		weakHf = adler32.New()
		multiHf = io.MultiWriter(hf, weakHf)
	}

	var blocks []protocol.BlockInfo
	var hashes, thisHash []byte

	if sizehint >= 0 {
		// Allocate contiguous blocks for the BlockInfo structures and their
		// hashes once and for all, and stick to the specified size.
		r = io.LimitReader(r, sizehint)
		numBlocks := int(sizehint / int64(blocksize))
		blocks = make([]protocol.BlockInfo, 0, numBlocks)
		hashes = make([]byte, 0, hashLength*numBlocks)
	}

	// A 32k buffer is used for copying into the hash function.
	buf := make([]byte, 32<<10)

	var offset int64
	lr := io.LimitReader(r, int64(blocksize)).(*io.LimitedReader)
	for {
		select {
		case <-ctx.Done():
			return nil, ctx.Err()
		default:
		}

		lr.N = int64(blocksize)
		n, err := io.CopyBuffer(multiHf, lr, buf)
		if err != nil {
			return nil, err
		}

		if n == 0 {
			break
		}

		counter.Update(n)

		// Carve out a hash-sized chunk of "hashes" to store the hash for this
		// block.
		hashes = hf.Sum(hashes)
		thisHash, hashes = hashes[:hashLength], hashes[hashLength:]

		b := protocol.BlockInfo{
			Size:     int32(n),
			Offset:   offset,
			Hash:     thisHash,
			WeakHash: weakHf.Sum32(),
		}

		blocks = append(blocks, b)
		offset += n

		hf.Reset()
		weakHf.Reset()
	}

	if len(blocks) == 0 {
		// Empty file
		blocks = append(blocks, protocol.BlockInfo{
			Offset: 0,
			Size:   0,
			Hash:   SHA256OfNothing,
		})
	}

	return blocks, nil
}
230154
func (w *Writer) writeBlock() {
	_, w.err = w.w.Write(w.buf[w.written:])
	w.i = 0
	w.j = headerSize
	w.written = 0
}
55575
func (g *BlockGate) Block(hash chainhash.Hash) (*dcrutil.Block, error) {
	// Try block cache first.
	block, err := g.CachedBlock(hash)
	if err == nil {
		return block, nil
	}

	// Cache miss. Retrieve from dcrd RPC.
	block, err = GetBlockByHash(&hash, g.client)
	if err != nil {
		return nil, fmt.Errorf("GetBlock (%v) failed: %v", hash, err)
	}

	g.mtx.RLock()
	fmt.Printf("Block cache miss: requested %d, cache capacity %d, tip %d.",
		block.Height(), g.expireQueue.cap, g.height)
	g.mtx.RUnlock()
	return block, nil
}
714737
func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) {
	best := sp.server.chain.BestSnapshot()
	return &best.Hash, best.Height, nil
}
654919
func WriteBlock(db ethdb.Writer, block *types.Block) {
	WriteBody(db, block.Hash(), block.NumberU64(), block.Body())
	WriteHeader(db, block.Header())
}
186912
func NewBlockCapability(
	mode csi.VolumeCapability_AccessMode_Mode) *csi.VolumeCapability {

	return &csi.VolumeCapability{
		AccessMode: &csi.VolumeCapability_AccessMode{
			Mode: mode,
		},
		AccessType: &csi.VolumeCapability_Block{
			Block: &csi.VolumeCapability_BlockVolume{},
		},
	}
}
830071
func NewBlockEvent(block *cb.Block, sourceURL string) *connection.Event {
	return connection.NewEvent(
		&pb.DeliverResponse{
			Type: &pb.DeliverResponse_Block{
				Block: block,
			},
		}, sourceURL,
	)
}
1261501
func NewBlockMap(name string, labels []string, nested *Spec) *Spec {
	return BlockMapSpec(&BlockMap{
		Name:   name,
		Labels: labels,
		Nested: nested,
	})
}
932186
func (r *Rollsum32Base) SetBlock(block []byte) {
	r.Reset()
	r.AddBytes(block)
}
1313773
func NewBlockLibrary(hashes []BlockHash) *BlockLibrary {
	// A single β-hash may correlate with many unique hashes.
	hashLookup := make(map[uint32][]BlockHash)

	for _, hash := range hashes {
		key := hash.WeakHash
		if hashLookup[key] == nil {
			hashLookup[key] = []BlockHash{hash}
		} else {
			hashLookup[key] = append(hashLookup[key], hash)
		}
	}

	return &BlockLibrary{hashLookup}
}
1100548
func NewBlockStatement(pos int, line int) *BlockStatement {
	return &BlockStatement{
		NodeType: NodeBlock,
		Loc:      Loc{pos, line},
	}
}
932216
func initialize() {
	if lowPort+maxBlocks*blockSize > 65535 {
		panic("freeport: block size too big or too many blocks requested")
	}

	rand.Seed(time.Now().UnixNano())
	firstPort, lockLn = alloc()
}
204366
func NewBlockFromJSON(data string) (*Block, error) {
	b := &Block{
		block: new(types.Block),
	}
	if err := json.Unmarshal([]byte(data), b.block); err != nil {
		return nil, err
	}
	return b, nil
}
110000
func New(headerTypes ...cb.HeaderType) fab.BlockFilter {
	return func(block *cb.Block) bool {
		return hasType(block, headerTypes...)
	}
}
881578
func NewConf(blockStorageDir string, maxBlockfileSize int) *Conf {
	if maxBlockfileSize <= 0 {
		maxBlockfileSize = defaultMaxBlockfileSize
	}
	return &Conf{blockStorageDir, maxBlockfileSize}
}