changeset 153:2a8c2cf48674

common: XDR nvclock code needs to allocate an nvclock at times Since we're always dealing with nvclocks as pointers, we need to deal with them as double-pointers here. This allows us to properly allocate a new nvclock during a decode operation. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sun, 18 Oct 2015 15:53:41 -0400
parents 3bf63abd5bd9
children a3de133fce62
files src/common/include/nomad/vclock.h src/common/oid.c src/common/vclock.c
diffstat 3 files changed, 38 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/common/include/nomad/vclock.h	Sun Oct 18 15:50:48 2015 -0400
+++ b/src/common/include/nomad/vclock.h	Sun Oct 18 15:53:41 2015 -0400
@@ -62,6 +62,6 @@
 extern int nvclock_set(struct nvclock *clock, uint64_t seq);
 extern int nvclock_inc(struct nvclock *clock);
 
-extern bool_t xdr_nvclock(XDR *xdrs, struct nvclock *clock);
+extern bool_t xdr_nvclock(XDR *xdrs, struct nvclock **clock);
 
 #endif
--- a/src/common/oid.c	Sun Oct 18 15:50:48 2015 -0400
+++ b/src/common/oid.c	Sun Oct 18 15:53:41 2015 -0400
@@ -57,7 +57,7 @@
 {
 	if (!xdr_noid(xdrs, &hndl->oid))
 		return FALSE;
-	if (!xdr_nvclock(xdrs, hndl->clock))
+	if (!xdr_nvclock(xdrs, &hndl->clock))
 		return FALSE;
 	return TRUE;
 }
--- a/src/common/vclock.c	Sun Oct 18 15:50:48 2015 -0400
+++ b/src/common/vclock.c	Sun Oct 18 15:53:41 2015 -0400
@@ -412,21 +412,54 @@
 	return 0xbad; /* pacify gcc */
 }
 
-bool_t xdr_nvclock(XDR *xdrs, struct nvclock *clock)
+bool_t xdr_nvclock(XDR *xdrs, struct nvclock **arg)
 {
+	bool shouldfree = false;
+	struct nvclock dummy;
+	struct nvclock *clock;
 	int i;
 
+	clock = *arg;
+
 	if (xdrs->x_op == XDR_FREE) {
 		nvclock_free(clock);
 		return TRUE;
 	}
 
+	/*
+	 * If we don't have clock...
+	 */
+	if (!clock && (xdrs->x_op == XDR_ENCODE)) {
+		/*
+		 * ...send one filled with zeros.
+		 */
+		memset(&dummy, 0, sizeof(struct nvclock));
+		clock = &dummy;
+	} else if (!clock && (xdrs->x_op == XDR_DECODE)) {
+		/*
+		 * ...recieve into a newly allocated one.
+		 */
+		clock = nvclock_alloc();
+		if (!clock)
+			return FALSE;
+		*arg = clock;
+		shouldfree = true;
+	}
+
 	for (i = 0; i < NVCLOCK_NUM_NODES; i++) {
 		if (!xdr_uint64_t(xdrs, &clock->ent[i].node))
-			return FALSE;
+			goto err;
 		if (!xdr_uint64_t(xdrs, &clock->ent[i].seq))
-			return FALSE;
+			goto err;
 	}
 
 	return TRUE;
+
+err:
+	if (shouldfree) {
+		nvclock_free(clock);
+		*arg = NULL;
+	}
+
+	return FALSE;
 }