//=============================================================================================== // FUNCTION: WriteStrings // PURPOSE: Write the Protocol Strings from the cache to the file. // BOOL CProtocolWriterABF2::WriteStrings() { MEMBERASSERT(); long lCount = m_Strings.GetNumStrings(); if( lCount == 0 ) { m_FileInfo.StringsSection.Set( 0, 0, 0 ); return TRUE; } // Pad out the current block. m_pFI->FillCurrentBlock( &m_lNextBlock ); // Work out if we can overwrite the existing string table or need to create a new one. if( m_FileInfo.StringsSection.uBytes ) { UINT uOldNumBlocks = BytesToBlocks( m_FileInfo.StringsSection.uBytes ); UINT uNewNumBlocks = BytesToBlocks( m_Strings.GetTotalSize() ); if( uNewNumBlocks <= uOldNumBlocks ) { // Strings fit into the original space - overwrite the old table with the new one. m_pFI->Seek( m_FileInfo.StringsSection.uBlockIndex * ABF_BLOCKSIZE, FILE_BEGIN ); } else { // Strings need more blocks - rewrite them at the end (and orphan the original table). m_pFI->FillToNextBlock( &m_lNextBlock ); } } UINT uOffset = 0; if( !m_Strings.Write( m_pFI->GetFileHandle(), uOffset ) ) return FALSE; // Update the FileInfo. m_FileInfo.StringsSection.Set( m_lNextBlock, m_Strings.GetTotalSize(), lCount ); m_pFI->FillCurrentBlock( &m_lNextBlock ); // Clear the cache. m_Strings.Clear(); return TRUE; }
void zfree(PTR p, size_t size) { unsigned blocks = BytesToBlocks(size); if (blocks > POOLSZ) free(p); else { ((ZBLOCK *) p)->link = pool[--blocks]; pool[blocks] = (ZBLOCK *) p; } }
void zfree(PTR p, size_t size) { unsigned blocks = BytesToBlocks(size); if (!IsPoolable(blocks)) { free(p); FinishPtr(p, size); } else { ((ZBLOCK *) p)->link = pool[--blocks]; pool[blocks] = (ZBLOCK *) p; } }
PTR zmalloc(size_t size) { unsigned blocks = BytesToBlocks(size); size_t bytes = (size_t) BlocksToBytes(blocks); register ZBLOCK *p; static unsigned amt_avail; static ZBLOCK *avail; if (!IsPoolable(blocks)) { p = (ZBLOCK *) Malloc(bytes); if (!p) out_of_mem(); RecordPtr(p, size); } else { if ((p = pool[blocks - 1]) != 0) { pool[blocks - 1] = p->link; } else { if (blocks > amt_avail) { if (amt_avail != 0) /* free avail */ { avail->link = pool[--amt_avail]; pool[amt_avail] = avail; } if (!(avail = (ZBLOCK *) Malloc((size_t) (CHUNK * ZBLOCKSZ)))) { /* if we get here, almost out of memory */ amt_avail = 0; p = (ZBLOCK *) Malloc(bytes); if (!p) out_of_mem(); RecordPtr(p, bytes); return (PTR) p; } else { RecordPtr(avail, CHUNK * ZBLOCKSZ); amt_avail = CHUNK; } } /* get p from the avail pile */ p = avail; avail += blocks; amt_avail -= blocks; } } return (PTR) p; }