I am trying to figure out th right way to call this function:
size_t
fz_buffer_storage(fz_context *ctx, fz_buffer *buf, unsigned char **datap)
{
if (datap)
*datap = (buf ? buf->data : NULL);
return (buf ? buf->len : 0);
}
using CGo to get the underlying string and its length as a byte array in Go.
Is this the right way to do it?
var bufferContents *C.uchar
length := C.fz_buffer_storage(ctx, buf, &bufferContents)
bytes := C.GoBytes(unsafe.Pointer(bufferContents), C.int(length))
Since the C code overwrites *datap, I am not sure if the garbage collector will still do the right thing.
I saw an answer here suggesting something along the lines of
var tempUcharPtr *C.uchar
bufferContents := C.malloc(C.size_t(unsafe.Sizeof(tempUcharPtr)))
defer C.free(bufferContents)
length := C.fz_buffer_storage(ctx, buf, (**C.uchar)(bufferContents))
bytes := C.GoBytes(unsafe.Pointer(*(**C.uchar)(bufferContents)), C.int(length))
which also seems to work, but it's much more convoluted and I'm wondering if it's better / safer than the previous version.
Apparently, the first version is fine. Quoting the docs:
Go code may pass a Go pointer to C provided the Go memory to which it points does not contain any Go pointers.
From what I understand, since var bufferContents *C.uchar will be initialised to nil, it does not count as a "Go pointer" for the above rule. The following simplified code examples confirm this:
package main
// void F(char **p) {}
import "C"
func main() {
var p *C.char = new(C.char)
C.F(&p)
}
will trigger "panic: runtime error: cgo argument has Go pointer to Go pointer"
package main
// void F(char **p) {}
import "C"
func main() {
var p *C.char
C.F(&p)
}
works just fine, even when setting GODEBUG=cgocheck=2.
Thanks to the folks on the #cgo channel on the Gophers Slack community for helping me understand this!
I am writing an R package that contains C and Rcpp. The goal is to call the C function from R and within Rcpp, eventually performing most of the analysis in Rcpp and only returning to R for minimal tasks. My package compiles and calling my function from R works fine.
#generate some matrix. Numeric is fine too. Must have column names, no row names
myMat <- matrix(data = 1:100, nrow = 10, ncol = 10,
dimnames = list(NULL, LETTERS[1:10]))
#This works. Put in full path, no expansion. It returns null to the console.
MinimalExample::WriteMat(mat = myMat, file = "Full_Path_Please/IWork.csv",
sep = "," ,eol = "\n", dec = ".", buffMB = 8L)
However, attempting the same thing in Rcpp produces a SIGSEV error. I think the problem is how I am passing arguments to the function, but I can't figure out the proper way.
#include <Rcpp.h>
using namespace Rcpp;
extern "C"{
#include "fwrite.h"
}
//' #export
// [[Rcpp::export]]
void WriteMatCpp(String& fileName, NumericMatrix& testMat){
Rcpp::Rcout<<"I did start!"<<std::endl;
String patchName = fileName;
int whichRow = 1;
std::string newString = std::string(3 - toString(whichRow).length(), '0')
+ toString(whichRow);
patchName.replace_last(".csv", newString+".csv");
//Set objects to pass to print function
String comma = ",";
String eol = "\n";
String dot = ".";
int buffMem = 8;
//This is where I crash, giving a SIGSEV error
fwriteMain(testMat, (SEXP)&patchName, (SEXP)&comma, (SEXP)&eol,
(SEXP)&dot, (SEXP)&buffMem);
}
Here is a link to the GitHub repository with the package. https://github.com/GilChrist19/MinimalExample
Your call from C++ to C is wrong. You can't just write (SEXP)& in front of an arbitrary data structure and hope for it to become a SEXP.
Fix
Use a line such as this to convert what you have in C++ to the SEXP your C function expects using Rcpp::wrap() on each argument:
//This is where I crash, giving a SIGSEV error
fwriteMain(wrap(testMat), wrap(patchName), wrap(comma),
wrap(eol), wrap(dot), wrap(buffMem));
Demo
edd#brad:/tmp/MinimalExample/MinEx(master)$ Rscript RunMe.R
I did start!
edd#brad:/tmp/MinimalExample/MinEx(master)$ cat /tmp/IDoNotWork.csv
A,B,C,D,E,F,G,H,I,J
1,11,21,31,41,51,61,71,81,91
2,12,22,32,42,52,62,72,82,92
3,13,23,33,43,53,63,73,83,93
4,14,24,34,44,54,64,74,84,94
5,15,25,35,45,55,65,75,85,95
6,16,26,36,46,56,66,76,86,96
7,17,27,37,47,57,67,77,87,97
8,18,28,38,48,58,68,78,88,98
9,19,29,39,49,59,69,79,89,99
10,20,30,40,50,60,70,80,90,100
edd#brad:/tmp/MinimalExample/MinEx(master)$
See https://github.com/GilChrist19/MinimalExample/tree/master/MinEx for a complete example.
I have few experience developing C applications and I am having a specific cast problem.
I have a char variable hard coded that I need to pass as a parameter in a function.
char * data = "058dd54970d65c";
This is the function:
PJ_DECL(pj_status_t) pjsua_call_make_call(pjsua_acc_id acc_id,
const pj_str_t *dst_uri,
const pjsua_call_setting *opt,
void *user_data,
const pjsua_msg_data *msg_data,
pjsua_call_id *p_call_id);
In order to use it:
pjsua_call_make_call(acc_id, &uri, 0, NULL, data, NULL);
As you can see I need a cast in the parameter 5. I am getting this error:
note: expected const struct pjsua_msg_data * but argument is of type char *
I try to use sprintf and other solutions but do not work. I would be gratefull if somebody could help me.
For completion i write my comment as new answer.
Create a new object of the pjsua_msg_data struct and fill your data into the msg_body property. After that you can simply use this object with the pjsua_call_make_call call.
Try this:
pjsua_msg_data data_alice;
data_alice.msg_body = data;
pjsua_call_make_call(acc_id, &uri, 0, NULL, data_alice, NULL);
Having successfully built LLVM using MinGW I am now trying to use the C API to implement the program.
As just a starter application to see if the build has been successful I have converted the llvmpy example found here http://www.llvmpy.org/llvmpy-doc/0.9/doc/firstexample.html into (what I think is the) C equivalent however I'm not getting the output I expect from the print function.
My C program is:
#include "llvm-c/Core.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
LLVMInitializeCore(LLVMGetGlobalPassRegistry());
LLVMModuleRef my_module = LLVMModuleCreateWithName("my_module");
LLVMTypeRef ty_int = LLVMInt32Type();
LLVMTypeRef* ParamTypes = new LLVMTypeRef[2];
ParamTypes[0] = ty_int;
ParamTypes[1] = ty_int;
LLVMTypeRef ty_func = LLVMFunctionType(ty_int, ParamTypes, 2, false);
delete[] ParamTypes;
LLVMValueRef f_sum = LLVMAddFunction(my_module, "sum", ty_func);
LLVMValueRef* Params = new LLVMValueRef[2];
LLVMGetParams(f_sum, Params);
LLVMSetValueName(Params[0], "a");
LLVMSetValueName(Params[1], "b");
LLVMBasicBlockRef bb = LLVMAppendBasicBlock(f_sum, "entry");
LLVMBuilderRef builder = LLVMCreateBuilder();
LLVMPositionBuilderAtEnd(builder, bb);
LLVMValueRef tmp = LLVMBuildAdd(builder, Params[0], Params[1], "tmp");
delete[] Params;
LLVMBuildRet(builder, tmp);
printf(LLVMPrintModuleToString(my_module));
//do shutdown
LLVMDisposeBuilder(builder);
LLVMDisposeModule(my_module);
LLVMShutdown();
return 0;
}
The output I get is:
; ModuleID = 'my_module'
define i32 #sum(i32 0x1.74bb00p-1012, i32 b) {
entry:
tmp = add i32 0x1.95bc40p+876, b
ret i32 tmp
}
Note that 0x1.74bb00p-1012 and 0x1.95bc40p+876 should read "%a"
I can only think that is some kind of memory corruption however I don't know the likely cause. How could I change the code so this works?
As it turns out this is a problem with LLVMPrintModuleToStringNw, it uses the C printf function to print to the string and so the percentages are either removed or spew false values out of whatever is on the stack.
See for why %a came out as "0x1.74bb00p-1012" -Aka hexadecimal floating point format.
http://www.cplusplus.com/reference/cstdio/printf/
In the end LLVMPrintModuleToString should be replaced with a function that doesn't use the C print family of functions.
Can anybody help me to find out how to call rrd_update_r function of the rrdtool c API from http://oss.oetiker.ch/rrdtool/index.en.html?
It was quite easy to call the non-threadsafe version of rrd_update, but this one is more tricky...
normal rrd_update:
char *updateparams[] = {
"rrdupdate",
rrd_file,
values,
NULL
};
rrd_clear_error();
result = rrd_update(3, updateparams); //argc is first arg
Because the programm has to run in a multithreaded environment I got several errors by not using the threadsafe functions!
But it is not so easy to use rrd_update_r, because it requires a template too...
int rrd_update_r(const char *filename, const char *_template,
int argc, const char **argv);
and I really have no idea how to create one...
char *updateparams[] = {
"rrdupdate",
rrd_file,
values,
NULL
};
rrd_clear_error();
result = rrd_update_r(rrd_file, NULL,3, updateparams);
does not work and produces the following error when executing it...
error: /var/tmp/rrds/1.rrd: expected timestamp not found in data source from rrdupdate
Hopefully someone can help me!
thx and br,
roegi
Well, looking at the source code...
It appears that rrd_update_r does not want to see the "rrupdate" argument. So try just passing rrd_file and values as a 2-element argv.
Actually the source for rrd_update is not hard to read; you can find it in src/rrd_update.c. And rrd_update_r appears to be a much lower-level function that rrd_update itself calls. So this may not actually fix your underlying problem.
Now it is working!
Nemo - thx for your help!
It was not exactly your solution but it was a hint to the right direction!
It works with:
/*
rrd_file is a char * to "/var/tmp/1.rrd"
NULL says not to use a template
1 --> argc
values is a char * to "N:value-1:value-2:.....:value-n"
*/
result = rrd_update_r(rrd_file, NULL, 1, (void *) &values);