[or-cvs] Add macros and functions to wrap memcpy/alignment logic.

Nick Mathewson nickm at seul.org
Sun Mar 21 02:01:19 UTC 2004


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv16486/src/common

Modified Files:
	util.c util.h 
Log Message:
Add macros and functions to wrap memcpy/alignment logic.

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.68
retrieving revision 1.69
diff -u -d -r1.68 -r1.69
--- util.c	20 Mar 2004 21:22:16 -0000	1.68
+++ util.c	21 Mar 2004 02:01:17 -0000	1.69
@@ -86,6 +86,29 @@
   }
 }
 
+#ifndef UNALIGNED_ACCESS_OK
+uint16_t get_uint16(char *cp)
+{
+  uint16_t v;
+  memcpy(&v,cp,2);
+  return v;
+}
+uint32_t get_uint32(char *cp)
+{
+  uint32_t v;
+  memcpy(&v,cp,4);
+  return v;
+}
+void set_uint16(char *cp, uint16_t v)
+{
+  memcpy(cp,&v,2);
+}
+void set_uint32(char *cp, uint32_t v)
+{
+  memcpy(cp,&v,4);
+}
+#endif
+
 
 /*
  * A simple smartlist interface to make an unordered list of acceptable

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -d -r1.38 -r1.39
--- util.h	20 Mar 2004 01:48:05 -0000	1.38
+++ util.h	21 Mar 2004 02:01:17 -0000	1.39
@@ -43,6 +43,21 @@
 #define tor_free(p) do {if(p) {free(p); (p)=NULL;}} while(0)
 void tor_strlower(char *s);
 
+#ifdef UNALIGNED_ACCESS_OK
+/* XXX Not actually used yet, but would probably be faster on non-sun
+ * hardare.
+ */
+#define get_uint16(cp) (*(uint16_t*)(cp))
+#define get_uint32(cp) (*(uint32_t*)(cp))
+#define set_uint16(cp,v) do { *(uint16_t)(cp) = (v) } while (0)
+#define set_uint32(cp,v) do { *(uint32_t)(cp) = (v) } while (0)
+#else
+uint16_t get_uint16(char *cp);
+uint32_t get_uint32(char *cp);
+void set_uint16(char *cp, uint16_t v);
+void set_uint32(char *cp, uint32_t v);
+#endif
+
 typedef struct {
   void **list;
   int num_used;



More information about the tor-commits mailing list