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[])
{
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<void *>args;
CPart::addArg<double>(&args, 12.63);

16
cpart.h
View File

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

View File

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