浏览代码

Add HV_STACK_ALLOC, HV_STACK_FREE

ithewei 3 年之前
父节点
当前提交
52651ec0a2
共有 4 个文件被更改,包括 25 次插入12 次删除
  1. 17 0
      base/hbase.h
  2. 2 2
      examples/protorpc/protorpc_client.cpp
  3. 2 2
      examples/protorpc/protorpc_server.cpp
  4. 4 8
      examples/tinyhttpd.c

+ 17 - 0
base/hbase.h

@@ -31,6 +31,23 @@ HV_EXPORT void  safe_free(void* ptr);
         }\
     } while(0)
 
+#define STACK_OR_HEAP_ALLOC(ptr, size, stack_size)\
+    unsigned char _stackbuf_[stack_size] = { 0 };\
+    if ((size) > (stack_size)) {\
+        HV_ALLOC(ptr, size);\
+    } else {\
+        *(unsigned char**)&(ptr) = _stackbuf_;\
+    }
+
+#define STACK_OR_HEAP_FREE(ptr)\
+    if ((unsigned char*)(ptr) != _stackbuf_) {\
+        HV_FREE(ptr);\
+    }
+
+#define HV_DEFAULT_STACKBUF_SIZE    1024
+#define HV_STACK_ALLOC(ptr, size)   STACK_OR_HEAP_ALLOC(ptr, size, HV_DEFAULT_STACKBUF_SIZE)
+#define HV_STACK_FREE(ptr)          STACK_OR_HEAP_FREE(ptr)
+
 HV_EXPORT long hv_alloc_cnt();
 HV_EXPORT long hv_free_cnt();
 HV_INLINE void hv_memcheck() {

+ 2 - 2
examples/protorpc/protorpc_client.cpp

@@ -153,14 +153,14 @@ public:
         msg.head.length = req->ByteSize();
         int packlen = protorpc_package_length(&msg.head);
         unsigned char* writebuf = NULL;
-        HV_ALLOC(writebuf, packlen);
+        HV_STACK_ALLOC(writebuf, packlen);
         packlen = protorpc_pack(&msg, writebuf, packlen);
         if (packlen > 0) {
             printf("%s\n", req->DebugString().c_str());
             req->SerializeToArray(writebuf + PROTORPC_HEAD_LENGTH, msg.head.length);
             channel->write(writebuf, packlen);
         }
-        HV_FREE(writebuf);
+        HV_STACK_FREE(writebuf);
         // wait until response come or timeout
         ctx->wait(timeout_ms);
         auto res = ctx->res;

+ 2 - 2
examples/protorpc/protorpc_server.cpp

@@ -109,14 +109,14 @@ private:
         msg.head.length = res.ByteSize();
         packlen = protorpc_package_length(&msg.head);
         unsigned char* writebuf = NULL;
-        HV_ALLOC(writebuf, packlen);
+        HV_STACK_ALLOC(writebuf, packlen);
         packlen = protorpc_pack(&msg, writebuf, packlen);
         if (packlen > 0) {
             printf("< %s\n", res.DebugString().c_str());
             res.SerializeToArray(writebuf + PROTORPC_HEAD_LENGTH, msg.head.length);
             channel->write(writebuf, packlen);
         }
-        HV_FREE(writebuf);
+        HV_STACK_FREE(writebuf);
     }
 };
 

+ 4 - 8
examples/tinyhttpd.c

@@ -131,9 +131,6 @@ static int http_reply(http_conn_t* conn,
             int status_code, const char* status_message,
             const char* content_type,
             const char* body, int body_len) {
-    char stackbuf[HTTP_MAX_HEAD_LENGTH + 1024] = {0};
-    char* buf = stackbuf;
-    int buflen = sizeof(stackbuf);
     http_msg_t* req  = &conn->request;
     http_msg_t* resp = &conn->response;
     resp->major_version = req->major_version;
@@ -147,12 +144,11 @@ static int http_reply(http_conn_t* conn,
         resp->content_length = body_len;
         resp->body = (char*)body;
     }
-    if (resp->content_length > buflen - HTTP_MAX_HEAD_LENGTH) {
-        HV_ALLOC(buf, HTTP_MAX_HEAD_LENGTH + resp->content_length);
-    }
-    int msglen = http_response_dump(resp, buf, buflen);
+    char* buf = NULL;
+    STACK_OR_HEAP_ALLOC(buf, HTTP_MAX_HEAD_LENGTH + resp->content_length, HTTP_MAX_HEAD_LENGTH + 1024);
+    int msglen = http_response_dump(resp, buf, HTTP_MAX_HEAD_LENGTH + resp->content_length);
     int nwrite = hio_write(conn->io, buf, msglen);
-    if (buf != stackbuf) HV_FREE(buf);
+    STACK_OR_HEAP_FREE(buf);
     return nwrite < 0 ? nwrite : msglen;
 }