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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
%% options
copyright owner = Dirk Krause
copyright year = 2015-xxxx
SPDX-License-Identifier: BSD-3-Clause
%% header
/** @file dk4bf.h Bit fields.
*/
#ifndef DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif
#ifndef DK4ERROR_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif
/** Bit field structure.
*/
typedef struct {
unsigned char *d; /**< Data buffer. */
size_t s; /**< Number of bits in the array. */
} dk4_bit_field_t;
#ifdef __cplusplus
extern "C" {
#endif
/** Open bit field.
@param sz Size of the bit field (number of bits).
@param erp Error report, may be NULL.
@return Pointer to new bit field on success, NULL on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if sz is 0,
- DK4_E_MEMORY_ALLOCATION_FAILED<br>
if not enough memory was available.
*/
dk4_bit_field_t *
dk4bf_open(size_t sz, dk4_er_t *erp);
/** Set one bit in a bit field.
@param dptr Bit field.
@param bitno Index of bit to modify.
@param val Bit value.
@param erp Error report, may be NULL.
@return 1 on success, 0 on error.
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if dptr is 0 or bitno is outside the bit field size.
*/
int
dk4bf_set(dk4_bit_field_t *dptr, size_t bitno, int val, dk4_er_t *erp);
/** Retrieve one value from bit field.
@param dptr Address of result variable.
@param bfptr Bit field.
@param bitno Number of bit to obtain.
@param erp Error report, may be NULL.
@return 1 on success (dptr set), 0 on error (dptr unchanged).
Error codes:
- DK4_E_INVALID_ARGUMENTS<br>
if dptr or bfptr is NULL or bitno is outside the bit field size.
*/
int
dk4bf_get(int *dptr, dk4_bit_field_t const *bfptr, size_t bitno, dk4_er_t *erp);
/** Close bit field.
@param ptr Pointer to bit field previously opened using
dk4bf_open().
*/
void
dk4bf_close(dk4_bit_field_t *ptr);
#ifdef __cplusplus
}
#endif
%% module
#include "dk4conf.h"
#if DK4_HAVE_ASSERT_H
#ifndef ASSERT_H_INCLUDED
#include <assert.h>
#define ASSERT_H_INCLUDED 1
#endif
#endif
#include "dk4bf.h"
#include "dk4mem.h"
$!trace-include
/** Position of single bits within a byte.
*/
static const unsigned char dk4bf_bits_in_byte[] = {
0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};
dk4_bit_field_t *
dk4bf_open(size_t sz, dk4_er_t *erp)
{
dk4_bit_field_t *back = NULL;
size_t bsz;
#if DK4_USE_ASSERT
assert(0 < sz);
#endif
if (0 < sz) {
back = dk4mem_new(dk4_bit_field_t,1,erp);
if (NULL != back) {
back->s = sz;
bsz = sz / 8;
if (0 != (sz % 8)) {
bsz++;
}
back->d = dk4mem_new(unsigned char,bsz,erp);
if (NULL == back->d) {
dk4bf_close(back);
back = NULL;
}
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
int
dk4bf_set(dk4_bit_field_t *dptr, size_t bitno, int val, dk4_er_t *erp)
{
size_t bytei; /* Byte index */
size_t biti; /* Bit index */
int back = 0;
#if DK4_USE_ASSERT
assert(NULL != dptr);
#endif
if (NULL != dptr) {
if (bitno < dptr->s) {
bytei = bitno / 8;
biti = bitno % 8;
if (0 != val) {
(dptr->d)[bytei] |= dk4bf_bits_in_byte[biti];
} else {
(dptr->d)[bytei] &= (~(dk4bf_bits_in_byte[biti]));
}
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
int
dk4bf_get(int *dptr, dk4_bit_field_t const *bfptr, size_t bitno, dk4_er_t *erp)
{
size_t bytei; /* Byte index */
size_t biti; /* Bit index within byte */
int back = 0;
#if DK4_USE_ASSERT
assert(NULL != dptr);
assert(NULL != bfptr);
#endif
if ((NULL != dptr) && (NULL != bfptr)) {
if (bitno < bfptr->s) {
bytei = bitno / 8;
biti = bitno % 8;
if (0x00 != (((bfptr->d)[bytei]) & (dk4bf_bits_in_byte[biti]))) {
*dptr = 1;
} else {
*dptr = 0;
}
back = 1;
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
} else {
dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
}
return back;
}
void
dk4bf_close(dk4_bit_field_t *ptr)
{
#if DK4_USE_ASSERT
assert(NULL != ptr);
#endif
if (NULL != ptr) {
dk4mem_release(ptr->d);
dk4mem_free(ptr);
}
}
|