2003-06-11 02:33:44 +04:00
|
|
|
// KDiskDeviceUtils.h
|
|
|
|
|
|
|
|
#ifndef _K_DISK_DEVICE_UTILS_H
|
|
|
|
#define _K_DISK_DEVICE_UTILS_H
|
|
|
|
|
2003-06-23 03:00:47 +04:00
|
|
|
#include <stdlib.h>
|
2003-06-11 02:33:44 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
2005-01-30 19:09:57 +03:00
|
|
|
#include <util/AutoLock.h>
|
|
|
|
|
2005-11-13 02:27:14 +03:00
|
|
|
// instantiations
|
|
|
|
#include <KDiskDevice.h>
|
|
|
|
#include <KDiskDeviceManager.h>
|
|
|
|
#include <KDiskSystem.h>
|
|
|
|
#include <KPartition.h>
|
|
|
|
|
2003-06-11 02:33:44 +04:00
|
|
|
namespace BPrivate {
|
|
|
|
namespace DiskDevice {
|
|
|
|
|
|
|
|
// helper functions
|
|
|
|
|
2003-08-01 02:47:17 +04:00
|
|
|
// min
|
|
|
|
template<typename T>
|
|
|
|
static inline
|
|
|
|
const T&
|
|
|
|
min(const T &a, const T &b)
|
|
|
|
{
|
|
|
|
if (a < b)
|
|
|
|
return a;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// max
|
|
|
|
template<typename T>
|
|
|
|
static inline
|
|
|
|
const T&
|
|
|
|
max(const T &a, const T &b)
|
|
|
|
{
|
|
|
|
if (a < b)
|
|
|
|
return b;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2003-06-11 02:33:44 +04:00
|
|
|
// set_string
|
|
|
|
static inline
|
|
|
|
status_t
|
|
|
|
set_string(char *&location, const char *newValue)
|
|
|
|
{
|
|
|
|
// unset old value
|
|
|
|
if (location) {
|
|
|
|
free(location);
|
|
|
|
location = NULL;
|
|
|
|
}
|
|
|
|
// set new value
|
|
|
|
status_t error = B_OK;
|
|
|
|
if (newValue) {
|
|
|
|
location = strdup(newValue);
|
|
|
|
if (!location)
|
|
|
|
error = B_NO_MEMORY;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2003-08-02 03:59:56 +04:00
|
|
|
// compare_string
|
|
|
|
/*! \brief \c NULL aware strcmp().
|
|
|
|
|
|
|
|
\c NULL is considered the least of all strings. \c NULL equals \c NULL.
|
|
|
|
|
|
|
|
\param str1 First string.
|
|
|
|
\param str2 Second string.
|
|
|
|
\return A value less than 0, if \a str1 is less than \a str2,
|
|
|
|
0, if they are equal, or a value greater than 0, if
|
|
|
|
\a str1 is greater \a str2.
|
|
|
|
*/
|
|
|
|
static inline
|
|
|
|
int
|
|
|
|
compare_string(const char *str1, const char *str2)
|
|
|
|
{
|
|
|
|
if (str1 == NULL) {
|
|
|
|
if (str2 == NULL)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
} else if (str2 == NULL)
|
|
|
|
return -1;
|
|
|
|
return strcmp(str1, str2);
|
|
|
|
}
|
|
|
|
|
2003-06-11 02:33:44 +04:00
|
|
|
|
|
|
|
// locking
|
|
|
|
|
|
|
|
// instantiations
|
|
|
|
class KDiskDevice;
|
|
|
|
class KDiskDeviceManager;
|
2003-07-09 03:21:34 +04:00
|
|
|
class KDiskSystem;
|
2003-06-24 03:42:15 +04:00
|
|
|
class KPartition;
|
2003-09-30 01:42:33 +04:00
|
|
|
//typedef struct disk_device_data disk_device_data;
|
2003-06-11 02:33:44 +04:00
|
|
|
|
|
|
|
typedef AutoLocker<KDiskDevice, AutoLockerReadLocking<KDiskDevice> >
|
|
|
|
DeviceReadLocker;
|
|
|
|
typedef AutoLocker<KDiskDevice, AutoLockerWriteLocking<KDiskDevice> >
|
|
|
|
DeviceWriteLocker;
|
|
|
|
typedef AutoLocker<KDiskDeviceManager > ManagerLocker;
|
|
|
|
|
2003-06-24 03:42:15 +04:00
|
|
|
// AutoLockerPartitionRegistration
|
|
|
|
template<const int dummy = 0>
|
|
|
|
class AutoLockerPartitionRegistration {
|
|
|
|
public:
|
|
|
|
inline bool Lock(KPartition *partition)
|
|
|
|
{
|
2016-12-23 08:19:01 +03:00
|
|
|
if (partition == NULL)
|
|
|
|
return true;
|
2003-06-24 03:42:15 +04:00
|
|
|
partition->Register();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(KPartition *partition)
|
|
|
|
{
|
2016-12-23 08:19:01 +03:00
|
|
|
if (partition != NULL)
|
|
|
|
partition->Unregister();
|
2003-06-24 03:42:15 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
|
|
|
|
PartitionRegistrar;
|
|
|
|
|
2003-07-09 03:21:34 +04:00
|
|
|
// AutoLockerDiskSystemLoading
|
|
|
|
template<const int dummy = 0>
|
|
|
|
class AutoLockerDiskSystemLoading {
|
|
|
|
public:
|
|
|
|
inline bool Lock(KDiskSystem *diskSystem)
|
|
|
|
{
|
|
|
|
return (diskSystem->Load() == B_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(KDiskSystem *diskSystem)
|
|
|
|
{
|
|
|
|
diskSystem->Unload();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<KDiskSystem, AutoLockerDiskSystemLoading<> >
|
|
|
|
DiskSystemLoader;
|
|
|
|
|
2003-09-30 01:42:33 +04:00
|
|
|
/*
|
|
|
|
// AutoLockerDeviceStructReadLocker
|
|
|
|
template<const int dummy = 0>
|
|
|
|
class AutoLockerDeviceStructReadLocker {
|
|
|
|
public:
|
|
|
|
inline bool Lock(disk_device_data *device)
|
|
|
|
{
|
|
|
|
return read_lock_disk_device(device->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(disk_device_data *device)
|
|
|
|
{
|
|
|
|
read_unlock_disk_device(device->id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<disk_device_data, AutoLockerDeviceStructReadLocker<> >
|
|
|
|
DeviceStructReadLocker;
|
|
|
|
|
|
|
|
// AutoLockerDeviceStructWriteLocker
|
|
|
|
template<const int dummy = 0>
|
|
|
|
class AutoLockerDeviceStructWriteLocker {
|
|
|
|
public:
|
|
|
|
inline bool Lock(disk_device_data *device)
|
|
|
|
{
|
|
|
|
return write_lock_disk_device(device->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(disk_device_data *device)
|
|
|
|
{
|
|
|
|
write_unlock_disk_device(device->id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<disk_device_data, AutoLockerDeviceStructWriteLocker<> >
|
|
|
|
DeviceStructWriteLocker;
|
|
|
|
*/
|
|
|
|
|
2003-06-11 02:33:44 +04:00
|
|
|
} // namespace DiskDevice
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using BPrivate::DiskDevice::set_string;
|
|
|
|
using BPrivate::DiskDevice::DeviceReadLocker;
|
|
|
|
using BPrivate::DiskDevice::DeviceWriteLocker;
|
|
|
|
using BPrivate::DiskDevice::ManagerLocker;
|
2003-07-07 02:57:40 +04:00
|
|
|
using BPrivate::DiskDevice::PartitionRegistrar;
|
2003-07-09 03:21:34 +04:00
|
|
|
using BPrivate::DiskDevice::DiskSystemLoader;
|
2003-09-30 01:42:33 +04:00
|
|
|
//using BPrivate::DiskDevice::DeviceStructReadLocker;
|
|
|
|
//using BPrivate::DiskDevice::DeviceStructReadLocker;
|
2003-06-11 02:33:44 +04:00
|
|
|
|
|
|
|
#endif // _K_DISK_DEVICE_H
|