github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/cmd/cc/bv.c (about)

     1  // Copyright 2013 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  #include <u.h>
     6  #include <libc.h>
     7  #include "cc.h"
     8  
     9  enum {
    10  	WORDSIZE = sizeof(uint32),
    11  	WORDBITS = 32,
    12  };
    13  
    14  uintptr
    15  bvsize(uintptr n)
    16  {
    17  	return ((n + WORDBITS - 1) / WORDBITS) * WORDSIZE;
    18  }
    19  
    20  Bvec*
    21  bvalloc(int32 n)
    22  {
    23  	Bvec *bv;
    24  	uintptr nbytes;
    25  
    26  	if(n < 0)
    27  		fatal(Z, "bvalloc: initial size is negative\n");
    28  	nbytes = sizeof(Bvec) + bvsize(n);
    29  	bv = malloc(nbytes);
    30  	if(bv == nil)
    31  		fatal(Z, "bvalloc: malloc failed\n");
    32  	memset(bv, 0, nbytes);
    33  	bv->n = n;
    34  	return bv;
    35  }
    36  
    37  void
    38  bvset(Bvec *bv, int32 i)
    39  {
    40  	uint32 mask;
    41  
    42  	if(i < 0 || i >= bv->n)
    43  		fatal(Z, "bvset: index %d is out of bounds with length %d\n", i, bv->n);
    44  	mask = 1 << (i % WORDBITS);
    45  	bv->b[i / WORDBITS] |= mask;
    46  }