From f09f7bc6a8b64585cd74d1fca712c6d0cab2f737 Mon Sep 17 00:00:00 2001 From: Bulat Shakirzyanov Date: Thu, 5 Apr 2012 01:39:20 +0200 Subject: [PATCH] Add functions to look up req and handle sizes Useful for FFI bindings. Closes #370. --- include/uv.h | 12 ++++++++++++ src/uv-common.c | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/include/uv.h b/include/uv.h index 4d06d512..0ede3083 100644 --- a/include/uv.h +++ b/include/uv.h @@ -382,6 +382,18 @@ struct uv_handle_s { UV_HANDLE_FIELDS }; +/* + * Returns size of various handle types, useful for FFI + * bindings to allocate correct memory without copying struct + * definitions + */ +UV_EXTERN size_t uv_handle_size(uv_handle_type type); + +/* + * Returns size of request types, useful for dynamic lookup with FFI + */ +UV_EXTERN size_t uv_req_size(uv_req_type type); + /* * Returns 1 if the prepare/check/idle/timer handle has been started, 0 * otherwise. For other handle types this always returns 1. diff --git a/src/uv-common.c b/src/uv-common.c index 2e424a9c..c8192bd7 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -32,6 +32,25 @@ #include "ares/inet_net_pton.h" #include "ares/inet_ntop.h" +#define XX(uc, lc) case UV_##uc: return sizeof(uv_##lc##_t); + +size_t uv_handle_size(uv_handle_type type) { + switch (type) { + UV_HANDLE_TYPE_MAP(XX) + default: + return -1; + } +} + +size_t uv_req_size(uv_req_type type) { + switch(type) { + UV_REQ_TYPE_MAP(XX) + default: + return -1; + } +} + +#undef XX size_t uv_strlcpy(char* dst, const char* src, size_t size) { size_t n;