03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

i += p.Length();<br />

p.Set(aBuffer->Ptr(i));<br />

};<br />

_LIT(KNewLine,"\n");<br />

console->Printf(KNewLine);<br />

}<br />

The result is<br />

buffer=[hell][o!]<br />

buffer=[hell][o wo][rld!]<br />

buffer=[he][ld!]<br />

buffer=[held][!]<br />

buffer=[held][ `<br />

I`<br />

I `<br />

I `<br />

I][!ld!]<br />

buffer=[hell][o wo][rld!]<br />

Because I specified a granularity of 4, at first, the segments are all 4 bytes long.<br />

When I delete the seven characters from the middle of the string, the segments are<br />

optimized for minimum data shuffling. After random operations on the buffer over a long<br />

period – say, operations controlled by a user typing and editing text in a word processor –<br />

the buffer can become very fragmented, with many segments containing less than the<br />

maximum amount of data. Compress() moves the data so that as few segments as<br />

possible are used.<br />

ExpandL() and ResizeL() (which puts extra bytes at the end, if the new size is greater<br />

than the current size) are useful for making a sequence of InsertL() operations atomic<br />

and for improving their performance. Say you needed to insert six items: if you used six<br />

InsertL()s within your function InsertSixItemsL(), you would need to trap each one<br />

and, if it failed because of an out-of-memory error, you would need to delete all the items<br />

you had so far inserted. In addition, the use of repeated allocation for each InsertL()<br />

would impact performance and fragment the heap – especially for flat buffers. You can avoid<br />

all these problems by using ExpandL() or ResizeL(), and then a series of Write()s –<br />

which cannot leave.<br />

If you're at your PC, replace the allocation of CBufSeg with a CBufFlat and run buffers<br />

again. You should have little difficulty predicting or explaining the results.<br />

Dynamic buffers are used to store array elements in expandable arrays. Both flat and<br />

segmented arrays are provided, corresponding to the buffer types used to implement them.<br />

CArrayFixFlat uses a flat buffer to store an array of T objects, while<br />

CArrayFixSeg uses a segmented buffer. Ts are stored so that the nth T occupies<br />

sizeof(T)bytes from position n*sizeof(T) in the buffer. A CBufSeg granularity of a<br />

multiple of sizeof(T) is specified, so that a T never spans a segment boundary. These<br />

and other array types are described in Section 8.1.3.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!