github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/cmd/gc/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 "go.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("bvalloc: initial size is negative\n");
    28  	nbytes = sizeof(Bvec) + bvsize(n);
    29  	bv = malloc(nbytes);
    30  	if(bv == nil)
    31  		fatal("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("bvset: index %d is out of bounds with length %d\n", i, bv->n);
    44  	mask = 1U << (i % WORDBITS);
    45  	bv->b[i / WORDBITS] |= mask;
    46  }
    47  
    48  void
    49  bvres(Bvec *bv, int32 i)
    50  {
    51  	uint32 mask;
    52  
    53  	if(i < 0 || i >= bv->n)
    54  		fatal("bvres: index %d is out of bounds with length %d\n", i, bv->n);
    55  	mask = ~(1 << (i % WORDBITS));
    56  	bv->b[i / WORDBITS] &= mask;
    57  }
    58  
    59  int
    60  bvget(Bvec *bv, int32 i)
    61  {
    62  	uint32 mask, word;
    63  
    64  	if(i < 0 || i >= bv->n)
    65  		fatal("bvget: index %d is out of bounds with length %d\n", i, bv->n);
    66  	mask = 1 << (i % WORDBITS);
    67  	word = bv->b[i / WORDBITS] & mask;
    68  	return word ? 1 : 0;
    69  }
    70  
    71  int
    72  bvisempty(Bvec *bv)
    73  {
    74  	int32 i;
    75  
    76  	for(i = 0; i < bv->n; i += WORDBITS)
    77  		if(bv->b[i / WORDBITS] != 0)
    78  			return 0;
    79  	return 1;
    80  }