8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-02-01 06:43:03 +01:00
firebird-mirror/src/common/classes/Aligner.h

125 lines
2.8 KiB
C
Raw Normal View History

/*
* PROGRAM: JRD Access Method
* MODULE: Aligner.h
2008-12-05 01:56:15 +01:00
* DESCRIPTION: Aligner, OutAligner - templates to help
* with alignment on RISC machines.
* Should be used ONLY as temporary on-stack buffers!
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
*
* Software distributed under the License is distributed AS IS,
* WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights
* and limitations under the License.
*
* The Original Code was created by Alexander Peshkoff
* for the Firebird Open Source RDBMS project.
*
* Copyright (c) 2007 Alexander Peshkoff <peshkoff@mail.ru>
* and all contributors signed below.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#ifndef CLASSES_ALIGN_H
#define CLASSES_ALIGN_H
#include "../common/classes/array.h"
namespace Firebird {
// Aligns output parameter (i.e. transfers data in destructor).
template <typename C>
2008-04-19 13:11:10 +02:00
class OutAligner
{
private:
2009-09-10 08:04:54 +02:00
UCHAR* const userBuffer;
#ifdef RISC_ALIGNMENT
Firebird::HalfStaticArray<C, BUFFER_SMALL> localBuffer;
ULONG bSize;
C* bPointer;
#endif
public:
OutAligner(UCHAR* buf, ULONG len) : userBuffer(buf)
#ifdef RISC_ALIGNMENT
, bSize(len), bPointer(0)
2008-12-05 01:56:15 +01:00
#endif
{
fb_assert(len % sizeof(C) == 0);
#ifdef RISC_ALIGNMENT
2007-10-06 18:07:31 +02:00
if ((IPTR) userBuffer & (sizeof(C) - 1))
{
bPointer = localBuffer.getBuffer(len / sizeof(C) + (bSize % sizeof(C) ? 1 : 0));
}
#endif
}
2008-12-05 01:56:15 +01:00
operator C*()
{
#ifdef RISC_ALIGNMENT
2007-10-06 18:07:31 +02:00
return bPointer ? bPointer : reinterpret_cast<C*>(userBuffer);
#else
return reinterpret_cast<C*>(userBuffer);
#endif
2008-12-05 01:56:15 +01:00
}
2008-12-05 01:56:15 +01:00
~OutAligner()
{
#ifdef RISC_ALIGNMENT
if (bPointer)
{
memcpy(userBuffer, bPointer, bSize);
}
#endif
}
};
// Aligns input parameter.
template <typename C>
2008-04-19 13:11:10 +02:00
class Aligner
{
private:
#ifdef RISC_ALIGNMENT
Firebird::HalfStaticArray<C, BUFFER_SMALL> localBuffer;
#endif
const C* bPointer;
public:
Aligner(const UCHAR* buf, ULONG len)
{
fb_assert(len % sizeof(C) == 0);
#ifdef RISC_ALIGNMENT
2007-10-06 18:07:31 +02:00
if ((IPTR) buf & (sizeof(C) - 1))
{
C* tempPointer = localBuffer.getBuffer(len / sizeof(C) + (len % sizeof(C) ? 1 : 0));
memcpy(tempPointer, buf, len);
bPointer = tempPointer;
}
else
#endif
bPointer = reinterpret_cast<const C*>(buf);
}
2008-12-05 01:56:15 +01:00
operator const C*()
{
return bPointer;
2008-12-05 01:56:15 +01:00
}
};
// Aligns tail in *_rpt structures when later too active casts are used
#if defined(RISC_ALIGNMENT) && (SIZEOF_VOID_P < FB_DOUBLE_ALIGN)
#define RPT_ALIGN(rpt) union { rpt; SINT64 dummy; }
2009-05-23 03:33:36 +02:00
#else
#define RPT_ALIGN(rpt) rpt
#endif
2007-10-06 18:07:31 +02:00
} // namespace Firebird
#endif // CLASSES_ALIGN_H