File: bm_find_blk.c

package info (click to toggle)
fis-gtm 6.2-000-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 30,784 kB
  • ctags: 42,554
  • sloc: ansic: 358,483; asm: 4,847; csh: 4,574; sh: 2,261; awk: 200; makefile: 86; sed: 13
file content (186 lines) | stat: -rw-r--r-- 4,059 bytes parent folder | download | duplicates (2)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/****************************************************************
 *								*
 *	Copyright 2001, 2007 Fidelity Information Services, Inc	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"
#include "gdsroot.h"
#include "gdsblk.h"
#include "gdsbml.h"

#define MAX_FFS_SIZE 32

/* Returns the location of the first set bit in the field.	*/
/* The search starts at the hint and does not wrap 		*/

int4 bm_find_blk(int4 hint, sm_uc_ptr_t base_addr, int4 total_bits, boolean_t *used)
{
	int4		bits;
	sm_uc_ptr_t 	ptr, top;
	unsigned char	valid;

	assert(hint < total_bits);
	total_bits *= BML_BITS_PER_BLK;
	/* Currently bm_find_blk has been coded to assume that if "hint" is
	 * non-zero then it is less than total_bits. We better assert that.
	 */
	if (hint)
	{
		ptr = base_addr + (hint * BML_BITS_PER_BLK) / 8;
		top = base_addr + (total_bits + 7) / 8 - 1;
		if (ptr == top)
		{
			bits = total_bits % 8;
			if (bits == 6)
				valid = *ptr & 63;
			else if (bits == 4)
				valid = *ptr & 15;
			else if (bits == 2)
				valid = *ptr & 3;
			else
				valid = *ptr;
		}
		else
			valid = *ptr;
		switch (hint % (8 / BML_BITS_PER_BLK))
		{
		        case 0:	break;
			case 1:	valid = valid & 252;
				break;
			case 2: valid = valid & 240;
				break;
			case 3: valid = valid & 192;
				break;
		}
		if (valid)
		{
			if (valid & 1)
			{
				if (valid & 2)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK));
			}
			else if (valid & 4)
			{
				if (valid & 8)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 1);
			}
			else if (valid & 16)
			{
				if (valid & 32)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 2);
			}
			else
			{
				if (valid & 128)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 3);
			}
		}
		ptr++;
	}
	else
		ptr = base_addr;

	for (top = base_addr + (total_bits +7) / 8 - 1; ptr < top; ptr++)
	{
		if (*ptr)
		{
			if (*ptr & 1)
			{
				if (*ptr & 2)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK));
			}
			else if (*ptr & 4)
			{
				if (*ptr & 8)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 1);
			}
			else if (*ptr & 16)
			{
				if (*ptr & 32)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 2);
			}
			else
			{	if (*ptr & 128)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 3);
			}
		}
	}
	if ((ptr == top) && *ptr)	/* Special processing for last byte--may be only partially valid , if had hint may */
	{
		bits = total_bits % 8;	/* have already done last byte, then ptr will be greater than top */
		if (bits == 6)
			valid = *ptr & 63;
		else if (bits == 4)
			valid = *ptr & 15;
		else if (bits == 2)
			valid = *ptr & 3;
		else
			valid = *ptr;
		if (valid)
		{
			if (valid & 1)
			{
				if (valid & 2)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK));
			}
			else if (valid & 4)
			{
				if (valid & 8)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 1);
			}
			else if (valid & 16)
			{
				if (valid & 32)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 2);
			}
			else
			{
				if (valid & 128)
					*used = TRUE;
				else
					*used = FALSE;
				return (int4)((ptr - base_addr) * (8 / BML_BITS_PER_BLK) + 3);
			}
		}
	}
	return -1;
}