- Timestamp:
- Sep 2, 2011, 11:50:41 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpSimpleBuffer.h
r2458 r2460 214 214 215 215 /// Minimum number of members is set to the number you can fit in 256 bytes 216 const static int _minNumElems=(256/sizeof(T));216 const static size_t _minNumElems = (256/sizeof(T)); 217 217 218 218 size_t __guesslen(const T* bytes); … … 355 355 template<class T> 356 356 int 357 SimpleBuffer<T>::append(const T* bytes, int nmemb) 358 { 359 size_t newMembCnt = 0; 360 361 void* dest = NULL; 362 void const* src = NULL; 363 size_t size = 0; 357 SimpleBuffer<T>::append(const T* bytes, int numElems) 358 { 364 359 365 360 // User specified NULL buffer to append 366 if ( (bytes == NULL) && (nmemb < 1) ) { 367 return 0; 368 } 369 370 #ifdef notdef 371 372 /* This is dead code. The test above catches the condition. */ 361 if (bytes == NULL) { 362 return 0; // Should not give append a NULL 363 // buffer pointer. 364 } 373 365 374 366 // FIXME: i think this needs to be division, … … 377 369 // bytes in *bytes. 378 370 371 size_t numBytes = 0; 372 if (numElems == -1) { 373 /* This should be moved into the SimpleCharBuffer. It doesn't make 374 * sense to look for a NUL byte unless it's a string buffer. We 375 * can then change numElems to be size_t. */ 376 numBytes = __guesslen(bytes); 377 numElems = numBytes / sizeof(T); 378 } 379 if (numElems <= 0) { 380 return numElems; 381 } 382 383 size_t newSize; 384 newSize = _numElemsUsed + numElems; 385 if (newSize > _numElemsAllocated) { 386 387 // buffer sizes less than min_size are set to min_size 388 if (newSize < _minNumElems) { 389 newSize = _minNumElems; 390 } 391 392 /* 393 * Allocate a larger buffer for the string if the current one isn't 394 * large enough. Allocate extra space in the new buffer so that there 395 * will be room to grow before we have to allocate again. 396 */ 397 size_t size; 398 size = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems; 399 while (newSize > size) { 400 size += size; 401 } 402 403 /* 404 * reallocate to a larger buffer 405 */ 406 if (set(size) != size) { 407 return 0; 408 } 409 } 410 memcpy(_buf + _numElemsUsed, bytes, numElems * sizeof(T)); 411 _numElemsUsed += numElems; 412 return numElems; 413 } 414 415 /** 416 * Append bytes to the end of this buffer 417 * @param pointer to bytes to be added 418 * @param number of bytes to be added 419 * @return number of bytes appended. 420 */ 421 template<class T> 422 size_t 423 SimpleBuffer<T>::extend(size_t numExtraElems) 424 { 425 size_t newSize; 426 427 newSize = _numElemsUsed + numExtraElems; 428 if (newSize > _numElemsAllocated) { 429 430 /* Enforce a minimum buffer size. */ 431 if (newSize < (size_t) _minNumElems) { 432 newSize = (size_t) _minNumElems; 433 } 434 435 size_t size; 436 size = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems; 437 438 /* Keep doubling the size of the buffer until we have enough space to 439 * hold the extra elements. */ 440 while (newSize > size) { 441 size += size; 442 } 443 /* Reallocate to a larger buffer. */ 444 if (set(size) != size) { 445 return 0; 446 } 447 } 448 return _numElemsAllocated; 449 } 450 451 /** 452 * Append formatted bytes to the end of this buffer 453 * @param pointer to bytes to be added 454 * @param number of bytes to be added 455 * @return number of bytes appended. 456 */ 457 template<class T> 458 int 459 SimpleBuffer<T>::appendf(const char *format, ...) 460 { 461 size_t newMembCnt = 0; 379 462 size_t nbytes = 0; 380 if (nmemb == -1) { 381 // user signaled null terminated string 382 // or that we should make an educated guess 383 // at the length of the object. 384 nbytes = __guesslen(bytes); 385 nmemb = nbytes/sizeof(T); 386 } 387 388 if (nmemb <= 0) { 463 int nmemb = 0; 464 465 char* dest = NULL; 466 size_t size = 0; 467 size_t bytesAdded = 0; 468 va_list arg; 469 470 // User specified NULL format 471 if (format == NULL) { 472 return 0; 473 } 474 475 // FIXME: i think this needs to be division, 476 // need to create test case with array of ints 477 // i'm not sure we can even guess the number 478 // bytes in *bytes. 479 480 481 // add one for terminating null character 482 nbytes = strlen(format) + 1; 483 484 if (nbytes <= 0) { 389 485 // no data written, invalid option 390 return nmemb; 391 } 392 #endif 486 return nbytes; 487 } 488 489 // FIXME: we need ceil of nbytes/sizeof(T), instead we add 1 for safety 490 491 nmemb = nbytes/sizeof(T); 492 if (nmemb == 0) { 493 nmemb++; 494 } 393 495 394 496 newMembCnt = (size_t)(_numElemsUsed + nmemb); … … 420 522 } 421 523 422 dest = (void*) (_buf + _numElemsUsed);423 src = (void const*) bytes;424 size = (size_t) (nmemb*sizeof(T));425 memcpy(dest,src,size);426 427 _numElemsUsed += nmemb;428 429 return nmemb;430 }431 432 /**433 * Append bytes to the end of this buffer434 * @param pointer to bytes to be added435 * @param number of bytes to be added436 * @return number of bytes appended.437 */438 template<class T>439 size_t440 SimpleBuffer<T>::extend(size_t numExtraElems)441 {442 size_t newSize;443 444 newSize = _numElemsUsed + numExtraElems;445 if (newSize > _numElemsAllocated) {446 447 /* Enforce a minimum buffer size. */448 if (newSize < (size_t) _minNumElems) {449 newSize = (size_t) _minNumElems;450 }451 452 size_t size;453 size = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems;454 455 /* Keep doubling the size of the buffer until we have enough space to456 * hold the extra elements. */457 while (newSize > size) {458 size += size;459 }460 /* Reallocate to a larger buffer. */461 if (set(size) != size) {462 return 0;463 }464 }465 return _numElemsAllocated;466 }467 468 /**469 * Append formatted bytes to the end of this buffer470 * @param pointer to bytes to be added471 * @param number of bytes to be added472 * @return number of bytes appended.473 */474 template<class T>475 int476 SimpleBuffer<T>::appendf(const char *format, ...)477 {478 size_t newMembCnt = 0;479 size_t nbytes = 0;480 int nmemb = 0;481 482 char* dest = NULL;483 size_t size = 0;484 size_t bytesAdded = 0;485 va_list arg;486 487 // User specified NULL format488 if (format == NULL) {489 return 0;490 }491 492 // FIXME: i think this needs to be division,493 // need to create test case with array of ints494 // i'm not sure we can even guess the number495 // bytes in *bytes.496 497 498 // add one for terminating null character499 nbytes = strlen(format) + 1;500 501 if (nbytes <= 0) {502 // no data written, invalid option503 return nbytes;504 }505 506 // FIXME: we need ceil of nbytes/sizeof(T), instead we add 1 for safety507 508 nmemb = nbytes/sizeof(T);509 if (nmemb == 0) {510 nmemb++;511 }512 513 newMembCnt = (size_t)(_numElemsUsed + nmemb);514 515 if (newMembCnt > _numElemsAllocated) {516 517 // buffer sizes less than min_size are set to min_size518 if (newMembCnt < (size_t) _minNumElems) {519 newMembCnt = (size_t) _minNumElems;520 }521 522 /*523 * Allocate a larger buffer for the string if the current one isn't524 * large enough. Allocate extra space in the new buffer so that there525 * will be room to grow before we have to allocate again.526 */527 size_t membAvl;528 membAvl = (_numElemsAllocated > 0) ? _numElemsAllocated : _minNumElems;529 while (newMembCnt > membAvl) {530 membAvl += membAvl;531 }532 533 /*534 * reallocate to a larger buffer535 */536 if (set(membAvl) != membAvl) {537 return 0;538 }539 }540 541 524 dest = (char*) (_buf + _numElemsUsed); 542 525 size = (_numElemsAllocated-_numElemsUsed)*sizeof(T);
Note: See TracChangeset
for help on using the changeset viewer.