From 94571c5dd28858ff68c44b648fd41c5d87c0e28d Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 10 Jul 2014 10:51:32 +0200 Subject: [PATCH] AC: shrink output table after initialization --- src/util-mpm-ac.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/util-mpm-ac.c b/src/util-mpm-ac.c index fffebf0f45..6271f2fb70 100644 --- a/src/util-mpm-ac.c +++ b/src/util-mpm-ac.c @@ -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