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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
*
* The Contents of this file are made available subject to the terms of
* the Sun Industry Standards Source License Version 1.2
*
* Sun Microsystems Inc., March, 2001
*
*
* Sun Industry Standards Source License Version 1.2
* =================================================
* The contents of this file are subject to the Sun Industry Standards
* Source License Version 1.2 (the "License"); You may not use this file
* except in compliance with the License. You may obtain a copy of the
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
*
* Software provided under this License is provided on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
* See the License for the specific provisions governing your rights and
* obligations concerning the Software.
*
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
*
* Copyright: 2001 by Sun Microsystems, Inc.
*
* All Rights Reserved.
*
************************************************************************/
/*___INFO__MARK_END__*/
#include <stdio.h>
#include <string.h>
#include "uti/sge_bitfield.h"
#include "uti/sge_stdlib.h"
/****** uti/bitfield/--Bitfield ****************************************
* NAME
* Bitfield -- A variable size bitfield implementation
*
* SYNOPSIS
* bitfield sge_bitfield_new(unsigned int size)
*
* FUNCTION
* This module provides variable size bitfields.
* The size of a bitfield can be defined when the bitfield is created.
* Individual bits can be set, read and cleared.
* The contents of a bitfield can be printed to stdout or any
* file handle.
*
* EXAMPLE
* See main program (module test) in libs/uti/sge_bitfield.c
*
* NOTES
* MT-NOTE: this module is MT safe
*
* SEE ALSO
* uti/bitfield/sge_bitfield_new()
* uti/bitfield/sge_bitfield_free()
* uti/bitfield/sge_bitfield_set()
* uti/bitfield/sge_bitfield_get()
* uti/bitfield/sge_bitfield_clear()
* uti/bitfield/sge_bitfield_reset()
* uti/bitfield/sge_bitfield_copy()
* uti/bitfield/sge_bitfield_bitwise_copy()
* uti/bitfield/sge_bitfield_print()
*******************************************************************************/
/****** uti/bitfield/-Bitfield_Typedefs ****************************************
* NAME
* Bitfield_Typedefs -- type definitions for the Bitfield module
*
* SYNOPSIS
* typedef struct {
* unsigned int size;
* union {
* char fix[sizeof(char *)];
* char *dyn;
* } bf;
* } bitfield;
*
* typedef _bitfield *bitfield;
*
* FUNCTION
* The _bitfield structure is the internal representation of a bitfield.
* All operations on bitfields use the bitfield type.
*
* For small bitfields, no memory is allocated, but the space available in
* bf.fix is used (depending on the architecture, this suffices for 32 or 64
* bit bitfields).
* This saves a considerable amount of memory and above all processing time.
*******************************************************************************/
/****** uti/bitfield/sge_bitfield_new() ****************************************
* NAME
* sge_bitfield_new() -- create a new bitfield
*
* SYNOPSIS
* bitfield *
* sge_bitfield_new(unsigned int size)
*
* FUNCTION
* Allocates and initializes the necessary memory.
* It is in the responsibility of the caller to free the bitfield
* once it is no longer needed.
*
* INPUTS
* unsigned int size - size in bits
*
* RESULT
* bitfield - a new bitfield or NULL, if the creation of the bitfield
* failed
*
* NOTES
* MT-NOTE: sge_bitfield_new() is MT safe
*
* SEE ALSO
* uti/bitfield/sge_bitfield_free()
*******************************************************************************/
bitfield *
sge_bitfield_new(unsigned int size)
{
bitfield *bf;
/* allocate storage for bitfield object */
bf = (bitfield *) malloc(sizeof(bitfield));
if (bf != NULL) {
/* initialize bitfield, on errors, free bitfield */
if (!sge_bitfield_init(bf, size)) {
sge_free(&bf);
}
}
return bf;
}
/****** uti/bitfield/sge_bitfield_init() ***************************************
* NAME
* sge_bitfield_init() -- initialize a bitfield object
*
* SYNOPSIS
* bool
* sge_bitfield_init(bitfield *bf, unsigned int size)
*
* FUNCTION
* Initializes a bitfield object.
* Storage for the bits is allocated if necessary (size of the bitfield is
* bigger than the preallocated storage) and the size is stored.
*
* INPUTS
* bitfield *bf - the bitfield to initialize
* unsigned int size - the targeted size of the bitfield
*
* RESULT
* bool - true on success, else false
*
* NOTES
* MT-NOTE: sge_bitfield_init() is MT safe
*******************************************************************************/
bool
sge_bitfield_init(bitfield *bf, unsigned int size)
{
bool ret = true;
if (bf == NULL) {
ret = false;
} else {
unsigned int char_size = sge_bitfield_get_size_bytes(size);
/* malloc bitfield buffer only if char * has less bits than required */
if (size <= fixed_bits) {
/* clear buffer */
bf->bf.dyn = (char *)0;
} else {
bf->bf.dyn = (char *)malloc(char_size);
if (bf->bf.dyn == NULL) {
ret = false;
} else {
/* clear buffer */
memset(bf->bf.dyn, 0, char_size);
}
}
bf->size = size;
}
return ret;
}
/****** uti/bitfield/sge_bitfield_copy() ***************************************
* NAME
* sge_bitfield_copy() -- copies a bitfield into another one.
*
* SYNOPSIS
* bool
* sge_bitfield_copy(const bitfield *source, bitfield *target)
*
* FUNCTION
* The memory has to be allocated before, and source and target has to have
* the same size. Otherwise it will return false and does not copy anything.
*
* INPUTS
* const bitfield *source - source bitfield
* bitfield *target - target bitfield
*
* RESULT
* bool - false, if one of the bitfields is NULL or
* the bitfield sizes are different
*
* NOTES
* MT-NOTE: sge_bitfield_copy() is MT safe
*
*******************************************************************************/
bool
sge_bitfield_copy(const bitfield *source, bitfield *target)
{
bool ret = true;
if (source == NULL || target == NULL) {
ret = false;
}
if (ret && source->size != target->size) {
ret = false;
}
if (ret) {
unsigned int char_size = sge_bitfield_get_size_bytes(source->size);
if (source->size <= fixed_bits) {
target->bf.dyn = source->bf.dyn;
} else {
memcpy(target->bf.dyn, source->bf.dyn, char_size);
}
}
return ret;
}
/****** uti/bitfield/sge_bitfield_bitwise_copy() *******************************
* NAME
* sge_bitfield_copy() -- copies a bitfield into another one.
*
* SYNOPSIS
* bool
* sge_bitfield_bitwise_copy(const bitfield *source, bitfield *target)
*
* FUNCTION
* The memory has to be allocated before, but the bitfields can have
* different sizes. If the source is longer than the target, only the bits
* up to target's length are copied.
*
* INPUTS
* const bitfield *source - source bitfield
* bitfield *target - target bitfield
*
* RESULT
* bool - false, if one of the bitfields is NULL
*
* NOTES
* MT-NOTE: sge_bitfield_bitwise_copy() is MT safe
*
*******************************************************************************/
bool
sge_bitfield_bitwise_copy(const bitfield *source, bitfield *target)
{
bool ret = true;
if (source == NULL || target == NULL) {
ret = false;
}
if (ret) {
unsigned int char_size = 0;
const char *source_buffer = sge_bitfield_get_buffer(source);
char *target_buffer = sge_bitfield_get_buffer(target);
if (source->size > target->size) {
/* This may result in the target getting a few more bits than it wants
* (if target->size isn't a multiple of 8), but that shouldn't matter
* because sge_bitfield_get() guards against accessing those extra
* bits. */
char_size = sge_bitfield_get_size_bytes(target->size);
} else {
char_size = sge_bitfield_get_size_bytes(source->size);
}
memcpy(target_buffer, source_buffer, char_size);
}
return ret;
}
/****** uti/bitfield/sge_bitfield_changed() ************************************
* NAME
* sge_bitfield_changed() -- figures out if something was changed.
*
* SYNOPSIS
* bool
* sge_bitfield_changed(const bitfield *source)
*
* FUNCTION
*
* INPUTS
* bitfield *source - bitfield to analyze
*
* RESULT
* bool - true, if the bitfield has a changed bit set.
*
* NOTES
* MT-NOTE: sge_bitfield_copy() is MT safe
*
*******************************************************************************/
bool
sge_bitfield_changed(const bitfield *bf)
{
bool ret = false;
if (bf != NULL) {
const char *buf = sge_bitfield_get_buffer(bf);
unsigned int char_size = sge_bitfield_get_size_bytes(bf->size);
unsigned int i;
for (i = 0; i < char_size; i++) {
if (buf[i] != 0) {
ret = true;
break;
}
}
}
return ret;
}
/****** uti/bitfield/sge_bitfield_reset() ***************************************
* NAME
* sge_bitfield_reset() -- clears a bitfield
*
* SYNOPSIS
* bool
* sge_bitfield_reset(bitfield *bf)
*
* FUNCTION
*
* INPUTS
* bitfield *bf - bitfield to reset
*
* RESULT
* bool - false, if bf is NULL
*
* NOTES
* MT-NOTE: sge_bitfield_copy() is MT safe
*
*******************************************************************************/
bool
sge_bitfield_reset(bitfield *bf)
{
if (bf != NULL) {
if (bf->size > fixed_bits) {
unsigned int char_size = sge_bitfield_get_size_bytes(bf->size);
memset(bf->bf.dyn, 0, char_size);
} else {
bf->bf.dyn = (char *)0;
}
return true;
}
return false;
}
/****** uti/bitfield/sge_bitfield_free() ***************************************
* NAME
* sge_bitfield_free() -- destroy a bitfield
*
* SYNOPSIS
* bitfield sge_bitfield_free(bitfield *bf)
*
* FUNCTION
* Destroys a bitfield. Frees all memory allocated by the bitfield.
*
* INPUTS
* bitfield *bf - the bitfield to destroy
*
* NOTES
* MT-NOTE: sge_bitfield_free() is MT safe
*
* RESULT
* bitfield * - NULL
*******************************************************************************/
bitfield *sge_bitfield_free(bitfield *bf)
{
if (bf != NULL) {
if (bf->size > fixed_bits) {
if (bf->bf.dyn != NULL) {
sge_free(&(bf->bf.dyn));
}
}
sge_free(&bf);
}
return NULL;
}
/****** uti/bitfield/sge_bitfield_free_data() **********************************
* NAME
* sge_bitfield_free_data() -- free the bitfield data
*
* SYNOPSIS
* bool
* sge_bitfield_free_data(bitfield *bf)
*
* FUNCTION
* Frees the data part of a bitfield.
* The bitfield itself is not freed.
*
* INPUTS
* bitfield *bf - the bitfield to work on
*
* RESULT
* bool - true on success, else false
*
* NOTES
* MT-NOTE: sge_bitfield_free_data() is MT safe
*******************************************************************************/
bool
sge_bitfield_free_data(bitfield *bf)
{
bool ret = true;
if (bf == NULL) {
ret = false;
} else {
if (bf->size > fixed_bits) {
if (bf->bf.dyn != NULL) {
sge_free(&(bf->bf.dyn));
}
}
}
return ret;
}
/****** uti/bitfield/sge_bitfield_set() ****************************************
* NAME
* sge_bitfield_set() -- set a bit
*
* SYNOPSIS
* bool
* sge_bitfield_set(bitfield *bf, unsigned int bit)
*
* FUNCTION
* Sets a certain bit in a bitfield to 1.
*
* INPUTS
* bitfield *bf - the bitfield to manipulate
* unsigned int bit - the bit to set
*
* NOTES
* MT-NOTE: sge_bitfield_set() is MT safe
*
* RESULT
* bool - true on success,
* false on error
*******************************************************************************/
bool
sge_bitfield_set(bitfield *bf, unsigned int bit)
{
bool ret = true;
if(bf == NULL || bit >= bf->size) {
ret = false;
}
if (ret) {
char *buf = sge_bitfield_get_buffer(bf);
unsigned int byte_offset = bit / 8;
unsigned int bit_offset = bit % 8;
buf[byte_offset] |= 1 << bit_offset;
}
return ret;
}
/****** uti/bitfield/sge_bitfield_get() ****************************************
* NAME
* sge_bitfield_get() -- read a bit
*
* SYNOPSIS
* bool
* sge_bitfield_get(const bitfield *bf, unsigned int bit)
*
* FUNCTION
* Reads a certain bit of a bitfield and returns it's contents.
*
* INPUTS
* bitfield *bf - the bitfield to read from
* unsigned int bit - the bit to read
*
* NOTES
* MT-NOTE: sge_bitfield_get() is MT safe
*
* RESULT
* bool - false, if bit is not set (or input params invalid),
* true, if bit is set
*******************************************************************************/
bool
sge_bitfield_get(const bitfield *bf, unsigned int bit)
{
bool ret = false;
if (bf != NULL && bit < bf->size) {
const char *buf = sge_bitfield_get_buffer(bf);
unsigned int byte_offset = bit / 8;
unsigned int bit_offset = bit % 8;
if ((buf[byte_offset] & (1 << bit_offset)) > 0) {
ret = true;
}
}
return ret;
}
/****** uti/bitfield/sge_bitfield_clear() **************************************
* NAME
* sge_bitfield_clear() -- clear a bit
*
* SYNOPSIS
* bool
* sge_bitfield_clear(bitfield *bf, unsigned int bit)
*
* FUNCTION
* Clears a certain bit in a bitfield (sets its content to 0).
*
* INPUTS
* bitfield *bf - the bitfield to manipulate
* unsigned int bit - the bit to clear
*
* NOTES
* MT-NOTE: sge_bitfield_clear() is MT safe
*
* RESULT
* bool - true on success,
* false on error
*******************************************************************************/
bool
sge_bitfield_clear(bitfield *bf, unsigned int bit)
{
bool ret = true;
if(bf == NULL || bit >= bf->size) {
ret = false;
}
if (ret) {
char *buf = sge_bitfield_get_buffer(bf);
unsigned int byte_offset = bit / 8;
unsigned int bit_offset = bit % 8;
buf[byte_offset] &= 0xff ^ (1 << bit_offset);
}
return ret;
}
/****** uti/bitfield/sge_bitfield_print() **************************************
* NAME
* sge_bitfield_print() -- print contents of a bitfield
*
* SYNOPSIS
* void sge_bitfield_print(bitfield *bf, FILE *fd)
*
* FUNCTION
* Prints the contents of a bitfield.
* For each bit one digit (0/1) is printed.
* If NULL is passed as file descriptor, output is sent to stdout.
*
* NOTES
* MT-NOTE: sge_bitfield_print() is MT safe
*
* INPUTS
* bitfield bf - the bitfield to output
* FILE *fd - filehandle or NULL
*******************************************************************************/
void sge_bitfield_print(const bitfield *bf, FILE *fd)
{
unsigned int i;
if (bf == NULL) {
return;
}
if (fd == NULL) {
fd = stdout;
}
for (i = 0; i < bf->size; i++) {
int value = sge_bitfield_get(bf, i) ? 1 : 0;
fprintf(fd, "%d ", value);
}
}
|