haiku/headers/private/kernel/util/FixedWidthPointer.h
Alex Smith d8efc6caf6 Changes to kernel_args to make it identical for x86 and x86_64.
* Added a FixedWidthPointer template class which uses 64-bit storage to hold
  a pointer. This is used in place of raw pointers in kernel_args.
* Added __attribute__((packed)) to kernel_args and all structures contained
  within it. This is necessary due to different alignment behaviour for
  32-bit and 64-bit compilation with GCC.
* With these changes, kernel_args will now come out the same size for both
  the x86_64 kernel and the loader, excluding the preloaded_image structure
  which has not yet been changed.
* Tested both an x86 GCC2 and GCC4 build, no problems caused by these changes.
2012-06-21 18:02:23 +01:00

124 lines
1.9 KiB
C++

/*
* 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:
operator Type*() const
{
return (Type *)(addr_t)fValue;
}
operator addr_t() const
{
return (addr_t)fValue;
}
Type &operator*() const
{
return *(Type *)*this;
}
Type *operator->() const
{
return *this;
}
FixedWidthPointer &operator=(const FixedWidthPointer &p)
{
fValue = p.fValue;
return *this;
}
FixedWidthPointer &operator=(Type *p)
{
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;
}
private:
uint64 fValue;
} _PACKED;
// Specialization for void pointers, can be converted to another pointer type.
template<>
class FixedWidthPointer<void> {
public:
operator void*() const
{
return (void *)(addr_t)fValue;
}
template<typename OtherType>
operator OtherType*() const
{
return (OtherType *)(addr_t)fValue;
}
operator addr_t() const
{
return (addr_t)fValue;
}
FixedWidthPointer &operator=(const FixedWidthPointer &p)
{
fValue = p.fValue;
return *this;
}
FixedWidthPointer &operator=(void *p)
{
fValue = (addr_t)p;
return *this;
}
uint64 Get() const
{
return fValue;
}
void SetTo(uint64 addr)
{
fValue = addr;
}
private:
uint64 fValue;
} _PACKED;
#endif /* KERNEL_UTIL_FIXED_WIDTH_POINTER_H */