diff --git a/lib/spdylay_map.c b/lib/spdylay_map.c index eaa6a42..842eef1 100644 --- a/lib/spdylay_map.c +++ b/lib/spdylay_map.c @@ -24,11 +24,10 @@ */ #include "spdylay_map.h" -int spdylay_map_init(spdylay_map *map) +void spdylay_map_init(spdylay_map *map) { map->root = NULL; map->size = 0; - return 0; } static void spdylay_map_entry_free(spdylay_map_entry *entry) diff --git a/lib/spdylay_map.h b/lib/spdylay_map.h index d78b2cd..780a620 100644 --- a/lib/spdylay_map.h +++ b/lib/spdylay_map.h @@ -49,18 +49,59 @@ typedef struct { size_t size; } spdylay_map; -int spdylay_map_init(spdylay_map *map); +/* + * Initializes the map |map|. + */ +void spdylay_map_init(spdylay_map *map); +/* + * Deallocates any resources allocated for |map|. The stored items are + * not freed by this function. Use spdylay_map_each() to free each + * item. + */ void spdylay_map_free(spdylay_map *map); +/* + * Inserts the new item |val| with the key |key| to the map |map|. + * + * This function returns 0 if it succeeds, or one of the following + * negative error code: + * + * SPDYLAY_ERR_INVALID_ARGUMENT + * The item associated by |key| already exists. + * + * SPDYLAY_ERR_NOMEM + * Out of memory. + */ int spdylay_map_insert(spdylay_map *map, key_type key, void *val); +/* + * Returns the item associated by the key |key|. If there is no such + * item, this function returns NULL. + */ void* spdylay_map_find(spdylay_map *map, key_type key); +/* + * Erases the item associated by the key |key|. The erased item is + * not freed by this function. + * + * This function returns 0 if it succeeds, or one of the following + * negative error codes: + * + * SPDYLAY_ERR_INVALID_ARGUMENT + * The item associated by |key| does not exist. + */ void spdylay_map_erase(spdylay_map *map, key_type key); +/* + * Returns the number of items stored in the map |map|. + */ size_t spdylay_map_size(spdylay_map *map); +/* + * Applies the function |func| to each key/item pair in the map |map|. + * This function is useful to free item in the map. + */ void spdylay_map_each(spdylay_map *map, void (*func)(key_type key, void *val)); #endif /* SPDYLAY_MAP_H */ diff --git a/lib/spdylay_session.c b/lib/spdylay_session.c index ec6fcf9..ded051c 100644 --- a/lib/spdylay_session.c +++ b/lib/spdylay_session.c @@ -102,10 +102,7 @@ static int spdylay_session_new(spdylay_session **session_ptr, if(r != 0) { goto fail_hd_inflater; } - r = spdylay_map_init(&(*session_ptr)->streams); - if(r != 0) { - goto fail_streams; - } + spdylay_map_init(&(*session_ptr)->streams); r = spdylay_pq_init(&(*session_ptr)->ob_pq, spdylay_outbound_item_compar); if(r != 0) { goto fail_ob_pq; @@ -159,7 +156,6 @@ static int spdylay_session_new(spdylay_session **session_ptr, spdylay_pq_free(&(*session_ptr)->ob_pq); fail_ob_pq: spdylay_map_free(&(*session_ptr)->streams); - fail_streams: spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater); fail_hd_inflater: spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater); diff --git a/tests/spdylay_map_test.c b/tests/spdylay_map_test.c index af3a046..99b2cb9 100644 --- a/tests/spdylay_map_test.c +++ b/tests/spdylay_map_test.c @@ -31,7 +31,7 @@ void test_spdylay_map() { spdylay_map map; - CU_ASSERT(0 == spdylay_map_init(&map)); + spdylay_map_init(&map); CU_ASSERT(0 == spdylay_map_insert(&map, 1, "foo")); CU_ASSERT(strcmp("foo", spdylay_map_find(&map, 1)) == 0); CU_ASSERT(1 == spdylay_map_size(&map));