基础类库和系统内核尽可能的使用int替代uint,方便计算,避免溢出了也不知道。

This commit is contained in:
大石头X2 2017-02-27 18:36:07 +08:00
parent 91229367f1
commit fc216e5f69
30 changed files with 325 additions and 312 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
################################################################################
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
################################################################################
/vs/Debug
*.lib
*.db
*.opendb
*.suo

View File

@ -50,9 +50,9 @@ bool Alarm::Set(const Pair& args, Stream& result)
result.Write(id);
// 马上调度一次
if(id) Sys.SetTask(_taskid, true, 0);
if (id) Sys.SetTask(_taskid, true, 0);
return id;
return id > 0;
}
bool Alarm::Get(const Pair& args, Stream& result)

View File

@ -59,7 +59,7 @@ public:
#endif
protected:
uint _Capacity; // 最大个数。非字节数
int _Capacity; // 最大个数。非字节数
bool _needFree; // 是否需要释放
bool _canWrite; // 是否可写
ushort _Size; // 单个元素大小。字节

View File

@ -91,7 +91,7 @@ void IList::Add(void* item)
}
// 添加多个元素
void IList::Add(void** items, uint count)
void IList::Add(void** items, int count)
{
if(!items || !count) return;
@ -101,7 +101,7 @@ void IList::Add(void** items, uint count)
}
// 删除指定位置元素
void IList::RemoveAt(uint index)
void IList::RemoveAt(int index)
{
int len = _Count;
if(len <= 0 || index >= len) return;

View File

@ -23,10 +23,10 @@ public:
void Add(void* item);
// 添加多个元素
void Add(void** items, uint count);
void Add(void** items, int count);
// 删除指定位置元素
void RemoveAt(uint index);
void RemoveAt(int index);
// 删除指定元素
int Remove(const void* item);
@ -49,8 +49,8 @@ public:
private:
void** _Arr;
uint _Count;
uint _Capacity;
int _Count;
int _Capacity;
void* Arr[0x04];

View File

@ -9,7 +9,7 @@ Queue::Queue() : _s((void*)nullptr, 0)
Clear();
}
void Queue::SetCapacity(uint len)
void Queue::SetCapacity(int len)
{
_s.SetLength(len);
}
@ -63,7 +63,7 @@ INROOT byte Queue::Dequeue()
return dat;
}
uint Queue::Write(const Buffer& bs)
int Queue::Write(const Buffer& bs)
{
int total = _s.Capacity();
if(!total) _s.SetLength(64);
@ -77,17 +77,17 @@ uint Queue::Write(const Buffer& bs)
4
*/
uint len = bs.Length();
int len = bs.Length();
// 如果队列满了,不需要覆盖
if(_size + len > total)
len = total - _size;
uint rs = 0;
int rs = 0;
while(true)
{
// 计算这一个循环剩下的位置
uint remain = _s.Capacity() - _head;
int remain = _s.Capacity() - _head;
// 如果要写入的数据足够存放
if(len <= remain)
{
@ -113,7 +113,7 @@ uint Queue::Write(const Buffer& bs)
return rs;
}
uint Queue::Read(Buffer& bs)
int Queue::Read(Buffer& bs)
{
if(_size == 0) return 0;
@ -124,17 +124,17 @@ uint Queue::Read(Buffer& bs)
4
*/
uint len = bs.Length();
int len = bs.Length();
if(!len) return 0;
if(len > _size) len = _size;
uint rs = 0;
int rs = 0;
while(true)
{
int total = _s.Capacity();
// 计算这一个循环剩下的位置
uint remain = total - _tail;
int remain = total - _tail;
// 如果要读取的数据都在这里
if(len <= remain)
{

View File

@ -9,25 +9,25 @@ class Queue
{
private:
Array _s; // 数据流
uint _head; // 头部位置
uint _tail; // 尾部位置
uint _size; // 长度
int _head; // 头部位置
int _tail; // 尾部位置
int _size; // 长度
public:
Queue();
bool Empty() const { return _size == 0; } // 队列空
uint Capacity() const { return _s.Capacity(); } // 队列容量
uint Length() const { return _size; } // 队列大小
void SetCapacity(uint len);
int Capacity() const { return _s.Capacity(); } // 队列容量
int Length() const { return _size; } // 队列大小
void SetCapacity(int len);
void Clear();
void Enqueue(byte dat);
byte Dequeue();
uint Write(const Buffer& bs); // 批量写入
uint Read(Buffer& bs); // 批量读取
int Write(const Buffer& bs); // 批量写入
int Read(Buffer& bs); // 批量读取
};
#endif

View File

@ -11,19 +11,19 @@ Random::Random()
srand((uint)time(NULL));
}
Random::Random(uint seed)
Random::Random(int seed)
{
srand(seed);
}
uint Random::Next() const
int Random::Next() const
{
return rand();
}
uint Random::Next(uint max) const
int Random::Next(int max) const
{
uint value = rand();
int value = rand();
if(max == 0x100) return value & 0xFF;

View File

@ -6,10 +6,10 @@ class Random
{
public:
Random();
Random(uint seed);
Random(int seed);
uint Next() const;
uint Next(uint max) const;
int Next() const;
int Next(int max) const;
void Next(Buffer& bs) const;
};

View File

@ -158,14 +158,14 @@ private:
void init();
void release();
bool Concat(cstring cstr, uint length);
bool Concat(cstring cstr, int length);
String& copy(cstring cstr, uint length);
String& copy(cstring cstr, int length);
void move(String& rhs);
bool CopyOrWrite();
using Array::CheckCapacity;
bool CheckCapacity(uint size);
bool CheckCapacity(int size);
virtual void* Alloc(int len);
int Search(cstring str, int len, int startIndex, bool rev) const;

View File

@ -11,12 +11,12 @@ extern ushort _REV16(ushort);
extern uint _REV(uint);
// 使用缓冲区初始化数据流。注意此时指针位于0而内容长度为缓冲区长度
Stream::Stream(void* buf, uint len)
Stream::Stream(void* buf, int len)
{
Init(buf, len);
}
Stream::Stream(const void* buf, uint len)
Stream::Stream(const void* buf, int len)
{
Init((void*)buf, len);
@ -36,9 +36,9 @@ Stream::Stream(const Buffer& bs)
CanWrite = false;
}
Stream::~Stream(){ }
Stream::~Stream() { }
void Stream::Init(void* buf, uint len)
void Stream::Init(void* buf, int len)
{
assert(buf, "buf");
@ -51,13 +51,13 @@ void Stream::Init(void* buf, uint len)
Little = true;
}
bool Stream::CheckRemain(uint count)
bool Stream::CheckRemain(int count)
{
uint remain = _Capacity - _Position;
int remain = _Capacity - _Position;
// 容量不够,需要扩容
if(count > remain)
if (count > remain)
{
if(CanResize)
if (CanResize)
debug_printf("数据流 0x%p 剩余容量 (%d - %d) = %d 不足 %d ,无法扩容!\r\n", this, _Capacity, _Position, remain, count);
else
assert(false, "无法扩容");
@ -69,18 +69,18 @@ bool Stream::CheckRemain(uint count)
}
// 数据流容量
uint Stream::Capacity() const { return _Capacity; }
void Stream::SetCapacity(uint len) { CheckRemain(len - _Position); }
int Stream::Capacity() const { return _Capacity; }
void Stream::SetCapacity(int len) { CheckRemain(len - _Position); }
// 当前位置
uint Stream::Position() const { return _Position; }
int Stream::Position() const { return _Position; }
// 设置位置
bool Stream::SetPosition(int p)
{
// 允许移动到最后一个字节之后也就是Length
//assert(p <= Length, "设置的位置超出长度");
if(p < 0 && p > Length)
if (p < 0 && p > Length)
{
debug_printf("设置的位置 %d 超出长度 %d\r\n", p, Length);
return false;
@ -91,12 +91,12 @@ bool Stream::SetPosition(int p)
}
// 余下的有效数据流长度。0表示已经到达终点
uint Stream::Remain() const { return Length - _Position; };
int Stream::Remain() const { return Length - _Position; };
// 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标
bool Stream::Seek(int offset)
{
if(offset == 0) return true;
if (offset == 0) return true;
return SetPosition(offset + _Position);
}
@ -108,25 +108,25 @@ byte* Stream::GetBuffer() const { return _Buffer; }
byte* Stream::Current() const { return &_Buffer[_Position]; }
// 读取7位压缩编码整数
uint Stream::ReadEncodeInt()
int Stream::ReadEncodeInt()
{
uint value = 0;
int value = 0;
// 同时计算最大4字节避免无限读取错误数据
for(int i = 0, k = 0; i < 4; i++, k += 7)
for (int i = 0, k = 0; i < 4; i++, k += 7)
{
int temp = ReadByte();
if(temp < 0) break;
if (temp < 0) break;
value |= (temp & 0x7F) << k;
if((temp & 0x80) == 0) break;
if ((temp & 0x80) == 0) break;
}
return value;
}
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
uint Stream::Read(Buffer& bs)
int Stream::Read(Buffer& bs)
{
if(bs.Length() == 0) return 0;
if (bs.Length() == 0) return 0;
Buffer ss(_Buffer, Length);
int count = bs.Copy(0, ss, _Position, bs.Length());
@ -140,13 +140,13 @@ uint Stream::Read(Buffer& bs)
}
// 写入7位压缩编码整数
uint Stream::WriteEncodeInt(uint value)
int Stream::WriteEncodeInt(int value)
{
if(!CanWrite) return 0;
if (!CanWrite) return 0;
byte buf[8];
int k = 0;
for(int i = 0; i < 4 && value >= 0x80; i++)
for (int i = 0; i < 4 && value >= 0x80; i++)
{
buf[k++] = (byte)(value | 0x80);
@ -163,16 +163,16 @@ uint Stream::WriteEncodeInt(uint value)
bool Stream::Write(const Buffer& bs)
{
int count = bs.Length();
if(count == 0) return true;
if(!CanWrite) return false;
if(!CheckRemain(count)) return false;
if (count == 0) return true;
if (!CanWrite) return false;
if (!CheckRemain(count)) return false;
Buffer ss(_Buffer, _Capacity);
count = ss.Copy((uint)_Position, bs, 0, count);
count = ss.Copy(_Position, bs, 0, count);
_Position += count;
// 内容长度不是累加,而是根据位置而扩大
if(_Position > Length) Length = _Position;
if (_Position > Length) Length = _Position;
return true;
}
@ -181,10 +181,10 @@ bool Stream::Write(const Buffer& bs)
byte* Stream::ReadBytes(int count)
{
// 默认小于0时读取全部数据
if(count < 0) count = Remain();
if (count < 0) count = Remain();
byte* p = Current();
if(!Seek(count)) return nullptr;
if (!Seek(count)) return nullptr;
return p;
}
@ -192,22 +192,22 @@ byte* Stream::ReadBytes(int count)
// 读取一个字节,不移动游标。如果没有可用数据,则返回-1
int Stream::Peek() const
{
if(!Remain()) return -1;
if (!Remain()) return -1;
return *Current();
}
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
uint Stream::ReadArray(Buffer& bs)
int Stream::ReadArray(Buffer& bs)
{
uint len = ReadEncodeInt();
if(!len)
int len = ReadEncodeInt();
if (!len)
{
bs.SetLength(0);
return 0;
}
if(len > bs.Length() && !bs.SetLength(len))
if (len > bs.Length() && !bs.SetLength(len))
{
// 在设计时,如果取得的长度超级大,可能是设计错误
//if(len > 0x40)
@ -257,10 +257,10 @@ String Stream::ReadString()
int Stream::ReadByte()
{
if(Length == _Position) return -1;
if (Length == _Position) return -1;
byte* p = Current();
if(!Seek(1)) return 0;
if (!Seek(1)) return 0;
return *p;
}
@ -269,8 +269,8 @@ ushort Stream::ReadUInt16()
{
ushort v;
Buffer bs(&v, sizeof(v));
if(!Read(bs)) return 0;
if(!Little) v = _REV16(v);
if (!Read(bs)) return 0;
if (!Little) v = _REV16(v);
return v;
}
@ -278,8 +278,8 @@ uint Stream::ReadUInt32()
{
uint v;
Buffer bs(&v, sizeof(v));
if(!Read(bs)) return 0;
if(!Little) v = _REV(v);
if (!Read(bs)) return 0;
if (!Little) v = _REV(v);
return v;
}
@ -287,8 +287,8 @@ UInt64 Stream::ReadUInt64()
{
UInt64 v;
Buffer bs(&v, sizeof(v));
if(!Read(bs)) return 0;
if(!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32);
if (!Read(bs)) return 0;
if (!Little) v = _REV(v >> 32) | ((UInt64)_REV(v & 0xFFFFFFFF) << 32);
return v;
}
@ -299,32 +299,32 @@ bool Stream::Write(byte value)
bool Stream::Write(ushort value)
{
if(!Little) value = _REV16(value);
if (!Little) value = _REV16(value);
return Write(Buffer(&value, sizeof(value)));
}
bool Stream::Write(uint value)
{
if(!Little) value = _REV(value);
if (!Little) value = _REV(value);
return Write(Buffer(&value, sizeof(value)));
}
bool Stream::Write(UInt64 value)
{
if(!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32);
if (!Little) value = _REV(value >> 32) | ((UInt64)_REV(value & 0xFFFFFFFF) << 32);
return Write(Buffer(&value, sizeof(value)));
}
/******************************** MemoryStream ********************************/
MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr))
MemoryStream::MemoryStream(int len) : Stream(_Arr, ArrayLength(_Arr))
{
Length = 0;
_needFree = false;
if(len > ArrayLength(_Arr))
if (len > ArrayLength(_Arr))
{
byte* buf = new byte[len];
Init(buf, len);
@ -332,7 +332,7 @@ MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr))
}
}
MemoryStream::MemoryStream(void* buf, uint len) : Stream(buf, len)
MemoryStream::MemoryStream(void* buf, int len) : Stream(buf, len)
{
_needFree = false;
}
@ -340,32 +340,32 @@ MemoryStream::MemoryStream(void* buf, uint len) : Stream(buf, len)
// 销毁数据流
MemoryStream::~MemoryStream()
{
if(_needFree)
if (_needFree)
{
if(_Buffer != _Arr) delete[] _Buffer;
if (_Buffer != _Arr) delete[] _Buffer;
_Buffer = nullptr;
}
}
bool MemoryStream::CheckRemain(uint count)
bool MemoryStream::CheckRemain(int count)
{
uint remain = _Capacity - _Position;
int remain = _Capacity - _Position;
// 容量不够,需要扩容
if(count > remain)
if (count > remain)
{
if(!CanResize) return Stream::CheckRemain(count);
if (!CanResize) return Stream::CheckRemain(count);
// 原始容量成倍扩容
uint total = _Position + count;
uint size = _Capacity;
if(size < 0x10) size = 0x10;
while(size < total) size <<= 1;
int total = _Position + count;
int size = _Capacity;
if (size < 0x10) size = 0x10;
while (size < total) size <<= 1;
// 申请新的空间,并复制数据
byte* bufNew = new byte[size];
if(Length > 0) Buffer(_Buffer, Length).CopyTo(0, bufNew, -1);
if (Length > 0) Buffer(_Buffer, Length).CopyTo(0, bufNew, -1);
if(_Buffer != _Arr && _needFree == true) delete[] _Buffer;
if (_Buffer != _Arr && _needFree == true) delete[] _Buffer;
_Buffer = bufNew;
_Capacity = size;
@ -382,5 +382,5 @@ WEAK ushort _REV16(ushort value)
WEAK uint _REV(uint value)
{
return (_REV16(value & 0xFFFF) << 16) | (_REV16(value >> 16) >> 16);
return (_REV16(value & 0xFFFF) << 16) | (_REV16(value >> 16));
}

View File

@ -7,34 +7,34 @@ class Stream
{
protected:
byte* _Buffer; // 数据缓冲区。扩容后会重新分配缓冲区
uint _Capacity; // 缓冲区容量
uint _Position; // 游标位置
int _Capacity; // 缓冲区容量
int _Position; // 游标位置
void Init(void* buf, uint len);
virtual bool CheckRemain(uint count);
void Init(void* buf, int len);
virtual bool CheckRemain(int count);
public:
uint Length; // 数据长度
int Length; // 数据长度
bool Little; // 默认小字节序。仅影响数据读写操作
bool CanWrite; // 是否可写
bool CanResize; // 是否可以自动扩容
// 使用缓冲区初始化数据流。注意此时指针位于0而内容长度为缓冲区长度
Stream(void* buf, uint len);
Stream(const void* buf, uint len);
Stream(void* buf, int len);
Stream(const void* buf, int len);
// 使用字节数组初始化数据流。注意此时指针位于0而内容长度为缓冲区长度
explicit Stream(Buffer& bs);
explicit Stream(const Buffer& bs);
virtual ~Stream();
// 数据流容量
uint Capacity() const;
void SetCapacity(uint len);
int Capacity() const;
void SetCapacity(int len);
// 当前位置
uint Position() const;
int Position() const;
// 设置位置
bool SetPosition(int p);
// 余下的有效数据流长度。0表示已经到达终点
uint Remain() const;
int Remain() const;
// 尝试前后移动一段距离,返回成功或者失败。如果失败,不移动游标
bool Seek(int offset);
@ -44,17 +44,17 @@ public:
byte* Current() const;
// 读取7位压缩编码整数
uint ReadEncodeInt();
int ReadEncodeInt();
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
uint Read(Buffer& bs);
int Read(Buffer& bs);
// 写入7位压缩编码整数
uint WriteEncodeInt(uint value);
int WriteEncodeInt(int value);
// 把字节数组的数据写入到数据流。不包含长度前缀
bool Write(const Buffer& bs);
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
uint ReadArray(Buffer& bs);
int ReadArray(Buffer& bs);
ByteArray ReadArray(int count);
// 把字节数组作为变长数据写入到数据流。以压缩整数开头表示长度
bool WriteArray(const Buffer& bs);
@ -105,13 +105,13 @@ private:
bool _needFree; // 是否自动释放
//bool _resize; // 是否可以自动扩容
virtual bool CheckRemain(uint count);
virtual bool CheckRemain(int count);
public:
// 分配指定大小的数据流
MemoryStream(uint len = 0);
MemoryStream(int len = 0);
// 使用缓冲区初始化数据流,支持自动扩容
MemoryStream(void* buf, uint len);
MemoryStream(void* buf, int len);
// 销毁数据流
virtual ~MemoryStream();
};

View File

@ -17,7 +17,7 @@
#include "SString.h"
char* utohex(uint value, byte size, char* string, bool upper);
extern char* itoa(int value, char* string, int radix);
//extern char* itoa(int value, char* string, int radix);
extern char* ltoa(Int64 value, char* string, int radix);
extern char* utoa(uint value, char* string, int radix);
extern char* ultoa(UInt64 value, char* string, int radix);
@ -173,7 +173,7 @@ void String::release()
init();
}
bool String::CheckCapacity(uint size)
bool String::CheckCapacity(int size)
{
int old = _Capacity;
CheckCapacity(size + 1, _Length);
@ -201,7 +201,7 @@ void* String::Alloc(int len)
}
}
String& String::copy(cstring cstr, uint length)
String& String::copy(cstring cstr, int length)
{
if (!cstr || !length) return *this;
@ -324,12 +324,12 @@ bool String::Concat(const String& s)
return Concat(s._Arr, s._Length);
}
bool String::Concat(cstring cstr, uint length)
bool String::Concat(cstring cstr, int length)
{
if (!cstr) return false;
if (length == 0) return true;
uint newlen = _Length + length;
int newlen = _Length + length;
if (!CheckCapacity(newlen)) return false;
//strcpy(_Arr + _Length, cstr);

View File

@ -84,7 +84,7 @@ bool Port::Open()
#if DEBUG
// 保护引脚
auto name = typeid(*this).name();
while(*name >= '0' && *name <= '9') name++;
while (*name >= '0' && *name <= '9') name++;
debug_printf("%s", name);
Port_Reserve(_Pin, true);
#endif
@ -106,7 +106,7 @@ void Port::Close()
#if DEBUG
// 保护引脚
auto name = typeid(*this).name();
while(*name >= '0' && *name <= '9') name++;
while (*name >= '0' && *name <= '9') name++;
debug_printf("%s", name);
Port_Reserve(_Pin, false);
debug_printf("\r\n");
@ -238,7 +238,9 @@ WEAK bool OutputPort::ReadInput() const
{
if (Empty()) return false;
return Port::Read() ^ Invert;
bool v = Port::Read();
if (Invert)return !v;
return v;
}
void OutputPort::Up(uint ms) const
@ -341,7 +343,9 @@ InputPort& InputPort::Init(Pin pin, bool invert)
// 读取本组所有引脚任意脚为true则返回true主要为单一引脚服务
bool InputPort::Read() const
{
return Port::Read() ^ Invert;
bool v = Port::Read();
if (Invert)return !v;
return v;
}
void InputPort::OnPress(bool down)
@ -367,7 +371,7 @@ void InputPort::OnPress(bool down)
if (down)
_Start = now;
else
PressTime = now - _Start;
PressTime = (ushort)(now - _Start);
/*if(down)
{

View File

@ -147,8 +147,8 @@ bool SerialPort::Flush(uint times)
// 从某个端口读取数据
uint SerialPort::OnRead(Buffer& bs)
{
uint count = 0;
uint len = Rx.Length();
int count = 0;
int len = Rx.Length();
// 如果没有数据,立刻返回,不要等待浪费时间
if(!len)
{

View File

@ -27,7 +27,7 @@ Timer::~Timer()
{
Close();
if(OnTick.Method) SetHandler(false);
if (OnTick.Method) SetHandler(false);
Timers[_index] = nullptr;
}
@ -40,13 +40,13 @@ Timer* Timer::Create(byte index)
byte tcount = ArrayLength(Timers);
//byte tcount = 46;
// 特殊处理随机分配
if(index == 0xFF)
if (index == 0xFF)
{
// 找到第一个可用的位置,没有被使用,并且该位置定时器存在
byte i = 0;
for(; i<tcount && (Timers[i] || !GetTimer(i)); i++);
for (; i < tcount && (Timers[i] || !GetTimer(i)); i++);
if(i >= tcount)
if (i >= tcount)
{
debug_printf("Timer::Create 失败!没有空闲定时器!\r\n");
return nullptr;
@ -57,7 +57,7 @@ Timer* Timer::Create(byte index)
assert(index < tcount, "index");
if(Timers[index])
if (Timers[index])
return Timers[index];
else
return new Timer((TIMER)index);
@ -65,7 +65,7 @@ Timer* Timer::Create(byte index)
void Timer::Open()
{
if(Opened) return;
if (Opened) return;
TS("Timer::Open");
@ -76,7 +76,7 @@ void Timer::Open()
void Timer::Close()
{
if(!Opened) return;
if (!Opened) return;
TS("Timer::Close");
@ -91,7 +91,7 @@ void Timer::Register(const Delegate<Timer&>& dlg)
{
OnTick = dlg;
SetHandler(dlg.Method);
SetHandler(dlg.Method != nullptr);
}
void Timer::OnInterrupt()

View File

@ -27,7 +27,7 @@ typedef struct MemoryBlock_
} MemoryBlock;
/******************************** Heap ********************************/
Heap::Heap(uint addr, uint size)
Heap::Heap(uint addr, int size)
{
Current = this;
@ -55,11 +55,11 @@ Heap::Heap(uint addr, uint size)
debug_printf("Heap::Init(%p, %d) Free=%d \r\n", Address, Size, FreeSize());
}
uint Heap::Used() const { return _Used; }
uint Heap::Count() const { return _Count; }
uint Heap::FreeSize() const { return Size - _Used; }
int Heap::Used() const { return _Used; }
int Heap::Count() const { return _Count; }
int Heap::FreeSize() const { return Size - _Used; }
void* Heap::Alloc(uint size)
void* Heap::Alloc(int size)
{
// 要申请的内存大小需要对齐
size = (size+MEMORY_ALIGN-1) & (~(MEMORY_ALIGN-1));
@ -73,7 +73,7 @@ void* Heap::Alloc(uint size)
#if DEBUG
// 检查头部完整性
auto head = (MemoryBlock*)Address;
assert(head->Used <= Size && (uint)head + head->Used <= (uint)head->Next, "堆头被破坏!");
assert(head->Used <= (uint)Size && (uint)head + head->Used <= (uint)head->Next, "堆头被破坏!");
assert(_Used <= Size, "Heap::Used异常");
#endif

View File

@ -6,23 +6,23 @@ class Heap
{
public:
uint Address;// 开始地址
uint Size; // 大小
int Size; // 大小
Heap(uint addr, uint size);
Heap(uint addr, int size);
uint Used() const; // 已使用内存数
uint Count() const; // 已使用内存块数
uint FreeSize() const; // 可用内存数
int Used() const; // 已使用内存数
int Count() const; // 已使用内存块数
int FreeSize() const; // 可用内存数
void* Alloc(uint size);
void* Alloc(int size);
void Free(void* ptr);
// 当前堆
static Heap* Current;
private:
uint _Used;
uint _Count;
int _Used;
int _Count;
void* _First; // 第一个有空闲的内存块,加速搜索
};

View File

@ -179,7 +179,7 @@ TaskScheduler::TaskScheduler(cstring name)
}
// 使用外部缓冲区初始化任务列表,避免频繁的堆分配
void TaskScheduler::Set(Task* tasks, uint count)
void TaskScheduler::Set(Task* tasks, int count)
{
for(int i=0; i<count; i++)
{
@ -358,9 +358,9 @@ INROOT void TaskScheduler::Execute(uint msMax, bool& cancel)
Sleeping = true;
// 通知外部,需要睡眠若干毫秒
if(EnterSleep)
EnterSleep(min);
EnterSleep((int)min);
else
Time.Sleep(min, &Sleeping);
Time.Sleep((int)min, &Sleeping);
Sleeping = false;
// 累加睡眠时间
@ -433,7 +433,7 @@ void TaskScheduler::ShowStatus()
debug_printf("Task::ShowStatus [%d]", ts);
debug_printf(" 负载 %d.%d%%", p/100, p%100);
debug_printf(" 平均 %dus 当前 ", ts ? ct/ts : 0);
DateTime(now/1000).Show();
DateTime((int)(now/1000)).Show();
debug_printf(" 启动 ");
TimeSpan(now).Show(false);

View File

@ -77,14 +77,14 @@ public:
UInt64 TotalSleep; // 所有任务的总睡眠时间ms
UInt64 LastTrace; // 最后统计跟踪时间ms
typedef void (*SAction)(uint ms);
typedef void (*SAction)(int ms);
SAction EnterSleep; // 通知外部,需要睡眠若干毫秒
Func ExitSleep; // 通知外部,要求退出睡眠,恢复调度
TaskScheduler(cstring name = nullptr);
// 使用外部缓冲区初始化任务列表,避免频繁的堆分配
void Set(Task* tasks, uint count);
void Set(Task* tasks, int count);
// 查找任务 返回使用此函数的首个任务的ID
uint FindID(Action func);
// 查找任务 返回使用此函数的首个任务

View File

@ -44,7 +44,7 @@ void TTime::SetTime(UInt64 sec)
{
if (sec >= BASE_YEAR_US) sec -= BASE_YEAR_US;
BaseSeconds = (uint)(sec - Seconds);
BaseSeconds = (int)(sec - Seconds);
#if DEBUG
/*DateTime dt(sec);

View File

@ -36,7 +36,7 @@ Buffer BinaryPair::Get(cstring name) const
String sn = name;
auto& ms = *_s;
uint p = ms.Position();
int p = ms.Position();
for(int i=0; i<2; i++)
{
while(ms.Remain() && (i ==0 || ms.Position() < p))

View File

@ -4,9 +4,9 @@
#define NET_DEBUG DEBUG
//#define NET_DEBUG 0
#if NET_DEBUG
#define net_printf debug_printf
#define net_printf debug_printf
#else
#define net_printf(format, ...)
#define net_printf(format, ...)
#endif
List<NetworkInterface*> NetworkInterface::All;
@ -52,25 +52,25 @@ NetworkInterface::~NetworkInterface()
debug_printf("Network::Remove 0x%p\r\n", this);
All.Remove(this);
if(!Opened) Close();
if (!Opened) Close();
}
bool NetworkInterface::Open()
{
if(Opened) return true;
if (Opened) return true;
// 打开接口
Opened = OnOpen();
// 建立连接任务
_taskLink = Sys.AddTask([](void* s){ ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测");
_taskLink = Sys.AddTask([](void* s) { ((NetworkInterface*)s)->OnLoop(); }, this, 0, 1000, "网络检测");
return Opened;
}
void NetworkInterface::Close()
{
if(!Opened) return;
if (!Opened) return;
Sys.RemoveTask(_taskLink);
@ -89,10 +89,10 @@ void NetworkInterface::OnLoop()
{
bool old = Linked;
// 未连接时执行连接
if(!old)
if (!old)
{
bool link = OnLink(++_retry);
if(link ^ old)
if (link ^ old)
{
debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开");
Linked = link;
@ -103,7 +103,7 @@ void NetworkInterface::OnLoop()
{
// 检测并通知状态改变
bool link = CheckLink();
if(link ^ old)
if (link ^ old)
{
debug_printf("%s::Change %s => %s \r\n", Name, old ? "连接" : "断开", link ? "连接" : "断开");
Linked = link;
@ -115,10 +115,10 @@ void NetworkInterface::OnLoop()
Socket* NetworkInterface::CreateClient(const NetUri& uri)
{
if(!Active()) return nullptr;
if (!Active()) return nullptr;
auto socket = CreateSocket(uri.Type);
if(socket)
if (socket)
{
socket->Local.Address = uri.Address;
socket->Local.Port = uri.Port;
@ -129,10 +129,10 @@ Socket* NetworkInterface::CreateClient(const NetUri& uri)
Socket* NetworkInterface::CreateRemote(const NetUri& uri)
{
if(!Active()) return nullptr;
if (!Active()) return nullptr;
auto socket = CreateSocket(uri.Type);
if(socket)
if (socket)
{
socket->Local.Address = IP;
socket->Remote.Address = uri.Address;
@ -150,7 +150,7 @@ void NetworkInterface::InitConfig()
// 随机IP取ID最后一个字节
IP = defip;
byte first = Sys.ID[0];
if(first <= 1 || first >= 254) first = 2;
if (first <= 1 || first >= 254) first = 2;
IP[3] = first;
Mask = IPAddress(255, 255, 255, 0);
@ -166,12 +166,12 @@ void NetworkInterface::InitConfig()
// 第一个字节最低位为1表示组播地址所以第一个字节必须是偶数
mac[0] = 'N';
mac[1] = 'X';
for(int i=0; i< 4; i++)
for (int i = 0; i < 4; i++)
mac[2 + i] = Sys.ID[3 - i];
// 多网卡时MAC不能相同
int k=0;
for(;k<All.Count() && All[k]!=this;k++);
int k = 0;
for (; k < All.Count() && All[k] != this; k++);
mac[5] += k;
Mode = NetworkType::Wire;
@ -182,10 +182,10 @@ void NetworkInterface::InitConfig()
bool NetworkInterface::LoadConfig()
{
auto cfg = Config::Current;
if(!cfg) return false;
if (!cfg) return false;
auto nc = (NetConfig*)cfg->Get(Name);
if(!nc) return false;
if (!nc) return false;
debug_printf("%s::Load %d 字节\r\n", Name, sizeof(NetConfig));
@ -206,7 +206,7 @@ bool NetworkInterface::LoadConfig()
bool NetworkInterface::SaveConfig() const
{
auto cfg = Config::Current;
if(!cfg) return false;
if (!cfg) return false;
NetConfig nc;
nc.IP = IP.Value;
@ -225,7 +225,7 @@ bool NetworkInterface::SaveConfig() const
debug_printf("%s::Save %d 字节\r\n", Name, sizeof(nc));
Buffer bs(&nc, sizeof(nc));
return cfg->Set(Name, bs);
return cfg->Set(Name, bs) != nullptr;
}
void NetworkInterface::ShowConfig() const
@ -243,14 +243,14 @@ void NetworkInterface::ShowConfig() const
//DHCPServer.Show();
net_printf("\r\n DNS:\t");
DNSServer.Show();
if(!DNSServer2.IsAny())
if (!DNSServer2.IsAny())
{
net_printf("\r\n DNS2:\t");
DNSServer2.Show();
}
net_printf("\r\n Speed:\t%dMps", Speed);
net_printf("\r\n Mode:\t");
switch(Mode)
switch (Mode)
{
case NetworkType::Wire:
net_printf("有线");
@ -302,10 +302,10 @@ bool WiFiInterface::IsAP() const
Socket* WiFiInterface::CreateClient(const NetUri& uri)
{
// WiFi无需连接路由器就可以建立本地监听
if(!Opened) return nullptr;
if (!Opened) return nullptr;
auto socket = CreateSocket(uri.Type);
if(socket)
if (socket)
{
socket->Local.Address = uri.Address;
socket->Local.Port = uri.Port;
@ -316,12 +316,12 @@ Socket* WiFiInterface::CreateClient(const NetUri& uri)
void WiFiInterface::OnInitConfig()
{
if(!SSID)
if (!SSID)
SSID = new String();
else
SSID->Clear();
if(!Pass)
if (!Pass)
Pass = new String();
else
Pass->Clear();
@ -330,19 +330,19 @@ void WiFiInterface::OnInitConfig()
void WiFiInterface::OnLoadConfig(const Buffer& buf)
{
Stream ms(buf);
if(SSID) *SSID = ms.ReadString();
if(Pass) *Pass = ms.ReadString();
if (SSID) *SSID = ms.ReadString();
if (Pass) *Pass = ms.ReadString();
}
void WiFiInterface::OnSaveConfig(Buffer& buf) const
{
Stream ms(buf);
if(SSID) ms.WriteArray(*SSID);
if(Pass) ms.WriteArray(*Pass);
if (SSID) ms.WriteArray(*SSID);
if (Pass) ms.WriteArray(*Pass);
}
void WiFiInterface::OnShowConfig() const
{
if(SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); }
if(Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); }
if (SSID) { net_printf("\r\n SSID:\t"); SSID->Show(false); }
if (Pass) { net_printf("\r\n Pass:\t"); Pass->Show(false); }
}

View File

@ -1,4 +1,4 @@
#include "Kernel\Sys.h"
#include "Kernel\Sys.h"
#if DEBUG
static void TestAssign()

View File

@ -1,4 +1,4 @@
#include "Kernel\Sys.h"
#include "Kernel\Sys.h"
#include "Core\Dictionary.h"
#if DEBUG

View File

@ -7,7 +7,7 @@ void TestFlash()
debug_printf("TestFlash Start......\r\n");
byte buf[] = "STM32F10x SPI Firmware Library Example: communication with an AT45DB SPI FLASH";
uint size = ArrayLength(buf);
int size = ArrayLength(buf);
Flash flash;
// 凑一个横跨两页的地址

View File

@ -1,4 +1,4 @@
#include "Kernel\Sys.h"
#include "Kernel\Sys.h"
#include "Message\Json.h"
#if DEBUG

View File

@ -1,4 +1,4 @@
#include "Kernel\Sys.h"
#include "Kernel\Sys.h"
#include "Core\List.h"
#if DEBUG

View File

@ -28,7 +28,7 @@ TinyController::TinyController() : Controller()
while(Address < 2 || Address > 254)
{
Sys.Delay(30);
Address = Sys.Ms();
Address = (byte)Sys.Ms();
}
// 接收模式。0只收自己1接收自己和广播2接收所有。默认0
@ -350,7 +350,7 @@ void TinyController::AckRequest(const TinyMessage& msg)
auto& node = _Queue[i];
if(node.Using && node.Seq == msg.Seq)
{
int cost = Sys.Ms() - node.StartTime;
int cost = (int)(Sys.Ms() - node.StartTime);
if(cost < 0) cost = -cost;
Total.Cost += cost;

View File

@ -105,7 +105,7 @@ void TinyMessage::Write(Stream& ms) const
// 实际数据拷贝到占位符
auto p = (TinyMessage*)this;
p->_Code = Code;
p->_Length = len;
p->_Length = (byte)len;
p->_Reply = Reply;
p->_Error = Error;