Skip to contentSkip to conversation

solo/vxWorks: add rngLib (with testsuite)

PATCH In review Niklaus Giger 2 participants 3 messages Oct 24, 2008 – Oct 28, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 solo/vxWorks: add rngLib (with testsuite)

Hi

I just found the time to prepare a patch for a substitute for the vxWorks rngLib library I have written quite some time ago.

The remark from the vxWorks description applies here too
"In particular, ring buffers by themselves provide no task synchronization or mutual exclusion."
(http://www.slac.stanford.edu/exp/glast/flight/sw/vxdocs/vxworks/ref/rngLib.html)

Therefore this library is only good were there is exactly one writer an one reader envolved.

It would be nice if it could find a place in the xenomai-solo. I am willing to address any criticism/nitpicking needed to get it into a good shape.

Best regards

---
 include/vxworks/Makefile.am |    1 +
 include/vxworks/Makefile.in |    1 +
 vxworks/Makefile.am         |    2 +
 vxworks/Makefile.in         |   18 ++++-
 vxworks/rngLib.c            |  152 +++++++++++++++++++++++++++++++++++++++++++
 vxworks/rngLib.h            |   34 ++++++++++
 vxworks/testsuite/Makefile  |    2 +-
 vxworks/testsuite/rng-1.c   |  118 +++++++++++++++++++++++++++++++++
 8 files changed, 323 insertions(+), 5 deletions(-)
 create mode 100644 vxworks/rngLib.c
 create mode 100644 vxworks/rngLib.h
 create mode 100644 vxworks/testsuite/rng-1.c
include/vxworks/Makefile.am +1 -0
index 06dcab2..0c20861 100644
@@ -7,6 +7,7 @@ includesub_HEADERS =	\
 	lstLib.h	\
 	memPartLib.h	\
 	msgQLib.h	\
+	rngLib.h	\
 	semLib.h	\
 	sysLib.h	\
 	taskInfo.h	\
include/vxworks/Makefile.in +1 -0
index 0fdc3c4..ad10fd1 100644
@@ -205,6 +205,7 @@ includesub_HEADERS = \
 	lstLib.h	\
 	memPartLib.h	\
 	msgQLib.h	\
+	rngLib.h	\
 	semLib.h	\
 	sysLib.h	\
 	taskInfo.h	\
vxworks/Makefile.am +2 -0
index 08e7cc8..a64141e 100644
@@ -11,6 +11,8 @@ libvxworks_la_SOURCES = \
 	memPartLib.h	\
 	msgQLib.c	\
 	msgQLib.h	\
+	rngLib.c	\
+	rngLib.h	\
 	semLib.c	\
 	semLib.h	\
 	taskLib.c	\
vxworks/Makefile.in +7 -4
index b8d0ce7..2d08bbd 100644
@@ -57,10 +57,10 @@ libvxworks_la_LIBADD =
 am_libvxworks_la_OBJECTS = libvxworks_la-errnoLib.lo \
 	libvxworks_la-intLib.lo libvxworks_la-kernelLib.lo \
 	libvxworks_la-lstLib.lo libvxworks_la-memPartLib.lo \
-	libvxworks_la-msgQLib.lo libvxworks_la-semLib.lo \
-	libvxworks_la-taskLib.lo libvxworks_la-taskInfo.lo \
-	libvxworks_la-tickLib.lo libvxworks_la-wdLib.lo \
-	libvxworks_la-sysLib.lo
+	libvxworks_la-msgQLib.lo libvxworks_la-rngLib.lo \
+	libvxworks_la-semLib.lo libvxworks_la-taskLib.lo \
+	libvxworks_la-taskInfo.lo libvxworks_la-tickLib.lo \
+	libvxworks_la-wdLib.lo libvxworks_la-sysLib.lo
 libvxworks_la_OBJECTS = $(am_libvxworks_la_OBJECTS)
 libvxworks_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
 	$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
@@ -232,6 +232,8 @@ libvxworks_la_SOURCES = \
 	memPartLib.h	\
 	msgQLib.c	\
 	msgQLib.h	\
+	rngLib.c	\
+	rngLib.h	\
 	semLib.c	\
 	semLib.h	\
 	taskLib.c	\
@@ -324,6 +326,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @[email protected]@
 @AMDEP_TRUE@@am__include@ @[email protected]@
 @AMDEP_TRUE@@am__include@ @[email protected]@
+@AMDEP_TRUE@@am__include@ @[email protected]@
 @AMDEP_TRUE@@am__include@ @[email protected]@
 @AMDEP_TRUE@@am__include@ @[email protected]@
 @AMDEP_TRUE@@am__include@ @[email protected]@
@@ -394,6 +397,13 @@ libvxworks_la-msgQLib.lo: msgQLib.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS) 

