@@ -19,12 +19,58 @@ std::map<std::string, const char *> SID_USRTAG;
19
19
20
20
PYBIND11_EMBEDDED_MODULE (aiges_embed, module ) {
21
21
module .def (" callback" , &callBack, py::return_value_policy::automatic_reference);
22
- py::class_<ResponseData> responseData (module , " ResponseData" );
22
+ py::class_<ResponseData> responseData (module , " ResponseData" , py::buffer_protocol () );
23
23
responseData.def (py::init<>())
24
+ .def (py::init<std::string, unsigned int , int , int >())
25
+ .def (py::init ([](const py::buffer &b) {
26
+ py::buffer_info info = b.request ();
27
+ if (info.format != py::format_descriptor<unsigned char >::format () || info.ndim != 1 ) {
28
+ throw std::runtime_error (" Incompatible buffer format! Please Pass Bytes..." );
29
+ }
30
+ auto *v = new ResponseData ();
31
+ v->len = info.shape [0 ];
32
+ v->data = info.ptr ;
33
+ // memcpy( v->data, info.ptr, info.shape[0] );
34
+ return (v);
35
+ }))
36
+ .def (" setData" , [](ResponseData &r, const py::buffer &b) {
37
+ py::buffer_info info = b.request ();
38
+ if (info.format != py::format_descriptor<unsigned char >::format () || info.ndim != 1 ) {
39
+ throw std::runtime_error (" Incompatible buffer format! Please Pass Bytes.." );
40
+ }
41
+ if (r.data == nullptr ) {
42
+ void *p = malloc (info.shape [0 ]);
43
+ if (p == nullptr ) {
44
+ throw std::runtime_error (" Can't Allocate memory!" );
45
+ }
46
+ r.data = p;
47
+ }
48
+ r.len = info.shape [0 ];
49
+ r.data = info.ptr ;
50
+ // memcpy(r.data, info.ptr, info.shape[0]);
51
+ })
52
+ /* / Bare bones interface */
53
+ .def (" setDataType" ,
54
+ [](ResponseData &r, int i) {
55
+ r.type = i;
56
+ })
57
+ /* / Provide buffer access */
58
+ .def_buffer ([](ResponseData &r) -> py::buffer_info {
59
+ return (py::buffer_info (
60
+ r.data , /* Pointer to buffer */
61
+ sizeof (unsigned char ), /* Size of one scalar */
62
+ py::format_descriptor<unsigned char >::format (), /* Python struct-style format descriptor */
63
+ 1 , /* Number of dimensions */
64
+ {size_t (r.len )}, /* Buffer dimensions */
65
+ { /* Strides (in bytes) for each index */
66
+ sizeof (unsigned char )}
67
+ ));
68
+ })
24
69
.def_readwrite (" key" , &ResponseData::key, py::return_value_policy::automatic_reference)
25
- .def_readwrite (" data" , &ResponseData::data, py::return_value_policy::automatic_reference)
70
+ .def_property_readonly (" data" ,
71
+ py::cpp_function (&ResponseData::get_data, py::return_value_policy::automatic_reference))
26
72
.def_readwrite (" status" , &ResponseData::status, py::return_value_policy::automatic_reference)
27
- .def_readwrite (" len" , &ResponseData::len , py::return_value_policy::automatic_reference)
73
+ .def_property_readonly (" len" , py::cpp_function ( &ResponseData::get_len , py::return_value_policy::automatic_reference) )
28
74
.def_readwrite (" type" , &ResponseData::type, py::return_value_policy::automatic_reference);
29
75
30
76
py::class_<Response> response (module , " Response" );
@@ -263,19 +309,20 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
263
309
tmpData->type = DataType (itemData.type );
264
310
tmpData->desc = nullptr ;
265
311
// 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
266
- void *pr;
267
- pr = malloc (itemData.len );
268
- if (pr == nullptr ) {
269
- int ret = -1 ;
270
- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid:{}" , sid);
271
- return ret;
272
- }
273
- ptr = PyBytes_AsString (itemData.data .ptr ());
274
- Py_ssize_t size = PyBytes_GET_SIZE (itemData.data .ptr ());
275
- memcpy (pr, ptr, itemData.len );
312
+ // void *pr;
313
+ // pr = malloc(itemData.len);
314
+ // if (pr == nullptr) {
315
+ // int ret = -1;
316
+ // spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
317
+ // return ret;
318
+ // }
319
+ // ptr = PyBytes_AsString(itemData.data.ptr());
320
+ // Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
321
+ // printf("GetSIze data len: %d", itemData.len);
322
+ // memcpy(pr, ptr, itemData.len);
276
323
// char *data_ = new char[itemData.data.length()+1];
277
324
// strdup(.c_str());
278
- tmpData->data = pr ;
325
+ tmpData->data = itemData. data ;
279
326
tmpData->status = DataStatus (itemData.status );
280
327
if (idx == 0 ) {
281
328
headPtr = tmpData;
@@ -284,10 +331,9 @@ int PyWrapper::wrapperOnceExec(const char *usrTag, std::map <std::string, std::s
284
331
curPtr->next = tmpData;
285
332
curPtr = tmpData;
286
333
}
287
- spdlog::debug (" get result,key:{},data:{},len:{},size:{}, type:{},status:{},sid:{}" ,
288
- tmpData->key , (char *) tmpData->data , tmpData->len , size, tmpData->type ,
334
+ spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},sid:{}" ,
335
+ tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
289
336
tmpData->status , sid);
290
-
291
337
}
292
338
*respData = headPtr;
293
339
}
@@ -416,7 +462,6 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
416
462
resp = r.cast <Response *>();
417
463
pDataList headPtr;
418
464
pDataList curPtr;
419
- char *ptr;
420
465
// 先判断python有没有抛出错误. response中的 errorCode
421
466
if (resp->errCode != 0 ) {
422
467
spdlog::get (" stderr_console" )->error (" find error from python: {}" , resp->errCode );
@@ -437,20 +482,18 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
437
482
tmpData->len = itemData.len ;
438
483
tmpData->type = DataType (itemData.type );
439
484
tmpData->desc = nullptr ;
440
- // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
441
- void *pr;
442
- pr = malloc (itemData.len );
443
- if (pr == nullptr ) {
444
- int ret = -1 ;
445
- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid:{}" , sid);
446
- return ret;
447
- }
448
- ptr = PyBytes_AsString (itemData.data .ptr ());
449
- Py_ssize_t size = PyBytes_GET_SIZE (itemData.data .ptr ());
450
- memcpy (pr, ptr, size);
485
+ // // 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
486
+ // void *pr;
487
+ // pr = malloc(itemData.len);
488
+ // if (pr == nullptr) {
489
+ // int ret = -1;
490
+ // spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
491
+ // return ret;
492
+ // }
493
+ // memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
451
494
// char *data_ = new char[itemData.data.length()+1];
452
495
// strdup(.c_str());
453
- tmpData->data = pr ;
496
+ tmpData->data = itemData. data ;
454
497
tmpData->status = DataStatus (itemData.status );
455
498
if (idx == 0 ) {
456
499
headPtr = tmpData;
@@ -459,8 +502,8 @@ int PyWrapper::wrapperRead(char *handle, pDataList *respData, std::string sid) {
459
502
curPtr->next = tmpData;
460
503
curPtr = tmpData;
461
504
}
462
- spdlog::debug (" callback result,key:{},data:{},len:{},size, {},type:{},status:{},sid:{}" ,
463
- tmpData->key , (char *) tmpData->data , tmpData->len , size, tmpData->type ,
505
+ spdlog::debug (" get result,key:{},data:{},len:{},type:{},status:{},sid:{}" ,
506
+ tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
464
507
tmpData->status , sid);
465
508
}
466
509
*respData = headPtr;
@@ -559,20 +602,20 @@ int callBack(Response *resp, std::string sid) {
559
602
tmpData->type = DataType (itemData.type );
560
603
tmpData->desc = nullptr ;
561
604
// 这里判断数据类型,todo 未来根据数据类型 决定是否拷贝,比如某些数据比较大,可以不拷贝
562
- void *pr;
563
- pr = malloc (itemData.len );
564
- if (pr == nullptr ) {
565
- int ret = -1 ;
566
- spdlog::get (" stderr_console" )->error (" can't malloc memory for data, sid:{}" , sid);
567
- return ret;
568
- }
569
- ptr = PyBytes_AsString (itemData.data .ptr ());
570
- Py_ssize_t size = PyBytes_GET_SIZE (itemData.data .ptr ());
571
- memcpy (pr, ptr, itemData.len );
605
+ // void *pr;
606
+ // pr = malloc(itemData.len);
607
+ // if (pr == nullptr) {
608
+ // int ret = -1;
609
+ // spdlog::get("stderr_console")->error("can't malloc memory for data, sid:{}", sid);
610
+ // return ret;
611
+ // }
612
+ // ptr = PyBytes_AsString(itemData.data.ptr());
613
+ // Py_ssize_t size = PyBytes_GET_SIZE(itemData.data.ptr());
614
+ // memcpy(pr, ptr, itemData.len);
572
615
// 还是有问题::memcpy(pr, (const void *) itemData.data.ptr(), itemData.len);
573
616
// char *data_ = new char[itemData.data.length()+1];
574
617
// strdup(.c_str());
575
- tmpData->data = pr ;
618
+ tmpData->data = itemData. data ;
576
619
tmpData->status = DataStatus (itemData.status );
577
620
if (idx == 0 ) {
578
621
headPtr = tmpData;
@@ -581,8 +624,8 @@ int callBack(Response *resp, std::string sid) {
581
624
curPtr->next = tmpData;
582
625
curPtr = tmpData;
583
626
}
584
- spdlog::debug (" callback result,key:{},data:{},len:{},size,{}, type:{},status:{},sid:{}" ,
585
- tmpData->key , (char *) tmpData->data , tmpData->len , size, tmpData->type ,
627
+ spdlog::debug (" callback result,key:{},data:{},len:{},type:{},status:{},sid:{}" ,
628
+ tmpData->key , (char *) tmpData->data , tmpData->len , tmpData->type ,
586
629
tmpData->status , sid);
587
630
}
588
631
0 commit comments