]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
stream: implement memory handling functions
authorVictor Julien <victor@inliniac.net>
Fri, 6 May 2016 15:12:42 +0000 (17:12 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 20 Apr 2017 15:41:11 +0000 (17:41 +0200)
src/stream-tcp-reassemble.c

index e56ebacd314e1b7c38da711ccb618f0bb83dcbdb..9ebe9b914be42848a3d080390d25514830db27b1 100644 (file)
@@ -157,6 +157,66 @@ int StreamTcpReassembleCheckMemcap(uint32_t size)
     return 0;
 }
 
+/* memory functions for the streaming buffer API */
+
+/*
+    void *(*Malloc)(size_t size);
+*/
+static void *ReassembleMalloc(size_t size)
+{
+    if (StreamTcpReassembleCheckMemcap(size) == 0)
+        return NULL;
+    void *ptr = SCMalloc(size);
+    if (ptr == NULL)
+        return NULL;
+    StreamTcpReassembleIncrMemuse(size);
+    return ptr;
+}
+
+/*
+    void *(*Calloc)(size_t n, size_t size);
+*/
+static void *ReassembleCalloc(size_t n, size_t size)
+{
+    if (StreamTcpReassembleCheckMemcap(n * size) == 0)
+        return NULL;
+    void *ptr = SCCalloc(n, size);
+    if (ptr == NULL)
+        return NULL;
+    StreamTcpReassembleIncrMemuse(n * size);
+    return ptr;
+}
+
+/*
+    void *(*Realloc)(void *ptr, size_t orig_size, size_t size);
+*/
+static void *ReassembleRealloc(void *optr, size_t orig_size, size_t size)
+{
+    if (size > orig_size) {
+        if (StreamTcpReassembleCheckMemcap(size - orig_size) == 0)
+            return NULL;
+    }
+    void *nptr = SCRealloc(optr, size);
+    if (nptr == NULL)
+        return NULL;
+
+    if (size > orig_size) {
+        StreamTcpReassembleIncrMemuse(size - orig_size);
+    } else {
+        StreamTcpReassembleDecrMemuse(orig_size - size);
+    }
+    return nptr;
+}
+
+/*
+    void (*Free)(void *ptr, size_t size);
+*/
+static void ReassembleFree(void *ptr, size_t size)
+{
+    SCFree(ptr);
+    StreamTcpReassembleDecrMemuse(size);
+}
+
 /** \brief alloc a tcp segment pool entry */
 void *TcpSegmentPoolAlloc()
 {
@@ -521,6 +581,10 @@ int StreamTcpReassemblyConfig(char quiet)
 
     stream_config.sbcnf.flags = STREAMING_BUFFER_NOFLAGS;
     stream_config.sbcnf.buf_size = 2048;
+    stream_config.sbcnf.Malloc = ReassembleMalloc;
+    stream_config.sbcnf.Calloc = ReassembleCalloc;
+    stream_config.sbcnf.Realloc = ReassembleRealloc;
+    stream_config.sbcnf.Free = ReassembleFree;
 
     return 0;
 }