$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libvxworks_la-msgQLib.lo test -f 'msgQLib.c' || echo '$(srcdir)/'msgQLib.c

+libvxworks_la-rngLib.lo: rngLib.c
+@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS)
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libvxworks_la-rngLib.lo -MD -MP -MF $(DEPDIR)/libvxworks_la-rngLib.Tpo -c -o libvxworks_la-rngLib.lo test -f 'rngLib.c' || echo '$(srcdir)/'rngLib.c
+@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/libvxworks_la-rngLib.Tpo $(DEPDIR)/libvxworks_la-rngLib.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='rngLib.c' object='libvxworks_la-rngLib.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS)
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libvxworks_la-rngLib.lo test -f 'rngLib.c' || echo '$(srcdir)/'rngLib.c
+
libvxworks_la-semLib.lo: semLib.c
@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS)
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libvxworks_la-semLib.lo -MD -MP -MF $(DEPDIR)/libvxworks_la-semLib.Tpo -c -o libvxworks_la-semLib.lo test -f 'semLib.c' || echo '$(srcdir)/'semLib.c
@am__fastdepCC_TRUE@ mv -f $(DEPDIR)/libvxworks_la-semLib.Tpo $(DEPDIR)/libvxworks_la-semLib.Plo

vxworks/rngLib.c new file +152 -0

152 lines not on this page · raw message

vxworks/rngLib.h new file +34 -0
new file mode 100644
index 0000000..d646614
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2008 Philippe Gerum <[email protected]>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#ifndef _VXWORKS_RNGLIB_H
+#define _VXWORKS_RNGLIB_H
+
+#include <xenomai/syncobj.h>
+#include <xenomai/heapobj.h>
+#include <vxworks/rngLib.h>
+
+typedef struct  {
+	unsigned int magic;
+	unsigned int bufSize;
+	unsigned int readPos;
+	unsigned int writePos;
+	char buffer[];
+} RING_DESCRIPTOR ;
+
+#endif /* _VXWORKS_RNGLIB_H */
vxworks/testsuite/Makefile +1 -1
index d1ce203..d391406 100644
@@ -5,7 +5,7 @@ ifeq ($(prefix),)
 $(error Please add <xenomai-install-path>/bin to your PATH variable)
 endif
 
-TESTS := task-1 task-2 msgQ-1 msgQ-2 msgQ-3 wd-1 sem-1 sem-2 sem-3 sem-4 lst-1
+TESTS := task-1 task-2 msgQ-1 msgQ-2 msgQ-3 wd-1 sem-1 sem-2 sem-3 sem-4 lst-1 rng-1
 
 CFLAGS := $(shell $(XENO_CONFIG) --cflags) -g
 LDFLAGS := -lvxworks $(shell $(XENO_CONFIG) --ldflags)
vxworks/testsuite/rng-1.c new file +118 -0

118 lines not on this page · raw message

+
--
1.6.0.2

#1 lore raw
Quoted 5 lines · Niklaus Giger

Hi

I just found the time to prepare a patch for a substitute for the vxWorks rngLib library I have written quite some time ago.

Quoted 7 lines

