Switches to GCC's internal swaps for swap32 and swap64 in OSX. Changes CDIO from using char*** to std::vector<std::string>, which fixes a memory leak I was noticing and also makes it look cleaner. This is not tested much in Windows/Linux, please see if it compiles and doesn't fail out in some mysterious way

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5067 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Sonicadvance1 2010-02-16 08:46:21 +00:00
parent 7c76d51c67
commit 215f1f74be
9 changed files with 51 additions and 92 deletions

View File

@ -52,54 +52,6 @@ void cdio_follow_symlink(const char * src, char * dst) {
#endif #endif
} }
// Add/allocate a drive to the end of drives.
// Use cdio_free_device_list() to free this device_list.
void cdio_add_device_list(char **device_list[], const char *drive,
unsigned int *num_drives) {
if (NULL != drive) {
unsigned int j;
char real_device_1[PATH_MAX];
char real_device_2[PATH_MAX];
cdio_follow_symlink(drive, real_device_1);
/* Check if drive is already in list. */
for (j=0; j<*num_drives; j++) {
cdio_follow_symlink((*device_list)[j], real_device_2);
if (strcmp(real_device_1, real_device_2) == 0) break;
}
if (j==*num_drives) {
/* Drive not in list. Add it. */
(*num_drives)++;
*device_list = (char **)realloc(*device_list, (*num_drives) * sizeof(char *));
(*device_list)[*num_drives-1] = __strdup(drive);
}
} else {
(*num_drives)++;
if (*device_list) {
*device_list = (char **)realloc(*device_list, (*num_drives) * sizeof(char *));
} else {
*device_list = (char **)malloc((*num_drives) * sizeof(char *));
}
(*device_list)[*num_drives-1] = NULL;
}
}
// Free device list returned by cdio_get_devices or
// cdio_get_devices_with_cap.
void cdio_free_device_list(char * ppsz_device_list[]) {
char **ppsz_device_list_save=ppsz_device_list;
if (!ppsz_device_list) return;
for ( ; NULL != *ppsz_device_list ; ppsz_device_list++ ) {
free(*ppsz_device_list);
*ppsz_device_list = NULL;
}
free(ppsz_device_list_save);
}
#ifdef _WIN32 #ifdef _WIN32
// Returns a string that can be used in a CreateFile call if // Returns a string that can be used in a CreateFile call if
// c_drive letter is a character. If not NULL is returned. // c_drive letter is a character. If not NULL is returned.
@ -128,9 +80,8 @@ const char *is_cdrom_win32(const char c_drive_letter) {
} }
// Returns a pointer to an array of strings with the device names // Returns a pointer to an array of strings with the device names
char ** cdio_get_devices_win32() { std::vector<std::string> cdio_get_devices_win32() {
char **drives = NULL; std::vector<std::string> drives;
unsigned int num_drives=0;
char drive_letter; char drive_letter;
// Scan the system for CD-ROM drives. // Scan the system for CD-ROM drives.
@ -138,11 +89,10 @@ char ** cdio_get_devices_win32() {
for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) { for (drive_letter='A'; drive_letter <= 'Z'; drive_letter++) {
const char *drive_str=is_cdrom_win32(drive_letter); const char *drive_str=is_cdrom_win32(drive_letter);
if (drive_str != NULL) { if (drive_str != NULL) {
cdio_add_device_list(&drives, drive_str, &num_drives); drives.push_back(drive_str);
delete drive_str; delete drive_str;
} }
} }
cdio_add_device_list(&drives, NULL, &num_drives);
return drives; return drives;
} }
#endif // WIN32 #endif // WIN32
@ -153,23 +103,22 @@ char ** cdio_get_devices_win32() {
/* /*
Returns a pointer to an array of strings with the device names Returns a pointer to an array of strings with the device names
*/ */
char **cdio_get_devices_osx(void) { std::vector<std::string> cdio_get_devices_osx(void) {
io_object_t next_media; io_object_t next_media;
mach_port_t master_port; mach_port_t master_port;
kern_return_t kern_result; kern_return_t kern_result;
io_iterator_t media_iterator; io_iterator_t media_iterator;
CFMutableDictionaryRef classes_to_match; CFMutableDictionaryRef classes_to_match;
char **drives = NULL; std::vector<std::string> drives;
unsigned int num_drives=0;
kern_result = IOMasterPort( MACH_PORT_NULL, &master_port ); kern_result = IOMasterPort( MACH_PORT_NULL, &master_port );
if( kern_result != KERN_SUCCESS ) { if( kern_result != KERN_SUCCESS ) {
return( NULL ); return( drives );
} }
classes_to_match = IOServiceMatching( kIOCDMediaClass ); classes_to_match = IOServiceMatching( kIOCDMediaClass );
if( classes_to_match == NULL ) { if( classes_to_match == NULL ) {
return( NULL ); return( drives );
} }
CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey), CFDictionarySetValue( classes_to_match, CFSTR(kIOMediaEjectableKey),
@ -179,7 +128,7 @@ char **cdio_get_devices_osx(void) {
classes_to_match, classes_to_match,
&media_iterator ); &media_iterator );
if( kern_result != KERN_SUCCESS) { if( kern_result != KERN_SUCCESS) {
return( NULL ); return( drives );
} }
next_media = IOIteratorNext( media_iterator ); next_media = IOIteratorNext( media_iterator );
@ -209,7 +158,11 @@ char **cdio_get_devices_osx(void) {
(char*)&psz_buf + dev_path_length, (char*)&psz_buf + dev_path_length,
sizeof(psz_buf) - dev_path_length, sizeof(psz_buf) - dev_path_length,
kCFStringEncodingASCII)) { kCFStringEncodingASCII)) {
cdio_add_device_list(&drives, strdup(psz_buf), &num_drives); if(psz_buf != NULL)
{
std::string str = psz_buf;
drives.push_back(str);
}
} }
CFRelease( str_bsd_path ); CFRelease( str_bsd_path );
IOObjectRelease( next_media ); IOObjectRelease( next_media );
@ -217,7 +170,6 @@ char **cdio_get_devices_osx(void) {
} while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 ); } while( ( next_media = IOIteratorNext( media_iterator ) ) != 0 );
} }
IOObjectRelease( media_iterator ); IOObjectRelease( media_iterator );
cdio_add_device_list(&drives, NULL, &num_drives);
return drives; return drives;
} }
#endif #endif
@ -355,31 +307,33 @@ static char *check_mounts_linux(const char *mtab)
} }
// Returns a pointer to an array of strings with the device names // Returns a pointer to an array of strings with the device names
char **cdio_get_devices_linux () { std::vector<std::string> cdio_get_devices_linux () {
unsigned int i; unsigned int i;
char drive[40]; char drive[40];
char *ret_drive; char *ret_drive;
char **drives = NULL; std::vector<std::string> drives;
unsigned int num_drives=0;
// Scan the system for CD-ROM drives. // Scan the system for CD-ROM drives.
for ( i=0; strlen(checklist1[i]) > 0; ++i ) { for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
sprintf(drive, "/dev/%s", checklist1[i]); sprintf(drive, "/dev/%s", checklist1[i]);
if ( is_cdrom_linux(drive, NULL) > 0 ) { if ( is_cdrom_linux(drive, NULL) > 0 ) {
cdio_add_device_list(&drives, drive, &num_drives); std::string str = drive;
drives.push_back(str);
} }
} }
/* Now check the currently mounted CD drives */ /* Now check the currently mounted CD drives */
if (NULL != (ret_drive = check_mounts_linux("/etc/mtab"))) { if (NULL != (ret_drive = check_mounts_linux("/etc/mtab"))) {
cdio_add_device_list(&drives, ret_drive, &num_drives); std::string str = ret_drive;
drives.push_back(str);
free(ret_drive); free(ret_drive);
} }
/* Finally check possible mountable drives in /etc/fstab */ /* Finally check possible mountable drives in /etc/fstab */
if (NULL != (ret_drive = check_mounts_linux("/etc/fstab"))) { if (NULL != (ret_drive = check_mounts_linux("/etc/fstab"))) {
cdio_add_device_list(&drives, ret_drive, &num_drives); std::string str = ret_drive;
drives.push_back(str);
free(ret_drive); free(ret_drive);
} }
@ -390,17 +344,17 @@ char **cdio_get_devices_linux () {
for ( j=checklist2[i].num_min; j<=checklist2[i].num_max; ++j ) { for ( j=checklist2[i].num_min; j<=checklist2[i].num_max; ++j ) {
sprintf(drive, checklist2[i].format, j); sprintf(drive, checklist2[i].format, j);
if ( (is_cdrom_linux(drive, NULL)) > 0 ) { if ( (is_cdrom_linux(drive, NULL)) > 0 ) {
cdio_add_device_list(&drives, drive, &num_drives); std::string str = drive;
drives.push_back(str);
} }
} }
} }
cdio_add_device_list(&drives, NULL, &num_drives);
return drives; return drives;
} }
#endif #endif
// Returns a pointer to an array of strings with the device names // Returns a pointer to an array of strings with the device names
char **cdio_get_devices() { std::vector<std::string> cdio_get_devices() {
#ifdef _WIN32 #ifdef _WIN32
return cdio_get_devices_win32(); return cdio_get_devices_win32();
#elif __APPLE__ #elif __APPLE__
@ -416,17 +370,17 @@ char **cdio_get_devices() {
// Returns true if device is cdrom/dvd // Returns true if device is cdrom/dvd
bool cdio_is_cdrom(const char *device) { bool cdio_is_cdrom(std::string device) {
char **devices = cdio_get_devices(); std::vector<std::string> devices = cdio_get_devices();
bool res = false; bool res = false;
for (int i = 0; devices[i] != NULL; i++) { for (int i = 0; i < devices.size(); i++) {
if (strncmp(devices[i], device, PATH_MAX) == 0) { if (strncmp(devices[i].c_str(), device.c_str(), PATH_MAX) == 0) {
res = true; res = true;
break; break;
} }
} }
cdio_free_device_list(devices); devices.clear();
return res; return res;
} }

View File

@ -2,17 +2,14 @@
#define _CDUTILS_H_ #define _CDUTILS_H_
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <vector>
#include <string>
#include <stdio.h> #include <stdio.h>
// Returns a pointer to an array of strings with the device names // Returns a pointer to an array of strings with the device names
char **cdio_get_devices(); std::vector<std::string> cdio_get_devices();
// Free device list returned by cdio_get_devices or
// cdio_get_devices_with_cap.
void cdio_free_device_list(char * ppsz_device_list[]);
// Returns true if device is cdrom/dvd // Returns true if device is cdrom/dvd
bool cdio_is_cdrom(const char *device); bool cdio_is_cdrom(std::string device);
#endif // _CDUTILS_H_ #endif // _CDUTILS_H_

View File

@ -120,6 +120,10 @@ inline u64 swap64(u64 _data) {return _byteswap_uint64(_data);}
inline u16 swap16(u16 _data) {return bswap_16(_data);} inline u16 swap16(u16 _data) {return bswap_16(_data);}
inline u32 swap32(u32 _data) {return bswap_32(_data);} inline u32 swap32(u32 _data) {return bswap_32(_data);}
inline u64 swap64(u64 _data) {return bswap_64(_data);} inline u64 swap64(u64 _data) {return bswap_64(_data);}
#elif __APPLE__
inline u16 swap16(u16 _data) {return (_data >> 8) | (_data << 8);}
inline u32 swap32(u32 _data) {return __builtin_bswap32(_data);}
inline u64 swap64(u64 _data) {return __builtin_bswap64(_data);}
#else #else
// Slow generic implementation. // Slow generic implementation.
inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);} inline u16 swap16(u16 data) {return (data >> 8) | (data << 8);}

View File

@ -86,7 +86,7 @@ bool SCoreStartupParameter::AutoSetup(EBootBS2 _BootBS2)
{ {
case BOOT_DEFAULT: case BOOT_DEFAULT:
{ {
bool bootDrive = cdio_is_cdrom(m_strFilename.c_str()); bool bootDrive = cdio_is_cdrom(m_strFilename);
// Check if the file exist, we may have gotten it from a --elf command line // Check if the file exist, we may have gotten it from a --elf command line
// that gave an incorrect file name // that gave an incorrect file name
if (!bootDrive && !File::Exists(m_strFilename.c_str())) if (!bootDrive && !File::Exists(m_strFilename.c_str()))

View File

@ -121,7 +121,7 @@ bool SectorReader::ReadMultipleAlignedBlocks(u64 block_num, u64 num_blocks, u8 *
IBlobReader* CreateBlobReader(const char* filename) IBlobReader* CreateBlobReader(const char* filename)
{ {
if (cdio_is_cdrom(filename)) if (cdio_is_cdrom(std::string(filename)))
return DriveReader::Create(filename); return DriveReader::Create(filename);
if (!File::Exists(filename)) if (!File::Exists(filename))

View File

@ -510,7 +510,7 @@ CFrame::~CFrame()
{ {
m_bControlsCreated = false; m_bControlsCreated = false;
cdio_free_device_list(drives); drives.clear();
/* The statbar sample has this so I add this to, but I guess timer will be deleted after /* The statbar sample has this so I add this to, but I guess timer will be deleted after
this anyway */ this anyway */
#if wxUSE_TIMER #if wxUSE_TIMER

View File

@ -29,6 +29,8 @@
#define id toolid #define id toolid
#endif #endif
#include <wx/aui/aui.h> #include <wx/aui/aui.h>
#include <string>
#include <vector>
#include "CDUtils.h" #include "CDUtils.h"
#include "CodeWindow.h" #include "CodeWindow.h"
@ -202,7 +204,7 @@ class CFrame : public wxFrame
char newDiscpath[2048]; char newDiscpath[2048];
wxMessageDialog *m_StopDlg; wxMessageDialog *m_StopDlg;
char **drives; std::vector<std::string> drives;
enum EToolbar enum EToolbar
{ {

View File

@ -115,8 +115,9 @@ void CFrame::CreateMenu()
m_pSubMenuDrive = fileMenu->AppendSubMenu(externalDrive, _T("&Boot from DVD Drive...")); m_pSubMenuDrive = fileMenu->AppendSubMenu(externalDrive, _T("&Boot from DVD Drive..."));
drives = cdio_get_devices(); drives = cdio_get_devices();
for (int i = 0; drives[i] != NULL && i < 24; i++) { // Windows Limitation of 24 character drives
externalDrive->Append(IDM_DRIVE1 + i, wxString::FromAscii(drives[i])); for (int i = 0; i < drives.size() && i < 24; i++) {
externalDrive->Append(IDM_DRIVE1 + i, wxString::FromAscii(drives[i].c_str()));
} }
fileMenu->AppendSeparator(); fileMenu->AppendSeparator();
@ -666,7 +667,7 @@ void CFrame::StartGame(const std::string& filename)
void CFrame::OnBootDrive(wxCommandEvent& event) void CFrame::OnBootDrive(wxCommandEvent& event)
{ {
BootManager::BootCore(drives[event.GetId()-IDM_DRIVE1]); BootManager::BootCore(drives[event.GetId()-IDM_DRIVE1].c_str());
} }

View File

@ -559,11 +559,12 @@ void CGameListCtrl::ScanForISOs()
if (SConfig::GetInstance().m_ListDrives) if (SConfig::GetInstance().m_ListDrives)
{ {
char **drives = cdio_get_devices(); std::vector<std::string> drives = cdio_get_devices();
GameListItem * Drive[24]; GameListItem * Drive[24];
for (int i = 0; drives[i] != NULL && i < 24; i++) // Another silly Windows limitation of 24 drive letters
for (int i = 0; i < drives.size() != NULL && i < 24; i++)
{ {
Drive[i] = new GameListItem(drives[i]); Drive[i] = new GameListItem(drives[i].c_str());
if (Drive[i]->IsValid()) m_ISOFiles.push_back(*Drive[i]); if (Drive[i]->IsValid()) m_ISOFiles.push_back(*Drive[i]);
} }
} }