map/msgpack-rpc: Declare/define maps rpc method handlers

The new map type uses `String` instances as keys to avoid unnecessary copying to
zero-terminated buffers.
This commit is contained in:
Thiago de Arruda
2014-09-03 11:46:35 -03:00
parent e2143674ae
commit c39ae3e4d4
4 changed files with 24 additions and 2 deletions

View File

@@ -93,6 +93,7 @@ output:write([[
#include "nvim/map.h" #include "nvim/map.h"
#include "nvim/log.h" #include "nvim/log.h"
#include "nvim/vim.h"
#include "nvim/os/msgpack_rpc.h" #include "nvim/os/msgpack_rpc.h"
#include "nvim/os/msgpack_rpc_helpers.h" #include "nvim/os/msgpack_rpc_helpers.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
@@ -268,8 +269,6 @@ end
output:write('\n}\n\n') output:write('\n}\n\n')
output:write([[ output:write([[
#define min(X, Y) (X < Y ? X : Y)
Object msgpack_rpc_dispatch(uint64_t channel_id, Object msgpack_rpc_dispatch(uint64_t channel_id,
msgpack_object *req, msgpack_object *req,
Error *error) Error *error)

View File

@@ -1,10 +1,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include "nvim/map.h" #include "nvim/map.h"
#include "nvim/map_defs.h" #include "nvim/map_defs.h"
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/memory.h" #include "nvim/memory.h"
#include "nvim/os/msgpack_rpc.h"
#include "nvim/lib/khash.h" #include "nvim/lib/khash.h"
@@ -87,7 +89,23 @@
return rv; \ return rv; \
} }
static inline khint_t String_hash(String s)
{
khint_t h = 0;
for (size_t i = 0; i < s.size && s.data[i]; i++) {
h = (h << 5) - h + (uint8_t)s.data[i];
}
return h;
}
static inline bool String_eq(String a, String b)
{
return strncmp(a.data, b.data, min(a.size, b.size)) == 0;
}
MAP_IMPL(cstr_t, uint64_t, DEFAULT_INITIALIZER) MAP_IMPL(cstr_t, uint64_t, DEFAULT_INITIALIZER)
MAP_IMPL(cstr_t, ptr_t, DEFAULT_INITIALIZER) MAP_IMPL(cstr_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(ptr_t, ptr_t, DEFAULT_INITIALIZER) MAP_IMPL(ptr_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(uint64_t, ptr_t, DEFAULT_INITIALIZER) MAP_IMPL(uint64_t, ptr_t, DEFAULT_INITIALIZER)
MAP_IMPL(String, rpc_method_handler_fn, DEFAULT_INITIALIZER)

View File

@@ -4,6 +4,8 @@
#include <stdbool.h> #include <stdbool.h>
#include "nvim/map_defs.h" #include "nvim/map_defs.h"
#include "nvim/api/private/defs.h"
#include "nvim/os/msgpack_rpc.h"
#define MAP_DECLS(T, U) \ #define MAP_DECLS(T, U) \
KHASH_DECLARE(T##_##U##_map, T, U) \ KHASH_DECLARE(T##_##U##_map, T, U) \
@@ -23,6 +25,7 @@ MAP_DECLS(cstr_t, uint64_t)
MAP_DECLS(cstr_t, ptr_t) MAP_DECLS(cstr_t, ptr_t)
MAP_DECLS(ptr_t, ptr_t) MAP_DECLS(ptr_t, ptr_t)
MAP_DECLS(uint64_t, ptr_t) MAP_DECLS(uint64_t, ptr_t)
MAP_DECLS(String, rpc_method_handler_fn)
#define map_new(T, U) map_##T##_##U##_new #define map_new(T, U) map_##T##_##U##_new
#define map_free(T, U) map_##T##_##U##_free #define map_free(T, U) map_##T##_##U##_free

View File

@@ -8,6 +8,8 @@
#ifndef NVIM_VIM_H #ifndef NVIM_VIM_H
# define NVIM_VIM_H # define NVIM_VIM_H
#define min(X, Y) (X < Y ? X : Y)
#include "nvim/types.h" #include "nvim/types.h"
#include "nvim/pos.h" // for linenr_T, MAXCOL, etc... #include "nvim/pos.h" // for linenr_T, MAXCOL, etc...