The remark from the vxWorks description applies here too
"In particular, ring buffers by themselves provide no task synchronization or mutual exclusion."
(http://www.slac.stanford.edu/exp/glast/flight/sw/vxdocs/vxworks/ref/rngLib.html)

Therefore this library is only good were there is exactly one writer an one reader envolved.

Fair enough, but we would have to pin all VxWorks tasks to the same CPU on SMP systems; otherwise, out-of-order memory accesses would kill that code.

Quoted 5 lines

It would be nice if it could find a place in the xenomai-solo. I am willing to address any criticism/nitpicking needed to get it into a good shape.

Best regards

Quoted 8 lines
---
 include/vxworks/Makefile.am |    1 +
 include/vxworks/Makefile.in |    1 +
 vxworks/Makefile.am         |    2 +
 vxworks/Makefile.in         |   18 ++++-
 vxworks/rngLib.c            |  152 +++++++++++++++++++++++++++++++++++++++++++
 vxworks/rngLib.h            |   34 ++++++++++

Something must be missing here. vxworks/rngLib.h should contain internal definitions, used by the ring buffer implementation code, and that is correct in your code. However, include/vxworks/rngLib.h should provide the interface to applications; it seems we are missing such a file.

Since we don't want to export the innards of a ring buffer implementation to applications, the interface header should only define the opaque ring buffer handle type (i.e. typedef intptr_t RING_ID), and the RNG routine prototypes.

Quoted 7 lines
 vxworks/testsuite/Makefile  |    2 +-
 vxworks/testsuite/rng-1.c   |  118 +++++++++++++++++++++++++++++++++
 8 files changed, 323 insertions(+), 5 deletions(-)
 create mode 100644 vxworks/rngLib.c
 create mode 100644 vxworks/rngLib.h
 create mode 100644 vxworks/testsuite/rng-1.c

[snip]

diff --git a/vxworks/rngLib.c b/vxworks/rngLib.c

Quoted 25 lines

new file mode 100644
index 0000000..d033db9

vxworks/rngLib.c new file +20 -0
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2008 Niklaus Giger <[email protected]>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#include <stdlib.h>
+#include <vxworks/errnoLib.h>

vxworks/rngLib.c line 30

Quoted 10 lines
vxworks/rngLib.c new file +10 -0
+#include "rngLib.h"
+
+#define WIND_RING_MAGIC 0x5432affe
+
+RING_ID rngCreate(int nbytes)
+{
+	RING_DESCRIPTOR *ring;
+	void *ring_mem;
+
+	if ( nbytes<=0) { errnoSet(S_memLib_NOT_ENOUGH_MEMORY); return 0; }

Please use the linux-c coding style, basically K&R + preposterously large indent tabs (8 chars)

Emacs can be taught this way:

(defun linux-c-mode ()
"C mode with adjusted defaults for use with Xenomai." (interactive)
(c-mode)
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))

Indent will reformat almost properly with (-T should be used to teach indent about the known non-standard types as well):

$ indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1

vxworks/rngLib.c line 31

Quoted 1 line
vxworks/rngLib.c new file +1 -0
+	ring_mem = malloc( sizeof(RING_DESCRIPTOR*) + nbytes);

The sizeof() argument is wrong. Out-of-bound memory writes are knocking at the door.

Also, please use xnmalloc(). This call will pull memory from the allocator selected at built time for the SOLO stack, either TLSF, or standard malloc(). --enable-malloc is false by default (i.e. TLSF enabled).

vxworks/rngLib.c line 33

Quoted 2 lines
vxworks/rngLib.c new file +2 -0
+	if ( ring_mem == 0) { errno = errnoSet(S_memLib_NOT_ENOUGH_MEMORY); return 0; }
+

Please compare pointers to NULL (and return null pointers the same way), that gives a better hint about the pointerness of the data involved.

vxworks/rngLib.c line 34

Quoted 1 line
vxworks/rngLib.c new file +1 -0
+	ring = (RING_DESCRIPTOR *) ring_mem;

Cast is useless; let's remove visual clutter as much as we can. Actually, ring_mem is useless as well; you could optimally work only using the ring variable directly. Reducing the number of variable hops allows people to work late at night with half a brain.

vxworks/rngLib.c line 38

Quoted 4 lines
vxworks/rngLib.c new file +4 -0
+	ring->magic = WIND_RING_MAGIC;
+	ring->bufSize = nbytes;
+	ring->readPos = 0;
+	ring->writePos = 0;

Nitpicking alert: line-feed please. It is visually easier to locate the main return value.

vxworks/rngLib.c line 46

Quoted 8 lines
vxworks/rngLib.c new file +8 -0
+	return (RING_ID) ring_mem;
+}
+
+
+void rngDelete(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return;

K&R

vxworks/rngLib.c line 48

Quoted 2 lines
vxworks/rngLib.c new file +2 -0
+	ring->magic = 0;
+	free(ring);

xnfree() is the converse call for xnmalloc().

Quoted 9 lines
vxworks/rngLib.c new file +9 -0
+}
+
+
+void rngFlush(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return;
+	ring->readPos = 0;
+	ring->writePos = 0;

vxworks/rngLib.c line 67

Quoted 10 lines
vxworks/rngLib.c new file +10 -0
+}
+
+
+int rngBufGet(RING_ID ring_id,
+	char *  buffer,
+	int     maxbytes)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	int j, bytesRead=0;
+	unsigned int savedWritePos = ring->writePos;

