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 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
|
/*
* Worldvisions Weaver Software:
* Copyright (C) 1997-2002 Net Integration Technologies, Inc.
*
* Defines basic buffer storage classes.
* These are not intended for use directly by clients.
* See "wvbufbase.h" for the public API.
*/
#include "wvbufstore.h"
#include <string.h>
#include <sys/types.h>
/**
* An abstraction for memory transfer operations.
*
* This is in preparation for supporting buffers of full-blown
* objects that have special copy and destruction semantics,
* someday...
*/
struct MemOps
{
/** Copies initialized region to uninitialized region. */
inline void uninit_copy(void *target, const void *source,
size_t count)
{
memcpy(target, source, count);
}
/** Copies initialized region to initialized region. */
inline void copy(void *target, const void *source, size_t count)
{
uninit(target, count);
memcpy(target, source, count);
}
/**
* Moves initialized region to uninitialized region.
* Source data becomes uninitialized.
*/
inline void uninit_move(void *target, void *source,
size_t count)
{
memmove(target, source, count);
uninit(source, count);
}
/** Swaps initialized regions. */
inline void swap(void *target, void *source, size_t count)
{
register unsigned char *t1 = (unsigned char*)target;
register unsigned char *t2 = (unsigned char*)source;
while (count-- > 0)
{
register unsigned char temp;
temp = *t1;
*(t1++) = *t2;
*(t2++) = temp;
}
}
/** Uninitializes a region. */
inline void uninit(void *target, size_t count)
{
}
/** Creates a new array. */
inline void *newarray(size_t count)
{
return new unsigned char[count];
}
/** Deletes an uninitialized array. */
inline void deletearray(void *buf)
{
deletev (unsigned char*)buf;
}
} memops;
/** Rounds the value up to the specified boundary. */
inline size_t roundup(size_t value, size_t boundary)
{
size_t mod = value % boundary;
return mod ? value + boundary - mod : value;
}
/***** WvBufStore *****/
WvBufStore::WvBufStore(int _granularity) :
granularity(_granularity)
{
}
size_t WvBufStore::peekable(int offset) const
{
if (offset == 0)
{
return used();
}
else if (offset < 0)
{
if (size_t(-offset) <= ungettable())
return size_t(-offset) + used();
}
else
{
int avail = int(used()) - offset;
if (avail > 0)
return avail;
}
return 0; // out-of-bounds
}
void WvBufStore::move(void *buf, size_t count)
{
while (count > 0)
{
size_t amount = count;
assert(amount != 0 ||
!"attempted to move() more than used()");
if (amount > count)
amount = count;
const void *data = get(amount);
memops.uninit_copy(buf, data, amount);
buf = (unsigned char*)buf + amount;
count -= amount;
}
}
void WvBufStore::copy(void *buf, int offset, size_t count)
{
while (count > 0)
{
size_t amount = optpeekable(offset);
assert(amount != 0 ||
!"attempted to copy() with invalid offset");
if (amount > count)
amount = count;
const void *data = peek(offset, amount);
memops.uninit_copy(buf, data, amount);
buf = (unsigned char*)buf + amount;
count -= amount;
offset += amount;
}
}
void WvBufStore::put(const void *data, size_t count)
{
while (count > 0)
{
size_t amount = optallocable();
assert(amount != 0 ||
!"attempted to put() more than free()");
if (amount > count)
amount = count;
void *buf = alloc(amount);
memops.uninit_copy(buf, data, amount);
data = (const unsigned char*)data + amount;
count -= amount;
}
}
void WvBufStore::fastput(const void *data, size_t count)
{
void *buf = alloc(count);
memops.uninit_copy(buf, data, count);
}
void WvBufStore::poke(const void *data, int offset, size_t count)
{
int limit = int(used());
assert(offset <= limit ||
!"attempted to poke() beyond end of buffer");
int end = offset + count;
if (end >= limit)
{
size_t tail = end - limit;
count -= tail;
put((const unsigned char*)data + count, tail);
}
while (count > 0)
{
size_t amount = optpeekable(offset);
assert(amount != 0 ||
!"attempted to poke() with invalid offset");
if (amount > count)
amount = count;
void *buf = mutablepeek(offset, amount);
memops.copy(buf, data, amount);
data = (const unsigned char*)data + amount;
count -= amount;
offset += amount;
}
}
void WvBufStore::merge(WvBufStore &instore, size_t count)
{
if (count == 0)
return;
if (usessubbuffers() && instore.usessubbuffers())
{
// merge quickly by stealing subbuffers from the other buffer
for (;;)
{
WvBufStore *buf = instore.firstsubbuffer();
if (! buf)
break; // strange!
size_t avail = buf->used();
if (avail > count)
break;
// move the entire buffer
bool autofree = instore.unlinksubbuffer(buf, false);
appendsubbuffer(buf, autofree);
count -= avail;
if (count == 0)
return;
}
}
// merge slowly by copying data
basicmerge(instore, count);
}
void WvBufStore::basicmerge(WvBufStore &instore, size_t count)
{
// move bytes as efficiently as we can using only the public API
if (count == 0)
return;
const void *indata = NULL;
void *outdata = NULL;
size_t inavail = 0;
size_t outavail = 0;
for (;;)
{
if (inavail == 0)
{
inavail = instore.optgettable();
assert(inavail != 0 ||
!"attempted to merge() more than instore.used()");
if (inavail > count)
inavail = count;
indata = instore.get(inavail);
}
if (outavail == 0)
{
outavail = optallocable();
assert(outavail != 0 ||
!"attempted to merge() more than free()");
if (outavail > count)
outavail = count;
outdata = alloc(outavail);
}
if (inavail < outavail)
{
memops.uninit_copy(outdata, indata, inavail);
count -= inavail;
outavail -= inavail;
if (count == 0)
{
unalloc(outavail);
return;
}
outdata = (unsigned char*)outdata + inavail;
inavail = 0;
}
else
{
memops.uninit_copy(outdata, indata, outavail);
count -= outavail;
if (count == 0) return;
inavail -= outavail;
indata = (const unsigned char*)indata + outavail;
outavail = 0;
}
}
}
/***** WvInPlaceBufStore *****/
WvInPlaceBufStore::WvInPlaceBufStore(int _granularity,
void *_data, size_t _avail, size_t _size, bool _autofree) :
WvBufStore(_granularity), data(NULL)
{
reset(_data, _avail, _size, _autofree);
}
WvInPlaceBufStore::WvInPlaceBufStore(int _granularity, size_t _size) :
WvBufStore(_granularity), data(NULL)
{
reset(memops.newarray(_size), 0, _size, true);
}
WvInPlaceBufStore::~WvInPlaceBufStore()
{
if (data && xautofree)
memops.deletearray(data);
}
void WvInPlaceBufStore::reset(void *_data, size_t _avail,
size_t _size, bool _autofree = false)
{
assert(_data != NULL || _avail == 0);
if (data && _data != data && xautofree)
memops.deletearray(data);
data = _data;
xautofree = _autofree;
xsize = _size;
setavail(_avail);
}
void WvInPlaceBufStore::setavail(size_t _avail)
{
assert(_avail <= xsize);
readidx = 0;
writeidx = _avail;
}
size_t WvInPlaceBufStore::used() const
{
return writeidx - readidx;
}
const void *WvInPlaceBufStore::get(size_t count)
{
assert(count <= writeidx - readidx ||
!"attempted to get() more than used()");
const void *tmpptr = (const unsigned char*)data + readidx;
readidx += count;
return tmpptr;
}
void WvInPlaceBufStore::unget(size_t count)
{
assert(count <= readidx ||
!"attempted to unget() more than ungettable()");
readidx -= count;
}
size_t WvInPlaceBufStore::ungettable() const
{
return readidx;
}
void WvInPlaceBufStore::zap()
{
readidx = writeidx = 0;
}
size_t WvInPlaceBufStore::free() const
{
return xsize - writeidx;
}
void *WvInPlaceBufStore::alloc(size_t count)
{
assert(count <= xsize - writeidx ||
!"attempted to alloc() more than free()");
void *tmpptr = (unsigned char*)data + writeidx;
writeidx += count;
return tmpptr;
}
void WvInPlaceBufStore::unalloc(size_t count)
{
assert(count <= writeidx - readidx ||
!"attempted to unalloc() more than unallocable()");
writeidx -= count;
}
size_t WvInPlaceBufStore::unallocable() const
{
return writeidx - readidx;
}
void *WvInPlaceBufStore::mutablepeek(int offset, size_t count)
{
if (count == 0)
return NULL;
assert(((offset <= 0) ?
size_t(-offset) <= readidx :
size_t(offset) < writeidx - readidx) ||
! "attempted to peek() with invalid offset or count");
return (unsigned char*)data + readidx + offset;
}
/***** WvConstInPlaceBufStore *****/
WvConstInPlaceBufStore::WvConstInPlaceBufStore(int _granularity,
const void *_data, size_t _avail) :
WvReadOnlyBufferStoreMixin<WvBufStore>(_granularity), data(NULL)
{
reset(_data, _avail);
}
void WvConstInPlaceBufStore::reset(const void *_data, size_t _avail)
{
assert(_data != NULL || _avail == 0);
data = _data;
setavail(_avail);
}
size_t WvConstInPlaceBufStore::used() const
{
return avail - readidx;
}
void WvConstInPlaceBufStore::setavail(size_t _avail)
{
avail = _avail;
readidx = 0;
}
const void *WvConstInPlaceBufStore::get(size_t count)
{
assert(count <= avail - readidx ||
! "attempted to get() more than used()");
const void *ptr = (const unsigned char*)data + readidx;
readidx += count;
return ptr;
}
void WvConstInPlaceBufStore::unget(size_t count)
{
assert(count <= readidx ||
! "attempted to unget() more than ungettable()");
readidx -= count;
}
size_t WvConstInPlaceBufStore::ungettable() const
{
return readidx;
}
const void *WvConstInPlaceBufStore::peek(int offset, size_t count)
{
if (count == 0)
return NULL;
assert(((offset <= 0) ?
size_t(-offset) <= readidx :
size_t(offset) < avail - readidx) ||
! "attempted to peek() with invalid offset or count");
return (const unsigned char*)data + readidx + offset;
}
void WvConstInPlaceBufStore::zap()
{
readidx = avail = 0;
}
/***** WvCircularBufStore *****/
WvCircularBufStore::WvCircularBufStore(int _granularity,
void *_data, size_t _avail, size_t _size, bool _autofree) :
WvBufStore(_granularity), data(NULL)
{
reset(_data, _avail, _size, _autofree);
}
WvCircularBufStore::WvCircularBufStore(int _granularity, size_t _size) :
WvBufStore(_granularity), data(NULL)
{
reset(memops.newarray(_size), 0, _size, true);
}
WvCircularBufStore::~WvCircularBufStore()
{
if (data && xautofree)
memops.deletearray(data);
}
void WvCircularBufStore::reset(void *_data, size_t _avail,
size_t _size, bool _autofree = false)
{
assert(_data != NULL || _avail == 0);
if (data && _data != data && xautofree)
memops.deletearray(data);
data = _data;
xautofree = _autofree;
xsize = _size;
setavail(_avail);
}
void WvCircularBufStore::setavail(size_t _avail)
{
assert(_avail <= xsize);
head = 0;
totalused = totalinit = _avail;
}
size_t WvCircularBufStore::used() const
{
return totalused;
}
size_t WvCircularBufStore::optgettable() const
{
size_t avail = xsize - head;
if (avail > totalused)
avail = totalused;
return avail;
}
const void *WvCircularBufStore::get(size_t count)
{
assert(count <= totalused ||
! "attempted to get() more than used()");
size_t first = ensurecontiguous(0, count, false /*keephistory*/);
const void *tmpptr = (const unsigned char*)data + first;
head = (head + count) % xsize;
totalused -= count;
return tmpptr;
}
void WvCircularBufStore::unget(size_t count)
{
assert(count <= totalinit - totalused ||
!"attempted to unget() more than ungettable()");
head = (head + xsize - count) % xsize;
totalused += count;
}
size_t WvCircularBufStore::ungettable() const
{
return totalinit - totalused;
}
void WvCircularBufStore::zap()
{
head = 0;
totalused = totalinit = 0;
}
size_t WvCircularBufStore::free() const
{
return xsize - totalused;
}
size_t WvCircularBufStore::optallocable() const
{
size_t tail = head + totalused;
if (tail >= xsize)
return xsize - totalused;
return xsize - tail;
}
void *WvCircularBufStore::alloc(size_t count)
{
assert(count <= xsize - totalused ||
!"attempted to alloc() more than free()");
totalinit = totalused; // always discard history
size_t first = ensurecontiguous(totalused, count,
false /*keephistory*/);
void *tmpptr = (unsigned char*)data + first;
totalused += count;
totalinit += count;
return tmpptr;
}
void WvCircularBufStore::unalloc(size_t count)
{
assert(count <= totalused ||
!"attempted to unalloc() more than unallocable()");
totalused -= count;
totalinit -= count;
}
size_t WvCircularBufStore::unallocable() const
{
return totalused;
}
void *WvCircularBufStore::mutablepeek(int offset, size_t count)
{
if (count == 0)
return NULL;
assert(((offset <= 0) ?
size_t(-offset) <= totalinit - totalused :
size_t(offset) < totalused) ||
! "attempted to peek() with invalid offset or count");
size_t first = ensurecontiguous(offset, count,
true /*keephistory*/);
void *tmpptr = (unsigned char*)data + first;
return tmpptr;
}
void WvCircularBufStore::normalize()
{
// discard history to minimize data transfers
totalinit = totalused;
// normalize the buffer
compact(data, xsize, head, totalused);
head = 0;
}
size_t WvCircularBufStore::ensurecontiguous(int offset,
size_t count, bool keephistory)
{
// determine the region of interest
size_t start = (head + offset + xsize) % xsize;
if (count != 0)
{
size_t end = start + count;
if (end > xsize)
{
// the region is not entirely contiguous
// determine the region that must be normalized
size_t keepstart = head;
if (keephistory)
{
// adjust the region to include history
keepstart += totalused - totalinit + xsize;
}
else
{
// discard history to minimize data transfers
totalinit = totalused;
}
keepstart %= xsize;
// normalize the buffer over this region
compact(data, xsize, keepstart, totalinit);
head = totalinit - totalused;
// compute the new start offset
start = (head + offset + xsize) % xsize;
}
}
return start;
}
void WvCircularBufStore::compact(void *data, size_t size,
size_t head, size_t count)
{
if (count == 0)
{
// Case 1: Empty region
// Requires 0 moves
return;
}
if (head + count <= size)
{
// Case 2: Contiguous region
// Requires count moves
memops.uninit_move(data, (unsigned char*)data + head, count);
return;
}
size_t headcount = size - head;
size_t tailcount = count - headcount;
size_t freecount = size - count;
if (freecount >= headcount)
{
// Case 3: Non-contiguous region, does not require swapping
// Requires count moves
memops.uninit_move((unsigned char*)data + headcount,
data, tailcount);
memops.uninit_move(data, (unsigned char*)data + head,
headcount);
return;
}
// Case 4: Non-contiguous region, requires swapping
// Requires count * 2 moves
unsigned char *start = (unsigned char*)data;
unsigned char *end = (unsigned char*)data + head;
while (tailcount >= headcount)
{
memops.swap(start, end, headcount);
start += headcount;
tailcount -= headcount;
}
// Now the array looks like: |a|b|c|g|h|_|d|e|f|
// FIXME: this is an interim solution
void *buf = memops.newarray(tailcount);
memops.uninit_move(buf, start, tailcount);
memops.uninit_move(start, end, headcount);
memops.uninit_move(start + headcount, buf, tailcount);
memops.deletearray(buf);
}
/***** WvLinkedBufferStore *****/
WvLinkedBufferStore::WvLinkedBufferStore(int _granularity) :
WvBufStore(_granularity), totalused(0), maxungettable(0)
{
}
bool WvLinkedBufferStore::usessubbuffers() const
{
return true;
}
size_t WvLinkedBufferStore::numsubbuffers() const
{
return list.count();
}
WvBufStore *WvLinkedBufferStore::firstsubbuffer() const
{
return list.first();
}
void WvLinkedBufferStore::appendsubbuffer(WvBufStore *buffer,
bool autofree)
{
list.append(buffer, autofree);
totalused += buffer->used();
}
void WvLinkedBufferStore::prependsubbuffer(WvBufStore *buffer,
bool autofree)
{
list.prepend(buffer, autofree);
totalused += buffer->used();
maxungettable = 0;
}
bool WvLinkedBufferStore::unlinksubbuffer(WvBufStore *buffer,
bool allowautofree)
{
WvBufStoreList::Iter it(list);
WvLink *link = it.find(buffer);
assert(link);
bool autofree = it.get_autofree();
totalused -= buffer->used();
if (buffer == list.first())
maxungettable = 0;
if (! allowautofree)
it.set_autofree(false);
it.unlink(); // do not recycle the buffer
return autofree;
}
size_t WvLinkedBufferStore::used() const
{
assert(!totalused || !list.isempty());
return totalused;
}
size_t WvLinkedBufferStore::optgettable() const
{
// find the first buffer with an optgettable() and return that
size_t count;
WvBufStoreList::Iter it(list);
for (it.rewind(); it.next(); )
if ((count = it->optgettable()) != 0)
return count;
return 0;
}
const void *WvLinkedBufferStore::get(size_t count)
{
assert(!totalused || !list.isempty());
if (count == 0)
return NULL;
assert(count <= totalused);
assert(count > 0);
totalused -= count;
assert(totalused >= 0);
// search for first non-empty buffer
WvBufStore *buf;
size_t availused;
WvBufStoreList::Iter it(list);
for (;;)
{
it.rewind(); it.next();
buf = it.ptr();
assert(buf && "attempted to get() more than used()" &&
"totalused is wrong!");
availused = buf->used();
if (availused != 0)
break;
// unlink the leading empty buffer
do_xunlink(it);
}
// return the data
if (availused < count)
buf = coalesce(it, count);
maxungettable += count;
return buf->get(count);
}
void WvLinkedBufferStore::unget(size_t count)
{
assert(!totalused || !list.isempty());
if (count == 0)
return;
assert(count > 0);
assert(!list.isempty());
assert(count <= maxungettable);
totalused += count;
maxungettable -= count;
list.first()->unget(count);
}
size_t WvLinkedBufferStore::ungettable() const
{
assert(!totalused || !list.isempty());
if (list.isempty())
{
assert(maxungettable == 0);
return 0;
}
// maxungettable and list.first()->ungettable() can get out of sync in two ways:
// - coalescing moves data from later buffers to the first one, which
// leaves it as ungettable in those buffers. So when we first start to
// use a buffer, its ungettable() count may be too high. (This is the
// reason maxungettable exists.)
// - some calls (ie. alloc) may clear all ungettable data from the first
// buffer without telling us. So there might be less data to unget than we
// think.
size_t avail = list.first()->ungettable();
if (avail > maxungettable)
avail = maxungettable;
return avail;
}
void WvLinkedBufferStore::zap()
{
totalused = 0;
maxungettable = 0;
WvBufStoreList::Iter it(list);
for (it.rewind(); it.next(); )
do_xunlink(it);
}
size_t WvLinkedBufferStore::free() const
{
if (!list.isempty())
return list.last()->free();
return 0;
}
size_t WvLinkedBufferStore::optallocable() const
{
if (!list.isempty())
return list.last()->optallocable();
return 0;
}
void *WvLinkedBufferStore::alloc(size_t count)
{
if (count == 0)
return NULL;
assert(!list.isempty() && "attempted to alloc() more than free()");
totalused += count;
return list.last()->alloc(count);
}
void WvLinkedBufferStore::unalloc(size_t count)
{
assert(count <= totalused);
totalused -= count;
while (count > 0)
{
assert(!list.isempty() &&
"attempted to unalloc() more than unallocable()" &&
"totalused is wrong");
WvBufStore *buf = list.last();
size_t avail = buf->unallocable();
if (count < avail)
{
buf->unalloc(count);
break;
}
WvBufStoreList::Iter it(list);
it.find(buf);
do_xunlink(it);
count -= avail;
}
}
size_t WvLinkedBufferStore::unallocable() const
{
return totalused;
}
size_t WvLinkedBufferStore::optpeekable(int offset) const
{
// search for the buffer that contains the offset
WvBufStoreList::Iter it(list);
int newoffset = search(it, offset);
WvBufStore *buf = it.ptr();
if (!buf)
return 0; // out of bounds
return buf->optpeekable(newoffset);
}
void *WvLinkedBufferStore::mutablepeek(int offset, size_t count)
{
if (count == 0)
return NULL;
// search for the buffer that contains the offset
WvBufStoreList::Iter it(list);
offset = search(it, offset);
WvBufStore *buf = it.ptr();
assert(buf && "attempted to peek() with invalid offset or count");
// return data if we have enough
size_t availpeek = buf->peekable(offset);
if (availpeek < count)
buf = coalesce(it, count);
return buf->mutablepeek(offset, count);
}
WvBufStore *WvLinkedBufferStore::newbuffer(size_t minsize)
{
minsize = roundup(minsize, granularity);
//return new WvInPlaceBufStore(granularity, minsize);
return new WvCircularBufStore(granularity, minsize);
}
void WvLinkedBufferStore::recyclebuffer(WvBufStore *buffer)
{
delete buffer;
}
int WvLinkedBufferStore::search(WvBufStoreList::Iter &it,
int offset) const
{
it.rewind();
if (it.next())
{
if (offset < 0)
{
// inside unget() region
WvBufStore *buf = it.ptr();
if (size_t(-offset) <= buf->ungettable())
return offset;
it.rewind(); // mark out of bounds
}
else
{
// inside get() region
do
{
WvBufStore *buf = it.ptr();
size_t avail = buf->used();
if (size_t(offset) < avail)
return offset;
offset -= avail;
}
while (it.next());
}
}
return 0;
}
WvBufStore *WvLinkedBufferStore::coalesce(WvBufStoreList::Iter &it,
size_t count)
{
WvBufStore *buf = it.ptr();
size_t availused = buf->used();
if (count <= availused)
return buf;
// allocate a new buffer if there is not enough room to coalesce
size_t needed = count - availused;
size_t availfree = buf->free();
size_t mustskip = 0;
if (availfree < needed)
{
// if this is the first buffer, then we need to unget as
// much as possible to ensure it does not get discarded
// during the coalescing phase
if (buf == list.first() && totalused != 0)
{
// use ungettable() instead of buf->ungettable() because we might
// have reset it to 0
// FIXME: uh... who might have reset it to 0, and why?
mustskip = ungettable();
buf->unget(mustskip);
}
needed = count + mustskip;
buf = newbuffer(needed);
// insert the buffer before the previous link
list.add_after(it.prev, buf, true);
it.find(buf);
}
// coalesce subsequent buffers into the first
while (it.next())
{
WvBufStore *itbuf = it.ptr();
size_t chunk = itbuf->used();
if (chunk > 0)
{
if (chunk > needed)
chunk = needed;
buf->merge(*itbuf, chunk);
needed -= chunk;
if (needed == 0)
{
buf->skip(mustskip);
return buf;
}
}
do_xunlink(it); // buffer is now empty
}
assert(false && "invalid count during get() or peek()");
return NULL;
}
void WvLinkedBufferStore::do_xunlink(WvBufStoreList::Iter &it)
{
WvBufStore *buf = it.ptr();
if (buf == list.first())
maxungettable = 0;
bool autofree = it.get_autofree();
it.set_autofree(false);
it.xunlink();
if (autofree)
recyclebuffer(buf);
}
/***** WvDynBufStore *****/
WvDynBufStore::WvDynBufStore(size_t _granularity,
size_t _minalloc, size_t _maxalloc) :
WvLinkedBufferStore(_granularity),
minalloc(_minalloc), maxalloc(_maxalloc)
{
assert(maxalloc >= minalloc);
}
size_t WvDynBufStore::free() const
{
return UNLIMITED_FREE_SPACE;
}
size_t WvDynBufStore::optallocable() const
{
size_t avail = WvLinkedBufferStore::optallocable();
if (avail == 0)
avail = UNLIMITED_FREE_SPACE;
return avail;
}
void *WvDynBufStore::alloc(size_t count)
{
if (count > WvLinkedBufferStore::free())
{
WvBufStore *buf = newbuffer(count);
appendsubbuffer(buf, true);
}
return WvLinkedBufferStore::alloc(count);
}
WvBufStore *WvDynBufStore::newbuffer(size_t minsize)
{
// allocate a new buffer
// try to approximate exponential growth by at least doubling
// the amount of space available for immediate use
size_t size = used();
if (size < minsize * 2)
size = minsize * 2;
if (size < minalloc)
size = minalloc;
else if (size > maxalloc)
size = maxalloc;
if (size < minsize)
size = minsize;
return WvLinkedBufferStore::newbuffer(size);
}
/***** WvNullBufStore *****/
WvNullBufStore::WvNullBufStore(size_t _granularity) :
WvWriteOnlyBufferStoreMixin<
WvReadOnlyBufferStoreMixin<WvBufStore> >(_granularity)
{
}
/***** WvBufCursorStore *****/
WvBufCursorStore::WvBufCursorStore(size_t _granularity,
WvBufStore *_buf, int _start, size_t _length) :
WvReadOnlyBufferStoreMixin<WvBufStore>(_granularity),
buf(_buf), start(_start), length(_length), shift(0)
{
}
bool WvBufCursorStore::isreadable() const
{
return buf->isreadable();
}
size_t WvBufCursorStore::used() const
{
return length - shift;
}
size_t WvBufCursorStore::optgettable() const
{
size_t avail = buf->optpeekable(start + shift);
assert(avail != 0 || length == shift ||
! "buffer cursor operating over invalid region");
if (avail > length)
avail = length;
return avail;
}
const void *WvBufCursorStore::get(size_t count)
{
assert(count <= length - shift ||
! "attempted to get() more than used()");
const void *data = buf->peek(start + shift, count);
shift += count;
return data;
}
void WvBufCursorStore::skip(size_t count)
{
assert(count <= length - shift ||
! "attempted to skip() more than used()");
shift += count;
}
void WvBufCursorStore::unget(size_t count)
{
assert(count <= shift ||
! "attempted to unget() more than ungettable()");
shift -= count;
}
size_t WvBufCursorStore::ungettable() const
{
return shift;
}
void WvBufCursorStore::zap()
{
shift = length;
}
size_t WvBufCursorStore::peekable(int offset) const
{
offset += shift;
offset -= start;
if (offset < 0 || offset > int(length))
return 0;
return length - size_t(offset);
}
size_t WvBufCursorStore::optpeekable(int offset) const
{
size_t avail = buf->optpeekable(start + shift + offset);
assert(avail != 0 || length == shift ||
! "buffer cursor operating over invalid region");
size_t max = peekable(offset);
if (avail > max)
avail = max;
return avail;
}
const void *WvBufCursorStore::peek(int offset, size_t count)
{
offset += shift;
assert((offset >= start && offset - start + count <= length) ||
! "attempted to peek() with invalid offset or count");
return buf->peek(offset, count);
}
bool WvBufCursorStore::iswritable() const
{
// check if mutablepeek() is supported
return buf->iswritable();
}
void *WvBufCursorStore::mutablepeek(int offset, size_t count)
{
offset += shift;
assert((offset >= start && offset - start + count <= length) ||
! "attempted to peek() with invalid offset or count");
return buf->mutablepeek(offset, count);
}
|