This commit is contained in:
Saturneic 2019-01-18 12:42:57 +08:00
parent d94f2e54e6
commit 073b9d734d
3 changed files with 14 additions and 10 deletions

View File

@ -13,7 +13,7 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
try { try {
Server client(9049,"127.0.0.1",9048); CNodeServer client(9049,"127.0.0.1",9048);
vector<int> fargs = {1,0,0,1}; vector<int> fargs = {1,0,0,1};
vector<void *>args; vector<void *>args;
CPart::addArg<double>(&args, 12.63); CPart::addArg<double>(&args, 12.63);

16
cpart.h
View File

@ -91,17 +91,16 @@ public:
void Clear(void); void Clear(void);
// 在对象的传入参数列表中添加参数值 // 在对象的传入参数列表中添加参数值
template<class T> void addArgsIn(T value){ void AddCPArgsIn(void *arg){
T *p_value = new T(value); void *p_value = main_pool.b_get(arg);
if(p_value == nullptr) throw "fail to malloc"; if(p_value == nullptr) throw "information lost";
args_in.push_back(p_value); args_in.push_back(p_value);
} }
// 一般由lib文件中的计算模块调用的向vector中添加参数并分配内存空间而后初始化 // 一般由lib文件中的计算模块调用的向vector中添加参数并分配内存空间而后初始化
template<class T> static void addArg(vector<void *> *args, void *arg){
static void addArg(vector<void *> *args,T value){ void *p_value = main_pool.b_get(arg);
T *p_value = (T *) main_pool.b_malloc(sizeof(T)); if(p_value == nullptr) throw "information lost";
*p_value = value;
if(p_value == nullptr) throw "fail to malloc";
args->push_back(p_value); args->push_back(p_value);
} }
// 一般由lib文件中的计算模块调用的从vector中获得参数并释放其占用的内存空间而后返回相关值 // 一般由lib文件中的计算模块调用的从vector中获得参数并释放其占用的内存空间而后返回相关值
@ -117,7 +116,6 @@ public:
p_value = main_pool.b_free(p_value); p_value = main_pool.b_free(p_value);
T value = *p_value; T value = *p_value;
args->pop_back(); args->pop_back();
return value; return value;
} }
}; };

View File

@ -20,14 +20,17 @@ struct block_info{
}; };
class BlocksPool{ class BlocksPool{
// 内存块表
map<void *, block_info> blocks_list; map<void *, block_info> blocks_list;
public: public:
// 声明某内存块
void *b_malloc(uint32_t size){ void *b_malloc(uint32_t size){
void *ptr = malloc(size); void *ptr = malloc(size);
if(ptr == nullptr) return nullptr; if(ptr == nullptr) return nullptr;
blocks_list.insert({ptr,{size,1}}); blocks_list.insert({ptr,{size,1}});
return ptr; return ptr;
} }
// 标记使用某内存块
void *b_get(void *ptr){ void *b_get(void *ptr){
auto blk = blocks_list.find(ptr); auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){ if(blk != blocks_list.end()){
@ -36,6 +39,7 @@ public:
} }
else return nullptr; else return nullptr;
} }
// 标记保护某内存块
void b_protect(void *ptr){ void b_protect(void *ptr){
auto blk = blocks_list.find(ptr); auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){ if(blk != blocks_list.end()){
@ -43,6 +47,7 @@ public:
} }
else throw "protect nil value"; else throw "protect nil value";
} }
// 标记不再保护某内存块
void b_noprotect(void *ptr){ void b_noprotect(void *ptr){
auto blk = blocks_list.find(ptr); auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end()){ if(blk != blocks_list.end()){
@ -50,6 +55,7 @@ public:
} }
else throw "noprotect nil value"; else throw "noprotect nil value";
} }
// 标记不再使用某内存块
void b_free(void *ptr){ void b_free(void *ptr){
auto blk = blocks_list.find(ptr); auto blk = blocks_list.find(ptr);
if(blk != blocks_list.end() && blk->second.pted == false){ if(blk != blocks_list.end() && blk->second.pted == false){