diff --git a/lualib-src/lsha1.c b/lualib-src/lsha1.c index 8f539844..310e8e14 100644 --- a/lualib-src/lsha1.c +++ b/lualib-src/lsha1.c @@ -263,11 +263,11 @@ lsha1(lua_State *L) { #define BLOCKSIZE 64 static inline void -xor_key(uint8_t key[BLOCKSIZE], uint32_t xor) { +xor_key(uint8_t key[BLOCKSIZE], uint32_t xor_) { int i; for (i=0;icap <= b->size + sz); if (b->ptr == b->buffer) { - b->ptr = malloc(b->cap); + b->ptr = (uint8_t*)malloc(b->cap); memcpy(b->ptr, b->buffer, b->size); } else { - b->ptr = realloc(b->ptr, b->cap); + b->ptr = (uint8_t*)realloc(b->ptr, b->cap); } } @@ -329,7 +329,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth) break; case LUA_TUSERDATA: { append_key(bs, L, BSON_DOCUMENT, key, sz); - int32_t * doc = lua_touserdata(L,-1); + int32_t * doc = (int32_t*)lua_touserdata(L,-1); int32_t sz = *doc; bson_reserve(bs,sz); memcpy(bs->ptr + bs->size, doc, sz); @@ -574,7 +574,7 @@ static int ltostring(lua_State *L) { size_t sz = lua_rawlen(L, 1); void * ud = lua_touserdata(L,1); - lua_pushlstring(L, ud, sz); + lua_pushlstring(L, (const char*)ud, sz); return 1; } @@ -591,7 +591,7 @@ make_object(lua_State *L, int type, const void * ptr, size_t len) { luaL_buffinit(L, &b); luaL_addchar(&b, 0); luaL_addchar(&b, type); - luaL_addlstring(&b, ptr, len); + luaL_addlstring(&b, (const char*)ptr, len); luaL_pushresult(&b); } @@ -600,7 +600,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { luaL_checkstack(L, 16, NULL); // reserve enough stack space to unpack table int sz = read_int32(L, br); const void * bytes = read_bytes(L, br, sz-5); - struct bson_reader t = { bytes, sz-5 }; + struct bson_reader t = { (const uint8_t*)bytes, sz-5 }; int end = read_byte(L, br); if (end != '\0') { luaL_error(L, "Invalid document end"); @@ -632,7 +632,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { if (sz <= 0) { luaL_error(L, "Invalid bson string , length = %d", sz); } - lua_pushlstring(L, read_bytes(L, &t, sz), sz-1); + lua_pushlstring(L, (const char*)read_bytes(L, &t, sz), sz-1); break; } case BSON_DOCUMENT: @@ -650,7 +650,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { luaL_addchar(&b, 0); luaL_addchar(&b, BSON_BINARY); luaL_addchar(&b, subtype); - luaL_addlstring(&b, read_bytes(L, &t, sz), sz); + luaL_addlstring(&b, (const char*)read_bytes(L, &t, sz), sz); luaL_pushresult(&b); break; } @@ -666,7 +666,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { case BSON_MINKEY: case BSON_MAXKEY: case BSON_NULL: { - char key[] = { 0, bt }; + char key[] = { 0, (char)bt }; lua_pushlstring(L, key, sizeof(key)); break; } @@ -739,7 +739,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) { static int lmakeindex(lua_State *L) { - int32_t *bson = luaL_checkudata(L,1,"bson"); + int32_t *bson = (int32_t*)luaL_checkudata(L,1,"bson"); const uint8_t * start = (const uint8_t *)bson; struct bson_reader br = { start+4, get_length(start) - 5 }; lua_newtable(L); @@ -871,7 +871,7 @@ lreplace(lua_State *L) { int id = lua_tointeger(L, -1); int type = id & ((1<<(BSON_TYPE_SHIFT)) - 1); int offset = id >> BSON_TYPE_SHIFT; - uint8_t * start = lua_touserdata(L,1); + uint8_t * start = (uint8_t*)lua_touserdata(L,1); struct bson b = { 0,16, start + offset }; switch (type) { case BSON_REAL: @@ -910,7 +910,7 @@ lreplace(lua_State *L) { static int ldecode(lua_State *L) { - const int32_t * data = lua_touserdata(L,1); + const int32_t * data = (const int32_t*)lua_touserdata(L,1); if (data == NULL) { return 0; } @@ -945,7 +945,7 @@ bson_meta(lua_State *L) { static int encode_bson(lua_State *L) { - struct bson *b = lua_touserdata(L, 2); + struct bson *b = (struct bson*)lua_touserdata(L, 2); lua_settop(L, 1); if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) { pack_meta_dict(L, b, 0); @@ -978,7 +978,7 @@ lencode(lua_State *L) { static int encode_bson_byorder(lua_State *L) { int n = lua_gettop(L); - struct bson *b = lua_touserdata(L, n); + struct bson *b = (struct bson*)lua_touserdata(L, n); lua_settop(L, --n); pack_ordered_dict(L, b, n, 0); lua_settop(L,0); @@ -1106,7 +1106,7 @@ lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) { char oid[24]; int i; const uint8_t * id = buf; - static char *hex = "0123456789abcdef"; + static const char *hex = "0123456789abcdef"; for (i=0;i<12;i++) { oid[i*2] = hex[id[i] >> 4]; oid[i*2+1] = hex[id[i] & 0xf]; @@ -1219,7 +1219,7 @@ ltype(lua_State *L) { static void typeclosure(lua_State *L) { - static const char * typename[] = { + static const char * typename_[] = { "number", // 1 "boolean", // 2 "table", // 3 @@ -1236,9 +1236,9 @@ typeclosure(lua_State *L) { "unsupported", // 14 }; int i; - int n = sizeof(typename)/sizeof(typename[0]); + int n = sizeof(typename_)/sizeof(typename_[0]); for (i=0;i SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, chunksz, 0); + buffer = (uint8_t*)lua_newuserdatauv(L, chunksz, 0); } int i; for (i=0;i<(int)textsz-7;i+=8) { @@ -503,7 +503,7 @@ ldesdecode(lua_State *L) { uint8_t tmp[SMALL_CHUNK]; uint8_t *buffer = tmp; if (textsz > SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, textsz, 0); + buffer = (uint8_t*)lua_newuserdatauv(L, textsz, 0); } for (i=0;i SMALL_CHUNK/2) { - buffer = lua_newuserdatauv(L, sz * 2, 0); + buffer = (char*)lua_newuserdatauv(L, sz * 2, 0); } int i; for (i=0;i SMALL_CHUNK*2) { - buffer = lua_newuserdatauv(L, sz / 2, 0); + buffer = (char*)lua_newuserdatauv(L, sz / 2, 0); } int i; for (i=0;i SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, encode_sz, 0); + buffer = (char*)lua_newuserdatauv(L, encode_sz, 0); } int i,j; j=0; @@ -953,7 +953,7 @@ lb64decode(lua_State *L) { char tmp[SMALL_CHUNK]; char *buffer = tmp; if (decode_sz > SMALL_CHUNK) { - buffer = lua_newuserdatauv(L, decode_sz, 0); + buffer = (char*)lua_newuserdatauv(L, decode_sz, 0); } int i,j; int output = 0; diff --git a/lualib-src/lua-mongo.c b/lualib-src/lua-mongo.c index 4ceffa06..1f6c6b69 100644 --- a/lualib-src/lua-mongo.c +++ b/lualib-src/lua-mongo.c @@ -89,10 +89,10 @@ buffer_reserve(struct buffer *b, int sz) { } while (b->cap <= b->size + sz); if (b->ptr == b->buffer) { - b->ptr = skynet_malloc(b->cap); + b->ptr = (uint8_t*)malloc(b->cap); memcpy(b->ptr, b->buffer, b->size); } else { - b->ptr = skynet_realloc(b->ptr, b->cap); + b->ptr = (uint8_t*)realloc(b->ptr, b->cap); } } @@ -208,7 +208,7 @@ static int op_reply(lua_State *L) { size_t data_len = 0; const char * data = luaL_checklstring(L,1,&data_len); - struct { + struct reply_type { // int32_t length; // total message size, including this int32_t request_id; // identifier for this message int32_t response_id; // requestID from the original request @@ -218,7 +218,8 @@ op_reply(lua_State *L) { int32_t cursor_id[2]; int32_t starting; int32_t number; - } const *reply = (const void *)data; + }; + const struct reply_type* reply = (const struct reply_type*)data; if (data_len < sizeof(*reply)) { lua_pushboolean(L, 0); diff --git a/skynet-src/atomic.h b/skynet-src/atomic.h index 4dd3ba82..ea98ffd5 100644 --- a/skynet-src/atomic.h +++ b/skynet-src/atomic.h @@ -24,41 +24,49 @@ #else +#if defined (__cplusplus) +#include +#define STD_ std:: +#define atomic_value_type_(p, v) decltype((p)->load())(v) +#else #include +#define STD_ +#define atomic_value_type_(p, v) v +#endif -#define ATOM_INT atomic_int -#define ATOM_POINTER atomic_uintptr_t -#define ATOM_SIZET atomic_size_t -#define ATOM_ULONG atomic_ulong -#define ATOM_INIT(ref, v) atomic_init(ref, v) -#define ATOM_LOAD(ptr) atomic_load(ptr) -#define ATOM_STORE(ptr, v) atomic_store(ptr, v) +#define ATOM_INT STD_ atomic_int +#define ATOM_POINTER STD_ atomic_uintptr_t +#define ATOM_SIZET STD_ atomic_size_t +#define ATOM_ULONG STD_ atomic_ulong +#define ATOM_INIT(ref, v) STD_ atomic_init(ref, v) +#define ATOM_LOAD(ptr) STD_ atomic_load(ptr) +#define ATOM_STORE(ptr, v) STD_ atomic_store(ptr, v) static inline int -ATOM_CAS(atomic_int *ptr, int oval, int nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS(STD_ atomic_int *ptr, int oval, int nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int -ATOM_CAS_SIZET(atomic_size_t *ptr, size_t oval, size_t nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS_SIZET(STD_ atomic_size_t *ptr, size_t oval, size_t nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int -ATOM_CAS_ULONG(atomic_ulong *ptr, unsigned long oval, unsigned long nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS_ULONG(STD_ atomic_ulong *ptr, unsigned long oval, unsigned long nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } static inline int -ATOM_CAS_POINTER(atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { - return atomic_compare_exchange_weak(ptr, &(oval), nval); +ATOM_CAS_POINTER(STD_ atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { + return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval); } -#define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1) -#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1) -#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n) -#define ATOM_FSUB(ptr,n) atomic_fetch_sub(ptr, n) -#define ATOM_FAND(ptr,n) atomic_fetch_and(ptr, n) +#define ATOM_FINC(ptr) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr,1)) +#define ATOM_FDEC(ptr) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, 1)) +#define ATOM_FADD(ptr,n) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr, n)) +#define ATOM_FSUB(ptr,n) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, n)) +#define ATOM_FAND(ptr,n) STD_ atomic_fetch_and(ptr, atomic_value_type_(ptr, n)) #endif diff --git a/skynet-src/spinlock.h b/skynet-src/spinlock.h index 7726904d..d0adcc56 100644 --- a/skynet-src/spinlock.h +++ b/skynet-src/spinlock.h @@ -47,10 +47,11 @@ spinlock_destroy(struct spinlock *lock) { #else // __STDC_NO_ATOMICS__ -#include -#define atomic_test_and_set_(ptr) atomic_exchange_explicit(ptr, 1, memory_order_acquire) -#define atomic_clear_(ptr) atomic_store_explicit(ptr, 0, memory_order_release); -#define atomic_load_relaxed_(ptr) atomic_load_explicit(ptr, memory_order_relaxed) +#include "atomic.h" + +#define atomic_test_and_set_(ptr) STD_ atomic_exchange_explicit(ptr, 1, STD_ memory_order_acquire) +#define atomic_clear_(ptr) STD_ atomic_store_explicit(ptr, 0, STD_ memory_order_release); +#define atomic_load_relaxed_(ptr) STD_ atomic_load_explicit(ptr, STD_ memory_order_relaxed) #if defined(__x86_64__) #include // For _mm_pause @@ -60,12 +61,12 @@ spinlock_destroy(struct spinlock *lock) { #endif struct spinlock { - atomic_int lock; + STD_ atomic_int lock; }; static inline void spinlock_init(struct spinlock *lock) { - atomic_init(&lock->lock, 0); + STD_ atomic_init(&lock->lock, 0); } static inline void