aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2004-04-03 00:58:54 +0000
committerNick Mathewson <nickm@torproject.org>2004-04-03 00:58:54 +0000
commitfc4d15baf6d3833015feeceb0c9374304bb9900c (patch)
tree21bff329b403fd81e2df0264c9d4f7b96d8781c9 /src/common
parentbbc94849576878955b58e1297e43d2b83db576cb (diff)
downloadtor-fc4d15baf6d3833015feeceb0c9374304bb9900c.tar
tor-fc4d15baf6d3833015feeceb0c9374304bb9900c.tar.gz
Hide smartlist internals
svn:r1451
Diffstat (limited to 'src/common')
-rw-r--r--src/common/util.c32
-rw-r--r--src/common/util.h18
2 files changed, 45 insertions, 5 deletions
diff --git a/src/common/util.c b/src/common/util.c
index a54ebe559..0b6c8d2c7 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -129,6 +129,13 @@ void hex_encode(const char *from, int fromlen, char *to)
* _choose() returns a random element.
*/
#define SMARTLIST_DEFAULT_CAPACITY 32
+
+struct smartlist_t {
+ void **list;
+ int num_used;
+ int capacity;
+};
+
smartlist_t *smartlist_create() {
smartlist_t *sl = tor_malloc(sizeof(smartlist_t));
sl->num_used = 0;
@@ -143,6 +150,8 @@ void smartlist_free(smartlist_t *sl) {
}
void smartlist_set_capacity(smartlist_t *sl, int n) {
+ if (n<0)
+ n = sl->num_used;
if (sl->capacity != n && sl->num_used < n) {
sl->capacity = n;
sl->list = tor_realloc(sl->list, sizeof(void*)*sl->capacity);
@@ -208,6 +217,29 @@ void *smartlist_choose(smartlist_t *sl) {
return NULL; /* no elements to choose from */
}
+void *smartlist_get(smartlist_t *sl, int idx)
+{
+ return sl->list[idx];
+}
+void *smartlist_set(smartlist_t *sl, int idx, void *val)
+{
+ void *old;
+ old = sl->list[idx];
+ sl->list[idx] = val;
+ return old;
+}
+void *smartlist_del(smartlist_t *sl, int idx)
+{
+ void *old;
+ old = sl->list[idx];
+ sl->list[idx] = sl->list[--sl->num_used];
+ return old;
+}
+int smartlist_len(smartlist_t *sl)
+{
+ return sl->num_used;
+}
+
/*
* Splay-tree implementation of string-to-void* map
*/
diff --git a/src/common/util.h b/src/common/util.h
index e5fdb4fde..60c5a5d75 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -83,11 +83,7 @@ void set_uint32(char *cp, uint32_t v);
void hex_encode(const char *from, int fromlen, char *to);
-typedef struct smartlist_t {
- void **list;
- int num_used;
- int capacity;
-} smartlist_t;
+typedef struct smartlist_t smartlist_t;
smartlist_t *smartlist_create();
void smartlist_free(smartlist_t *sl);
@@ -99,6 +95,18 @@ int smartlist_overlap(smartlist_t *sl1, smartlist_t *sl2);
void smartlist_intersect(smartlist_t *sl1, smartlist_t *sl2);
void smartlist_subtract(smartlist_t *sl1, smartlist_t *sl2);
void *smartlist_choose(smartlist_t *sl);
+void *smartlist_get(smartlist_t *sl, int idx);
+void *smartlist_set(smartlist_t *sl, int idx, void *val);
+void *smartlist_del(smartlist_t *sl, int idx);
+int smartlist_len(smartlist_t *sl);
+#define SMARTLIST_FOREACH(sl, type, var, cmd) \
+ do { \
+ int sl_idx, sl_len=smartlist_len(sl); \
+ type var; \
+ for(sl_idx = 0; sl_idx < sl_len; ++sl_idx) { \
+ var = smartlist_get((sl),sl_idx); \
+ do {cmd;} while(0); \
+ } } while (0)
/* Map from const char * to void*. Implemented with a splay tree. */
typedef struct strmap_t strmap_t;