+// allocate enough memory for nLen characters
+void wxString::Alloc(size_t nLen)
+{
+ wxStringData *pData = GetStringData();
+ if ( pData->nAllocLength <= nLen ) {
+ if ( pData->IsEmpty() ) {
+ nLen += EXTRA_ALLOC;
+
+ wxStringData* pData = (wxStringData*)
+ malloc(sizeof(wxStringData) + (nLen + 1)*sizeof(char));
+ pData->nRefs = 1;
+ pData->nDataLength = 0;
+ pData->nAllocLength = nLen;
+ m_pchData = pData->data(); // data starts after wxStringData
+ m_pchData[0u] = '\0';
+ }
+ else if ( pData->IsShared() ) {
+ pData->Unlock(); // memory not freed because shared
+ size_t nOldLen = pData->nDataLength;
+ AllocBuffer(nLen);
+ memcpy(m_pchData, pData->data(), nOldLen*sizeof(char));
+ }
+ else {
+ nLen += EXTRA_ALLOC;
+
+ wxStringData *p = (wxStringData *)
+ realloc(pData, sizeof(wxStringData) + (nLen + 1)*sizeof(char));
+
+ if ( p == NULL ) {
+ // @@@ what to do on memory error?
+ return;
+ }
+
+ // it's not important if the pointer changed or not (the check for this
+ // is not faster than assigning to m_pchData in all cases)
+ p->nAllocLength = nLen;
+ m_pchData = p->data();
+ }
+ }
+ //else: we've already got enough
+}
+
+// shrink to minimal size (releasing extra memory)
+void wxString::Shrink()
+{
+ wxStringData *pData = GetStringData();
+
+ // this variable is unused in release build, so avoid the compiler warning by
+ // just not declaring it
+#ifdef __WXDEBUG__
+ void *p =
+#endif
+ realloc(pData, sizeof(wxStringData) + (pData->nDataLength + 1)*sizeof(char));
+
+ wxASSERT( p != NULL ); // can't free memory?
+ wxASSERT( p == pData ); // we're decrementing the size - block shouldn't move!
+}
+