[apps] Use attribute "packed" on member variable directly instead of

using "pragma pack" to allow compiler optimization on one-byte object
accesses
This commit is contained in:
Émilie Feral
2019-09-04 14:56:31 +02:00
parent bfe887e627
commit f388fe7252
5 changed files with 11 additions and 20 deletions

View File

@@ -16,7 +16,7 @@ class CalculationStore;
* |m_displayOutput| m_height |m_expandedHeight|m_equalSign|m_inputText|m_exactOuputText|m_approximateOuputText|
*
* */
#pragma pack(push,1)
class Calculation {
public:
enum class EqualSign : uint8_t {
@@ -77,12 +77,11 @@ private:
* by user (of maximum length TextField::maxBufferSize()) because when we
* print an expression we add omitted signs (multiplications, parenthesis...) */
DisplayOutput m_displayOutput;
KDCoordinate m_height;
KDCoordinate m_expandedHeight;
KDCoordinate m_height __attribute__((packed));
KDCoordinate m_expandedHeight __attribute__((packed));
EqualSign m_equalSign;
char m_inputText[0]; // MUST be the last member variable
};
#pragma pack(pop)
}

View File

@@ -72,7 +72,6 @@ private:
/* SequenceRecordDataBuffer is the layout of the data buffer of Record
* representing a Sequence. See comment in
* Shared::Function::FunctionRecordDataBuffer about packing. */
#pragma pack(push,1)
class SequenceRecordDataBuffer : public FunctionRecordDataBuffer {
public:
SequenceRecordDataBuffer(KDColor color) :
@@ -100,12 +99,11 @@ private:
#if __EMSCRIPTEN__
// See comment about emscripten alignement in Shared::Function::FunctionRecordDataBuffer
static_assert(sizeof(emscripten_align1_short) == sizeof(uint16_t), "emscripten_align1_short should have the same size as uint16_t");
emscripten_align1_short m_initialConditionSizes[2];
emscripten_align1_short m_initialConditionSizes[2] __attribute__((packed));
#else
uint16_t m_initialConditionSizes[2];
uint16_t m_initialConditionSizes[2] __attribute__((packed));
#endif
};
#pragma pack(pop)
class SequenceModel : public Shared::ExpressionModel {
public:

View File

@@ -61,8 +61,7 @@ private:
/* CartesianFunctionRecordDataBuffer is the layout of the data buffer of Record
* representing a CartesianFunction. See comment on
* Shared::Function::FunctionRecordDataBuffer about packing. */
#pragma pack(push,1)
class CartesianFunctionRecordDataBuffer : public FunctionRecordDataBuffer {
class __attribute__((packed)) CartesianFunctionRecordDataBuffer : public FunctionRecordDataBuffer {
public:
CartesianFunctionRecordDataBuffer(KDColor color) :
FunctionRecordDataBuffer(color),
@@ -86,7 +85,6 @@ private:
* the expression of the function, directly copied from the pool. */
//char m_expression[0];
};
#pragma pack(pop)
class Model : public ExpressionModel {
public:
void * expressionAddress(const Ion::Storage::Record * record) const override;

View File

@@ -54,7 +54,6 @@ protected:
* - increase the size of the storage file
* - introduce junk memory zone which are then crc-ed in Storage::checksum
* creating dependency on uninitialized values. */
#pragma pack(push,1)
class FunctionRecordDataBuffer {
public:
FunctionRecordDataBuffer(KDColor color) : m_color(color), m_active(true) {}
@@ -73,13 +72,12 @@ protected:
* version of uint16_t type to avoid producing an alignment error on the
* emscripten platform. */
static_assert(sizeof(emscripten_align1_short) == sizeof(uint16_t), "emscripten_align1_short should have the same size as uint16_t");
emscripten_align1_short m_color;
emscripten_align1_short m_color __attribute__((packed));
#else
uint16_t m_color;
uint16_t m_color __attribute__((packed));
#endif
bool m_active;
};
#pragma pack(pop)
private:
FunctionRecordDataBuffer * recordData() const;
};

View File

@@ -9,8 +9,7 @@ namespace Shared {
/* This class is used in a DataBuffer of a Storage::Record. See comment in
* Shared::Function::FunctionRecordDataBuffer about packing. */
#pragma pack(push,1)
class Range1D final {
class __attribute__((packed)) Range1D final {
public:
constexpr static float k_minFloat = 1E-4f;
Range1D(float min = -10.0f, float max = 10.0f) :
@@ -27,14 +26,13 @@ private:
#if __EMSCRIPTEN__
// See comment about emscripten alignement in Shared::Function::FunctionRecordDataBuffer
static_assert(sizeof(emscripten_align1_short) == sizeof(uint16_t), "emscripten_align1_short should have the same size as uint16_t");
emscripten_align1_float m_min;
emscripten_align1_float m_max;
emscripten_align1_float m_min __attribute__((packed));
emscripten_align1_float m_max __attribute__((packed));
#else
float m_min;
float m_max;
#endif
};
#pragma pack(pop)
static_assert(Range1D::k_minFloat >= FLT_EPSILON, "InteractiveCurveViewRange's minimal float range is lower than float precision, it might draw uglily curves such as cos(x)^2+sin(x)^2");