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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<link href="../pool.css" rel="stylesheet" type="text/css">
<title>Guaranteeing Alignment</title>
</head>
<body>
<img src="../../../../boost.png" width="276" height="86" alt="C++ Boost">
<h1 align="center">Guaranteeing Alignment</h1>
<h2>Terminology</h2>
<p>Review the <a href="../concepts.html">concepts document</a> if you are
not already familiar with it. Remember that <em>block</em> is a contiguous
section of memory, which is <em>partitioned</em> or <em>segregated</em>
into fixed-size <em>chunks</em>. These <em>chunks</em> are what are
allocated and deallocated by the user.</p>
<h2>Overview</h2>
<p>Each <span class="code">Pool</span> has a single free list that can
extend over a number of memory blocks. Thus, <span class="code">Pool</span>
also has a linked list of allocated memory blocks. Each memory block, by
default, is allocated using <span class="code">new[]</span>, and all memory
blocks are freed on destruction. It is the use of <span class=
"code">new[]</span> that allows us to guarantee alignment.</p>
<h2>Proof of Concept: Guaranteeing Alignment</h2>
<p>Each block of memory is allocated as a POD type (specifically, an array
of characters) through <span class="code">operator new[]</span>. Let
<em>POD_size</em> be the number of characters allocated.</p>
<h3>Predicate 1: Arrays may not have padding</h3>
<p>This follows from the following quote:</p>
<p>[5.3.3/2] (Expressions::Unary expressions::Sizeof) "... When applied to
an array, the result is the total number of bytes in the array. This implies
that the size of an array of <em>n</em> elements is <em>n</em>
times the size of an element."</p>
<p>Therefore, arrays cannot contain padding, though the elements within the
arrays may contain padding.</p>
<h3>Predicate 2: Any block of memory allocated as an array of characters
through <span class="code">operator new[]</span> (hereafter referred to as
<em>the block</em>) is properly aligned for any object of that size or
smaller</h3>
<p>This follows from:</p>
<ul>
<li>[3.7.3.1/2] (Basic concepts::Storage duration::Dynamic storage
duration::Allocation functions) "... The pointer returned shall be
suitably aligned so that it can be converted to a pointer of any complete
object type and then used to access the object or array in the storage
allocated ..."</li>
<li>[5.3.4/10] (Expressions::Unary expressions::New) "... For arrays of
<span class="code">char</span> and <span class="code">unsigned char</span>,
the difference between the result of the
<em>new-expression</em> and the address returned by the allocation
function shall be an integral multiple of the most stringent alignment
requirement (3.9) of any object type whose size is no greater than the
size of the array being created. [<em>Note:</em> Because allocation
functions are assumed to return pointers to storage that is appropriately
aligned for objects of any type, this constraint on array allocation
overhead permits the common idiom of allocating character arrays into
which objects of other types will later be placed. ]"</li>
</ul>
<h3>Consider: imaginary object type <em>Element</em> of a size which is a
multiple of some actual object size; assume sizeof(Element) > POD_size</h3>
<p>Note that an object of that size <em>can</em> exist. One object of that
size is an array of the "actual" objects.</p>
<p>Note that the block is properly aligned for an Element. This directly
follows from Predicate 2.</p>
<h3>Corollary 1: The block is properly aligned for an array of Elements</h3>
<p>This follows from Predicates 1 and 2, and the following quote:</p>
<p>[3.9/9] (Basic concepts::Types) "An <em>object type</em> is a (possibly
cv-qualified) type that is not a function type, not a reference type, and
not a void type." (Specifically, array types are object types.)</p>
<h3>Corollary 2: For any pointer <em>p</em> and integer <em>i</em>, if p is
properly aligned for the type it points to, then p + i (when well-defined)
is properly aligned for that type; in other words, if an array is properly
aligned, then each element in that array is properly aligned</h3>
<p>There are no quotes from the Standard to directly support this argument,
but it fits the common conception of the meaning of "alignment".</p>
<p>Note that the conditions for p + i being well-defined are outlined in
[5.7/5]. We do not quote that here, but only make note that it is
well-defined if p and p + i both point into or one past the same array.</p>
<h3>Let: sizeof(Element) be the least common multiple of sizes of several
actual objects (T<sub>1</sub>, T<sub>2</sub>, T<sub>3</sub>, ...)</h3>
<h3>Let: <em>block</em> be a pointer to the memory block, <em>pe</em> be
(Element *) block, and <em>p<sub>n</sub></em> be (T<sub>n</sub> *) block</h3>
<h3>Corollary 3: For each integer <em>i</em>, such that pe + i is
well-defined, then for each <em>n</em>, there exists some integer
<em>j<sub>n</sub></em> such that p<sub>n</sub> + j<sub>n</sub> is
well-defined and refers to the same memory address as pe + i</h3>
<p>This follows naturally, since the memory block is an array of Elements,
and for each n, sizeof(Element) % sizeof(T<sub>n</sub>) == 0; thus, the
boundary of each element in the array of Elements is also a boundary of each
element in each array of T<sub>n</sub>.</p>
<h3>Theorem: For each integer <em>i</em>, such that pe + i is well-defined,
that address (pe + i) is properly aligned for each type T<sub>n</sub></h3>
<p>Since pe + i is well-defined, then by Corollary 3, p<sub>n</sub> + j<sub>n</sub>
is well-defined. It is properly aligned from Predicate 2 and Corollaries 1
and 2.</p>
<h2>Use of the Theorem</h2>
<p>The proof above covers alignment requirements for cutting chunks out of a
block. The implementation uses actual object sizes of:</p>
<ul>
<li>The requested object size (requested_size); this is the size of chunks
requested by the user</li>
<li>void * (pointer to void); this is because we interleave our free list
through the chunks</li>
<li>size_type; this is because we store the size of the next block within
each memory block</li>
</ul>
<p>Each block also contains a pointer to the next block; but that is stored
as a pointer to void and cast when necessary, to simplify alignment
requirements to the three types above.</p>
<p>Therefore, <span class="code">alloc_size</span> is defined to be the lcm
of the sizes of the three types above.</p>
<h2>A Look at the Memory Block</h2>
<p>Each memory block consists of three main sections. The first section is
the part that chunks are cut out of, and contains the interleaved free list.
The second section is the pointer to the next block, and the third section
is the size of the next block.</p>
<p>Each of these sections may contain padding as necessary to guarantee
alignment for each of the next sections. The size of the first section is
number_of_chunks * lcm(requested_size, sizeof(void *), sizeof(size_type));
the size of the second section is lcm(sizeof(void *), sizeof(size_type); and
the size of the third section is sizeof(size_type).</p>
<p>Here's an example memory block, where requested_size == sizeof(void *) ==
sizeof(size_type) == 4:</p>
<table cellspacing="0" cellpadding="0" border="3" style="clear: both;"
align="center" summary="">
<caption>
<em>Memory block containing 4 chunks, showing overlying array
structures; FLP = Interleaved Free List Pointer</em>
</caption>
<tr>
<th>Sections</th>
<th>size_type alignment</th>
<th>void * alignment</th>
<th>requested_size alignment</th>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;" rowspan="4">
Chunks section (16 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP for Chunk 1
(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Chunk 1 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">FLP for Chunk
2 (4 bytes)</td>
<td style="background-color: silver; text-align: center;">Chunk 2 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP for Chunk 3
(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Chunk 3 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">FLP for Chunk
4 (4 bytes)</td>
<td style="background-color: silver; text-align: center;">Chunk 4 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">Pointer to
next Block (4 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Pointer to next
Block (4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">Size of next
Block (4 bytes)</td>
<td style="background-color: silver; text-align: center;">Size of next
Block (4 bytes)</td>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
</table>
<p>To show a visual example of possible padding, here's an example memory
block where requested_size == 8 and sizeof(void *) == sizeof(size_type) ==
4:</p>
<table cellspacing="0" cellpadding="0" border="3" style="clear: both;"
align="center" summary="">
<caption>
<em>Memory block containing 4 chunks, showing overlying array
structures; FLP = Interleaved Free List Pointer</em>
</caption>
<tr>
<th>Sections</th>
<th>size_type alignment</th>
<th>void * alignment</th>
<th>requested_size alignment</th>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;" rowspan="8">
Chunks section (32 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP for Chunk 1
(4 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="2">
Chunk 1 (8 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP for Chunk 2
(4 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="2">
Chunk 2 (8 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP for Chunk 3
(4 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="2">
Chunk 3 (8 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP for Chunk 4
(4 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="2">
Chunk 4 (8 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">Pointer to
next Block (4 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Pointer to next
Block (4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">Size of next
Block (4 bytes)</td>
<td style="background-color: silver; text-align: center;">Size of next
Block (4 bytes)</td>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
</table>
<p>Finally, here is a convoluted example where the requested_size is 7,
sizeof(void *) == 3, and sizeof(size_type) == 5, showing how the least
common multiple guarantees alignment requirements even in the oddest of
circumstances:</p>
<table cellspacing="0" cellpadding="0" border="3" style="clear: both;"
align="center" summary="">
<caption>
<em>Memory block containing 2 chunks, showing overlying array structures</em>
</caption>
<tr>
<th>Sections</th>
<th>size_type alignment</th>
<th>void * alignment</th>
<th>requested_size alignment</th>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process <!-- First Section --></td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;" rowspan="42">
Chunks section (210 bytes)</td>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">
Interleaved free list pointer for Chunk 1 (15 bytes; 3 used)</td>
<td style="background-color: gray; text-align: center;" rowspan="21">
Chunk 1 (105 bytes; 7 used)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
(15 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">(15
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
(15 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">(15
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
(15 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">(15
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
Interleaved free list pointer for Chunk 2 (15 bytes; 3 used)</td>
<td style="background-color: silver; text-align: center;" rowspan="21">
Chunk 2 (105 bytes; 7 used)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">(15
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
(15 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">(15
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
(15 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">(15
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
<td style="background-color: silver; text-align: center;" rowspan="3">
(15 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)
<!-- Second Section --></td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;" rowspan="3">
Pointer to next Block (15 bytes; 3 used)</td>
<td style="background-color: gray; text-align: center;">(5 bytes)</td>
<td style="background-color: gray; text-align: center;" rowspan="3">
Pointer to next Block (15 bytes; 3 used)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(5 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(5 bytes)
<!-- Third Section --></td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">Size of next
Block (5 bytes; 5 used)</td>
<td style="background-color: silver; text-align: center;">Size of next
Block (5 bytes; 5 used)</td>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
</table>
<h2>How Contiguous Chunks are Handled</h2>
<p>The theorem above guarantees all alignment requirements for allocating
chunks and also implementation details such as the interleaved free list.
However, it does so by adding padding when necessary; therefore, we have to
treat allocations of contiguous chunks in a different way.</p>
<p>Using array arguments similar to the above, we can translate any request
for contiguous memory for <em>n</em> objects of requested_size into a
request for <em>m</em> contiguous chunks. <em>m</em> is simply ceil(n *
requested_size / alloc_size), where alloc_size is the actual size of the
chunks. To illustrate:</p>
<p>Here's an example memory block, where requested_size == 1 and sizeof(void
*) == sizeof(size_type) == 4:</p>
<table cellspacing="0" cellpadding="0" border="3" style="clear: both;"
align="center" summary="">
<caption>
<em>Memory block containing 4 chunks; requested_size is 1</em>
</caption>
<tr>
<th>Sections</th>
<th>size_type alignment</th>
<th>void * alignment</th>
<th>requested_size alignment</th>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;" rowspan="4">
Chunks section (16 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP to Chunk 2
(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Chunk 1 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">FLP to Chunk 3
(4 bytes)</td>
<td style="background-color: silver; text-align: center;">Chunk 2 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP to Chunk 4
(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Chunk 3 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">FLP to
end-of-list (4 bytes)</td>
<td style="background-color: silver; text-align: center;">Chunk 4 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">Pointer to
next Block (4 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Ptr to
end-of-list (4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">Size of next
Block (4 bytes)</td>
<td style="background-color: silver; text-align: center;">0 (4 bytes)</td>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
</table>
<br>
<table cellspacing="0" cellpadding="0" border="3" style="clear: both;"
align="center" summary="">
<caption>
<em>After user requests 7 contiguous elements of requested_size</em>
</caption>
<tr>
<th>Sections</th>
<th>size_type alignment</th>
<th>void * alignment</th>
<th>requested_size alignment</th>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;" rowspan="4">
Chunks section (16 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">4 bytes in use
by program</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">3 bytes in use
by program (1 byte unused)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">FLP to Chunk 4
(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Chunk 3 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">(4 bytes)</td>
<td style="background-color: silver; text-align: center;">FLP to
end-of-list (4 bytes)</td>
<td style="background-color: silver; text-align: center;">Chunk 4 (4
bytes)</td>
</tr>
<tr>
<td style="background-color: silver; text-align: center;">Pointer to
next Block (4 bytes)</td>
<td style="background-color: gray; text-align: center;">(4 bytes)</td>
<td style="background-color: gray; text-align: center;">Ptr to
end-of-list (4 bytes)</td>
</tr>
<tr>
<td style="background-color: gray; text-align: center;">Size of next
Block (4 bytes)</td>
<td style="background-color: silver; text-align: center;">0 (4 bytes)</td>
</tr>
<tr>
<td style="background-color: red; text-align: center;" colspan="4">
Memory not belonging to process</td>
</tr>
</table>
<p>Then, when the user deallocates the contiguous memory, we can split it up
into chunks again.</p>
<p>Note that the implementation provided for allocating contiguous chunks
uses a linear instead of quadratic algorithm. This means that it
<strong>may not find</strong> contiguous free chunks if the free list is not
ordered. Thus, it is recommended to always use an ordered free list when
dealing with contiguous allocation of chunks. (In the example above, if
Chunk 1 pointed to Chunk 3 pointed to Chunk 2 pointed to Chunk 4, instead of
being in order, the contiguous allocation algorithm would have failed to
find any of the contiguous chunks).</p>
<hr>
<p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
"http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01 Transitional"
height="31" width="88"></a></p>
<p>Revised
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->05 December, 2006<!--webbot bot="Timestamp" endspan i-checksum="38516" --></p>
<p><i>Copyright © 2000, 2001 Stephen Cleary (scleary AT jerviswebb DOT com)</i></p>
<p><i>Distributed under the Boost Software License, Version 1.0. (See
accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
or copy at <a href=
"http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
</body>
</html>
|