//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // DatabaseHandle::~DatabaseHandle() { Check_Object(this); // //--------------------------------------------------------------- // If we are closing a read only file, release the memmap handle, // otherwise delete the database object //--------------------------------------------------------------- // if (m_readOnly) gos_CloseMemoryMappedFile(m_handle); else { Save(); Check_Object(m_dataBase); // //--------------------------------------------------------------------- // Now we need to go through and delete all the records in the database //--------------------------------------------------------------------- // for (DWORD i=0; i<Database::e_DataBlockSize; ++i) { Record* record = reinterpret_cast<Record*>(m_dataBase->m_idOffsets[i]); if (record) { while (record) { record = reinterpret_cast<Record*>((DWORD)record + m_baseAddress); Check_Object(record); Record* this_record = record; record = reinterpret_cast<Record*>(record->m_nextIDRecord); if (this_record->m_mustFree) delete this_record; } } } // //-------------------------------- // Now free up the database itself //-------------------------------- // gos_Free(m_dataBase); } // //---------------------------------------------------- // Delete the memory heap if this is the last database //---------------------------------------------------- // if (--Database::FilesOpened == 0) gos_DestroyMemoryHeap(Database_Heap); }
//------------------------------------------------------------------------------------------ long Radio::playMessage (RadioMessageType msgType) { long i, roll, callsign, fragmentNum, dropOut = 0; if (!useSound) return(NO_PLAY); if (!enabled) return(NO_PLAY); if (!owner) return(NO_PLAY); if (!soundSystem->checkMessage(owner, messageInfo[msgType].priority, msgType)) return(NO_PLAY); if ((msgType == RADIO_AMMO_OUT) && ammoOutPlayed) return(NO_PLAY); if (messageInfo[msgType].styleCount > 1) { roll = RandomNumber(100); // choose which style of message to play for (i=0; i<messageInfo[msgType].styleCount; i++) { dropOut += messageInfo[msgType].styleChance[i]; if (roll < dropOut) break; } if (i != 0 && i == messageInfo[msgType].styleCount) return NO_PLAY; if (messageInfo[msgType].messageMapping + i == owner->getLastMessage()) i++; if (i >= messageInfo[msgType].styleCount) i = 0; } else i = 0; //RadioData *msgData = (RadioData *)radioHeap->Malloc(sizeof(RadioData)); RadioData *msgData = new RadioData; //magic 02102011 if (!msgData) { return(NO_PLAY); } memset(msgData,0,sizeof(RadioData)); msgData->noiseId = SHORT_STATIC; msgData->msgType = msgType; msgData->msgId = messageInfo[msgType].messageMapping + i; msgData->movieCode = messageInfo[msgType].movieCode; msgData->msgHeap = NULL ; //radioHeap; //magic 02102011 msgData->turnQueued = turn; msgData->priority = messageInfo[msgType].priority; msgData->pilot = owner; msgData->expirationDate = scenarioTime + messageInfo[msgType].shelfLife; //----------------------------------------------------------------------- // Load the pieces need for playback. callsign = 0; fragmentNum = 0; if (messageInfo[msgType].pilotIdentifiesSelf) { if (messageInfo[RADIO_CALLSIGN].styleCount > 1) { roll = RandomNumber(100); // choose which style of message to play for (i=0; i<messageInfo[RADIO_CALLSIGN].styleCount; i++) { dropOut += messageInfo[RADIO_CALLSIGN].styleChance[i]; if (roll < dropOut) break; } if (i != 0 && i == messageInfo[RADIO_CALLSIGN].styleCount) callsign = 0; if (i >= messageInfo[RADIO_CALLSIGN].styleCount) callsign = 0; } else callsign = 0; callsign += messageInfo[RADIO_CALLSIGN].messageMapping; } if (callsign) { if (messagesFile[radioID]->seekPacket(callsign) == NO_ERR) { unsigned long messageSize = messagesFile[radioID]->getPacketSize(); //msgData->data[fragmentNum] = (MemoryPtr)radioHeap->Malloc(messageSize); msgData->data[fragmentNum] = (MemoryPtr)gos_Malloc(messageSize); //magic 02102011 if (!msgData->data[fragmentNum]) { //radioHeap->Free(msgData); delete msgData; //magic 02102011 return(NO_PLAY); } messagesFile[radioID]->readPacket(callsign,msgData->data[fragmentNum]); msgData->dataSize[fragmentNum] = messageSize; fragmentNum++; } } if (messagesFile[radioID]->seekPacket(msgData->msgId) == NO_ERR) { unsigned long messageSize = messagesFile[radioID]->getPacketSize(); //magic 31032011 begin if (messageSize > 0) { //magic 31032011 end //msgData->data[fragmentNum] = (MemoryPtr)radioHeap->Malloc(messageSize); msgData->data[fragmentNum] = (MemoryPtr)gos_Malloc(messageSize); //magic 02102011 if (!msgData->data[fragmentNum]) { while (fragmentNum >= 0) { //radioHeap->Free(msgData->data[fragmentNum]); gos_Free(msgData->data[fragmentNum]); //magic 02102011 fragmentNum--; } //radioHeap->Free(msgData); delete msgData; //magic 02102011 return(NO_PLAY); } messagesFile[radioID]->readPacket(msgData->msgId,msgData->data[fragmentNum]); msgData->dataSize[fragmentNum] = messageSize; //magic 31032011 begin } //magic 31032011 end if (noiseFile->seekPacket(msgData->noiseId) == NO_ERR) { unsigned long messageSize = noiseFile->getPacketSize(); //msgData->noise[0] = (MemoryPtr)radioHeap->Malloc(messageSize); msgData->noise[0] = (MemoryPtr)gos_Malloc(messageSize); //magic 02102011 if (!msgData->noise[0]) { //radioHeap->Free(msgData); delete msgData; //magic 02102011 if (fragmentNum > 0) //radioHeap->Free(msgData->data[0]); gos_Free(msgData->data[0]); //magic 02102011 return(NO_PLAY); } noiseFile->readPacket(msgData->noiseId,msgData->noise[0]); msgData->noiseSize[fragmentNum] = messageSize; } } //------------------------------------------------------ // Big ol bug here. We weren't checking to see if the // queue was full. If it was, memory would leak from // the smacker window. It wouldn't leak from the RadioHeap // because we clear the radio heap every mission!! if (soundSystem->queueRadioMessage(msgData) != NO_ERR) { if (msgData) { for (long j=0;j<MAX_FRAGMENTS;j++) { //radioHeap->Free(msgData->data[j]); gos_Free(msgData->data[j]); //magic 02102011 msgData->data[j] = NULL; //radioHeap->Free(msgData->noise[j]); gos_Free(msgData->noise[j]); //magic 02102011 msgData->noise[j] = NULL; } //radioHeap->Free(msgData); delete msgData; msgData = NULL; } return (NO_PLAY); } if (msgType == RADIO_AMMO_OUT) ammoOutPlayed = true; return(msgData->msgId); }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // DatabaseHandle::DatabaseHandle( const char* filename, bool read_only ) { // //----------------------------------------------------- // Create the database heap if it doesn't already exist //----------------------------------------------------- // m_fileName = filename; if (!Database::FilesOpened++) Database_Heap = gos_CreateMemoryHeap("Database", 0); gos_PushCurrentHeap(Database_Heap); m_currentRecord = 0; m_currentPointer = NULL; m_dirtyFlag = false; // //-------------------------------------------- // If the file is read only, our job is simple //-------------------------------------------- // m_readOnly = read_only; if (m_readOnly) { if (gos_DoesFileExist(filename)) { FileStream::IsRedirected = false; DWORD size; m_handle = gos_OpenMemoryMappedFile( filename, reinterpret_cast<BYTE**>(&m_dataBase), &size ); if (m_dataBase->m_tag != Database::e_Tag) STOP(("Invalid database file \"%s\"", filename)); if (m_dataBase->m_version > Database::e_Version) STOP(("Application must be recompiled to use database \"%s\"", filename)); m_baseAddress = reinterpret_cast<DWORD>(m_dataBase); Check_Object(m_dataBase); } else STOP(("Database \"%s\" does not exist", filename)); } // //------------------------------------------------------------- // The file is not read only, so see if we load it or create it //------------------------------------------------------------- // else { m_handle = NULL; if (gos_DoesFileExist(filename)) { DWORD size; gos_GetFile( filename, reinterpret_cast<BYTE**>(&m_dataBase), &size ); FileStream::IsRedirected = false; Check_Pointer(m_dataBase); if (m_dataBase->m_tag != Database::e_Tag || m_dataBase->m_version > Database::e_Version || m_dataBase->m_version == 1) { gos_Free(m_dataBase); PAUSE(("Bad database file! Press 'Continue' to rebuild", filename)); m_dataBase = new(gos_Malloc(sizeof(Database), Database_Heap)) Database; m_baseAddress = 0; } else { m_baseAddress = reinterpret_cast<DWORD>(m_dataBase); Check_Object(m_dataBase); } } // //------------------------------------------ // The file doesn't exist, so create it here //------------------------------------------ // else { m_dataBase = new(gos_Malloc(sizeof(Database), Database_Heap)) Database; m_baseAddress = 0; } } gos_PopCurrentHeap(); }
void operator delete[](PVOID ptr) { gos_Free(ptr); }