linefeed please. Splitting declarations and pure executable statements makes it easier for the eyes as well.

vxworks/rngLib.c line 68

Quoted 1 line
vxworks/rngLib.c new file +1 -0
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;

Mm, I guess VxWorks is not even checking there, right? If it does not, then I would rather raise an assertion when a bad magic is detected, that would only trigger with --enable-debug/--enable-assert.

vxworks/rngLib.c line 71

Quoted 3 lines
vxworks/rngLib.c new file +3 -0
+	for (j=0; j < maxbytes; j++)
+	{
+		if ((ring->readPos) % (ring->bufSize + 1) == savedWritePos)

ring->readPos should never be out of bounds anyway. Do we actually need to constrain the value to bufSize again?

vxworks/rngLib.c line 72

Quoted 1 line
vxworks/rngLib.c new file +1 -0
+		{

Single statement, save energy spent typing, no brace needed.

Quoted 20 lines
vxworks/rngLib.c new file +20 -0
+			break;
+		}
+		buffer[j] = ring->buffer[ring->readPos];
+		++bytesRead;
+		ring->readPos = (ring->readPos + 1) % (ring->bufSize + 1);
+	}
+	return bytesRead;
+}
+
+
+int rngBufPut(RING_ID ring_id,
+	char *  buffer,
+	int     nbytes)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	int j, bytesWritten=0;
+	unsigned int savedReadPos = ring->readPos;
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;
+	for (j=0; j < nbytes; j++)
+	{

vxworks/rngLib.c line 102

Quoted 10 lines
vxworks/rngLib.c new file +10 -0
+		if ((ring->writePos + 1) % (ring->bufSize + 1) == savedReadPos)
+		{
+			break;
+		}
+		ring->buffer[ring->writePos] = buffer[j];
+		++bytesWritten;
+		ring->writePos = (ring->writePos + 1) % (ring->bufSize + 1);
+	}
+	return bytesWritten;
+}

Same remarks as for rngBufGet().

More generally, I see off-by-one errors in both rngGet() and rngPut(), the way they determine the next readable/writable byte index. In the following scenario for instance, we would write two bytes to the ring buffer at indices #0 et #1, albeit we only have space for a single character.

rngCreate(1);
rngPut(rng, buf, 1);
rngGet(rng, buf, 1);
rngPut(rng, buf, 1);

Dynamically maintaining a count of free bytes in the ring would allow easy watermark checks in rngPut() and rngGet(), while providing a straigthforward implementation for rngIsEmpy(), rngIsFull() and rngFreeBytes().

