#ifndef _K_DISK_DEVICE_UTILS_H
#define _K_DISK_DEVICE_UTILS_H
#include <stdlib.h>
#include <string.h>
#include <SupportDefs.h>
#include <util/AutoLock.h>
#include <KDiskDevice.h>
#include <KDiskDeviceManager.h>
#include <KDiskSystem.h>
#include <KPartition.h>
namespace BPrivate {
namespace DiskDevice {
template<typename T>
static inline
const T&
min(const T &a, const T &b)
{
if (a < b)
return a;
return b;
}
template<typename T>
static inline
const T&
max(const T &a, const T &b)
{
if (a < b)
return b;
return a;
}
static inline
status_t
set_string(char *&location, const char *newValue)
{
if (location) {
free(location);
location = NULL;
}
status_t error = B_OK;
if (newValue) {
location = strdup(newValue);
if (!location)
error = B_NO_MEMORY;
}
return error;
}
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);
}
class KDiskDevice;
class KDiskDeviceManager;
class KDiskSystem;
class KPartition;
typedef AutoLocker<KDiskDevice, AutoLockerReadLocking<KDiskDevice> >
DeviceReadLocker;
typedef AutoLocker<KDiskDevice, AutoLockerWriteLocking<KDiskDevice> >
DeviceWriteLocker;
typedef AutoLocker<KDiskDeviceManager > ManagerLocker;
template<const int dummy = 0>
class AutoLockerPartitionRegistration {
public:
inline bool Lock(KPartition *partition)
{
if (partition == NULL)
return true;
partition->Register();
return true;
}
inline void Unlock(KPartition *partition)
{
if (partition != NULL)
partition->Unregister();
}
};
typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
PartitionRegistrar;
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;
}
}
using BPrivate::DiskDevice::set_string;
using BPrivate::DiskDevice::DeviceReadLocker;
using BPrivate::DiskDevice::DeviceWriteLocker;
using BPrivate::DiskDevice::ManagerLocker;
using BPrivate::DiskDevice::PartitionRegistrar;
using BPrivate::DiskDevice::DiskSystemLoader;
#endif