Peuchele bio photo

Peuchele

Pedí pollo que yo pago la diferencia!

Email
Photo Credit: Wikimedia Commons

RXTX is a native lib providing serial and parallel communication for the Java Development Toolkit (JDK). All deliverables are under the gnu LGPL license. The native part is developed in C.

If you are using the parallel port functions of this project for version rxtx-2.1-7r2 or older, you may be interested in this code optimization, it is a small change but it may improve the resource usage in certain cases.

Let’s take a look to readArray function in ParallelImp.c, this is a summary of the current implementation:

buffer = (unsigned char *) malloc(sizeof(unsigned char) * length);
bytes = read_byte_array(fd, buffer, length, threshold, timeout);
for(i = 0; i < bytes; i++) body[i + offset] = buffer[i];
free(buffer);

In C we can increment the output buffer pointer to the position we want to start to write (offset) directly, avoiding the necessity of allocating a new buffer and copying the contents, for example:

/* buffer = (unsigned char *) malloc(sizeof(unsigned char) * length); */
buffer = body + offset
bytes = read_byte_array(fd, buffer, length, threshold, timeout);
/* for(i = 0; i < bytes; i++) body[i + offset] = buffer[i]; */
/* free(buffer); */

This kind of optimization can be also applied to the writeArray function in the same source file.

I’m going to send this information to the RXTX project mailing list, hopefully it will be available in next version!