diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-03-21 02:01:17 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-03-21 02:01:17 +0000 |
commit | 0d8feba6d8225f012cadbfccb001236260f83bf5 (patch) | |
tree | 9e17fd4713b8ecb0e5f0044c69d2ad87977de847 | |
parent | dfb16a62741fcc58ad1c2242741f58df14429ff6 (diff) | |
download | tor-0d8feba6d8225f012cadbfccb001236260f83bf5.tar.gz tor-0d8feba6d8225f012cadbfccb001236260f83bf5.zip |
Add macros and functions to wrap memcpy/alignment logic.
svn:r1326
-rw-r--r-- | src/common/util.c | 23 | ||||
-rw-r--r-- | src/common/util.h | 15 |
2 files changed, 38 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c index 615781a465..edd785cb87 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -86,6 +86,29 @@ void tor_strlower(char *s) } } +#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 diff --git a/src/common/util.h b/src/common/util.h index 46792b84b3..f05a012b4d 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -43,6 +43,21 @@ char *tor_strndup(const char *s, size_t n); #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; |