Quoted 23 lines
vxworks/rngLib.c new file +23 -0
+
+
+BOOL rngIsEmpty(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;
+	return rngFreeBytes(ring_id) == (int) ring->bufSize;
+}
+
+
+BOOL rngIsFull(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;
+	return rngFreeBytes(ring_id) == 0;
+}
+
+
+int rngFreeBytes(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;
+	return ((ring->bufSize - (ring->writePos - ring->readPos)) % (ring->bufSize+1));

vxworks/rngLib.c line 135

Quoted 10 lines
vxworks/rngLib.c new file +10 -0
+}
+
+
+int rngNBytes(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;
+	return ring->bufSize - rngFreeBytes(ring_id);
+}
+

Missing code in the following routines?

Quoted 56 lines
vxworks/rngLib.c new file +17 -0
+void rngPutAhead(RING_ID ring_id,
+	char    byte,
+	int     offset)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return;
+	return;
+}
+
+
+void rngMoveAhead(RING_ID ring_id,
+	int     n)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return;
+	return;
+}
vxworks/rngLib.h new file +33 -0
new file mode 100644
index 0000000..d646614
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2008 Philippe Gerum <[email protected]>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#ifndef _VXWORKS_RNGLIB_H
+#define _VXWORKS_RNGLIB_H
+
+#include <xenomai/syncobj.h>
+#include <xenomai/heapobj.h>
+#include <vxworks/rngLib.h>
+
+typedef struct  {
+	unsigned int magic;
+	unsigned int bufSize;
+	unsigned int readPos;
+	unsigned int writePos;
+	char buffer[];
+} RING_DESCRIPTOR ;
+
Signature · 1 line
Philippe.
#2 lore raw

Am Sonntag 26 Oktober 2008 17.02:08 schrieb Philippe Gerum:

Quoted 5 lines
Quoted 4 lines · Niklaus Giger

Hi

I just found the time to prepare a patch for a substitute for the vxWorks rngLib library I have written quite some time ago.

<..>
Thank you for your review. Attached a corrected version of my patch. But there a some more points, which I would like to discuss.

Quoted 3 lines

Fair enough, but we would have to pin all VxWorks tasks to the same CPU on SMP systems; otherwise, out-of-order memory accesses would kill that code.

Can you do it this?

I am not familiar with this kind of problem as I am still running single processor boards. But I am quite sure that our next board will have two (or more) processore cores sitting on it. Though I have quite an interest in providing a correct solution.

I am not familiar with vxWorks-SMP and therefore I am not sure whether it is a good idea to pin all vxWorks tasks to one CPU or whether the user should be able to select the tasks he wants. E.g. you might want only the reader and the writer of a ring or list on one CPU and other ones to other CPU(s).

<..>

Quoted 9 lines
Quoted 4 lines
 vxworks/Makefile.in         |   18 ++++-
 vxworks/rngLib.c            |  152

+++++++++++++++++++++++++++++++++++++++++++ vxworks/rngLib.h |
34 ++++++++++

Something must be missing here. vxworks/rngLib.h should contain internal definitions, used by the ring buffer implementation code, and that is correct in your code. However, include/vxworks/rngLib.h should provide the interface to applications; it seems we are missing such a file.

Sorry, I forgot to do git add include/vxworks/rngLib.h, which is attached now and which provides the (opaque) interface to the vxWorks users.

Quoted 5 lines

Since we don't want to export the innards of a ring buffer implementation to applications, the interface header should only define the opaque ring buffer handle type (i.e. typedef intptr_t RING_ID), and the RNG routine prototypes.

<..>

Quoted 3 lines

Please use the linux-c coding style, basically K&R + preposterously large indent tabs (8 chars)

Thank you for code snippets for emacs and ident (did put it into my notes as it is not the first time I have to deal with such problems).

Quoted 14 lines

Emacs can be taught this way:

(defun linux-c-mode ()
"C mode with adjusted defaults for use with Xenomai." (interactive)
(c-mode)
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))

Indent will reformat almost properly with (-T should be used to teach indent about the known non-standard types as well):

Quoted 6 lines
$ indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1

Review

