]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
AC: shrink output table after initialization 1049/head
authorVictor Julien <victor@inliniac.net>
Thu, 10 Jul 2014 08:51:32 +0000 (10:51 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 18 Jul 2014 17:38:29 +0000 (19:38 +0200)
src/util-mpm-ac.c

index fffebf0f452486a570c22db36ac904b003e39a46..6271f2fb706990701005d7e4582ec068754f8f79 100644 (file)
@@ -417,6 +417,34 @@ static inline int SCACReallocState(SCACCtx *ctx, uint32_t cnt)
     return 0;//ctx->state_count++;
 }
 
+/** \internal
+ *  \brief Shrink state after setup is done
+ *
+ *  Shrinks only the output table, goto table is freed after calling this
+ */
+static void SCACShrinkState(SCACCtx *ctx)
+{
+    /* reallocate space in the output table for the new state */
+#ifdef DEBUG
+    int oldsize = ctx->allocated_state_count * sizeof(SCACOutputTable);
+#endif
+    int newsize = ctx->state_count * sizeof(SCACOutputTable);
+
+    SCLogDebug("oldsize %d newsize %d ctx->allocated_state_count %u "
+               "ctx->state_count %u: shrink by %d bytes", oldsize,
+               newsize, ctx->allocated_state_count, ctx->state_count,
+               oldsize - newsize);
+
+    void *ptmp = SCRealloc(ctx->output_table, newsize);
+    if (ptmp == NULL) {
+        SCFree(ctx->output_table);
+        ctx->output_table = NULL;
+        SCLogError(SC_ERR_MEM_ALLOC, "Error allocating memory");
+        exit(EXIT_FAILURE);
+    }
+    ctx->output_table = ptmp;
+}
+
 static inline int SCACInitNewState(MpmCtx *mpm_ctx)
 {
     SCACCtx *ctx = (SCACCtx *)mpm_ctx->ctx;;
@@ -951,6 +979,9 @@ static inline void SCACPrepareStateTable(MpmCtx *mpm_ctx)
     /* club nocase entries */
     SCACInsertCaseSensitiveEntriesForPatterns(mpm_ctx);
 
+    /* shrink the memory */
+    SCACShrinkState(ctx);
+
 #if 0
     SCACPrintDeltaTable(mpm_ctx);
 #endif