Saturday, May 28, 2005

21. Why is there no HBuf class?

Well, there is and there isn't. Up until Symbian OS v8.0, there was no modifiable heap descriptor class, HBuf, in symmetry with the TBufC and TBuf stack buffers. There were a number of reasons for this:

(1) The expectation might be that, for a modifiable heap descriptor, the maximum length would expand and contract on the heap as the content was modified. An HBuf class which is modifiable but does not dynamically resize could be considered odd and rather pointless. But to add dynamic resizing to HBuf would be difficult because these methods could leave under low memory conditions and would have to be adjusted accordingly. But, as I’ve previously described, all the modifiable descriptor operations are implemented in the base class, TDes, thus affecting all descriptor types.

(2) If dynamic reallocation were added to HBuf, the location of the buffer might change as code is reallocated, affecting all TPtr objects referencing that heap buffer's data, as well as compromising the cleanup stack, if the HBuf pointer is stored there.

(3) Heap buffers were initially intended to allow efficient reading of constant resource strings of variable length. Because they are constant, they save on the additional four bytes required to store a maximum length, which allows them to be as compact as possible. They can still be modified using Des() if necessary, If a separate HBuf class were provided, it would have an extra 4-byte overhead - and may frequently be created unnecessarily, which might add to a significant waste of heap space over time.

However, this is a somewhat spurious argument since making an HBufC::Des() call is non-trivial and the resultant TPtr itself occupies 12 bytes of stack space. If a modifiable heap descriptor is definitely needed, creating a constant buffer and then an additional, TPtr to update wastes memory *and* processor instructions.

To this end, Symbian OS 8.0 and 8.1 introduced class RBuf for resizable descriptors. The Symbian documentation for this class can be found in [reference 6].

I discuss class RBuf in more detail in 32. How do I use RBuf? What is it?.

Comments:
There actually is HBuf class in Symbian OS, but if I remember correctly it is only available in EKA2 kernel side development.
 
Is there anything like HBuf in EKA2? As per my knowledge, there is nothing like that.
 
"The expectation might be that, for a modifiable heap descriptor, the maximum length would expand and contract on the heap as the content was modified."
I think maximum length can't expand and contract; instead length of the data can expand upto max length. Am I right?
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?

Google
WWW Descriptors FAQ