ithewei 5 vuotta sitten
vanhempi
commit
0c479ee622

+ 16 - 0
base/hdef.h

@@ -78,6 +78,22 @@ typedef void (*procedure_t)(void* userdata);
 #define REPEATED
 #endif
 
+#ifndef LD
+#define LD(v)   ((long)(v))
+#endif
+
+#ifndef LU
+#define LU(v)   ((unsigned long)(v))
+#endif
+
+#ifndef LLD
+#define LLD(v)   ((long long)(v))
+#endif
+
+#ifndef LLU
+#define LLU(v)   ((unsigned long long)(v))
+#endif
+
 #ifndef ABS
 #define ABS(n)  ((n) > 0 ? (n) : -(n))
 #endif

+ 0 - 1
base/hthreadpool.h

@@ -32,7 +32,6 @@ public:
             status = RUNNING;
             for (int i = 0; i < pool_size; ++i) {
                 workers.emplace_back(std::thread([this]{
-                    //hlogd("work thread[%X] running...", gettid());
                     while (status != STOP) {
                         while (status == PAUSE) {
                             std::this_thread::yield();

+ 5 - 3
examples/hloop_test.c

@@ -15,13 +15,15 @@ void mylogger(int loglevel, const char* buf, int len) {
 }
 
 void on_idle(hidle_t* idle) {
-    printf("on_idle: event_id=%llu\tpriority=%d\tuserdata=%ld\n", hevent_id(idle), hevent_priority(idle), (long)(hevent_userdata(idle)));
+    printf("on_idle: event_id=%llu\tpriority=%d\tuserdata=%ld\n",
+        LLU(hevent_id(idle)), hevent_priority(idle), (long)(intptr_t)(hevent_userdata(idle)));
 }
 
 void on_timer(htimer_t* timer) {
     hloop_t* loop = hevent_loop(timer);
     printf("on_timer: event_id=%llu\tpriority=%d\tuserdata=%ld\ttime=%llus\thrtime=%lluus\n",
-        hevent_id(timer), hevent_priority(timer), (long)(hevent_userdata(timer)), hloop_now(loop), hloop_now_hrtime(loop));
+        LLU(hevent_id(timer)), hevent_priority(timer), (long)(intptr_t)(hevent_userdata(timer)),
+        LLU(hloop_now(loop)), LLU(hloop_now_hrtime(loop)));
 }
 
 void cron_hourly(htimer_t* timer) {
@@ -64,7 +66,7 @@ int main() {
     // test timer timeout
     for (int i = 1; i <= 10; ++i) {
         htimer_t* timer = htimer_add(loop, on_timer, i*1000, 3);
-        hevent_set_userdata(timer, (void*)(long)i);
+        hevent_set_userdata(timer, (void*)(intptr_t)i);
     }
 
     // test timer period

+ 2 - 2
examples/hmain_test.cpp

@@ -276,9 +276,9 @@ int main(int argc, char** argv) {
 }
 
 void worker_fn(void* userdata) {
-    int num = (int)(intptr_t)(userdata);
+    long num = (long)(intptr_t)(userdata);
     while (1) {
-        printf("num=%d pid=%d tid=%d\n", num, getpid(), gettid());
+        printf("num=%ld pid=%d tid=%ld\n", num, getpid(), (long)gettid());
         sleep(60);
     }
 }

+ 8 - 8
examples/htimer_test.c

@@ -2,33 +2,33 @@
 #include "hbase.h"
 
 void on_timer(htimer_t* timer) {
-    printf("time=%llus on_timer\n", hloop_now(hevent_loop(timer)));
+    printf("time=%llus on_timer\n", LLU(hloop_now(hevent_loop(timer))));
 }
 
 void on_timer_add(htimer_t* timer) {
-    printf("time=%llus on_timer_add\n", hloop_now(hevent_loop(timer)));
+    printf("time=%llus on_timer_add\n", LLU(hloop_now(hevent_loop(timer))));
     htimer_add(hevent_loop(timer), on_timer_add, 1000, 1);
 }
 
 void on_timer_del(htimer_t* timer) {
-    printf("time=%llus on_timer_del\n", hloop_now(hevent_loop(timer)));
+    printf("time=%llus on_timer_del\n", LLU(hloop_now(hevent_loop(timer))));
     htimer_del(timer);
 }
 
 void on_timer_reset(htimer_t* timer) {
-    printf("time=%llus on_timer_reset\n", hloop_now(hevent_loop(timer)));
+    printf("time=%llus on_timer_reset\n", LLU(hloop_now(hevent_loop(timer))));
     htimer_reset((htimer_t*)hevent_userdata(timer));
 }
 
 void on_timer_quit(htimer_t* timer) {
-    printf("time=%llus on_timer_quit\n", hloop_now(hevent_loop(timer)));
+    printf("time=%llus on_timer_quit\n", LLU(hloop_now(hevent_loop(timer))));
     hloop_stop(hevent_loop(timer));
 }
 
 void cron_hourly(htimer_t* timer) {
     time_t tt;
     time(&tt);
-    printf("time=%llus cron_hourly: %s\n", hloop_now(hevent_loop(timer)), ctime(&tt));
+    printf("time=%llus cron_hourly: %s\n", LLU(hloop_now(hevent_loop(timer))), ctime(&tt));
 }
 
 int main() {
@@ -52,9 +52,9 @@ int main() {
     // quit application after 1 min
     htimer_add(loop, on_timer_quit, 60000, 1);
 
-    printf("time=%llus begin\n", hloop_now(loop));
+    printf("time=%llus begin\n", LLU(hloop_now(loop)));
     hloop_run(loop);
-    printf("time=%llus stop\n", hloop_now(loop));
+    printf("time=%llus stop\n", LLU(hloop_now(loop)));
     hloop_free(&loop);
     return 0;
 }

+ 1 - 1
http/server/FileCache.cpp

@@ -68,7 +68,7 @@ file_cache_t* FileCache::Open(const char* filepath, void* ctx) {
             fc->content_type = http_content_type_str(TEXT_HTML);
         }
         gmtime_fmt(fc->st.st_mtime, fc->last_modified);
-        snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, fc->st.st_mtime, fc->st.st_size);
+        snprintf(fc->etag, sizeof(fc->etag), ETAG_FMT, (size_t)fc->st.st_mtime, (size_t)fc->st.st_size);
     }
     return fc;
 }

+ 1 - 1
unittest/threadpool_test.cpp

@@ -4,7 +4,7 @@
 #include "htime.h"
 
 void print_task(int i) {
-    printf("thread[%x]: task[%d]\n", gettid(), i);
+    printf("thread[%ld]: task[%d]\n", (long)gettid(), i);
     sleep(1);
 }
 

+ 34 - 32
utils/hendian.h

@@ -44,53 +44,55 @@
 #include <endian.h>
 #endif
 
-#define I16(p)  *(int16_t*)(p)
-#define I32(p)  *(int32_t*)(p)
-#define I64(p)  *(int64_t*)(p)
+#define PI8(p)      *(int8_t*)(p)
+#define PI16(p)     *(int16_t*)(p)
+#define PI32(p)     *(int32_t*)(p)
+#define PI64(p)     *(int64_t*)(p)
 
-#define U16(p)  *(uint16_t*)(p)
-#define U32(p)  *(uint32_t*)(p)
-#define U64(p)  *(uint64_t*)(p)
+#define PU8(p)      *(uint8_t*)(p)
+#define PU16(p)     *(uint16_t*)(p)
+#define PU32(p)     *(uint32_t*)(p)
+#define PU64(p)     *(uint64_t*)(p)
 
-#define F32(p)  *(float*)(p)
-#define F64(p)  *(double*)(p)
+#define PF32(p)     *(float*)(p)
+#define PF64(p)     *(double*)(p)
 
-#define GET_BE16(p)     be16toh(U16(p))
-#define GET_BE32(p)     be32toh(U32(p))
-#define GET_BE64(p)     be64toh(U64(p))
+#define GET_BE16(p)     be16toh(PU16(p))
+#define GET_BE32(p)     be32toh(PU32(p))
+#define GET_BE64(p)     be64toh(PU64(p))
 
-#define GET_LE16(p)     le16toh(U16(p))
-#define GET_LE32(p)     le32toh(U32(p))
-#define GET_LE64(p)     le64toh(U64(p))
+#define GET_LE16(p)     le16toh(PU16(p))
+#define GET_LE32(p)     le32toh(PU32(p))
+#define GET_LE64(p)     le64toh(PU64(p))
 
-#define PUT_BE16(p, v)  U16(p) = htobe16(v)
-#define PUT_BE32(p, v)  U32(p) = htobe32(v)
-#define PUT_BE64(p, v)  U64(p) = htobe64(v)
+#define PUT_BE16(p, v)  PU16(p) = htobe16(v)
+#define PUT_BE32(p, v)  PU32(p) = htobe32(v)
+#define PUT_BE64(p, v)  PU64(p) = htobe64(v)
 
-#define PUT_LE16(p, v)  U16(p) = htole16(v)
-#define PUT_LE32(p, v)  U32(p) = htole32(v)
-#define PUT_LE64(p, v)  U64(p) = htole64(v)
+#define PUT_LE16(p, v)  PU16(p) = htole16(v)
+#define PUT_LE32(p, v)  PU32(p) = htole32(v)
+#define PUT_LE64(p, v)  PU64(p) = htole64(v)
 
 // NOTE: uint8_t* p = (uint8_t*)buf;
 #define POP_BE8(p, v)   v = *p; ++p
-#define POP_BE16(p, v)  v = be16toh(U16(p)); p += 2
-#define POP_BE32(p, v)  v = be32toh(U32(p)); p += 4
-#define POP_BE64(p, v)  v = be64toh(U64(p)); p += 8
+#define POP_BE16(p, v)  v = be16toh(PU16(p)); p += 2
+#define POP_BE32(p, v)  v = be32toh(PU32(p)); p += 4
+#define POP_BE64(p, v)  v = be64toh(PU64(p)); p += 8
 
 #define POP_LE8(p, v)   v= *p; ++p
-#define POP_LE16(p, v)  v = le16toh(U16(p)); p += 2
-#define POP_LE32(p, v)  v = le32toh(U32(p)); p += 4
-#define POP_LE64(p, v)  v = le64toh(U64(p)); p += 8
+#define POP_LE16(p, v)  v = le16toh(PU16(p)); p += 2
+#define POP_LE32(p, v)  v = le32toh(PU32(p)); p += 4
+#define POP_LE64(p, v)  v = le64toh(PU64(p)); p += 8
 
 #define PUSH_BE8(p, v)  *p = v; ++p
-#define PUSH_BE16(p, v) U16(p) = htobe16(v); p += 2
-#define PUSH_BE32(p, v) U32(p) = htobe32(v); p += 4
-#define PUSH_BE64(p, v) U64(p) = htobe64(v); p += 8
+#define PUSH_BE16(p, v) PU16(p) = htobe16(v); p += 2
+#define PUSH_BE32(p, v) PU32(p) = htobe32(v); p += 4
+#define PUSH_BE64(p, v) PU64(p) = htobe64(v); p += 8
 
 #define PUSH_LE8(p, v)  *p = v; ++p
-#define PUSH_LE16(p, v) U16(p) = htole16(v); p += 2
-#define PUSH_LE32(p, v) U32(p) = htole32(v); p += 4
-#define PUSH_LE64(p, v) U64(p) = htole64(v); p += 8
+#define PUSH_LE16(p, v) PU16(p) = htole16(v); p += 2
+#define PUSH_LE32(p, v) PU32(p) = htole32(v); p += 4
+#define PUSH_LE64(p, v) PU64(p) = htole64(v); p += 8
 
 static inline int detect_endian() {
     union {

+ 1 - 1
utils/hmain.cpp

@@ -522,7 +522,7 @@ void handle_signal(const char* signal) {
 
 // master-workers processes
 static HTHREAD_ROUTINE(worker_thread) {
-    hlogi("worker_thread pid=%d tid=%d", getpid(), gettid());
+    hlogi("worker_thread pid=%d tid=%ld", getpid(), (long)gettid());
     if (g_worker_fn) {
         g_worker_fn(g_worker_userdata);
     }