Bug 904110 - Move alignment features out of Util.h into a new header; r=Waldo

This commit is contained in:
Ms2ger 2013-08-14 09:00:52 +02:00
parent bce1d40751
commit 9d023a67df
22 changed files with 169 additions and 129 deletions

View File

@ -73,6 +73,7 @@
#include "jsapi.h"
#include "jsfriendapi.h"
#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/dom/ContentParent.h"

View File

@ -11,6 +11,7 @@
#include "jsfriendapi.h"
#include "jswrapper.h"
#include "mozilla/Alignment.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/CallbackObject.h"
#include "mozilla/dom/DOMJSClass.h"
@ -20,6 +21,7 @@
#include "mozilla/dom/workers/Workers.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/Likely.h"
#include "mozilla/Util.h"
#include "nsCycleCollector.h"
#include "nsIXPConnect.h"
#include "nsTraceRefcnt.h"

View File

@ -8,6 +8,7 @@
#endif
#include <math.h>
#include "mozilla/Alignment.h"
#include "mozilla/Constants.h"
#include "cairo.h"

View File

@ -12,8 +12,8 @@
#include "gfxMatrix.h"
#include "nsISupportsImpl.h"
#include "nsAutoPtr.h"
#include "mozilla/Alignment.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/Util.h"
class gfxContext;
class gfxASurface;

View File