Quoted 1 line
(quoted diff) +1 -0
+	ring_mem = malloc( sizeof(RING_DESCRIPTOR*) + nbytes);

The sizeof() argument is wrong. Out-of-bound memory writes are knocking at the door.

Thanks for catching this error. The following code should be correct.

	ring_mem = xnmalloc(sizeof(RING_DESCRIPTOR) + nbytes + 1);

I need 1 additional byte to be able to use only the read/write-position. This allows that the reader only modifies the readPosition and ther writer only the writePosition.

Quoted 6 lines

Also, please use xnmalloc(). This call will pull memory from the allocator selected at built time for the SOLO stack, either TLSF, or standard malloc(). --enable-malloc is false by default (i.e. TLSF enabled).

Quoted 2 lines
(quoted diff) +1 -0
+	if ( ring_mem == 0) { errno = errnoSet(S_memLib_NOT_ENOUGH_MEMORY);

return 0; } +

Ack.

Quoted 4 lines

Please compare pointers to NULL (and return null pointers the same way), that gives a better hint about the pointerness of the data involved.

Ack.

Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	ring = (RING_DESCRIPTOR *) ring_mem;

Cast is useless; let's remove visual clutter as much as we can. Actually, ring_mem is useless as well; you could optimally work only using the ring variable directly. Reducing the number of variable hops allows people to work late at night with half a brain.

Ack.

Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+	ring->magic = WIND_RING_MAGIC;
+	ring->bufSize = nbytes;
+	ring->readPos = 0;
+	ring->writePos = 0;

Nitpicking alert: line-feed please. It is visually easier to locate the main return value.

Ack.

Quoted 10 lines

Review

