1
0
ithewei 6 жил өмнө
parent
commit
112117647a
6 өөрчлөгдсөн 78 нэмэгдсэн , 65 устгасан
  1. 0 2
      h.h
  2. 61 42
      hbuf.h
  3. 3 3
      hbytearray.h
  4. 7 6
      hfile.h
  5. 1 1
      hframe.cpp
  6. 6 11
      hframe.h

+ 0 - 2
h.h

@@ -5,7 +5,6 @@
  * @copyright 2018 HeWei, all rights reserved.
  */
 
-// platform
 #include "hplatform.h"
 #include "hdef.h"
 #include "hversion.h"
@@ -38,4 +37,3 @@
 #include "iniparser.h"
 
 #endif  // HW_H_
-

+ 61 - 42
hbuf.h

@@ -10,75 +10,89 @@
 typedef struct hbuf_s {
     uint8* base;
     size_t len;
-    bool   cleanup_;
 
+#ifdef __cplusplus
     hbuf_s() {
         base = NULL;
         len  = 0;
-        cleanup_ = false;
     }
 
-    hbuf_s(uint8* base, size_t len) {
-        this->base = base;
+    hbuf_s(void* data, size_t len) {
+        this->base = (uint8*)data;
         this->len  = len;
+    }
+#endif
+} hbuf_t;
+
+class HBuf : public hbuf_t {
+public:
+    HBuf() : hbuf_t() {
         cleanup_ = false;
     }
+    HBuf(void* data, size_t len) : hbuf_t(data, len) {
+        cleanup_ = false;
+    }
+    HBuf(size_t cap) { resize(cap); }
 
-    virtual ~hbuf_s() {
+    virtual ~HBuf() {
         cleanup();
     }
 
-    // warn: must call cleanup when no longer in use, otherwise memory leak.
-    void init(size_t cap) {
-        if (cap == len) return;
-
-        if (!base) {
-            base = (uint8*)malloc(cap);
-            memset(base, 0, cap);
-        } else {
-            base = (uint8*)realloc(base, cap);
-        }
-        len = cap;
-        cleanup_ = true;
-    }
+    void*  data() { return base; }
+    size_t size() { return len; }
 
-    void resize(size_t cap) {
-        init(cap);
+    bool isNull() {
+        return base == NULL || len == 0;
     }
 
     void cleanup() {
         if (cleanup_) {
-            SAFE_FREE(base);
+            if (base) {
+                free(base);
+                base = NULL;
+            }
             len = 0;
             cleanup_ = false;
         }
     }
 
-    bool isNull() {
-        return base == NULL || len == 0;
+    void resize(size_t cap) {
+        if (cap == len) return;
+
+        if (base == NULL) {
+            base = (uint8*)malloc(cap);
+            memset(base, 0, cap);
+        }
+        else {
+            base = (uint8*)realloc(base, cap);
+        }
+        len = cap;
+        cleanup_ = true;
     }
-} hbuf_t;
 
-class HBuf : public hbuf_t {
- public:
-    HBuf() : hbuf_t() {}
-    HBuf(size_t cap) {init(cap);}
-    HBuf(void* data, size_t len) {
-        init(len);
+    void copy(void* data, size_t len) {
+        resize(len);
         memcpy(base, data, len);
     }
+
+    void copy(hbuf_t* buf) {
+        copy(buf->base, buf->len);
+    }
+
+private:
+    bool cleanup_;
 };
 
 // VL: Variable-Length
 class HVLBuf : public HBuf {
- public:
+public:
     HVLBuf() : HBuf() {_offset = _size = 0;}
-    HVLBuf(size_t cap) : HBuf(cap) {_offset = _size = 0;}
     HVLBuf(void* data, size_t len) : HBuf(data, len) {_offset = 0; _size = len;}
+    HVLBuf(size_t cap) : HBuf(cap) {_offset = _size = 0;}
     virtual ~HVLBuf() {}
 
-    uint8* data() {return base+_offset;}
-    size_t size() {return _size;}
+    uint8* data() { return base+_offset; }
+    size_t size() { return _size; }
 
     void push_front(void* ptr, size_t len) {
         if (len > this->len - _size) {
@@ -101,7 +115,8 @@ class HVLBuf : public HBuf {
         if (len > this->len - _size) {
             this->len = MAX(this->len, len)*2;
             base = (uint8*)realloc(base, this->len);
-        } else if (len > this->len - _offset - _size) {
+        }
+        else if (len > this->len - _offset - _size) {
             // move => start
             memmove(base, data(), _size);
             _offset = 0;
@@ -150,15 +165,16 @@ class HVLBuf : public HBuf {
         pop_front(NULL, len);
     }
 
- private:
+private:
     size_t _offset;
     size_t _size;
 };
 
 class HRingBuf : public HBuf {
- public:
+public:
     HRingBuf() : HBuf() {_head = _tail = _size = 0;}
     HRingBuf(size_t cap) : HBuf(cap) {_head = _tail = _size = 0;}
+    virtual ~HRingBuf() {}
 
     uint8* alloc(size_t len) {
         uint8* ret = NULL;
@@ -168,11 +184,13 @@ class HRingBuf : public HBuf {
                 ret = base + _tail;
                 _tail += len;
                 if (_tail == this->len) _tail = 0;
-            } else if (_head >= len) {
+            }
+            else if (_head >= len) {
                 ret = base;
                 _tail = len;
             }
-        } else {
+        }
+        else {
             // [_tail, _head)
             if (_head - _tail >= len) {
                 ret = base + _tail;
@@ -183,19 +201,20 @@ class HRingBuf : public HBuf {
         return ret;
     }
 
-    void   free(size_t len) {
+    void free(size_t len) {
         _size -= len;
         if (len <= this->len - _head) {
             _head += len;
             if (_head == this->len) _head = 0;
-        } else {
+        }
+        else {
             _head = len;
         }
     }
 
     size_t size() {return _size;}
 
- private:
+private:
     size_t _head;
     size_t _tail;
     size_t _size;

+ 3 - 3
hbytearray.h

@@ -5,20 +5,20 @@
 #include "base64.h"
 
 class HByteArray : public HVLBuf {
- public:
+public:
     HByteArray() : HVLBuf() {}
     HByteArray(int cap) : HVLBuf(cap) {}
     HByteArray(void* data, int len) : HVLBuf(data, len) {}
 
     bool encodeBase64(void* ptr, int len) {
         int base64_len = BASE64_ENCODE_OUT_SIZE(len) + 1;   // +1 for '\0'
-        init(base64_len);
+        resize(base64_len);
         return base64_encode((unsigned char*)ptr, len, (char*)data()) == BASE64_OK;
     }
 
     bool decodeBase64(const char* base64) {
         int out_len = BASE64_DECODE_OUT_SIZE(strlen(base64));
-        init(out_len);
+        resize(out_len);
         return base64_decode(base64, strlen(base64), data()) == BASE64_OK;
     }
 };

+ 7 - 6
hfile.h

@@ -7,10 +7,11 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <string>
+
 #include "hdef.h"
 #include "hbuf.h"
 #include "herr.h"
-#include "hstring.h"
 
 class HFile {
  public:
@@ -49,19 +50,19 @@ class HFile {
         return fread(ptr, 1, len, _fp);
     }
 
-    size_t readall(hbuf_t& buf) {
+    size_t readall(HBuf& buf) {
         size_t filesize = size();
-        buf.init(filesize);
-        return fread(buf.base, 1, buf.len, _fp);
+        buf.resize(filesize);
+        return fread(buf.base, 1, filesize, _fp);
     }
 
-    size_t readall(string& str) {
+    size_t readall(std::string& str) {
         size_t filesize = size();
         str.resize(filesize);
         return fread((void*)str.data(), 1, filesize, _fp);
     }
 
-    bool readline(string& str) {
+    bool readline(std::string& str) {
         str.clear();
         char ch;
         while (fread(&ch, 1, 1, _fp)) {

+ 1 - 1
hframe.cpp

@@ -28,7 +28,7 @@ int HFrameBuf::push(HFrame* pFrame) {
 
     int ret = 0;
     if (isNull()) {
-        init(pFrame->buf.len * cache_num);
+        resize(pFrame->buf.len * cache_num);
         ret = 1;    // note: first push
 
         frame_info.w = pFrame->w;

+ 6 - 11
hframe.h

@@ -2,13 +2,13 @@
 #define HW_FRAME_H_
 
 #include <deque>
+#include <mutex>
 
 #include "hbuf.h"
-#include "hmutex.h"
 
 class HFrame {
 public:
-    hbuf_t buf;
+    HBuf buf;
     int w;
     int h;
     int bpp;
@@ -16,13 +16,15 @@ public:
     uint64 ts;
     int64 useridx;
     void* userdata;
+
     HFrame() {
         w = h = bpp = type = ts = 0;
         useridx = -1;
         userdata = NULL;
     }
 
-    ~HFrame() {
+    bool isNull() {
+        return w == 0 || h == 0 || buf.isNull();
     }
 
     void copy(const HFrame& rhs) {
@@ -33,14 +35,7 @@ public:
         ts = rhs.ts;
         useridx = rhs.useridx;
         userdata = rhs.userdata;
-        if (buf.isNull() || buf.len != rhs.buf.len) {
-            buf.init(rhs.buf.len);
-        }
-        memcpy(buf.base, rhs.buf.base, rhs.buf.len);
-    }
-
-    bool isNull() {
-        return w == 0 || h == 0 || buf.isNull();
+        buf.copy(rhs.buf.base, rhs.buf.len);
     }
 };