diff --git a/trunk/src/common/util.c b/trunk/src/common/util.c
index 617c4c77aa..d41b0e2728 100644
--- a/trunk/src/common/util.c
+++ b/trunk/src/common/util.c
@@ -2,10 +2,12 @@
/* See LICENSE for licensing information */
/* $Id$ */
-/*****
- * util.c: Common functions for strings, IO, network, data structures,
+/**
+ * \file util.c
+ *
+ * \brief Common functions for strings, IO, network, data structures,
* process control, and cross-platform portability.
- *****/
+ **/
#include "orconfig.h"
@@ -99,11 +101,7 @@
#include "strlcat.c"
#endif
-/*****
- * Memory wrappers
- *****/
-
-/* Allocate a chunk of 'size' bytes of memory, and return a pointer to
+/** Allocate a chunk of size bytes of memory, and return a pointer to
* result. On error, log and terminate the process. (Same as malloc(size),
* but never returns NULL.)
*/
@@ -124,7 +122,7 @@ void *tor_malloc(size_t size) {
return result;
}
-/* Allocate a chunk of 'size' bytes of memory, fill the memory with
+/* Allocate a chunk of size bytes of memory, fill the memory with
* zero bytes, and return a pointer to the result. Log and terminate
* the process on error. (Same as calloc(size,1), but never returns NULL.)
*/
@@ -134,7 +132,7 @@ void *tor_malloc_zero(size_t size) {
return result;
}
-/* Change the size of the memory block pointed to by 'ptr' to 'size'
+/** Change the size of the memory block pointed to by ptr to size
* bytes long; return the new memory block. On error, log and
* terminate. (Like realloc(ptr,size), but never returns NULL.)
*/
@@ -149,7 +147,7 @@ void *tor_realloc(void *ptr, size_t size) {
return result;
}
-/* Return a newly allocated copy of the NUL-terminated string s. On
+/** Return a newly allocated copy of the NUL-terminated string s. On
* error, log and terminate. (Like strdup(s), but never returns
* NULL.)
*/
@@ -165,10 +163,11 @@ char *tor_strdup(const char *s) {
return dup;
}
-/* Allocate and return a new string containing the first 'n'
- * characters of 's'. If 's' is longer than 'n' characters, only the
- * first 'n' are copied. The result is always NUL-terminated. (Like
- * strndup(s,n), but never returns NULL.)
+/** Allocate and return a new string containing the first n
+ * characters of s. If s is longer than n
+ * characters, only the first n are copied. The result is
+ * always NUL-terminated. (Like strndup(s,n), but never returns
+ * NULL.)
*/
char *tor_strndup(const char *s, size_t n) {
char *dup;
@@ -179,41 +178,50 @@ char *tor_strndup(const char *s, size_t n) {
return dup;
}
-/* Convert all alphabetic characters in the nul-terminated string 's' to
- * lowercase. */
-void tor_strlower(char *s)
-{
- while (*s) {
- *s = tolower(*s);
- ++s;
- }
-}
#ifndef UNALIGNED_INT_ACCESS_OK
+/**
+ * Read a 16-bit value beginning at cp. Equaivalent to
+ * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
+ * unaligned memory access.
+ */
uint16_t get_uint16(const char *cp)
{
uint16_t v;
memcpy(&v,cp,2);
return v;
}
+/**
+ * Read a 32-bit value beginning at cp. Equaivalent to
+ * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
+ * unaligned memory access.
+ */
uint32_t get_uint32(const char *cp)
{
uint32_t v;
memcpy(&v,cp,4);
return v;
}
+/**
+ * Set a 16-bit value beginning at cp to v. Equivalent to
+ * *(uint16_t)(cp) = v, but will not cause segfaults on platforms that forbid
+ * unaligned memory access. */
void set_uint16(char *cp, uint16_t v)
{
memcpy(cp,&v,2);
}
+/**
+ * Set a 32-bit value beginning at cp to v. Equivalent to
+ * *(uint32_t)(cp) = v, but will not cause segfaults on platforms that forbid
+ * unaligned memory access. */
void set_uint32(char *cp, uint32_t v)
{
memcpy(cp,&v,4);
}
#endif
-/* Encode the first 'fromlen' bytes stored at 'from' in hexidecimal;
- * write the result as a NUL-terminated string to 'to'. 'to' must
+/** Encode the first fromlen bytes stored at from in hexidecimal;
+ * write the result as a NUL-terminated string to to. to must
* have at least (2*fromlen)+1 bytes of free space.
*/
void hex_encode(const char *from, int fromlen, char *to)
@@ -229,8 +237,8 @@ void hex_encode(const char *from, int fromlen, char *to)
*to = '\0';
}
-/* Return a pointer to a NUL-terminated hexidecimal string encoding
- * the first 'fromlen' bytes of 'from'. (fromlen must be <= 32.) The
+/** Return a pointer to a NUL-terminated hexidecimal string encoding
+ * the first fromlen bytes of from. (fromlen must be \<= 32.) The
* result does not need to be deallocated, but repeated calls to
* hex_str will trash old results.
*/
@@ -253,8 +261,8 @@ const char *hex_str(const char *from, int fromlen)
struct smartlist_t {
- /* 'list' has enough capacity to store exactly 'capacity' elements
- * before it needs to be resized. Only the first 'num_used' (<=
+ /** list has enough capacity to store exactly capacity elements
+ * before it needs to be resized. Only the first num_used (\<=
* capacity) elements point to valid data.
*/
void **list;
@@ -262,7 +270,7 @@ struct smartlist_t {
int capacity;
};
-/* Allocate and return an empty smartlist.
+/** Allocate and return an empty smartlist.
*/
smartlist_t *smartlist_create() {
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
@@ -272,7 +280,7 @@ smartlist_t *smartlist_create() {
return sl;
}
-/* Deallocate a smartlist. Does not release storage associated with the
+/** Deallocate a smartlist. Does not release storage associated with the
* list's elements.
*/
void smartlist_free(smartlist_t *sl) {
@@ -280,9 +288,9 @@ void smartlist_free(smartlist_t *sl) {
free(sl);
}
-/* Change the capacity of the smartlist to 'n', so that we can grow
- * the list up to 'n' elements with no further reallocation or wasted
- * space. If 'n' is less than or equal to the number of elements
+/** Change the capacity of the smartlist to n, so that we can grow
+ * the list up to n elements with no further reallocation or wasted
+ * space. If n is less than or equal to the number of elements
* currently in the list, reduce the list's capacity as much as
* possible without losing elements.
*/
@@ -295,13 +303,13 @@ void smartlist_set_capacity(smartlist_t *sl, int n) {
}
}
-/* Remove all elements from the list.
+/** Remove all elements from the list.
*/
void smartlist_clear(smartlist_t *sl) {
sl->num_used = 0;
}
-/* Set the list's new length to 'len' (which must be <= the list's
+/** Set the list's new length to len (which must be \<= the list's
* current size). Remove the last smartlist_len(sl)-len elements from the
* list.
*/
@@ -311,7 +319,7 @@ void smartlist_truncate(smartlist_t *sl, int len)
sl->num_used = len;
}
-/* Append element to the end of the list. */
+/** Append element to the end of the list. */
void smartlist_add(smartlist_t *sl, void *element) {
if (sl->num_used >= sl->capacity) {
sl->capacity *= 2;
@@ -320,13 +328,13 @@ void smartlist_add(smartlist_t *sl, void *element) {
sl->list[sl->num_used++] = element;
}
-/* Append each element from S2 to the end of S1. */
+/** Append each element from S2 to the end of S1. */
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2)
{
SMARTLIST_FOREACH(s2, void *, element, smartlist_add(sl, element));
}
-/* Remove all elements E from sl such that E==element. Does not preserve
+/** Remove all elements E from sl such that E==element. Does not preserve
* the order of s1.
*/
void smartlist_remove(smartlist_t *sl, void *element) {
@@ -340,7 +348,7 @@ void smartlist_remove(smartlist_t *sl, void *element) {
}
}
-/* Return true iff some element E of sl has E==element.
+/** Return true iff some element E of sl has E==element.
*/
int smartlist_isin(const smartlist_t *sl, void *element) {
int i;
@@ -350,7 +358,7 @@ int smartlist_isin(const smartlist_t *sl, void *element) {
return 0;
}
-/* Return true iff some element E of sl2 has smartlist_isin(sl1,E).
+/** Return true iff some element E of sl2 has smartlist_isin(sl1,E).
*/
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
int i;
@@ -360,7 +368,7 @@ int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2) {
return 0;
}
-/* Remove every element E of sl1 such that !smartlist_isin(sl2,E).
+/** Remove every element E of sl1 such that !smartlist_isin(sl2,E).
* Does not preserve the order of sl1.
*/
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
@@ -372,7 +380,7 @@ void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2) {
}
}
-/* Remove every element E of sl1 such that smartlist_isin(sl2,E).
+/** Remove every element E of sl1 such that smartlist_isin(sl2,E).
* Does not preserve the order of sl1.
*/
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
@@ -381,7 +389,7 @@ void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2) {
smartlist_remove(sl1, sl2->list[i]);
}
-/* Return a randomly chosen element of sl; or NULL if sl is empty.
+/** Return a randomly chosen element of sl; or NULL if sl is empty.
*/
void *smartlist_choose(const smartlist_t *sl) {
if(sl->num_used)
@@ -389,15 +397,15 @@ void *smartlist_choose(const smartlist_t *sl) {
return NULL; /* no elements to choose from */
}
-/* Return the 'idx'th element of sl.
+/** Return the idxth element of sl.
*/
void *smartlist_get(const smartlist_t *sl, int idx)
{
tor_assert(sl && idx>=0 && idx < sl->num_used);
return sl->list[idx];
}
-/* Change the value of the 'idx'th element of sl to 'val'; return the old
- * value of the 'idx'th element.
+/** Change the value of the idxth element of sl to val; return the old
+ * value of the idxth element.
*/
void *smartlist_set(smartlist_t *sl, int idx, void *val)
{
@@ -407,9 +415,9 @@ void *smartlist_set(smartlist_t *sl, int idx, void *val)
sl->list[idx] = val;
return old;
}
-/* Remove the 'idx'th element of sl; if idx is not the last element,
- * swap the last element of sl into the 'idx'th space. Return the old value
- * of the 'idx'th element.
+/** Remove the idxth element of sl; if idx is not the last
+ * element, swap the last element of sl into the idxth space.
+ * Return the old value of the idxth element.
*/
void *smartlist_del(smartlist_t *sl, int idx)
{
@@ -419,9 +427,9 @@ void *smartlist_del(smartlist_t *sl, int idx)
sl->list[idx] = sl->list[--sl->num_used];
return old;
}
-/* Remove the 'idx'th element of sl; if idx is not the last element,
+/** Remove the idxth element of sl; if idx is not the last element,
* moving all subsequent elements back one space. Return the old value
- * of the 'idx'th element.
+ * of the idxth element.
*/
void *smartlist_del_keeporder(smartlist_t *sl, int idx)
{
@@ -433,14 +441,15 @@ void *smartlist_del_keeporder(smartlist_t *sl, int idx)
memmove(sl->list+idx, sl->list+idx+1, sizeof(void*)*(sl->num_used-idx));
return old;
}
-/* Return the number of items in sl.
+/** Return the number of items in sl.
*/
int smartlist_len(const smartlist_t *sl)
{
return sl->num_used;
}
-/* Insert the value 'val' as the new 'idx'th element of 'sl', moving all
- * items previously at 'idx' or later forward one space.
+/** Insert the value val as the new idxth element of
+ * sl, moving all items previously at idx or later
+ * forward one space.
*/
void smartlist_insert(smartlist_t *sl, int idx, void *val)
{
@@ -462,9 +471,8 @@ void smartlist_insert(smartlist_t *sl, int idx, void *val)
}
}
-/*****
- * Splay-tree implementation of string-to-void* map
- *****/
+/* Splay-tree implementation of string-to-void* map
+ */
struct strmap_entry_t {
SPLAY_ENTRY(strmap_entry_t) node;
char *key;
@@ -484,7 +492,7 @@ static int compare_strmap_entries(struct strmap_entry_t *a,
SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
-/* Create a new empty map from strings to void*'s.
+/** Create a new empty map from strings to void*'s.
*/
strmap_t* strmap_new(void)
{
@@ -494,10 +502,10 @@ strmap_t* strmap_new(void)
return result;
}
-/* Set the current value for with . Returns the previous
- * value for if one was set, or NULL if one was not.
+/** Set the current value for key to val. Returns the previous
+ * value for key if one was set, or NULL if one was not.
*
- * This function makes a copy of 'key' if necessary, but not of 'val'.
+ * This function makes a copy of key if necessary, but not of val.
*/
void* strmap_set(strmap_t *map, const char *key, void *val)
{
@@ -520,7 +528,7 @@ void* strmap_set(strmap_t *map, const char *key, void *val)
}
}
-/* Return the current value associated with , or NULL if no
+/** Return the current value associated with key, or NULL if no
* value is set.
*/
void* strmap_get(strmap_t *map, const char *key)
@@ -537,9 +545,9 @@ void* strmap_get(strmap_t *map, const char *key)
}
}
-/* Remove the value currently associated with from the map.
+/** Remove the value currently associated with key from the map.
* Return the value if one was set, or NULL if there was no entry for
- * .
+ * key.
*
* Note: you must free any storage associated with the returned value.
*/
@@ -562,7 +570,7 @@ void* strmap_remove(strmap_t *map, const char *key)
}
}
-/* Same as strmap_set, but first converts to lowercase. */
+/** Same as strmap_set, but first converts key to lowercase. */
void* strmap_set_lc(strmap_t *map, const char *key, void *val)
{
/* We could be a little faster by using strcasecmp instead, and a separate
@@ -574,7 +582,7 @@ void* strmap_set_lc(strmap_t *map, const char *key, void *val)
tor_free(lc_key);
return v;
}
-/* Same as strmap_get, but first converts to lowercase. */
+/** Same as strmap_get, but first converts key to lowercase. */
void* strmap_get_lc(strmap_t *map, const char *key)
{
void *v;
@@ -584,7 +592,7 @@ void* strmap_get_lc(strmap_t *map, const char *key)
tor_free(lc_key);
return v;
}
-/* Same as strmap_remove, but first converts to lowercase */
+/** Same as strmap_remove, but first converts key to lowercase */
void* strmap_remove_lc(strmap_t *map, const char *key)
{
void *v;
@@ -595,14 +603,14 @@ void* strmap_remove_lc(strmap_t *map, const char *key)
return v;
}
-
-/* Invoke fn() on every entry of the map, in order. For every entry,
+/** Invoke fn() on every entry of the map, in order. For every entry,
* fn() is invoked with that entry's key, that entry's value, and the
- * value of supplied to strmap_foreach. fn() must return a new
+ * value of data supplied to strmap_foreach. fn() must return a new
* (possibly unmodified) value for each entry: if fn() returns NULL, the
* entry is removed.
*
* Example:
+ * \code
* static void* upcase_and_remove_empty_vals(const char *key, void *val,
* void* data) {
* char *cp = (char*)val;
@@ -620,6 +628,7 @@ void* strmap_remove_lc(strmap_t *map, const char *key)
* ...
*
* strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
+ * \endcode
*/
void strmap_foreach(strmap_t *map,
void* (*fn)(const char *key, void *val, void *data),
@@ -639,10 +648,11 @@ void strmap_foreach(strmap_t *map,
}
}
-/* return an 'iterator' pointer to the front of a map.
+/** return an iterator pointer to the front of a map.
*
* Iterator example:
*
+ * \code
* // uppercase values in "map", removing empty values.
*
* strmap_iter_t *iter;
@@ -661,6 +671,7 @@ void strmap_foreach(strmap_t *map,
* iter = strmap_iter_next(iter);
* }
* }
+ * \endcode
*
*/
strmap_iter_t *strmap_iter_init(strmap_t *map)
@@ -668,14 +679,14 @@ strmap_iter_t *strmap_iter_init(strmap_t *map)
tor_assert(map);
return SPLAY_MIN(strmap_tree, &map->head);
}
-/* Advance the iterator 'iter' for map a single step to the next entry.
+/** Advance the iterator iter for map a single step to the next entry.
*/
strmap_iter_t *strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
{
tor_assert(map && iter);
return SPLAY_NEXT(strmap_tree, &map->head, iter);
}
-/* Advance the iterator 'iter' a single step to the next entry, removing
+/** Advance the iterator iter a single step to the next entry, removing
* the current entry.
*/
strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
@@ -688,7 +699,7 @@ strmap_iter_t *strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
tor_free(iter);
return next;
}
-/* Set *keyp and *valp to the current entry pointed to by iter.
+/** Set *keyp and *valp to the current entry pointed to by iter.
*/
void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
{
@@ -696,14 +707,14 @@ void strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
*keyp = iter->key;
*valp = iter->val;
}
-/* Return true iff iter has advanced past the last entry of map.
+/** Return true iff iter has advanced past the last entry of map.
*/
int strmap_iter_done(strmap_iter_t *iter)
{
return iter == NULL;
}
-/* Remove all entries from