Quoted 9 lines
(quoted diff) +8 -0
+	return (RING_ID) ring_mem;
+}
+
+
+void rngDelete(RING_ID ring_id)
+{
+	RING_DESCRIPTOR *ring = (RING_DESCRIPTOR *) ring_id;
+	if (ring->magic != WIND_RING_MAGIC) return;

Ack.

Quoted 6 lines

K&R

Review

Quoted 2 lines
(quoted diff) +2 -0
+	ring->magic = 0;
+	free(ring);

xnfree() is the converse call for xnmalloc().

Ack.

Quoted 1 line

<..>

Quoted 2 lines

linefeed please. Splitting declarations and pure executable statements makes it easier for the eyes as well.

Ack.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +1 -0
+	if (ring->magic != WIND_RING_MAGIC) return -ENOSYS;

Mm, I guess VxWorks is not even checking there, right? If it does not, then I would rather raise an assertion when a bad magic is detected, that would only trigger with --enable-debug/--enable-assert.

It is true, that vxWorks does not use a magic.

What is your prefered way to raise an exception?

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+	for (j=0; j < maxbytes; j++)
+	{
+		if ((ring->readPos) % (ring->bufSize + 1) == savedWritePos)

ring->readPos should never be out of bounds anyway. Do we actually need to constrain the value to bufSize again?

Yes, we do. It happens if you want to get/put more characters from the ring buffer, than there are available.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		{

Single statement, save energy spent typing, no brace needed.

Ack.
<..>

Quoted 11 lines

Same remarks as for rngBufGet().

More generally, I see off-by-one errors in both rngGet() and rngPut(), the way they determine the next readable/writable byte index. In the following scenario for instance, we would write two bytes to the ring buffer at indices #0 et #1, albeit we only have space for a single character.

rngCreate(1);
rngPut(rng, buf, 1);
rngGet(rng, buf, 1);
rngPut(rng, buf, 1);

See my answer for rngCreate. BufferSize is one bigger than bufSize. But you really have a sharp eye to find corner cases! Added them to my unit test.

Quoted 4 lines

Dynamically maintaining a count of free bytes in the ring would allow easy watermark checks in rngPut() and rngGet(), while providing a straigthforward implementation for rngIsEmpy(), rngIsFull() and rngFreeBytes().

See discussion above. I want any additional dependency on a variable shared between the reader and the writer thread.
<..>

Quoted 2 lines

Missing code in the following routines?

Thanks for spotting them. I never used them, therefore they were very low on my priority list. But for the sake of completeness I added them with their corresponding test case.

All testcases were run on my vxWorks board.

Quoted 1 line
Quoted 1 line

+void rngPutAhead(RING_ID ring_id,

<..>

Best regards

Niklaus

From f7eea15b94af58602eef2d5f9c84a69743c94fe8 Mon Sep 17 00:00:00 2001
From: Niklaus Giger <[email protected]>
Date: Tue, 28 Oct 2008 17:09:35 +0100
Subject: [PATCH] SOLO: Add vxworks rngLib replacement

---
 include/vxworks/Makefile.am |    1 +
 include/vxworks/Makefile.in |    1 +
 include/vxworks/rngLib.h    |   56 +++++++++++
 vxworks/Makefile.am         |    2 +
 vxworks/Makefile.in         |   18 +++-
 vxworks/rngLib.c            |  168 +++++++++++++++++++++++++++++++++
 vxworks/rngLib.h            |   34 +++++++
 vxworks/testsuite/Makefile  |    2 +-
 vxworks/testsuite/rng-1.c   |  215 +++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 492 insertions(+), 5 deletions(-)
 create mode 100644 include/vxworks/rngLib.h
 create mode 100644 vxworks/rngLib.c
 create mode 100644 vxworks/rngLib.h
 create mode 100644 vxworks/testsuite/rng-1.c
include/vxworks/Makefile.am +1 -0
index 06dcab2..0c20861 100644
@@ -7,6 +7,7 @@ includesub_HEADERS =	\
 	lstLib.h	\
 	memPartLib.h	\
 	msgQLib.h	\
+	rngLib.h	\
 	semLib.h	\
 	sysLib.h	\
 	taskInfo.h	\
include/vxworks/Makefile.in +1 -0
index 0fdc3c4..ad10fd1 100644
@@ -205,6 +205,7 @@ includesub_HEADERS = \
 	lstLib.h	\
 	memPartLib.h	\
 	msgQLib.h	\
+	rngLib.h	\
 	semLib.h	\
 	sysLib.h	\
 	taskInfo.h	\
include/vxworks/rngLib.h new file +56 -0

56 lines not on this page · raw message

vxworks/Makefile.am +2 -0
index 08e7cc8..a64141e 100644
@@ -11,6 +11,8 @@ libvxworks_la_SOURCES = \
 	memPartLib.h	\
 	msgQLib.c	\
 	msgQLib.h	\
+	rngLib.c	\
+	rngLib.h	\
 	semLib.c	\
 	semLib.h	\
 	taskLib.c	\
vxworks/Makefile.in +14 -4
index b8d0ce7..2d08bbd 100644
@@ -57,10 +57,10 @@ libvxworks_la_LIBADD =
 am_libvxworks_la_OBJECTS = libvxworks_la-errnoLib.lo \
 	libvxworks_la-intLib.lo libvxworks_la-kernelLib.lo \
 	libvxworks_la-lstLib.lo libvxworks_la-memPartLib.lo \
-	libvxworks_la-msgQLib.lo libvxworks_la-semLib.lo \
-	libvxworks_la-taskLib.lo libvxworks_la-taskInfo.lo \
-	libvxworks_la-tickLib.lo libvxworks_la-wdLib.lo \
-	libvxworks_la-sysLib.lo
+	libvxworks_la-msgQLib.lo libvxworks_la-rngLib.lo \
+	libvxworks_la-semLib.lo libvxworks_la-taskLib.lo \
+	libvxworks_la-taskInfo.lo libvxworks_la-tickLib.lo \
+	libvxworks_la-wdLib.lo libvxworks_la-sysLib.lo
 libvxworks_la_OBJECTS = $(am_libvxworks_la_OBJECTS)
 libvxworks_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \
 	$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
@@ -232,6 +232,8 @@ libvxworks_la_SOURCES = \
 	memPartLib.h	\
 	msgQLib.c	\
 	msgQLib.h	\
+	rngLib.c	\
+	rngLib.h	\
 	semLib.c	\
 	semLib.h	\
 	taskLib.c	\
@@ -324,6 +326,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @[email protected]@am__quote@
 @AMDEP_TRUE@@am__include@ @[email protected]@domain.hid@
 @AMDEP_TRUE@@am__include@ @[email protected]@am__quote@
+@AMDEP_TRUE@@am__include@ @[email protected]@am__quote@
 @AMDEP_TRUE@@am__include@ @[email protected]@am__quote@
 @AMDEP_TRUE@@am__include@ @[email protected]@am__quote@
 @AMDEP_TRUE@@am__include@ @[email protected]@domain.hid@
@@ -394,6 +397,13 @@ libvxworks_la-msgQLib.lo: msgQLib.c
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libvxworks_la-msgQLib.lo `test -f 'msgQLib.c' || echo '$(srcdir)/'`msgQLib.c
 
+libvxworks_la-rngLib.lo: rngLib.c
+@am__fastdepCC_TRUE@	$(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libvxworks_la-rngLib.lo -MD -MP -MF $(DEPDIR)/libvxworks_la-rngLib.Tpo -c -o libvxworks_la-rngLib.lo `test -f 'rngLib.c' || echo '$(srcdir)/'`rngLib.c
+@am__fastdepCC_TRUE@	mv -f $(DEPDIR)/libvxworks_la-rngLib.Tpo $(DEPDIR)/libvxworks_la-rngLib.Plo
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	source='rngLib.c' object='libvxworks_la-rngLib.lo' libtool=yes @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCC_FALSE@	$(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libvxworks_la-rngLib.lo `test -f 'rngLib.c' || echo '$(srcdir)/'`rngLib.c
+
 libvxworks_la-semLib.lo: semLib.c
 @am__fastdepCC_TRUE@	$(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libvxworks_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libvxworks_la-semLib.lo -MD -MP -MF $(DEPDIR)/libvxworks_la-semLib.Tpo -c -o libvxworks_la-semLib.lo `test -f 'semLib.c' || echo '$(srcdir)/'`semLib.c
 @am__fastdepCC_TRUE@	mv -f $(DEPDIR)/libvxworks_la-semLib.Tpo $(DEPDIR)/libvxworks_la-semLib.Plo
vxworks/rngLib.c new file +168 -0

168 lines not on this page · raw message

vxworks/rngLib.h new file +34 -0
new file mode 100644
index 0000000..eeb324c
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2008 Philippe Gerum <[email protected]>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
+ */
+
+#ifndef _VXWORKS_RNGLIB_H
+#define _VXWORKS_RNGLIB_H
+
+#include <xenomai/syncobj.h>
+#include <xenomai/heapobj.h>
+#include <vxworks/rngLib.h>
+
+typedef struct {
+	unsigned int magic;
+	unsigned int bufSize;
+	unsigned int readPos;
+	unsigned int writePos;
+	unsigned char buffer[];
+} RING_DESCRIPTOR;
+
+#endif /* _VXWORKS_RNGLIB_H */
vxworks/testsuite/Makefile +1 -1
index d1ce203..d391406 100644
@@ -5,7 +5,7 @@ ifeq ($(prefix),)
 $(error Please add <xenomai-install-path>/bin to your PATH variable)
 endif
 
-TESTS := task-1 task-2 msgQ-1 msgQ-2 msgQ-3 wd-1 sem-1 sem-2 sem-3 sem-4 lst-1
+TESTS := task-1 task-2 msgQ-1 msgQ-2 msgQ-3 wd-1 sem-1 sem-2 sem-3 sem-4 lst-1 rng-1
 
 CFLAGS := $(shell $(XENO_CONFIG) --cflags) -g
 LDFLAGS := -lvxworks $(shell $(XENO_CONFIG) --ldflags)
vxworks/testsuite/rng-1.c new file +215 -0

215 lines not on this page · raw message

Signature · 1 line
1.6.0.2