减少memcpy调用
This commit is contained in:
parent
232caf7e34
commit
ccb2934d0c
|
@ -492,17 +492,8 @@ void Enc28j60::ClockOut(byte clock)
|
||||||
WriteReg(ECOCON, clock & 0x7);
|
WriteReg(ECOCON, clock & 0x7);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*void Enc28j60::Init(byte mac[6])
|
|
||||||
{
|
|
||||||
assert_param(mac);
|
|
||||||
|
|
||||||
memcpy(Mac, mac, 6);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
bool Enc28j60::OnOpen()
|
bool Enc28j60::OnOpen()
|
||||||
{
|
{
|
||||||
//assert_param(Mac);
|
|
||||||
|
|
||||||
debug_printf("Enc28j60::Open(%s)\r\n", Mac.ToString().GetBuffer());
|
debug_printf("Enc28j60::Open(%s)\r\n", Mac.ToString().GetBuffer());
|
||||||
|
|
||||||
if(!_reset.Empty())
|
if(!_reset.Empty())
|
||||||
|
|
|
@ -11,20 +11,7 @@ Message::Message(byte code)
|
||||||
State = NULL;
|
State = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*// 设置数据。
|
// 设置数据。
|
||||||
void Message::SetData(const void* buf, uint len, uint offset)
|
|
||||||
{
|
|
||||||
//assert_param(len <= ArrayLength(Data));
|
|
||||||
|
|
||||||
Length = len + offset;
|
|
||||||
if(len > 0 && buf != Data + offset)
|
|
||||||
{
|
|
||||||
assert_ptr(buf);
|
|
||||||
assert_ptr(Data);
|
|
||||||
memcpy(Data + offset, buf, len);
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
void Message::SetData(const Array& bs, uint offset)
|
void Message::SetData(const Array& bs, uint offset)
|
||||||
{
|
{
|
||||||
Length = bs.Length() + offset;
|
Length = bs.Length() + offset;
|
||||||
|
|
|
@ -386,7 +386,7 @@ typedef struct _DHCP_OPT
|
||||||
{
|
{
|
||||||
Option = option;
|
Option = option;
|
||||||
Length = 4;
|
Length = 4;
|
||||||
memcpy(&Data, (byte*)&value, Length);
|
memcpy(&Data, &value, Length);
|
||||||
// 需要考虑地址对齐问题,只有4字节对齐,才可以直接使用整数赋值
|
// 需要考虑地址对齐问题,只有4字节对齐,才可以直接使用整数赋值
|
||||||
//*(uint*)&Data = value;
|
//*(uint*)&Data = value;
|
||||||
|
|
||||||
|
|
25
Net/Net.cpp
25
Net/Net.cpp
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
IPAddress::IPAddress(const byte* ips)
|
IPAddress::IPAddress(const byte* ips)
|
||||||
{
|
{
|
||||||
memcpy((byte*)&Value, ips, 4);
|
Array(ips, 4).CopyTo(&Value, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
|
IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
|
||||||
|
@ -19,7 +19,7 @@ IPAddress::IPAddress(byte ip1, byte ip2, byte ip3, byte ip4)
|
||||||
|
|
||||||
IPAddress::IPAddress(const Array& arr)
|
IPAddress::IPAddress(const Array& arr)
|
||||||
{
|
{
|
||||||
memcpy((byte*)&Value, arr.GetBuffer(), 4);
|
arr.CopyTo(&Value, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IPAddress::IsAny() const { return Value == 0; }
|
bool IPAddress::IsAny() const { return Value == 0; }
|
||||||
|
@ -45,8 +45,7 @@ const IPAddress& IPAddress::Broadcast()
|
||||||
|
|
||||||
IPAddress& IPAddress::operator=(const byte* v)
|
IPAddress& IPAddress::operator=(const byte* v)
|
||||||
{
|
{
|
||||||
//Value = *(uint*)v;
|
Array(v, 4).CopyTo(&Value, 4);
|
||||||
memcpy((byte*)&Value, v, 4);
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +69,7 @@ ByteArray IPAddress::ToArray() const
|
||||||
|
|
||||||
void IPAddress::CopyTo(byte* ips) const
|
void IPAddress::CopyTo(byte* ips) const
|
||||||
{
|
{
|
||||||
if(ips) memcpy(ips, (byte*)&Value, 4);
|
if(ips) Array(&Value, 4).CopyTo(ips, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
String& IPAddress::ToStr(String& str) const
|
String& IPAddress::ToStr(String& str) const
|
||||||
|
@ -187,9 +186,7 @@ MacAddress& MacAddress::operator=(ulong v)
|
||||||
|
|
||||||
MacAddress& MacAddress::operator=(const byte* buf)
|
MacAddress& MacAddress::operator=(const byte* buf)
|
||||||
{
|
{
|
||||||
/*Array bs(buf, 6);
|
Array(buf, 6).CopyTo(&Value, 6);
|
||||||
Value = bs.ToUInt64() & MAC_MASK;*/
|
|
||||||
memcpy((byte*)&Value, buf, 6);
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -213,19 +210,9 @@ ByteArray MacAddress::ToArray() const
|
||||||
|
|
||||||
void MacAddress::CopyTo(byte* macs) const
|
void MacAddress::CopyTo(byte* macs) const
|
||||||
{
|
{
|
||||||
if(macs) memcpy(macs, (byte*)&Value, 6);
|
if(macs) Array(&Value, 6).CopyTo(macs, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*bool MacAddress::operator==(MacAddress& addr1, MacAddress& addr2)
|
|
||||||
{
|
|
||||||
return addr1.v4 == addr2.v4 && addr1.v2 == addr2.v2;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MacAddress::operator!=(MacAddress& addr1, MacAddress& addr2)
|
|
||||||
{
|
|
||||||
return addr1.v4 != addr2.v4 || addr1.v2 != addr2.v2;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
String& MacAddress::ToStr(String& str) const
|
String& MacAddress::ToStr(String& str) const
|
||||||
{
|
{
|
||||||
byte* macs = (byte*)&Value;
|
byte* macs = (byte*)&Value;
|
||||||
|
|
|
@ -75,18 +75,15 @@ uint Queue::Write(const Array& bs)
|
||||||
// 如果要写入的数据足够存放
|
// 如果要写入的数据足够存放
|
||||||
if(len <= remain)
|
if(len <= remain)
|
||||||
{
|
{
|
||||||
//memcpy(_s.GetBuffer() + _head, buf, len);
|
|
||||||
_s.Copy(buf, len, _head);
|
_s.Copy(buf, len, _head);
|
||||||
rs += len;
|
rs += len;
|
||||||
_head += len;
|
_head += len;
|
||||||
//_head %= _s.Capacity();
|
|
||||||
if(_head >= _s.Capacity()) _head -= _s.Capacity();
|
if(_head >= _s.Capacity()) _head -= _s.Capacity();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 否则先写一段,指针回到开头
|
// 否则先写一段,指针回到开头
|
||||||
//memcpy(_s.GetBuffer() + _head, buf, remain);
|
|
||||||
_s.Copy(buf, remain, _head);
|
_s.Copy(buf, remain, _head);
|
||||||
buf += remain;
|
buf += remain;
|
||||||
len -= remain;
|
len -= remain;
|
||||||
|
@ -130,18 +127,15 @@ uint Queue::Read(Array& bs)
|
||||||
// 如果要读取的数据都在这里
|
// 如果要读取的数据都在这里
|
||||||
if(len <= remain)
|
if(len <= remain)
|
||||||
{
|
{
|
||||||
//memcpy(buf, _s.GetBuffer() + _tail, len);
|
|
||||||
_s.CopyTo(buf, len, _tail);
|
_s.CopyTo(buf, len, _tail);
|
||||||
rs += len;
|
rs += len;
|
||||||
_tail += len;
|
_tail += len;
|
||||||
//_tail %= _s.Capacity();
|
|
||||||
if(_tail >= _s.Capacity()) _tail -= _s.Capacity();
|
if(_tail >= _s.Capacity()) _tail -= _s.Capacity();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 否则先读一段,指针回到开头
|
// 否则先读一段,指针回到开头
|
||||||
//memcpy(buf + _tail, _s.GetBuffer(), remain);
|
|
||||||
_s.CopyTo(buf, remain, _tail);
|
_s.CopyTo(buf, remain, _tail);
|
||||||
buf += remain;
|
buf += remain;
|
||||||
len -= remain;
|
len -= remain;
|
||||||
|
|
|
@ -12,7 +12,7 @@ Timer::Timer(TIM_TypeDef* timer)
|
||||||
if(!Timers)
|
if(!Timers)
|
||||||
{
|
{
|
||||||
Timers = new Timer*[TimerCount];
|
Timers = new Timer*[TimerCount];
|
||||||
ArrayZero2(Timers, TimerCount);
|
memset(Timers, 0, TimerCount * sizeof(Timers[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
byte idx = 0xFF;
|
byte idx = 0xFF;
|
||||||
|
@ -61,7 +61,7 @@ Timer* Timer::Create(byte index)
|
||||||
if(!Timers)
|
if(!Timers)
|
||||||
{
|
{
|
||||||
Timers = new Timer*[TimerCount];
|
Timers = new Timer*[TimerCount];
|
||||||
ArrayZero2(Timers, TimerCount);
|
memset(Timers, 0, TimerCount * sizeof(Timers[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 找到第一个可用的位置,没有被使用,并且该位置定时器存在
|
// 找到第一个可用的位置,没有被使用,并且该位置定时器存在
|
||||||
|
|
225
Type.h
225
Type.h
|
@ -73,10 +73,6 @@ public:
|
||||||
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
|
#define ArrayLength(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||||
// 数组清零,固定长度
|
// 数组清零,固定长度
|
||||||
#define ArrayZero(arr) memset(arr, 0, ArrayLength(arr) * sizeof(arr[0]))
|
#define ArrayZero(arr) memset(arr, 0, ArrayLength(arr) * sizeof(arr[0]))
|
||||||
// 数组清零,可变长度
|
|
||||||
#define ArrayZero2(arr, len) memset(arr, 0, len * sizeof(arr[0]))
|
|
||||||
// 数组复制
|
|
||||||
#define ArrayCopy(dst, src) memcpy(dst, src, ArrayLength(src) * sizeof(src[0]))
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class IArray
|
class IArray
|
||||||
|
@ -368,227 +364,6 @@ String operator+(const Object& obj, const char* str);
|
||||||
// 从数组创建列表
|
// 从数组创建列表
|
||||||
#define MakeList(T, arr) List<T>(&arr[0], sizeof(arr)/sizeof(arr[0]))
|
#define MakeList(T, arr) List<T>(&arr[0], sizeof(arr)/sizeof(arr[0]))
|
||||||
|
|
||||||
// 数组
|
|
||||||
class JArray
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
void** _Arr;
|
|
||||||
int _Count;
|
|
||||||
int _Capacity;
|
|
||||||
|
|
||||||
public:
|
|
||||||
// 有效元素个数
|
|
||||||
int Count() const { return _Count; }
|
|
||||||
// 最大元素个数
|
|
||||||
int Capacity() const { return _Capacity; }
|
|
||||||
|
|
||||||
JArray(int capacity = 0x10)
|
|
||||||
{
|
|
||||||
_Capacity = capacity;
|
|
||||||
_Count = 0;
|
|
||||||
|
|
||||||
_Arr = new void*[capacity];
|
|
||||||
ArrayZero2(_Arr, capacity);
|
|
||||||
}
|
|
||||||
|
|
||||||
~JArray()
|
|
||||||
{
|
|
||||||
if(_Arr) delete _Arr;
|
|
||||||
_Arr = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 压入一个元素
|
|
||||||
int Push(void* item)
|
|
||||||
{
|
|
||||||
assert_param(_Count < _Capacity);
|
|
||||||
|
|
||||||
// 找到空闲位放置
|
|
||||||
int idx = _Count++;
|
|
||||||
_Arr[idx] = item;
|
|
||||||
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 弹出一个元素
|
|
||||||
const void* Pop()
|
|
||||||
{
|
|
||||||
assert_param(_Count > 0);
|
|
||||||
|
|
||||||
return _Arr[--_Count];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重载索引运算符[],让它可以像数组一样使用下标索引。
|
|
||||||
void* operator[](int i)
|
|
||||||
{
|
|
||||||
assert_param(i >= 0 && i < _Count);
|
|
||||||
return _Arr[i];
|
|
||||||
}
|
|
||||||
// 列表转为指针,注意安全
|
|
||||||
//T* operator=(TArray arr) { return arr.Arr; }
|
|
||||||
};
|
|
||||||
|
|
||||||
// 固定大小的指针数组。
|
|
||||||
/*
|
|
||||||
用于数据元素不多,有个上限的场合。主要为了方便遍历。
|
|
||||||
存储数据时,从头开始找到空位来存放,不用时清空相应空位。
|
|
||||||
所以需要注意的是,在数组里面存储的元素不一定连续。
|
|
||||||
*/
|
|
||||||
template<typename T, int ArraySize>
|
|
||||||
class FixedArray
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
uint _Count;
|
|
||||||
T* Arr[ArraySize];
|
|
||||||
|
|
||||||
public:
|
|
||||||
// 默认初始化。大小和数组元素全部清零
|
|
||||||
FixedArray()
|
|
||||||
{
|
|
||||||
_Count = 0;
|
|
||||||
ArrayZero(Arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用另一个固定数组来初始化
|
|
||||||
FixedArray(FixedArray& arr)
|
|
||||||
{
|
|
||||||
_Count = arr._Count;
|
|
||||||
ArrayCopy(Arr, arr.Arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重载等号运算符,使用另一个固定数组来初始化
|
|
||||||
FixedArray& operator=(FixedArray& arr)
|
|
||||||
{
|
|
||||||
_Count = arr._Count;
|
|
||||||
ArrayCopy(Arr, arr.Arr);
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~FixedArray() { DeleteAll(); }
|
|
||||||
|
|
||||||
// 实际元素个数
|
|
||||||
uint Count() const { return _Count; }
|
|
||||||
|
|
||||||
// 数组总长度
|
|
||||||
uint Length() const { return ArraySize; }
|
|
||||||
|
|
||||||
// 压入一个元素。返回元素所存储的索引
|
|
||||||
int Add(T* item)
|
|
||||||
{
|
|
||||||
assert_ptr(item);
|
|
||||||
//assert_param(_Count < ArraySize);
|
|
||||||
|
|
||||||
if(_Count >= ArraySize) return -1;
|
|
||||||
|
|
||||||
// 找到空闲位放置
|
|
||||||
int i = 0;
|
|
||||||
for(; i < ArraySize && Arr[i]; i++);
|
|
||||||
// 检查是否还有空位
|
|
||||||
if(i >= ArraySize) return -1;
|
|
||||||
|
|
||||||
Arr[i] = item;
|
|
||||||
_Count++;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 弹出最后一个元素
|
|
||||||
T* Pop()
|
|
||||||
{
|
|
||||||
if(_Count == 0) return NULL;
|
|
||||||
|
|
||||||
// 找到最后一个元素
|
|
||||||
int i = ArraySize - 1;
|
|
||||||
for(; i >=0 && !Arr[i]; i--);
|
|
||||||
|
|
||||||
T* item = Arr[i];
|
|
||||||
_Count--;
|
|
||||||
Arr[i] = NULL;
|
|
||||||
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除元素
|
|
||||||
bool Remove(T* item)
|
|
||||||
{
|
|
||||||
int idx = Find(item);
|
|
||||||
if(idx < 0) return false;
|
|
||||||
|
|
||||||
RemoveAt(idx);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 删除指定位置的元素
|
|
||||||
void RemoveAt(int idx)
|
|
||||||
{
|
|
||||||
assert_param(idx >= 0 && idx < ArraySize);
|
|
||||||
|
|
||||||
_Count--;
|
|
||||||
Arr[idx] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清空数组。个数设0,所有元素清零
|
|
||||||
FixedArray& Clear()
|
|
||||||
{
|
|
||||||
_Count = 0;
|
|
||||||
ArrayZero(Arr);
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 释放所有指针指向的内存
|
|
||||||
FixedArray& DeleteAll()
|
|
||||||
{
|
|
||||||
for(int i=0; i < ArraySize; i++)
|
|
||||||
{
|
|
||||||
if(Arr[i]) delete Arr[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查找元素
|
|
||||||
int Find(T* item)
|
|
||||||
{
|
|
||||||
assert_ptr(item);
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
for(; i < ArraySize && Arr[i] != item; i++);
|
|
||||||
if(i >= ArraySize) return -1;
|
|
||||||
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 移到下一个元素,累加参数,如果没有下一个元素则返回false。
|
|
||||||
bool MoveNext(int& idx)
|
|
||||||
{
|
|
||||||
//assert_ptr(idx);
|
|
||||||
|
|
||||||
// 从idx开始,找到第一个非空节点。注意,存储不一定连续
|
|
||||||
int i = idx + 1;
|
|
||||||
for(; i < ArraySize && !Arr[i]; i++);
|
|
||||||
if(i >= ArraySize) return false;
|
|
||||||
|
|
||||||
idx = i;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 重载索引运算符[],让它可以像数组一样使用下标索引。
|
|
||||||
/*T*& operator[](int i)
|
|
||||||
{
|
|
||||||
assert_param(i >= 0 && i < ArraySize);
|
|
||||||
return Arr[i];
|
|
||||||
}*/
|
|
||||||
// 不能允许[]作为左值,否则外部直接设置数据以后,Count并没有改变
|
|
||||||
T* operator[](int i)
|
|
||||||
{
|
|
||||||
assert_param(i >= 0 && i < ArraySize);
|
|
||||||
return Arr[i];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/*// 变长列表模版
|
/*// 变长列表模版
|
||||||
// T一般是指针,列表内部有一个数组用于存放指针
|
// T一般是指针,列表内部有一个数组用于存放指针
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
Loading…
Reference in New Issue