File: r_bitfield.c

package info (click to toggle)
ssldump 0.9b3-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,196 kB
  • ctags: 1,379
  • sloc: ansic: 6,151; sh: 1,508; yacc: 183; makefile: 90; lex: 67
file content (114 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
   r_bitfield.c

   Copyright (C) 2001 RTFM, Inc.
   All Rights Reserved.

   ekr@rtfm.com  Wed Oct  3 11:15:23 2001
 */


static char *RCSSTRING="$Id: r_bitfield.c,v 1.3 2001/12/24 06:06:26 ekr Exp $";

#include <string.h>
#include <r_common.h>
#include "r_bitfield.h"

int r_bitfield_create(setp,size)
  r_bitfield **setp;
  UINT4 size;
  {
    r_bitfield *set=0;
    int _status;
    int num_words=size/32+!!(size%32);
    
    if(!(set=(r_bitfield *)RMALLOC(sizeof(r_bitfield))))
      ABORT(R_NO_MEMORY);

    if(!(set->data=(UINT4 *)RMALLOC(num_words*4)))
      ABORT(R_NO_MEMORY);
    memset(set->data,0,4*num_words);
    
    set->base=0;
    set->len=num_words;

    *setp=set;
    
    _status=0;
  abort:
    if(_status){
      r_bitfield_destroy(&set);
    }
    return(_status);
  }

int r_bitfield_destroy(setp)
  r_bitfield **setp;
  {
    r_bitfield *set;
    
    if(!setp || !*setp)
      return(0);

    set=*setp;

    RFREE(set->data);
    RFREE(set);

    *setp=0;
    return(0);
  }

int r_bitfield_set(set,bit)
  r_bitfield *set;
  int bit;
  {
    int word=(bit-set->base)/32;
    int bbit=(bit-set->base)%32;
    int _status;

    /* Resize? */
    if(word>set->len){
      UINT4 newlen=set->len;
      UINT4 *tmp;
      
      while(newlen<word)
	newlen*=2;
      
      if(!(tmp=(UINT4 *)RMALLOC(newlen)))
	ABORT(R_NO_MEMORY);
      
      memcpy(tmp,set->data,set->len*4);
      memset(tmp+set->len*4,0,(newlen-set->len)*4);

      RFREE(set->data);
      set->data=tmp;
    }

    set->data[word]|=1<<bbit;

    _status=0;
  abort:
    return(_status);
  }

int r_bitfield_isset(set,bit)
  r_bitfield *set;
  int bit;
  {
    int word=(bit-set->base)/32;
    int bbit=(bit-set->base)%32;
    int _status;

    if(bit<set->base)
      return(0);

    /* Resize? */
    if(word>set->len)
      return(0);

    return(set->data[word]&(1<<bbit));

    _status=0;
    return(_status);
  }