@ -7,6 +7,7 @@
#define MOZILLA_IMAGELIB_CLIPPEDIMAGE_H_
#include "ImageWrapper.h"
#include "mozilla/Util.h" // for Maybe
namespace mozilla {
namespace image {

View File

@ -4,9 +4,9 @@
#include "base/pickle.h"
#include "mozilla/Alignment.h"
#include "mozilla/Endian.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Util.h"
#include <stdlib.h>

View File

@ -7,6 +7,7 @@
#ifndef js_HashTable_h
#define js_HashTable_h
#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/Casting.h"
@ -17,7 +18,6 @@
#include "mozilla/ReentrancyGuard.h"
#include "mozilla/TemplateLib.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Util.h"
#include "js/Utility.h"

View File

@ -9,6 +9,8 @@
#ifdef JS_ION
#include "mozilla/Alignment.h"
#include "jit/BaselineFrame.h"
#include "jit/BaselineJIT.h"
#include "jit/BaselineRegisters.h"

View File

@ -7,6 +7,7 @@
#ifndef jit_RegisterSets_h
#define jit_RegisterSets_h
#include "mozilla/Alignment.h"
#include "mozilla/MathAlgorithms.h"
#include "jit/IonAllocPolicy.h"

View File

@ -7,6 +7,9 @@
#ifndef jit_shared_CodeGenerator_shared_h
#define jit_shared_CodeGenerator_shared_h
#include "mozilla/Alignment.h"
#include "mozilla/Util.h"
#include "jit/IonCaches.h"
#include "jit/IonFrames.h"
#include "jit/IonMacroAssembler.h"

View File

@ -6,6 +6,7 @@
#include "vm/TypedArrayObject.h"
#include "mozilla/Alignment.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/PodOperations.h"

View File

@ -75,9 +75,14 @@
#ifndef xpcprivate_h___
#define xpcprivate_h___
#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/CycleCollectedJSRuntime.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Mutex.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/Util.h"
#include <math.h>
@ -100,7 +105,6 @@
#include "nsAutoPtr.h"
#include "nsCycleCollectionParticipant.h"
#include "nsCycleCollectorUtils.h"
#include "mozilla/CycleCollectedJSRuntime.h"
#include "nsDebug.h"
#include "nsISupports.h"
#include "nsIServiceManager.h"
@ -132,9 +136,6 @@
#include "nsAutoJSValHolder.h"
#include "js/HashTable.h"
#include "mozilla/GuardObjects.h"
#include "mozilla/ReentrantMonitor.h"
#include "mozilla/Mutex.h"
#include "nsThreadUtils.h"
#include "nsIJSEngineTelemetryStats.h"

137
mfbt/Alignment.h Normal file
View File

@ -0,0 +1,137 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Functionality related to memory alignment. */
#ifndef mozilla_Alignment_h
#define mozilla_Alignment_h
#include <stddef.h>
#include <stdint.h>
namespace mozilla {
/*
* This class, and the corresponding macro MOZ_ALIGNOF, figures out how many
* bytes of alignment a given type needs.
*/
template<typename T>
class AlignmentFinder
{
struct Aligner
{
char c;
T t;
};
public:
static const size_t alignment = sizeof(Aligner) - sizeof(T);
};
#define MOZ_ALIGNOF(T) mozilla::AlignmentFinder<T>::alignment
/*
* Declare the MOZ_ALIGNED_DECL macro for declaring aligned types.
*
* For instance,
*
* MOZ_ALIGNED_DECL(char arr[2], 8);
*
* will declare a two-character array |arr| aligned to 8 bytes.
*/
#if defined(__GNUC__)
# define MOZ_ALIGNED_DECL(_type, _align) \
_type __attribute__((aligned(_align)))
#elif defined(_MSC_VER)
# define MOZ_ALIGNED_DECL(_type, _align) \
__declspec(align(_align)) _type
#else
# warning "We don't know how to align variables on this compiler."
# define MOZ_ALIGNED_DECL(_type, _align) _type
#endif
/*
* AlignedElem<N> is a structure whose alignment is guaranteed to be at least N
* bytes.
*
* We support 1, 2, 4, 8, and 16-bit alignment.
*/
template<size_t Align>
struct AlignedElem;
/*
* We have to specialize this template because GCC doesn't like __attribute__((aligned(foo))) where
* foo is a template parameter.
*/
template<>
struct AlignedElem<1>
{
MOZ_ALIGNED_DECL(uint8_t elem, 1);
};
template<>
struct AlignedElem<2>
{
MOZ_ALIGNED_DECL(uint8_t elem, 2);
};
template<>
struct AlignedElem<4>
{
MOZ_ALIGNED_DECL(uint8_t elem, 4);
};
template<>
struct AlignedElem<8>
{
MOZ_ALIGNED_DECL(uint8_t elem, 8);
};
template<>
struct AlignedElem<16>
{
MOZ_ALIGNED_DECL(uint8_t elem, 16);
};
/*
* This utility pales in comparison to Boost's aligned_storage. The utility
* simply assumes that uint64_t is enough alignment for anyone. This may need
* to be extended one day...
*
* As an important side effect, pulling the storage into this template is
* enough obfuscation to confuse gcc's strict-aliasing analysis into not giving
* false negatives when we cast from the char buffer to whatever type we've
* constructed using the bytes.
*/
template<size_t Nbytes>
struct AlignedStorage
{
union U {
char bytes[Nbytes];
uint64_t _;
} u;
const void* addr() const { return u.bytes; }
void* addr() { return u.bytes; }
};
template<typename T>
struct AlignedStorage2
{
union U {
char bytes[sizeof(T)];
uint64_t _;
} u;
const T* addr() const { return reinterpret_cast<const T*>(u.bytes); }
T* addr() { return static_cast<T*>(static_cast<void*>(u.bytes)); }
};
} /* namespace mozilla */
#endif /* mozilla_Alignment_h */

View File

@ -18,126 +18,10 @@
#ifdef __cplusplus
#include "mozilla/Alignment.h"
namespace mozilla {
/*
* This class, and the corresponding macro MOZ_ALIGNOF, figure out how many
* bytes of alignment a given type needs.
*/
template<class T>
class AlignmentFinder
{
struct Aligner
{
char c;
T t;
};
public:
static const size_t alignment = sizeof(Aligner) - sizeof(T);
};
#define MOZ_ALIGNOF(T) mozilla::AlignmentFinder<T>::alignment
/*
* Declare the MOZ_ALIGNED_DECL macro for declaring aligned types.
*
* For instance,
*
* MOZ_ALIGNED_DECL(char arr[2], 8);
*
* will declare a two-character array |arr| aligned to 8 bytes.
*/
#if defined(__GNUC__)
# define MOZ_ALIGNED_DECL(_type, _align) \
_type __attribute__((aligned(_align)))
#elif defined(_MSC_VER)
# define MOZ_ALIGNED_DECL(_type, _align) \
__declspec(align(_align)) _type
#else
# warning "We don't know how to align variables on this compiler."
# define MOZ_ALIGNED_DECL(_type, _align) _type
#endif
/*
* AlignedElem<N> is a structure whose alignment is guaranteed to be at least N
* bytes.
*
* We support 1, 2, 4, 8, and 16-bit alignment.
*/
template<size_t align>
struct AlignedElem;
/*
* We have to specialize this template because GCC doesn't like __attribute__((aligned(foo))) where
* foo is a template parameter.
*/
template<>
struct AlignedElem<1>
{
MOZ_ALIGNED_DECL(uint8_t elem, 1);
};
template<>
struct AlignedElem<2>
{
MOZ_ALIGNED_DECL(uint8_t elem, 2);
};
template<>
struct AlignedElem<4>
{
MOZ_ALIGNED_DECL(uint8_t elem, 4);
};
template<>
struct AlignedElem<8>
{
MOZ_ALIGNED_DECL(uint8_t elem, 8);
};
template<>
struct AlignedElem<16>
{
MOZ_ALIGNED_DECL(uint8_t elem, 16);
};
/*
* This utility pales in comparison to Boost's aligned_storage. The utility
* simply assumes that uint64_t is enough alignment for anyone. This may need
* to be extended one day...
*
* As an important side effect, pulling the storage into this template is
* enough obfuscation to confuse gcc's strict-aliasing analysis into not giving
* false negatives when we cast from the char buffer to whatever type we've
* constructed using the bytes.
*/
template<size_t nbytes>
struct AlignedStorage
{
union U {
char bytes[nbytes];
uint64_t _;
} u;
const void* addr() const { return u.bytes; }
void* addr() { return u.bytes; }
};
template<class T>
struct AlignedStorage2
{
union U {
char bytes[sizeof(T)];
uint64_t _;
} u;
const T* addr() const { return reinterpret_cast<const T*>(u.bytes); }
T* addr() { return static_cast<T*>(static_cast<void*>(u.bytes)); }
};
/*
* Small utility for lazily constructing objects without using dynamic storage.
* When a Maybe<T> is constructed, it is |empty()|, i.e., no value of T has

View File

@ -9,6 +9,7 @@
#ifndef mozilla_Vector_h
#define mozilla_Vector_h
#include "mozilla/Alignment.h"
#include "mozilla/AllocPolicy.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
@ -19,7 +20,7 @@
#include "mozilla/ReentrancyGuard.h"
#include "mozilla/TemplateLib.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Util.h"
#include "mozilla/Util.h" // for PointerRangeSize
#include <new> // for placement new

View File

@ -9,6 +9,7 @@
EXPORTS_NAMESPACES += mozilla
EXPORTS_mozilla += \
Alignment.h \
AllocPolicy.h \
Array.h \
Assertions.h \

View File

@ -9,7 +9,8 @@
#include <time.h>
#include <unistd.h>
#include <android/log.h>
#include <mozilla/Util.h>
#include "mozilla/Alignment.h"
#include <vector>

View File

@ -6,6 +6,7 @@
#if !defined(nsMediaFragmentURIParser_h__)
#define nsMediaFragmentURIParser_h__
#include "mozilla/Util.h" // Maybe
#include "nsIURI.h"
#include "nsString.h"
#include "nsRect.h"

View File

@ -8,6 +8,7 @@
#include "nsString.h"
#include "nsCOMArray.h"
#include "mozilla/Attributes.h"
#include "mozilla/Util.h" // ArrayLength
#ifndef __nsWifiAccessPoint__
#define __nsWifiAccessPoint__

View File

@ -5,6 +5,7 @@
#include "JSCustomObjectBuilder.h"
#include "mozilla/Util.h" // for ArrayLength
#include "nsDataHashtable.h"
#include "nsStringGlue.h"
#include "nsTArray.h"

View File

@ -5,9 +5,9 @@
#ifndef nsIClassInfoImpl_h__
#define nsIClassInfoImpl_h__
#include "mozilla/Alignment.h"
#include "nsIClassInfo.h"
#include "nsISupportsImpl.h"
#include "mozilla/Util.h"
#include <new>

View File

@ -8,10 +8,10 @@
#define nsTArray_h__
#include "nsTArrayForwardDeclare.h"
#include "mozilla/Alignment.h"
#include "mozilla/Assertions.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Util.h"
#include <string.h>