基础类库和系统内核尽可能的使用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

@ -52,7 +52,7 @@ bool Alarm::Set(const Pair& args, Stream& result)
// 马上调度一次
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);
@ -38,7 +38,7 @@ Stream::Stream(const Buffer& bs)
Stream::~Stream() { }
void Stream::Init(void* buf, uint len)
void Stream::Init(void* buf, int len)
{
assert(buf, "buf");
@ -51,9 +51,9 @@ 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)
{
@ -69,11 +69,11 @@ 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)
@ -91,7 +91,7 @@ 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)
@ -108,9 +108,9 @@ 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)
{
@ -124,7 +124,7 @@ uint Stream::ReadEncodeInt()
}
// 读取数据到字节数组,由字节数组指定大小。不包含长度前缀
uint Stream::Read(Buffer& bs)
int Stream::Read(Buffer& bs)
{
if (bs.Length() == 0) return 0;
@ -140,7 +140,7 @@ uint Stream::Read(Buffer& bs)
}
// 写入7位压缩编码整数
uint Stream::WriteEncodeInt(uint value)
int Stream::WriteEncodeInt(int value)
{
if (!CanWrite) return 0;
@ -168,7 +168,7 @@ bool Stream::Write(const Buffer& bs)
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;
// 内容长度不是累加,而是根据位置而扩大
@ -197,9 +197,9 @@ int Stream::Peek() const
}
// 从数据流读取变长数据到字节数组。以压缩整数开头表示长度
uint Stream::ReadArray(Buffer& bs)
int Stream::ReadArray(Buffer& bs)
{
uint len = ReadEncodeInt();
int len = ReadEncodeInt();
if (!len)
{
bs.SetLength(0);
@ -320,7 +320,7 @@ bool Stream::Write(UInt64 value)
/******************************** MemoryStream ********************************/
MemoryStream::MemoryStream(uint len) : Stream(_Arr, ArrayLength(_Arr))
MemoryStream::MemoryStream(int len) : Stream(_Arr, ArrayLength(_Arr))
{
Length = 0;
_needFree = false;
@ -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;
}
@ -347,17 +347,17 @@ MemoryStream::~MemoryStream()
}
}
bool MemoryStream::CheckRemain(uint count)
bool MemoryStream::CheckRemain(int count)
{
uint remain = _Capacity - _Position;
int remain = _Capacity - _Position;
// 容量不够,需要扩容
if (count > remain)
{
if (!CanResize) return Stream::CheckRemain(count);
// 原始容量成倍扩容
uint total = _Position + count;
uint size = _Capacity;
int total = _Position + count;
int size = _Capacity;
if (size < 0x10) size = 0x10;
while (size < total) size <<= 1;
@ -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

@ -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

@ -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

@ -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

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;