Explorar o código

rm global var

hewei.it %!s(int64=5) %!d(string=hai) anos
pai
achega
861f0df1f1
Modificáronse 8 ficheiros con 37 adicións e 26 borrados
  1. 1 1
      Makefile
  2. 16 8
      base/hbase.c
  3. 3 5
      base/hbase.h
  4. 10 6
      base/ssl_ctx.c
  5. 1 2
      base/ssl_ctx.h
  6. 2 2
      event/nio.c
  7. 2 0
      examples/hloop_test.c
  8. 2 2
      http/client/http_client.cpp

+ 1 - 1
Makefile

@@ -32,7 +32,7 @@ endif
 
 default: all
 all: libhv examples
-examples: test timer loop tcp udp nc nmap httpd curl consul_cli
+examples: hmain_test htimer_test hloop_test tcp udp nc nmap httpd curl consul_cli
 
 clean:
 	$(MAKEF) clean SRCDIRS="$(ALL_SRCDIRS)"

+ 16 - 8
base/hbase.c

@@ -4,11 +4,19 @@
 #include <mach-o/dyld.h> // for _NSGetExecutablePath
 #endif
 
-unsigned int g_alloc_cnt = 0;
-unsigned int g_free_cnt = 0;
+static unsigned int s_alloc_cnt = 0;
+static unsigned int s_free_cnt = 0;
+
+unsigned int hv_alloc_cnt() {
+    return s_alloc_cnt;
+}
+
+unsigned int hv_free_cnt() {
+    return s_free_cnt;
+}
 
 void* safe_malloc(size_t size) {
-    ++g_alloc_cnt;
+    ++s_alloc_cnt;
     void* ptr = malloc(size);
     if (!ptr) {
         fprintf(stderr, "malloc failed!\n");
@@ -18,8 +26,8 @@ void* safe_malloc(size_t size) {
 }
 
 void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize) {
-    ++g_alloc_cnt;
-    ++g_free_cnt;
+    ++s_alloc_cnt;
+    ++s_free_cnt;
     void* ptr = realloc(oldptr, newsize);
     if (!ptr) {
         fprintf(stderr, "realloc failed!\n");
@@ -32,7 +40,7 @@ void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize) {
 }
 
 void* safe_calloc(size_t nmemb, size_t size) {
-    ++g_alloc_cnt;
+    ++s_alloc_cnt;
     void* ptr =  calloc(nmemb, size);
     if (!ptr) {
         fprintf(stderr, "calloc failed!\n");
@@ -42,7 +50,7 @@ void* safe_calloc(size_t nmemb, size_t size) {
 }
 
 void* safe_zalloc(size_t size) {
-    ++g_alloc_cnt;
+    ++s_alloc_cnt;
     void* ptr = malloc(size);
     if (!ptr) {
         fprintf(stderr, "malloc failed!\n");
@@ -56,7 +64,7 @@ void safe_free(void* ptr) {
     if (ptr) {
         free(ptr);
         ptr = NULL;
-        ++g_free_cnt;
+        ++s_free_cnt;
     }
 }
 

+ 3 - 5
base/hbase.h

@@ -8,9 +8,6 @@
 BEGIN_EXTERN_C
 
 //--------------------safe alloc/free---------------------------
-extern unsigned int g_alloc_cnt;
-extern unsigned int g_free_cnt;
-
 HV_EXPORT void* safe_malloc(size_t size);
 HV_EXPORT void* safe_realloc(void* oldptr, size_t newsize, size_t oldsize);
 HV_EXPORT void* safe_calloc(size_t nmemb, size_t size);
@@ -31,10 +28,11 @@ HV_EXPORT void  safe_free(void* ptr);
         printd("free( %p )\tat [%s:%d:%s]\n", ptr, __FILE__, __LINE__, __FUNCTION__);\
     } while(0)
 
+HV_EXPORT unsigned int hv_alloc_cnt();
+HV_EXPORT unsigned int hv_free_cnt();
 static inline void hv_memcheck() {
-    printf("Memcheck => alloc:%u free:%u\n", g_alloc_cnt, g_free_cnt);
+    printf("Memcheck => alloc:%u free:%u\n", hv_alloc_cnt(), hv_free_cnt());
 }
-
 #define HV_MEMCHECK    atexit(hv_memcheck);
 
 //--------------------safe string-------------------------------

+ 10 - 6
base/ssl_ctx.c

@@ -6,11 +6,11 @@
 #include "openssl/ssl.h"
 #endif
 
-void* g_ssl_ctx = 0;
+static void* s_ssl_ctx = 0;
 
 int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file) {
 #ifdef WITH_OPENSSL
-    if (g_ssl_ctx != NULL) {
+    if (s_ssl_ctx != NULL) {
         return 0;
     }
     SSL_CTX* ctx = SSL_CTX_new(TLS_method());
@@ -38,7 +38,7 @@ int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file
         }
     }
     SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
-    g_ssl_ctx = ctx;
+    s_ssl_ctx = ctx;
     return 0;
 #else
     fprintf(stderr, "Please recompile WITH_OPENSSL.\n");
@@ -48,10 +48,14 @@ int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file
 
 int ssl_ctx_destory() {
 #ifdef WITH_OPENSSL
-    if (g_ssl_ctx) {
-        SSL_CTX_free((SSL_CTX*)g_ssl_ctx);
-        g_ssl_ctx = NULL;
+    if (s_ssl_ctx) {
+        SSL_CTX_free((SSL_CTX*)s_ssl_ctx);
+        s_ssl_ctx = NULL;
     }
 #endif
     return 0;
 }
+
+void* ssl_ctx_instance() {
+    return s_ssl_ctx;
+}

+ 1 - 2
base/ssl_ctx.h

@@ -5,8 +5,7 @@
 
 BEGIN_EXTERN_C
 
-extern void* g_ssl_ctx; // for SSL_CTX
-
+HV_EXPORT void* ssl_ctx_instance();
 HV_EXPORT int ssl_ctx_init(const char* crt_file, const char* key_file, const char* ca_file);
 HV_EXPORT int ssl_ctx_destory();
 

+ 2 - 2
event/nio.c

@@ -67,7 +67,7 @@ accept:
 
 #ifdef WITH_OPENSSL
     if (io->io_type == HIO_TYPE_SSL) {
-        SSL_CTX* ssl_ctx = (SSL_CTX*)g_ssl_ctx;
+        SSL_CTX* ssl_ctx = (SSL_CTX*)ssl_ctx_instance();
         if (ssl_ctx == NULL) {
             goto accept_error;
         }
@@ -124,7 +124,7 @@ static void nio_connect(hio_t* io) {
         */
 #ifdef WITH_OPENSSL
         if (io->io_type == HIO_TYPE_SSL) {
-            SSL_CTX* ssl_ctx = (SSL_CTX*)g_ssl_ctx;
+            SSL_CTX* ssl_ctx = (SSL_CTX*)ssl_ctx_instance();
             if (ssl_ctx == NULL) {
                 goto connect_failed;
             }

+ 2 - 0
examples/hloop_test.c

@@ -78,7 +78,9 @@ int main() {
     htimer_add(loop, timer_write_log, 1000, INFINITE);
     logger_set_handler(hlog, mylogger);
     hlog_set_file("loop.log");
+#ifndef _MSC_VER
     logger_enable_color(hlog, 1);
+#endif
     nlog_listen(loop, DEFAULT_LOG_PORT);
 
     // test nonblock stdin

+ 2 - 2
http/client/http_client.cpp

@@ -320,10 +320,10 @@ static int __http_client_connect(http_client_t* cli) {
 
     if (cli->tls) {
 #ifdef WITH_OPENSSL
-        if (g_ssl_ctx == NULL) {
+        if (ssl_ctx_instance() == NULL) {
             ssl_ctx_init(NULL, NULL, NULL);
         }
-        cli->ssl = SSL_new((SSL_CTX*)g_ssl_ctx);
+        cli->ssl = SSL_new((SSL_CTX*)ssl_ctx_instance());
         SSL_set_fd(cli->ssl, connfd);
         if (SSL_connect(cli->ssl) != 1) {
             int err = SSL_get_error(cli->ssl, -1);