调整结构
调整了CPart 的结构与计算模块获取参数的方式,使之更加合理。为以后的对更加复杂的传参要求的支持奠定了基础。
This commit is contained in:
parent
073b9d734d
commit
3138d0f68a
@ -15,6 +15,7 @@
|
|||||||
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29921F0C5CC00340EFA /* clock.cpp */; };
|
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29921F0C5CC00340EFA /* clock.cpp */; };
|
||||||
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29D21F0C72C00340EFA /* addr.cpp */; };
|
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29D21F0C72C00340EFA /* addr.cpp */; };
|
||||||
92A1F2A121F1663300340EFA /* memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29F21F1663300340EFA /* memory.cpp */; };
|
92A1F2A121F1663300340EFA /* memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29F21F1663300340EFA /* memory.cpp */; };
|
||||||
|
92A1F2A521F21DC700340EFA /* a.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F2A421F21DC700340EFA /* a.cpp */; };
|
||||||
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92D6CE6721EE4920005AEF3B /* server.cpp */; };
|
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92D6CE6721EE4920005AEF3B /* server.cpp */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
@ -48,6 +49,7 @@
|
|||||||
92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = "<group>"; };
|
92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = "<group>"; };
|
||||||
92A1F29F21F1663300340EFA /* memory.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = memory.cpp; sourceTree = "<group>"; };
|
92A1F29F21F1663300340EFA /* memory.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = memory.cpp; sourceTree = "<group>"; };
|
||||||
92A1F2A021F1663300340EFA /* memory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = memory.h; sourceTree = "<group>"; };
|
92A1F2A021F1663300340EFA /* memory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = memory.h; sourceTree = "<group>"; };
|
||||||
|
92A1F2A421F21DC700340EFA /* a.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = a.cpp; path = build/Debug/PCS/a.cpp; sourceTree = "<group>"; };
|
||||||
92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
|
92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
|
||||||
92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
|
92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
@ -82,6 +84,7 @@
|
|||||||
9221DA0E21EB5FAD007310A7 /* Net */ = {
|
9221DA0E21EB5FAD007310A7 /* Net */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
92A1F2A421F21DC700340EFA /* a.cpp */,
|
||||||
925A13AA21EC989500CBD427 /* include */,
|
925A13AA21EC989500CBD427 /* include */,
|
||||||
9221DA1021EB5FB8007310A7 /* main.cpp */,
|
9221DA1021EB5FB8007310A7 /* main.cpp */,
|
||||||
92A1F29721F0C19500340EFA /* socket.cpp */,
|
92A1F29721F0C19500340EFA /* socket.cpp */,
|
||||||
@ -169,6 +172,7 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
9221DA1121EB5FB8007310A7 /* main.cpp in Sources */,
|
9221DA1121EB5FB8007310A7 /* main.cpp in Sources */,
|
||||||
|
92A1F2A521F21DC700340EFA /* a.cpp in Sources */,
|
||||||
925A13A621EC68D500CBD427 /* cpart.cpp in Sources */,
|
925A13A621EC68D500CBD427 /* cpart.cpp in Sources */,
|
||||||
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */,
|
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */,
|
||||||
925A13A921EC973000CBD427 /* cmap.cpp in Sources */,
|
925A13A921EC973000CBD427 /* cmap.cpp in Sources */,
|
||||||
|
21
cpart.cpp
21
cpart.cpp
@ -17,7 +17,7 @@
|
|||||||
@param name 计算模块的名字
|
@param name 计算模块的名字
|
||||||
@param ffresh 每次建立该结构都重新编译一次源文件
|
@param ffresh 每次建立该结构都重新编译一次源文件
|
||||||
*/
|
*/
|
||||||
CPart::CPart(string src_path,string src_name,string name,bool ffresh):func(nullptr),handle(nullptr),libargs_in(nullptr),libargs_out(nullptr){
|
CPart::CPart(string src_path,string src_name,string name,bool ffresh):func(nullptr),handle(nullptr){
|
||||||
this->src_path = src_path;
|
this->src_path = src_path;
|
||||||
this->name = name;
|
this->name = name;
|
||||||
|
|
||||||
@ -59,17 +59,17 @@ int CPart::BuildSo(void){
|
|||||||
*/
|
*/
|
||||||
int CPart::GetSo(void){
|
int CPart::GetSo(void){
|
||||||
// 读取lib文件
|
// 读取lib文件
|
||||||
this->handle = dlopen(("Libs/"+libname).data(), RTLD_NOW | RTLD_GLOBAL);
|
this->handle = dlopen(("Libs/"+libname).data(), RTLD_NOW | RTLD_LOCAL);
|
||||||
if(this->handle == nullptr) throw "can not open lib file";
|
if(this->handle == nullptr) throw "can not open lib file";
|
||||||
// 获得该模块的入口
|
// 获得该模块的入口
|
||||||
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
|
this->func = (PCSFUNC) dlsym(this->handle, this->name.data());
|
||||||
if(this->func == nullptr) throw "can not get func "+this->name;
|
if(this->func == nullptr) throw "can not get func "+this->name;
|
||||||
// 获得向该模块传入参数的vector的地址
|
// 获得向该模块传入参数的vector的地址
|
||||||
this->libargs_in = (vector<void *> *) dlsym(this->handle, ("__"+name+"_args_in").data());
|
this->libargs_in.args = (vector<block_info> *) dlsym(this->handle, ("__"+name+"_args_in").data());
|
||||||
if(this->libargs_in == nullptr) throw "can not get the address of __"+name+"_args_in";
|
if(this->libargs_in.args == nullptr) throw "can not get the address of __"+name+"_args_in";
|
||||||
// 获得该函数传出参数所在的vector的地址
|
// 获得该函数传出参数所在的vector的地址
|
||||||
this->libargs_out = (vector<void *> *) dlsym(this->handle, ("__"+name+"_args_out").data());
|
this->libargs_out.args = (vector<block_info> *) dlsym(this->handle, ("__"+name+"_args_out").data());
|
||||||
if(this->libargs_out == nullptr) throw "can not get the address of __"+name+"_args_out";
|
if(this->libargs_out.args == nullptr) throw "can not get the address of __"+name+"_args_out";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,16 +103,17 @@ void CPart::setArgsType(vector<int> fargs_in, vector<int> fargs_out){
|
|||||||
int CPart::Run(void){
|
int CPart::Run(void){
|
||||||
if(func == nullptr) throw "func is nullptr";
|
if(func == nullptr) throw "func is nullptr";
|
||||||
// 对计算模块传入参数
|
// 对计算模块传入参数
|
||||||
for(auto arg : args_in) libargs_in->push_back(arg);
|
for(auto arg : args_in)
|
||||||
|
libargs_in.addArgPtr(main_pool.size(arg), arg);
|
||||||
// 执行计算模块
|
// 执行计算模块
|
||||||
if(func() == SUCCESS){
|
if(func() == SUCCESS){
|
||||||
//储存计算结果
|
//储存计算结果
|
||||||
for(auto arg : *libargs_out){
|
for(auto libarg : *libargs_out.args){
|
||||||
// 获得内存块的访问量
|
// 获得内存块的访问量
|
||||||
main_pool.b_get(arg);
|
void *arg = main_pool.bp_malloc(libarg.size, libarg.pvle);
|
||||||
args_out.push_back(arg);
|
args_out.push_back(arg);
|
||||||
}
|
}
|
||||||
libargs_out->clear();
|
libargs_out.clear();
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
else return -1;
|
else return -1;
|
||||||
|
85
cpart.h
85
cpart.h
@ -13,17 +13,17 @@
|
|||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
|
||||||
//声明计算模块的传入与传出参数列表
|
//声明计算模块的传入与传出参数列表
|
||||||
#define ARGS_DECLAER(name) vector<void *> __##name##_args_in, __##name##_args_out
|
#define ARGS_DECLAER(name) vector<block_info> __##name##_args_in, __##name##_args_out
|
||||||
//声明计算模块的入口
|
//声明计算模块的入口
|
||||||
#define PCSFUNC_DEFINE(name) extern "C" int name(void)
|
#define PCSFUNC_DEFINE(name) extern "C" int name(void)
|
||||||
|
|
||||||
#define GET_ARGS(name,type) CPart::popArg<type>(&__##name##_args_in)
|
#define _PCSFUNC(name) ARGS_DECLAER(name); \
|
||||||
#define ADD_ARGS(name,type,value) CPart::addArg<type>(&__##name##_args_out, value);
|
PCSFUNC_DEFINE(name)
|
||||||
|
|
||||||
//从传入参数列表的第一个值,并删除该值
|
//从传入参数列表的第一个值,并删除该值
|
||||||
#define POP_ARGS(name,type) GET_ARGS( name ,type)
|
#define ARGS_IN(name) __##name##_args_in
|
||||||
//向传出参数列表中添加值
|
//向传出参数列表中添加值
|
||||||
#define PUSH_ARGS(name,type,value) ADD_ARGS( name ,type,value)
|
#define ARGS_OUT(name) __##name##_args_out
|
||||||
|
|
||||||
//整型
|
//整型
|
||||||
#define INT 0
|
#define INT 0
|
||||||
@ -49,6 +49,53 @@ public:
|
|||||||
vector<int> args;
|
vector<int> args;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class LibArgsTransfer{
|
||||||
|
public:
|
||||||
|
vector<block_info> *args = nullptr;
|
||||||
|
LibArgsTransfer(vector<block_info> &args){
|
||||||
|
this->args = &args;
|
||||||
|
}
|
||||||
|
LibArgsTransfer(){
|
||||||
|
|
||||||
|
}
|
||||||
|
void addArgPtr(int size, void *p_arg){
|
||||||
|
void *pc_arg = malloc(size);
|
||||||
|
memcpy(pc_arg, p_arg, size);
|
||||||
|
block_info pbifo(size,pc_arg);
|
||||||
|
args->push_back(pbifo);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
T getArg(int idx){
|
||||||
|
T *pvle = (T *)(*args)[idx].pvle;
|
||||||
|
if((*args)[idx].size == sizeof(T)) return *pvle;
|
||||||
|
else throw "arg size conflict";
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
T *getArgPtr(int idx){
|
||||||
|
T *pvle = (*args)[idx].pvle;
|
||||||
|
return pvle;
|
||||||
|
}
|
||||||
|
void LibAddArgPtr(int size, void *p_arg){
|
||||||
|
block_info pbifo(size,p_arg);
|
||||||
|
args->push_back(pbifo);
|
||||||
|
}
|
||||||
|
template<class T>
|
||||||
|
void LibAddArg(T arg){
|
||||||
|
T *p_arg = (T *)malloc(sizeof(T));
|
||||||
|
*p_arg = arg;
|
||||||
|
block_info pbifo(sizeof(T),p_arg);
|
||||||
|
args->push_back(pbifo);
|
||||||
|
}
|
||||||
|
void clear(void){
|
||||||
|
for(auto arg : *args)
|
||||||
|
free(arg.pvle);
|
||||||
|
args->clear();
|
||||||
|
}
|
||||||
|
~LibArgsTransfer(){
|
||||||
|
//clear();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
//计算模块类
|
//计算模块类
|
||||||
class CPart{
|
class CPart{
|
||||||
public:
|
public:
|
||||||
@ -57,7 +104,7 @@ public:
|
|||||||
// 参数操纵列表
|
// 参数操纵列表
|
||||||
vector<void *> args_in, args_out;
|
vector<void *> args_in, args_out;
|
||||||
// lib文件中相关参数操纵列表的地址
|
// lib文件中相关参数操纵列表的地址
|
||||||
vector<void *> *libargs_in,*libargs_out;
|
LibArgsTransfer libargs_in, libargs_out;
|
||||||
// 所依赖的计算对象列表
|
// 所依赖的计算对象列表
|
||||||
vector<Depends> depends;
|
vector<Depends> depends;
|
||||||
// lib文件中的计算模块的入口地址
|
// lib文件中的计算模块的入口地址
|
||||||
@ -73,8 +120,6 @@ public:
|
|||||||
// 源文件名
|
// 源文件名
|
||||||
string src_name;
|
string src_name;
|
||||||
|
|
||||||
// 当计算模块随着该工具同时编译时可以直接使用该构造函数
|
|
||||||
CPart(PCSFUNC func):func(func),handle(nullptr){}
|
|
||||||
// 一般构造函数,计算模块在文件中以源文件的形式独立存在时使用该构造函数
|
// 一般构造函数,计算模块在文件中以源文件的形式独立存在时使用该构造函数
|
||||||
CPart(string src_path,string src_name,string name,bool ffresh = true);
|
CPart(string src_path,string src_name,string name,bool ffresh = true);
|
||||||
// 析构函数
|
// 析构函数
|
||||||
@ -96,28 +141,8 @@ public:
|
|||||||
if(p_value == nullptr) throw "information lost";
|
if(p_value == nullptr) throw "information lost";
|
||||||
args_in.push_back(p_value);
|
args_in.push_back(p_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 一般由lib文件中的计算模块调用的向vector中添加参数并分配内存空间而后初始化
|
|
||||||
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中获得参数并释放其占用的内存空间而后返回相关值
|
|
||||||
template<class T>
|
|
||||||
static T popArg(vector<void *> *args){
|
|
||||||
if(args == nullptr) throw "the pointer to vector is null";
|
|
||||||
|
|
||||||
T *p_value = (T *)args->back();
|
|
||||||
|
|
||||||
p_value = main_pool.b_get(p_value);
|
|
||||||
if(p_value == nullptr) throw "infomation lost";
|
|
||||||
|
|
||||||
p_value = main_pool.b_free(p_value);
|
|
||||||
T value = *p_value;
|
|
||||||
args->pop_back();
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* cpart_h */
|
#endif /* cpart_h */
|
||||||
|
47
main.cpp
47
main.cpp
@ -14,34 +14,9 @@
|
|||||||
#include "cmap.h"
|
#include "cmap.h"
|
||||||
#include "cthread.h"
|
#include "cthread.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(void){
|
|
||||||
initClock();
|
|
||||||
Server srvr(9048);
|
|
||||||
//srvr.Deamon();
|
|
||||||
vector<int> fargs = {1,0,0,1};
|
|
||||||
vector<void *>args;
|
|
||||||
CPart::addArg<double>(&args, 12.63);
|
|
||||||
CPart::addArg<int>(&args, 10);
|
|
||||||
CPart::addArg<int>(&args, 6);
|
|
||||||
CPart::addArg<double>(&args, 8.2);
|
|
||||||
// 输入过程
|
|
||||||
/* struct compute_result cpur = {"Test",&args,&args,&fargs,&fargs};
|
|
||||||
packet pkt = srvr.CPURS2Packet(cpur);
|
|
||||||
raw_data rwd = srvr.Packet2Rawdata(pkt);*/
|
|
||||||
|
|
||||||
// 输出过程
|
|
||||||
//srvr.Rawdata2Packet(rwd);
|
|
||||||
//srvr.Deamon(NULL);
|
|
||||||
while(1){
|
|
||||||
sleep(100);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CPMT(void){
|
void CPMT(void){
|
||||||
CMap map("./PCS");
|
CMap map("./PCS");
|
||||||
|
|
||||||
CThread thread(&map);
|
CThread thread(&map);
|
||||||
thread.Analyse();
|
thread.Analyse();
|
||||||
thread.DoLine();
|
thread.DoLine();
|
||||||
@ -49,4 +24,24 @@ void CPMT(void){
|
|||||||
thread.CancelChildPCS(0);
|
thread.CancelChildPCS(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
initClock();
|
||||||
|
CPart ncp("./PCS","a.cpp","A");
|
||||||
|
void *a = main_pool.bv_malloc<double>(2.0);
|
||||||
|
void *b = main_pool.bv_malloc<double>(3.5);
|
||||||
|
void *c = main_pool.bv_malloc<int>(5);
|
||||||
|
ncp.AddCPArgsIn(a);
|
||||||
|
ncp.AddCPArgsIn(b);
|
||||||
|
ncp.AddCPArgsIn(c);
|
||||||
|
ncp.Run();
|
||||||
|
void *oa = ncp.args_out[0];
|
||||||
|
printf("%d",*((int *)oa));
|
||||||
|
main_pool.b_free(a);
|
||||||
|
main_pool.b_free(b);
|
||||||
|
main_pool.b_free(c);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
35
memory.h
35
memory.h
@ -11,12 +11,18 @@
|
|||||||
|
|
||||||
#include "type.h"
|
#include "type.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct block_info{
|
struct block_info{
|
||||||
uint32_t size = 0;
|
uint32_t size = 0;
|
||||||
int lock = 0;
|
int lock = 0;
|
||||||
bool pted = false;
|
bool pted = false;
|
||||||
|
void *pvle = nullptr;
|
||||||
|
block_info(uint32_t size,void *pvle){
|
||||||
|
this->size = size;
|
||||||
|
this->pvle = pvle;
|
||||||
|
}
|
||||||
|
block_info(){
|
||||||
|
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class BlocksPool{
|
class BlocksPool{
|
||||||
@ -27,9 +33,32 @@ 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}});
|
|
||||||
|
block_info bifo;
|
||||||
|
bifo.size = size;
|
||||||
|
bifo.lock = 1;
|
||||||
|
bifo.pted = false;
|
||||||
|
bifo.pvle = ptr;
|
||||||
|
|
||||||
|
blocks_list.insert(pair<void *, block_info>(ptr,bifo));
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void *bv_malloc(T value){
|
||||||
|
T *pvalue = (T *)b_malloc(sizeof(T));
|
||||||
|
*pvalue = T(value);
|
||||||
|
return pvalue;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *bp_malloc(uint32_t size, void *ptvle){
|
||||||
|
void *pvalue = b_malloc(size);
|
||||||
|
memcpy(pvalue, ptvle, size);
|
||||||
|
return pvalue;
|
||||||
|
}
|
||||||
|
uint32_t size(void *ptr){
|
||||||
|
return blocks_list.find(ptr)->second.size;
|
||||||
|
}
|
||||||
// 标记使用某内存块
|
// 标记使用某内存块
|
||||||
void *b_get(void *ptr){
|
void *b_get(void *ptr){
|
||||||
auto blk = blocks_list.find(ptr);
|
auto blk = blocks_list.find(ptr);
|
||||||
|
Loading…
Reference in New Issue
Block a user