修正一个字节对齐的BUG,各个Config尽量用4字节对齐,特别是含有字符串String的类

MDK编译String::copy的时候,使用了 LDR R0, R1, [R5, 0x04]指令,而这个指令要求4字节对齐。
This commit is contained in:
nnhy 2016-03-10 10:02:30 +00:00
parent 6c7aff547b
commit 86c6535448
5 changed files with 19 additions and 33 deletions

View File

@ -75,9 +75,9 @@ protected:
};
// 必须设定为1字节对齐否则offsetof会得到错误的位置
#pragma pack(push) // 保存对齐状态
//#pragma pack(push) // 保存对齐状态
// 强制结构体紧凑分配空间
#pragma pack(1)
//#pragma pack(1)
/******************************** HotConfig ********************************/
@ -101,7 +101,7 @@ public:
};
#pragma pack(pop) // 恢复对齐状态
//#pragma pack(pop) // 恢复对齐状态
/*

View File

@ -146,13 +146,13 @@ bool String::CheckCapacity(uint size)
if (_Arr && _Capacity >= size) return true;
// 外部需要放下size个字符那么需要size+1个字节空间
int sz = _Capacity;
int sz = 0x40;
while(sz <= size) sz <<= 1;
auto p = new char[sz];
if(!p) return false;
if(_Length)
if(_Arr && _Length)
//strcpy(p, _Arr);
// 为了安全,按照字节拷贝
Buffer(p, sz).Copy(0, _Arr, _Length);
@ -162,7 +162,7 @@ bool String::CheckCapacity(uint size)
if(_needFree && _Arr != Arr) delete _Arr;
_Arr = p;
_Capacity = sz;
_Capacity = sz - 1;
_needFree = true;
return true;
@ -259,7 +259,7 @@ bool String::Concat(const char* cstr, uint length)
if (!cstr) return false;
if (length == 0) return true;
if (!CheckCapacity(newlen)) return false;
//strcpy(_Arr + _Length, cstr);
Buffer(_Arr, _Capacity).Copy(_Length, cstr, length);
_Length = newlen;

View File

@ -6,9 +6,9 @@
#include "Config.h"
// 必须设定为1字节对齐否则offsetof会得到错误的位置
#pragma pack(push) // 保存对齐状态
//#pragma pack(push) // 保存对齐状态
// 强制结构体紧凑分配空间
#pragma pack(1)
//#pragma pack(1)
// 配置信息
class TinyConfig : public ConfigBase
@ -45,6 +45,6 @@ private:
byte TagEnd; // 数据区结束标识符
};
#pragma pack(pop) // 恢复对齐状态
//#pragma pack(pop) // 恢复对齐状态
#endif

View File

@ -57,35 +57,21 @@ TokenConfig* TokenConfig::Create(const char* vendor, byte protocol, ushort sport
{
TokenConfig::Current = &tc;
tc.Init();
//strcpy(tc.Vendor, vendor);
tc.Load();
bool rs = tc.New;
if(tc.Vendor.Length() == 0)
bool rs = tc.New;
if(!tc.Vendor)
{
/*// len 表示字符串真实长度,不包括结束零
auto len = strlen(vendor);
if(len > ArrayLength(tc.Vendor)) len = ArrayLength(tc.Vendor) - 1;
strncpy(tc.Vendor, vendor, len);
tc.Vendor[len] = '\0';*/
tc.Vendor = vendor;
rs = false;
}
if(tc.Server.Length() == 0)
if(!tc.Server)
{
/*// len 表示字符串真实长度,不包括结束零
auto len = strlen(tc.Vendor);
if(len > ArrayLength(tc.Server)) len = ArrayLength(tc.Server) - 1;
strncpy(tc.Server, tc.Vendor, ArrayLength(tc.Server));
tc.Server[len] = '\0';*/
tc.Server = tc.Vendor;
//tc.ServerIP = svr.Value;
tc.ServerPort = sport;
tc.Port = port;
tc.Port = port;
rs = false;
}

View File

@ -7,9 +7,9 @@
#include "Net\Net.h"
// 必须设定为1字节对齐否则offsetof会得到错误的位置
#pragma pack(push) // 保存对齐状态
//#pragma pack(push) // 保存对齐状态
// 强制结构体紧凑分配空间
#pragma pack(1)
//#pragma pack(1)
// 配置信息
class TokenConfig : public ConfigBase
@ -30,7 +30,7 @@ public:
char _VisitToken[16]; //访问服务器令牌
char _Server[32]; // 服务器域名。出厂为空,从厂商服务器覆盖,恢复出厂设置时清空
char _Vendor[32]; // 厂商服务器域名。原始厂商服务器地址
byte TagEnd; // 数据区结束标识符
TokenConfig();
@ -42,13 +42,13 @@ public:
String VisitToken;
String Server;
String Vendor;
static TokenConfig* Current;
static TokenConfig* Create(const char* vendor, byte protocol, ushort sport, ushort port);
private:
};
#pragma pack(pop) // 恢复对齐状态
//#pragma pack(pop) // 恢复对齐状态
#endif