2012-06-21 21:02:23 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef KERNEL_UTIL_FIXED_WIDTH_POINTER_H
|
|
|
|
#define KERNEL_UTIL_FIXED_WIDTH_POINTER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class FixedWidthPointer
|
|
|
|
\brief Pointer class with fixed size (64-bit) storage.
|
|
|
|
|
|
|
|
This class is a pointer-like class that uses a fixed size 64-bit storage.
|
|
|
|
This is used to make kernel_args compatible (i.e. the same size) for both
|
|
|
|
32-bit and 64-bit kernels.
|
|
|
|
*/
|
|
|
|
template<typename Type>
|
|
|
|
class FixedWidthPointer {
|
|
|
|
public:
|
2012-06-24 01:49:32 +04:00
|
|
|
Type * Pointer() const
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
2012-06-22 22:55:38 +04:00
|
|
|
return (Type*)(addr_t)fValue;
|
|
|
|
}
|
|
|
|
|
2012-06-24 01:49:32 +04:00
|
|
|
operator Type*() const
|
2012-06-22 22:55:38 +04:00
|
|
|
{
|
2012-06-24 01:49:32 +04:00
|
|
|
return Pointer();
|
2012-06-21 21:02:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-22 14:56:55 +04:00
|
|
|
Type& operator*() const
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
2012-06-24 01:49:32 +04:00
|
|
|
return *Pointer();
|
2012-06-21 21:02:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-22 14:56:55 +04:00
|
|
|
Type* operator->() const
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
2012-06-24 01:49:32 +04:00
|
|
|
return Pointer();
|
2012-06-22 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Type& operator[](size_t i) const
|
|
|
|
{
|
2012-06-24 01:49:32 +04:00
|
|
|
return Pointer()[i];
|
2012-06-21 21:02:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-22 14:56:55 +04:00
|
|
|
FixedWidthPointer& operator=(const FixedWidthPointer& p)
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
|
|
|
fValue = p.fValue;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-06-22 14:56:55 +04:00
|
|
|
FixedWidthPointer& operator=(Type* p)
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
|
|
|
fValue = (addr_t)p;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Get the 64-bit pointer value.
|
|
|
|
\return Pointer address.
|
|
|
|
*/
|
|
|
|
uint64 Get() const
|
|
|
|
{
|
|
|
|
return fValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Set the 64-bit pointer value.
|
|
|
|
\param addr New address for the pointer.
|
|
|
|
*/
|
|
|
|
void SetTo(uint64 addr)
|
|
|
|
{
|
|
|
|
fValue = addr;
|
|
|
|
}
|
2012-06-22 14:56:55 +04:00
|
|
|
|
2012-06-21 21:02:23 +04:00
|
|
|
private:
|
|
|
|
uint64 fValue;
|
|
|
|
} _PACKED;
|
|
|
|
|
|
|
|
|
|
|
|
// Specialization for void pointers, can be converted to another pointer type.
|
|
|
|
template<>
|
|
|
|
class FixedWidthPointer<void> {
|
|
|
|
public:
|
2012-06-24 01:49:32 +04:00
|
|
|
void * Pointer() const
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
2012-06-22 14:56:55 +04:00
|
|
|
return (void*)(addr_t)fValue;
|
2012-06-21 21:02:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-24 01:49:32 +04:00
|
|
|
operator void*() const
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
2012-06-24 01:49:32 +04:00
|
|
|
return Pointer();
|
2012-06-21 21:02:23 +04:00
|
|
|
}
|
|
|
|
|
2012-06-22 14:56:55 +04:00
|
|
|
FixedWidthPointer& operator=(const FixedWidthPointer& p)
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
|
|
|
fValue = p.fValue;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-06-22 14:56:55 +04:00
|
|
|
FixedWidthPointer& operator=(void* p)
|
2012-06-21 21:02:23 +04:00
|
|
|
{
|
|
|
|
fValue = (addr_t)p;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 Get() const
|
|
|
|
{
|
|
|
|
return fValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetTo(uint64 addr)
|
|
|
|
{
|
|
|
|
fValue = addr;
|
|
|
|
}
|
2012-06-22 14:56:55 +04:00
|
|
|
|
2012-06-21 21:02:23 +04:00
|
|
|
private:
|
|
|
|
uint64 fValue;
|
|
|
|
} _PACKED;
|
|
|
|
|
2012-06-24 01:49:32 +04:00
|
|
|
|
2012-06-22 19:56:56 +04:00
|
|
|
template<typename Type>
|
|
|
|
inline bool
|
2012-06-24 01:49:32 +04:00
|
|
|
operator==(const FixedWidthPointer<Type>& a, const Type* b)
|
2012-06-22 19:56:56 +04:00
|
|
|
{
|
|
|
|
return a.Get() == (addr_t)b;
|
|
|
|
}
|
|
|
|
|
2012-06-24 01:49:32 +04:00
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline bool
|
|
|
|
operator!=(const FixedWidthPointer<Type>& a, const Type* b)
|
|
|
|
{
|
|
|
|
return a.Get() != (addr_t)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline bool
|
|
|
|
operator==(const FixedWidthPointer<Type>& a, Type* b)
|
|
|
|
{
|
|
|
|
return a.Get() == (addr_t)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-22 19:56:56 +04:00
|
|
|
template<typename Type>
|
|
|
|
inline bool
|
2012-06-24 01:49:32 +04:00
|
|
|
operator!=(const FixedWidthPointer<Type>& a, Type* b)
|
2012-06-22 19:56:56 +04:00
|
|
|
{
|
|
|
|
return a.Get() != (addr_t)b;
|
|
|
|
}
|
|
|
|
|
2012-06-21 21:02:23 +04:00
|
|
|
|
|
|
|
#endif /* KERNEL_UTIL_FIXED_WIDTH_POINTER_H */
|