Erlang Mailing Lists

Author Message

<  Erlang  ~  Efficient C binding (no sockets!)

senseiwa
Posted: Tue Jul 08, 2008 1:35 pm Reply with quote
Joined: 08 Jul 2008 Posts: 2
Hi everyone!

I have a question that has been answered in the Erlang book, unfortunately that solution isn't satisfying.

My test bed is as follows: some ISO C libraries containing functions that should be called from an erlang program. All C functions will need several IEEE floating points, and clearly, using a socket-based approach as described in the book isn't the best option: it isn't fast, and it raises portability issues that I'd like to avoid.

What would you suggest to get the job done?

I am new to erlang, I program mainly in C and assembly, so you can understand my point of view Smile

Another (maybe) stupid question. The "erlang:binary_to_float(Binary)" and its counterpart are marked as internal. So what is the portable, erlang-proof way of passing IEEE floating point values to and from an erlang program?

Thanks to anyone giving hints!
View user's profile Send private message
flodis
Posted: Wed Jul 09, 2008 8:28 am Reply with quote
User Joined: 09 Jul 2008 Posts: 27
I think you want to make a c driver. It allows you to call c functions directly from erlang processes.

I think binary_to_term/term_to_binary are the functions you are looking for.
View user's profile Send private message
francesco
Posted: Wed Jul 09, 2008 9:19 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
When interfacing to C, also take a look at erl_interface. It might be easier to use that writing your own drivers.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
martin
Posted: Wed Jul 09, 2008 11:19 am Reply with quote
User Joined: 06 Aug 2006 Posts: 11
Well what you want to look at are the example drivers in the otp_src_R12B-1/erts/example dir (also described in the ERTS user guide), further points worth mentioning is:

When writing drivers i find the control (see erl_ddll and driver_entry man pages) function easiest to use returning a binary constructed by the ei_x_encode_X API.

Further, do not hesitate to open ports to the driver as needed.
Use the try_load/3 try_unlaod/2 functions on demand since they are ref counted.

The threading model is to me unsound or its something i simply don't get so i simply avoid it and build a sep thread pool if needed...

Hope that helps
//Martin
View user's profile Send private message
martin
Posted: Wed Jul 09, 2008 11:23 am Reply with quote
User Joined: 06 Aug 2006 Posts: 11
Some things i missed....

the binary returned should be constructed using the driver_alloc_binary and copied from the er_x_buff struct.

When opening the port pass the binary flag and or use the
set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY); in the start function of your driver, then use the binary_to_term on the return value and term_to_binary on the input.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum