From 3d63d36cb77648cf57c2a17dda592ca8e287263d Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 5 Jul 2007 23:40:33 +0000 Subject: [PATCH] pstats track miles audio memory usage --- panda/src/audiotraits/fmodAudioSound.cxx | 1 - panda/src/audiotraits/milesAudioManager.cxx | 29 +++++++++---- panda/src/audiotraits/milesAudioManager.h | 3 +- panda/src/express/virtualFile.cxx | 46 ++++++++++++++------- panda/src/express/virtualFile.h | 6 ++- panda/src/express/virtualFileSystem.I | 19 +++++++++ panda/src/express/virtualFileSystem.h | 2 + 7 files changed, 77 insertions(+), 29 deletions(-) diff --git a/panda/src/audiotraits/fmodAudioSound.cxx b/panda/src/audiotraits/fmodAudioSound.cxx index aebf2e30c4..cfd1706ff6 100644 --- a/panda/src/audiotraits/fmodAudioSound.cxx +++ b/panda/src/audiotraits/fmodAudioSound.cxx @@ -272,7 +272,6 @@ unsigned long FmodAudioSound:: get_loop_count() const { FMOD_RESULT result; int loop_count; - unsigned long returnedNumber; result = _sound->getLoopCount( &loop_count ); fmod_audio_errcheck("_sound->getLoopCount()", result); diff --git a/panda/src/audiotraits/milesAudioManager.cxx b/panda/src/audiotraits/milesAudioManager.cxx index a6b83df86c..1e165e88ba 100644 --- a/panda/src/audiotraits/milesAudioManager.cxx +++ b/panda/src/audiotraits/milesAudioManager.cxx @@ -257,10 +257,15 @@ load(Filename file_name) { << "Unable to read " << file_name << "\n"; return NULL; } + if (sd->_raw_data.empty()) { + milesAudio_cat.warning() + << "File " << file_name << " is empty\n"; + return NULL; + } sd->_basename = file_name.get_basename(); sd->_file_type = - AIL_file_type(sd->_raw_data.data(), sd->_raw_data.size()); + AIL_file_type(&sd->_raw_data[0], sd->_raw_data.size()); bool expand_to_wav = false; @@ -279,24 +284,28 @@ load(Filename file_name) { // variable bit-rate MP3 encoding. void *wav_data; U32 wav_data_size; - if (AIL_decompress_ASI(sd->_raw_data.data(), sd->_raw_data.size(), + if (AIL_decompress_ASI(&sd->_raw_data[0], sd->_raw_data.size(), sd->_basename.c_str(), &wav_data, &wav_data_size, NULL)) { audio_debug("expanded " << sd->_basename << " from " << sd->_raw_data.size() << " bytes to " << wav_data_size << " bytes."); - // Now copy the memory into our own buffers, and free the - // Miles-allocated memory. - sd->_raw_data.assign((char *)wav_data, wav_data_size); + if (wav_data_size != 0) { + // Now copy the memory into our own buffers, and free the + // Miles-allocated memory. + sd->_raw_data.clear(); + sd->_raw_data.insert(sd->_raw_data.end(), + (unsigned char *)wav_data, (unsigned char *)wav_data + wav_data_size); + sd->_file_type = AILFILETYPE_PCM_WAV; + } AIL_mem_free_lock(wav_data); - sd->_file_type = AILFILETYPE_PCM_WAV; } else { audio_debug("unable to expand " << sd->_basename); } } - sd->_audio = AIL_quick_load_mem(sd->_raw_data.data(), sd->_raw_data.size()); + sd->_audio = AIL_quick_load_mem(&sd->_raw_data[0], sd->_raw_data.size()); if (!sd->_audio) { audio_error(" MilesAudioManager::load failed "<< AIL_last_error()); @@ -759,6 +768,7 @@ cleanup() { MilesAudioManager::SoundData:: SoundData() : _audio(0), + _raw_data(MilesAudioManager::get_class_type()), _has_length(false) { } @@ -789,7 +799,8 @@ get_length() { // Time to determine the length of the file. if (_file_type == AILFILETYPE_MPEG_L3_AUDIO && - (int)_raw_data.size() < miles_audio_calc_mp3_threshold) { + (int)_raw_data.size() < miles_audio_calc_mp3_threshold && + !_raw_data.empty()) { // If it's an mp3 file, we may not trust Miles to compute its // length correctly (Miles doesn't correctly compute the length // of VBR MP3 files). So in that case, decompress the whole @@ -798,7 +809,7 @@ get_length() { void *wav_data; U32 wav_data_size; - if (AIL_decompress_ASI(_raw_data.data(), _raw_data.size(), + if (AIL_decompress_ASI(&_raw_data[0], _raw_data.size(), _basename.c_str(), &wav_data, &wav_data_size, NULL)) { AILSOUNDINFO info; diff --git a/panda/src/audiotraits/milesAudioManager.h b/panda/src/audiotraits/milesAudioManager.h index 3bbc611f96..455bb226cc 100644 --- a/panda/src/audiotraits/milesAudioManager.h +++ b/panda/src/audiotraits/milesAudioManager.h @@ -28,6 +28,7 @@ #include "pset.h" #include "pmap.h" #include "pdeque.h" +#include "pvector.h" class MilesAudioSound; @@ -75,7 +76,7 @@ private: string _basename; HAUDIO _audio; S32 _file_type; - string _raw_data; + pvector _raw_data; bool _has_length; float _length; // in seconds. }; diff --git a/panda/src/express/virtualFile.cxx b/panda/src/express/virtualFile.cxx index b65bf7d41c..4e8d95890f 100644 --- a/panda/src/express/virtualFile.cxx +++ b/panda/src/express/virtualFile.cxx @@ -243,6 +243,29 @@ bool VirtualFile:: read_file(string &result, bool auto_unwrap) const { result = string(); + pvector pv; + if (!read_file(pv, auto_unwrap)) { + return false; + } + + if (!pv.empty()) { + result.append((const char *)&pv[0], pv.size()); + } + + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: VirtualFile::read_file +// Access: Public +// Description: Fills up the indicated pvector with the contents of +// the file, if it is a regular file. Returns true on +// success, false otherwise. +//////////////////////////////////////////////////////////////////// +bool VirtualFile:: +read_file(pvector &result, bool auto_unwrap) const { + result.clear(); + istream *in = open_read_file(auto_unwrap); if (in == (istream *)NULL) { express_cat.info() @@ -264,18 +287,13 @@ read_file(string &result, bool auto_unwrap) const { //////////////////////////////////////////////////////////////////// // Function: VirtualFile::read_file // Access: Public, Static -// Description: Fills up the indicated string with the contents of +// Description: Fills up the indicated pvector with the contents of // the just-opened file. Returns true on success, false -// otherwise. If the string was not empty on entry, the -// data read from the file will be concatenated onto it. +// otherwise. If the pvector was not empty on entry, the +// data read from the file will be appended onto it. //////////////////////////////////////////////////////////////////// bool VirtualFile:: -read_file(istream *in, string &result) { - // Repeatedly appending into a string seems to be prohibitively - // expensive on MSVC7's implementation of string, but the vector - // implementation works much better. Even still, it seems to be - // better to add the data in large chunks, rather than one byte at a - // time. +read_file(istream *in, pvector &result) { pvector result_vec; static const size_t buffer_size = 1024; @@ -285,11 +303,10 @@ read_file(istream *in, string &result) { size_t count = in->gcount(); while (count != 0) { thread_consider_yield(); - result_vec.insert(result_vec.end(), buffer, buffer + count); + result.insert(result.end(), buffer, buffer + count); in->read(buffer, buffer_size); count = in->gcount(); } - result.append(&result_vec[0], result_vec.size()); return (!in->fail() || in->eof()); } @@ -301,9 +318,7 @@ read_file(istream *in, string &result) { // only reads up to max_bytes bytes from the file. //////////////////////////////////////////////////////////////////// bool VirtualFile:: -read_file(istream *in, string &result, size_t max_bytes) { - pvector result_vec; - +read_file(istream *in, pvector &result, size_t max_bytes) { static const size_t buffer_size = 1024; char buffer[buffer_size]; @@ -312,12 +327,11 @@ read_file(istream *in, string &result, size_t max_bytes) { while (count != 0) { thread_consider_yield(); nassertr(count <= max_bytes, false); - result_vec.insert(result_vec.end(), buffer, buffer + count); + result.insert(result.end(), buffer, buffer + count); max_bytes -= count; in->read(buffer, min(buffer_size, max_bytes)); count = in->gcount(); } - result.append(&result_vec[0], result_vec.size()); return (!in->fail() || in->eof()); } diff --git a/panda/src/express/virtualFile.h b/panda/src/express/virtualFile.h index 404a2c1970..b16939d592 100644 --- a/panda/src/express/virtualFile.h +++ b/panda/src/express/virtualFile.h @@ -25,6 +25,7 @@ #include "pointerTo.h" #include "typedReferenceCount.h" #include "ordered_vector.h" +#include "pvector.h" class VirtualFileMount; class VirtualFileList; @@ -64,8 +65,9 @@ PUBLISHED: public: INLINE void set_original_filename(const Filename &filename); bool read_file(string &result, bool auto_unwrap) const; - static bool read_file(istream *stream, string &result); - static bool read_file(istream *stream, string &result, size_t max_bytes); + bool read_file(pvector &result, bool auto_unwrap) const; + static bool read_file(istream *stream, pvector &result); + static bool read_file(istream *stream, pvector &result, size_t max_bytes); protected: diff --git a/panda/src/express/virtualFileSystem.I b/panda/src/express/virtualFileSystem.I index 9f4b00c856..c54dfc0b73 100644 --- a/panda/src/express/virtualFileSystem.I +++ b/panda/src/express/virtualFileSystem.I @@ -156,3 +156,22 @@ read_file(const Filename &filename, string &result, bool auto_unwrap) const { PT(VirtualFile) file = get_file(filename); return (file != (VirtualFile *)NULL && file->read_file(result, auto_unwrap)); } + +//////////////////////////////////////////////////////////////////// +// Function: VirtualFileSystem::read_file +// Access: Public +// Description: Convenience function; fills the pvector up with the +// data from the indicated file, if it exists and can be +// read. Returns true on success, false otherwise. +// +// If auto_unwrap is true, an explicitly-named .pz file +// is automatically decompressed and the decompressed +// contents are returned. This is different than +// vfs-implicit-pz, which will automatically decompress +// a file if the extension .pz is *not* given. +//////////////////////////////////////////////////////////////////// +INLINE bool VirtualFileSystem:: +read_file(const Filename &filename, pvector &result, bool auto_unwrap) const { + PT(VirtualFile) file = get_file(filename); + return (file != (VirtualFile *)NULL && file->read_file(result, auto_unwrap)); +} diff --git a/panda/src/express/virtualFileSystem.h b/panda/src/express/virtualFileSystem.h index 42e0a9a582..985cf2bdb8 100644 --- a/panda/src/express/virtualFileSystem.h +++ b/panda/src/express/virtualFileSystem.h @@ -26,6 +26,7 @@ #include "dSearchPath.h" #include "pointerTo.h" #include "config_express.h" +#include "pvector.h" class Multifile; class VirtualFileMount; @@ -88,6 +89,7 @@ PUBLISHED: public: INLINE bool read_file(const Filename &filename, string &result, bool auto_unwrap) const; + INLINE bool read_file(const Filename &filename, pvector &result, bool auto_unwrap) const; void scan_mount_points(vector_string &names, const Filename &path) const;