From 90c6c37628c6a0e9a70b3420c4f29f3500f3b543 Mon Sep 17 00:00:00 2001 From: John Sully Date: Fri, 15 Feb 2019 16:55:40 -0500 Subject: [PATCH] make headers C++ safe --- .vscode/settings.json | 52 ++++++++++++++++++++++++++++++++++++++++++- src/adlist.h | 8 +++++++ src/ae.cpp | 23 ++++++++++++++++++- src/ae.h | 8 +++++++ src/anet.h | 8 +++++++ src/crc64.h | 8 +++++++ src/dict.h | 8 +++++++ src/endianconv.h | 8 +++++++ src/networking.cpp | 3 --- src/rax.h | 8 +++++++ src/rdb-s3.cpp | 2 +- src/sds.h | 8 +++++++ src/server.h | 8 +++++++ src/util.h | 8 +++++++ src/zmalloc.h | 8 +++++++ 15 files changed, 162 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6fac65a3d..af6a7e11f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,56 @@ { "files.associations": { "zmalloc.h": "c", - "stat.h": "c" + "stat.h": "c", + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "condition_variable": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "list": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "fstream": "cpp", + "functional": "cpp", + "future": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "numeric": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "scoped_allocator": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp" } } \ No newline at end of file diff --git a/src/adlist.h b/src/adlist.h index c954fac87..e9de81ceb 100644 --- a/src/adlist.h +++ b/src/adlist.h @@ -31,6 +31,10 @@ #ifndef __ADLIST_H__ #define __ADLIST_H__ +#ifdef __cplusplus +extern "C" { +#endif + /* Node, List, and Iterator are the only data structures used currently. */ typedef struct listNode { @@ -92,4 +96,8 @@ void listJoin(list *l, list *o); #define AL_START_HEAD 0 #define AL_START_TAIL 1 +#ifdef __cplusplus +} +#endif + #endif /* __ADLIST_H__ */ diff --git a/src/ae.cpp b/src/ae.cpp index f02ad1e2c..941debf00 100644 --- a/src/ae.cpp +++ b/src/ae.cpp @@ -73,6 +73,7 @@ thread_local aeEventLoop *g_eventLoopThisThread = NULL; enum class AE_ASYNC_OP { PostFunction, + PostCppFunction, DeleteFileEvent, }; typedef struct aeCommand @@ -80,7 +81,10 @@ typedef struct aeCommand AE_ASYNC_OP op; int fd; int mask; - aePostFunctionProc *proc; + union { + aePostFunctionProc *proc; + std::function *pfn; + }; void *clientData; } aeCommand; @@ -108,6 +112,13 @@ void aeProcessCmd(aeEventLoop *eventLoop, int fd, void *, int ) ((aePostFunctionProc*)cmd.proc)(cmd.clientData); break; } + + case AE_ASYNC_OP::PostCppFunction: + { + std::unique_lock ulock(g_lock); + (*cmd.pfn)(); + delete cmd.pfn; + } } } } @@ -123,6 +134,16 @@ int aePostFunction(aeEventLoop *eventLoop, aePostFunctionProc *proc, void *arg) return AE_OK; } +int aePostFunction(aeEventLoop *eventLoop, std::function fn) +{ + aeCommand cmd; + cmd.op = AE_ASYNC_OP::PostCppFunction; + cmd.pfn = new std::function(fn); + auto size = write(eventLoop->fdCmdWrite, &cmd, sizeof(cmd)); + AE_ASSERT(size == sizeof(cmd)); + return AE_OK; +} + aeEventLoop *aeCreateEventLoop(int setsize) { aeEventLoop *eventLoop; int i; diff --git a/src/ae.h b/src/ae.h index 5bbe0013a..5a9a359a7 100644 --- a/src/ae.h +++ b/src/ae.h @@ -33,6 +33,9 @@ #ifndef __AE_H__ #define __AE_H__ +#ifdef __cplusplus +#include +#endif #include #include "fastlock.h" @@ -126,6 +129,11 @@ typedef struct aeEventLoop { /* Prototypes */ aeEventLoop *aeCreateEventLoop(int setsize); int aePostFunction(aeEventLoop *eventLoop, aePostFunctionProc *proc, void *arg); +#ifdef __cplusplus +} // EXTERN C +int aePostFunction(aeEventLoop *eventLoop, std::function fn); +extern "C" { +#endif void aeDeleteEventLoop(aeEventLoop *eventLoop); void aeStop(aeEventLoop *eventLoop); int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask, diff --git a/src/anet.h b/src/anet.h index 7142f78d2..4e272284a 100644 --- a/src/anet.h +++ b/src/anet.h @@ -33,6 +33,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + #define ANET_OK 0 #define ANET_ERR -1 #define ANET_ERR_LEN 256 @@ -77,4 +81,8 @@ int anetFormatAddr(char *fmt, size_t fmt_len, char *ip, int port); int anetFormatPeer(int fd, char *fmt, size_t fmt_len); int anetFormatSock(int fd, char *fmt, size_t fmt_len); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/crc64.h b/src/crc64.h index c9fca519d..e63cbc2e3 100644 --- a/src/crc64.h +++ b/src/crc64.h @@ -3,10 +3,18 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l); #ifdef REDIS_TEST int crc64Test(int argc, char *argv[]); #endif +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/dict.h b/src/dict.h index 62018cc44..ba8fb11c1 100644 --- a/src/dict.h +++ b/src/dict.h @@ -35,6 +35,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + #ifndef __DICT_H #define __DICT_H @@ -186,4 +190,8 @@ extern dictType dictTypeHeapStringCopyKey; extern dictType dictTypeHeapStrings; extern dictType dictTypeHeapStringCopyKeyValue; +#ifdef __cplusplus +} +#endif + #endif /* __DICT_H */ diff --git a/src/endianconv.h b/src/endianconv.h index 475f72b08..3c8aef14f 100644 --- a/src/endianconv.h +++ b/src/endianconv.h @@ -36,6 +36,10 @@ #include "config.h" #include +#ifdef __cplusplus +extern "C" { +#endif + void memrev16(void *p); void memrev32(void *p); void memrev64(void *p); @@ -75,4 +79,8 @@ uint64_t intrev64(uint64_t v); int endianconvTest(int argc, char *argv[]); #endif +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/networking.cpp b/src/networking.cpp index 7844ad318..bb3788501 100644 --- a/src/networking.cpp +++ b/src/networking.cpp @@ -27,11 +27,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ -extern "C" -{ #include "server.h" #include "atomicvar.h" -} #include #include #include diff --git a/src/rax.h b/src/rax.h index 4823bd874..390549b16 100644 --- a/src/rax.h +++ b/src/rax.h @@ -33,6 +33,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + /* Representation of a radix tree as implemented in this file, that contains * the strings "foo", "foobar" and "footer" after the insertion of each * word. When the node represents a key inside the radix tree, we write it @@ -215,4 +219,8 @@ void raxSetDebugMsg(int onoff); * in a low level way, so this function is exported as well. */ void raxSetData(raxNode *n, void *data); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/rdb-s3.cpp b/src/rdb-s3.cpp index d6bf34ae0..bd00bb2bd 100644 --- a/src/rdb-s3.cpp +++ b/src/rdb-s3.cpp @@ -1,7 +1,7 @@ extern "C" { #include "rio.h" -#include "server.h" } +#include "server.h" #include #include diff --git a/src/sds.h b/src/sds.h index 55292ed83..1985fb263 100644 --- a/src/sds.h +++ b/src/sds.h @@ -40,6 +40,10 @@ extern const char *SDS_NOINIT; #include #include +#ifdef __cplusplus +extern "C" { +#endif + typedef char *sds; /* Note: sdshdr5 is never used, we just access the flags byte directly. @@ -288,4 +292,8 @@ void sds_free(void *ptr); int sdsTest(int argc, char *argv[]); #endif +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/server.h b/src/server.h index 5d507f534..dc595b05a 100644 --- a/src/server.h +++ b/src/server.h @@ -74,6 +74,10 @@ typedef long long mstime_t; /* millisecond time type. */ #include "endianconv.h" #include "crc64.h" +#ifdef __cplusplus +extern "C" { +#endif + /* Error codes */ #define C_OK 0 #define C_ERR -1 @@ -2307,4 +2311,8 @@ inline int ielFromEventLoop(const aeEventLoop *eventLoop) #define redisDebugMark() \ printf("-- MARK %s:%d --\n", __FILE__, __LINE__) +#ifdef __cplusplus +} +#endif + #endif \ No newline at end of file diff --git a/src/util.h b/src/util.h index b6c01aa59..97ca9471b 100644 --- a/src/util.h +++ b/src/util.h @@ -33,6 +33,10 @@ #include #include "sds.h" +#ifdef __cplusplus +extern "C" { +#endif + /* The maximum number of characters needed to represent a long double * as a string (long double has a huge range). * This should be the size of the buffer given to ld2string */ @@ -58,4 +62,8 @@ int pathIsBaseName(char *path); int utilTest(int argc, char **argv); #endif +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/zmalloc.h b/src/zmalloc.h index 30ba80492..1d1ff930b 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -86,6 +86,10 @@ #define HAVE_DEFRAG #endif +#ifdef __cplusplus +extern "C" { +#endif + void *zmalloc(size_t size, enum MALLOC_CLASS mclass); void *zcalloc(size_t size, enum MALLOC_CLASS mclass); void *zrealloc(void *ptr, size_t size, enum MALLOC_CLASS mclass); @@ -116,4 +120,8 @@ size_t zmalloc_usable(void *ptr); int zmalloc_test(int argc, char **argv); #endif +#ifdef __cplusplus +} +#endif + #endif /* __ZMALLOC_H */