title: Thread Local Memory, Using std::string's internal buffer for c-style Scratch Memory
body: <p>I am using <a href="http://code.google.com/p/protobuf/" rel="nofollow noreferrer">Protocol Buffers</a> and <a href="http://www.openssl.org/docs/crypto/crypto.html" rel="nofollow noreferrer">OpensSSL</a> to generate, HMACs and then CBC encrypt the two fields to obfuscate the session cookies -- similar Kerberos tokens. </p>
  
  <p>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 <a href="https://stackoverflow.com/questions/2623396/boost-thread-specific-storage-question-boost-thread-tss-hpp">this question</a> for some detail on why I use thread local memory and the massive amount of speedup it enables even with a single thread).</p>
  
  <p>The generation and deserialization, "my algorithms", of these cookie strings uses intermediary <code>void *</code>s and <code>std::string</code>s and since Protocol Buffers has an internal memory retention mechanism I want these characteristics for "my algorithms".</p>
  
  <p>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 ?</p>
  
  <p>My question I guess would be: <strong>" is the internal buffer of a string re-usable, and if so, how ?"</strong></p>
  
  <p><strong>Edit (new question):</strong></p>
  
  <p>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).</p>