Improve map module: Change scopes

- Move `Map` structure definition to `map_defs.h`
- Use `KHASH_DECLARE` on map_defs.h to declare khash function prototypes.
- Redefine `map_foreach` into a macro
- Refactor server.c module to use the new `map_foreach` macro.
This commit is contained in:
Thiago de Arruda 2014-05-19 10:52:04 -03:00
parent eb7513bbd2
commit 974eade1a6
4 changed files with 27 additions and 43 deletions

View File

@ -8,15 +8,7 @@
#include "nvim/lib/khash.h"
typedef struct {
void *ptr;
} Value;
KHASH_MAP_INIT_STR(Map, Value)
struct map {
khash_t(Map) *table;
};
__KHASH_IMPL(Map,, kh_cstr_t, void *, 1, kh_str_hash_func, kh_str_hash_equal)
Map *map_new()
{
@ -40,7 +32,7 @@ void *map_get(Map *map, const char *key)
return NULL;
}
return kh_val(map->table, k).ptr;
return kh_val(map->table, k);
}
bool map_has(Map *map, const char *key)
@ -53,15 +45,14 @@ void *map_put(Map *map, const char *key, void *value)
int ret;
void *rv = NULL;
khiter_t k = kh_put(Map, map->table, key, &ret);
Value val = {.ptr = value};
if (!ret) {
// key present, return the current value
rv = kh_val(map->table, k).ptr;
rv = kh_val(map->table, k);
kh_del(Map, map->table, k);
}
kh_val(map->table, k) = val;
kh_val(map->table, k) = value;
return rv;
}
@ -72,20 +63,10 @@ void *map_del(Map *map, const char *key)
khiter_t k;
if ((k = kh_get(Map, map->table, key)) != kh_end(map->table)) {
rv = kh_val(map->table, k).ptr;
rv = kh_val(map->table, k);
kh_del(Map, map->table, k);
}
return rv;
}
void map_foreach(Map *map, key_value_cb cb)
{
const char *key;
Value value;
kh_foreach(map->table, key, value, {
cb(map, (const char *)key, value.ptr);
});
}

View File

@ -47,11 +47,11 @@ void *map_put(Map *map, const char *key, void *value);
/// @return The current value if exists or NULL otherwise
void *map_del(Map *map, const char *key);
/// Iterates through each key/value pair in the map
///
/// @param map The `Map` instance
/// @param cb A function that will be called for each key/value
void map_foreach(Map *map, key_value_cb cb);
#define map_foreach(map, key, value, block) \
kh_foreach(map->table, key, value, block)
#define map_foreach_value(map, value, block) \
kh_foreach_value(map->table, value, block)
#endif // NVIM_MAP_H

View File

@ -1,7 +1,14 @@
#ifndef NVIM_MAP_DEFS_H
#define NVIM_MAP_DEFS_H
typedef struct map Map;
#include "nvim/lib/khash.h"
KHASH_DECLARE(Map, kh_cstr_t, void *)
typedef struct {
khash_t(Map) *table;
} Map;
/// Callback for iterating through each key/value pair in a map
///

View File

@ -45,7 +45,6 @@ typedef struct {
static Map *servers = NULL;
static void close_server(Map *map, const char *endpoint, void *server);
static void connection_cb(uv_stream_t *server, int status);
static void free_client(uv_handle_t *handle);
static void free_server(uv_handle_t *handle);
@ -70,7 +69,15 @@ void server_teardown()
return;
}
map_foreach(servers, close_server);
Server *server;
map_foreach_value(servers, server, {
if (server->type == kServerTypeTcp) {
uv_close((uv_handle_t *)&server->socket.tcp.handle, free_server);
} else {
uv_close((uv_handle_t *)&server->socket.pipe.handle, free_server);
}
});
}
void server_start(char *endpoint, ChannelProtocol prot)
@ -221,17 +228,6 @@ static void connection_cb(uv_stream_t *server, int status)
channel_from_stream(client, srv->protocol);
}
static void close_server(Map *map, const char *endpoint, void *srv)
{
Server *server = srv;
if (server->type == kServerTypeTcp) {
uv_close((uv_handle_t *)&server->socket.tcp.handle, free_server);
} else {
uv_close((uv_handle_t *)&server->socket.pipe.handle, free_server);
}
}
static void free_client(uv_handle_t *handle)
{
free(handle);