views:

174

answers:

1

I am using Protocol Buffers and OpensSSL to generate, HMACs and then CBC encrypt the two fields to obfuscate the session cookies -- similar Kerberos tokens.

Protocol Buffers' API communicates with std::strings and has a buffer caching mechanism; I exploit the caching mechanism, for successive calls in the the same thread, by placing it in thread local memory; additionally the OpenSSL HMAC and EVP CTX's are also placed in the same thread local memory structure ( see this question for some detail on why I use thread local memory and the massive amount of speedup it enables even with a single thread).

The generation and deserialization, "my algorithms", of these cookie strings uses intermediary void *s and std::strings and since Protocol Buffers has an internal memory retention mechanism I want these characteristics for "my algorithms".

So how do I implement a common scratch memory ? I don't know much about the rdbuf(streambuf - strinbuf ??) of the std::string object. I would presumeably need to grow it to the lowest common size ever encountered during the execution of "my algorithms". Thoughts ?

My question I guess would be: " is the internal buffer of a string re-usable, and if so, how ?"

Edit (new question):

It seems uppon reflection after Vlad's post that I do need a std::string as well a void * c-style scratch buffer. My question would then be: do popular stl's string implementations retain memory when they dont need it ? (my needs will probably stay between 128-bytes to 10-KB).

+2  A: 

You shouldn't expect the whole content of your std::string to reside in TLS, since std::string makes allocations and reallocations for data on its own. A simple idea would be to allocate a structure on heap and store a pointer to it in the TLS.

Edit:
AFAIK rdbuf is a feature of streams, not of string (see here and here).

Edit:
I would suggest using std::vector instead of string, it should be contiguous. Again, it's perhaps better to put just a pointer to the vector into TLS. The comments to the same article say that the standard requires even string to be contiguous, starting from &(str[0]) char.

Vlad
I hope to retain the string object for its active reference to the memory it retains on the heap :D. Yes I think you might be right, I will probably need to manage a separate scratch buffer for the c-style calls and grow it when needed. Especially since some calls will require both a "to" element and a "from" element.
Hassan Syed
I wonder if retaining a string whilst also retaining a seperate scratch buffer will still mean a lower chance of allocations --i.e., does the average string object keep around memory when it doesn't need it ?
Hassan Syed
ok, ok, vector it is :D
Hassan Syed
ok so I'm using vector and resize, and vector tends not to return memory; and I am using string -- hoping that it doesn't return memory either :D
Hassan Syed
@Hassan: The `std::vector` must retain the memory if you are not using it, because you can potentially access it at any time. If you however are _expanding_ the vector, it may possibly change the memory location (if the current memory block it uses is not big enough). Hope that clarifies the problem.
Vlad