a faster and more compact allocator

git-svn-id: file:///var/lib/svn/clamav-devel/branches/mpool@4286 77e5149b-7576-45b1-b177-96237e5ba77b
0.95
aCaB 17 years ago
parent eb8ab9d28a
commit e21657dfc9
  1. 12
      libclamav/dconf.c
  2. 4
      libclamav/dconf.h
  3. 13
      libclamav/filetypes.c
  4. 248
      libclamav/matcher-ac.c
  5. 63
      libclamav/matcher-bm.c
  6. 6
      libclamav/matcher.h
  7. 86
      libclamav/mpool-2.1.0/ChangeLog.1
  8. 56
      libclamav/mpool-2.1.0/Makefile
  9. 11
      libclamav/mpool-2.1.0/NEWS
  10. 46
      libclamav/mpool-2.1.0/README
  11. 1746
      libclamav/mpool-2.1.0/mpool.c
  12. 466
      libclamav/mpool-2.1.0/mpool.h
  13. 116
      libclamav/mpool-2.1.0/mpool_loc.h
  14. 914
      libclamav/mpool-2.1.0/mpool_t.c
  15. 253
      libclamav/mpool.c
  16. 62
      libclamav/mpool.h
  17. 12
      libclamav/phish_whitelist.c
  18. 20
      libclamav/phishcheck.c
  19. 447
      libclamav/readdb.c
  20. 55
      libclamav/regex_list.c
  21. 4
      libclamav/regex_list.h

@ -35,9 +35,7 @@
#include "str.h"
#include "others.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
struct dconf_module {
const char *mname; /* module name */
@ -112,8 +110,8 @@ static struct dconf_module modules[] = {
{ NULL, NULL, 0, 0 }
};
#ifdef USE_MPOOL /* MPOOLFIXME : worthless? */
struct cli_dconf *cli_dconf_init(mpool_t *mempool)
#ifdef USE_MPOOL
struct cli_dconf *cli_dconf_init(mp_t *mempool)
#else
struct cli_dconf *cli_dconf_init(void)
#endif
@ -121,11 +119,7 @@ struct cli_dconf *cli_dconf_init(void)
unsigned int i;
struct cli_dconf *dconf;
#ifdef USE_MPOOL
dconf = (struct cli_dconf *) mpool_calloc(mempool, sizeof(struct cli_dconf), 1, NULL);
#else
dconf = (struct cli_dconf *) cli_calloc(sizeof(struct cli_dconf), 1);
#endif
dconf = (struct cli_dconf *) mp_calloc(mempool, sizeof(struct cli_dconf), 1);
if(!dconf)
return NULL;

@ -28,9 +28,7 @@
#include "cltypes.h"
#include "cvd.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
struct cli_dconf {
uint32_t pe;
@ -101,7 +99,7 @@ struct cli_dconf {
#define PHISHING_CONF_ENTCONV 0x2
#ifdef USE_MPOOL
struct cli_dconf *cli_dconf_init(mpool_t *);
struct cli_dconf *cli_dconf_init(mp_t *);
#else
struct cli_dconf *cli_dconf_init(void);
#endif

@ -40,6 +40,7 @@
#include "htmlnorm.h"
#include "entconv.h"
#include "mpool.h"
static const struct ftmap_s {
const char *name;
@ -107,15 +108,9 @@ void cli_ftfree(const struct cl_engine *engine)
while(ftypes) {
pt = ftypes;
ftypes = ftypes->next;
#ifdef USE_MPOOL
mpool_free(engine->mempool, pt->magic);
mpool_free(engine->mempool, pt->tname);
mpool_free(engine->mempool, pt);
#else
free(pt->magic);
free(pt->tname);
free(pt);
#endif
mp_free(engine->mempool, pt->magic);
mp_free(engine->mempool, pt->tname);
mp_free(engine->mempool, pt);
}
}

@ -39,9 +39,7 @@
#include "str.h"
#include "readdb.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
uint8_t cli_ac_mindepth = AC_DEFAULT_MIN_DEPTH;
uint8_t cli_ac_maxdepth = AC_DEFAULT_MAX_DEPTH;
@ -70,11 +68,7 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
for(i = 0; i < len; i++) {
if(!pt->trans) {
#ifdef USE_MPOOL
pt->trans = (struct cli_ac_node **) mpool_calloc(root->mempool, 256, sizeof(struct cli_ac_node *), NULL);
#else
pt->trans = (struct cli_ac_node **) cli_calloc(256, sizeof(struct cli_ac_node *));
#endif
pt->trans = (struct cli_ac_node **) mp_calloc(root->mempool, 256, sizeof(struct cli_ac_node *));
if(!pt->trans) {
cli_errmsg("cli_ac_addpatt: Can't allocate memory for pt->trans\n");
return CL_EMEM;
@ -84,29 +78,17 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
next = pt->trans[(unsigned char) (pattern->pattern[i] & 0xff)];
if(!next) {
#ifdef USE_MPOOL
next = (struct cli_ac_node *) mpool_calloc(root->mempool, 1, sizeof(struct cli_ac_node), NULL);
#else
next = (struct cli_ac_node *) cli_calloc(1, sizeof(struct cli_ac_node));
#endif
next = (struct cli_ac_node *) mp_calloc(root->mempool, 1, sizeof(struct cli_ac_node));
if(!next) {
cli_errmsg("cli_ac_addpatt: Can't allocate memory for AC node\n");
return CL_EMEM;
}
if(i != len - 1) {
#ifdef USE_MPOOL
next->trans = (struct cli_ac_node **) mpool_calloc(root->mempool, 256, sizeof(struct cli_ac_node *), NULL);
#else
next->trans = (struct cli_ac_node **) cli_calloc(256, sizeof(struct cli_ac_node *));
#endif
next->trans = (struct cli_ac_node **) mp_calloc(root->mempool, 256, sizeof(struct cli_ac_node *));
if(!next->trans) {
cli_errmsg("cli_ac_addpatt: Can't allocate memory for next->trans\n");
#ifdef USE_MPOOL
mpool_free(root->mempool, next);
#else
free(next);
#endif
mp_free(root->mempool, next);
return CL_EMEM;
}
} else {
@ -114,22 +96,13 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
}
root->ac_nodes++;
#ifdef USE_MPOOL
newtable = mpool_resize(root->mempool, root->ac_nodetable, root->ac_nodes * sizeof(struct cli_ac_node *), NULL);
#else
newtable = cli_realloc(root->ac_nodetable, root->ac_nodes * sizeof(struct cli_ac_node *));
#endif
newtable = mp_realloc(root->mempool, root->ac_nodetable, root->ac_nodes * sizeof(struct cli_ac_node *));
if(!newtable) {
root->ac_nodes--;
cli_errmsg("cli_ac_addpatt: Can't realloc ac_nodetable\n");
if(next->trans)
#ifdef USE_MPOOL
mpool_free(root->mempool, next->trans);
mpool_free(root->mempool, next);
#else
free(next->trans);
free(next);
#endif
mp_free(root->mempool, next->trans);
mp_free(root->mempool, next);
return CL_EMEM;
}
root->ac_nodetable = (struct cli_ac_node **) newtable;
@ -143,11 +116,7 @@ int cli_ac_addpatt(struct cli_matcher *root, struct cli_ac_patt *pattern)
}
root->ac_patterns++;
#ifdef USE_MPOOL
newtable = mpool_resize(root->mempool, root->ac_pattable, root->ac_patterns * sizeof(struct cli_ac_patt *), NULL);
#else
newtable = cli_realloc(root->ac_pattable, root->ac_patterns * sizeof(struct cli_ac_patt *));
#endif
newtable = mp_realloc(root->mempool, root->ac_pattable, root->ac_patterns * sizeof(struct cli_ac_patt *));
if(!newtable) {
root->ac_patterns--;
cli_errmsg("cli_ac_addpatt: Can't realloc ac_pattable\n");
@ -332,28 +301,16 @@ int cli_ac_buildtrie(struct cli_matcher *root)
int cli_ac_init(struct cli_matcher *root, uint8_t mindepth, uint8_t maxdepth)
{
#ifdef USE_MPOOL
root->ac_root = (struct cli_ac_node *) mpool_calloc(root->mempool, 1, sizeof(struct cli_ac_node), NULL);
#else
root->ac_root = (struct cli_ac_node *) cli_calloc(1, sizeof(struct cli_ac_node));
#endif
root->ac_root = (struct cli_ac_node *) mp_calloc(root->mempool, 1, sizeof(struct cli_ac_node));
if(!root->ac_root) {
cli_errmsg("cli_ac_init: Can't allocate memory for ac_root\n");
return CL_EMEM;
}
#ifdef USE_MPOOL
root->ac_root->trans = (struct cli_ac_node **) mpool_calloc(root->mempool, 256, sizeof(struct cli_ac_node *), NULL);
#else
root->ac_root->trans = (struct cli_ac_node **) cli_calloc(256, sizeof(struct cli_ac_node *));
#endif
root->ac_root->trans = (struct cli_ac_node **) mp_calloc(root->mempool, 256, sizeof(struct cli_ac_node *));
if(!root->ac_root->trans) {
cli_errmsg("cli_ac_init: Can't allocate memory for ac_root->trans\n");
#ifdef USE_MPOOL
mpool_free(root->mempool, root->ac_root);
#else
free(root->ac_root);
#endif
mp_free(root->mempool, root->ac_root);
return CL_EMEM;
}
@ -364,7 +321,7 @@ int cli_ac_init(struct cli_matcher *root, uint8_t mindepth, uint8_t maxdepth)
}
#ifdef USE_MPOOL
static void ac_free_alt(mpool_t *mempool, struct cli_ac_patt *p)
static void ac_free_alt(mp_t *mempool, struct cli_ac_patt *p)
#else
static void ac_free_alt(struct cli_ac_patt *p)
#endif
@ -382,20 +339,11 @@ static void ac_free_alt(struct cli_ac_patt *p)
a2 = a1;
a1 = a1->next;
if(a2->str)
#ifdef USE_MPOOL
mpool_free(mempool, a2->str);
mpool_free(mempool, a2);
#else
free(a2->str);
free(a2);
#endif
mp_free(mempool, a2->str);
mp_free(mempool, a2);
}
}
#ifdef USE_MPOOL
mpool_free(mempool, p->alttable);
#else
free(p->alttable);
#endif
mp_free(mempool, p->alttable);
}
void cli_ac_free(struct cli_matcher *root)
@ -406,67 +354,32 @@ void cli_ac_free(struct cli_matcher *root)
for(i = 0; i < root->ac_patterns; i++) {
patt = root->ac_pattable[i];
#ifdef USE_MPOOL
patt->prefix ? mpool_free(root->mempool, patt->prefix) : mpool_free(root->mempool, patt->pattern);
#else
patt->prefix ? free(patt->prefix) : free(patt->pattern);
#endif
#ifdef USE_MPOOL
mpool_free(root->mempool, patt->virname);
#else
free(patt->virname);
#endif
mp_free(root->mempool, patt->prefix ? patt->prefix : patt->pattern);
mp_free(root->mempool, patt->virname);
if(patt->offset)
#ifdef USE_MPOOL
mpool_free(root->mempool, patt->offset);
#else
free(patt->offset);
#endif
mp_free(root->mempool, patt->offset);
if(patt->alt)
#ifdef USE_MPOOL
ac_free_alt(root->mempool, patt);
#else
ac_free_alt(patt);
#endif
#ifdef USE_MPOOL
mpool_free(root->mempool, patt);
#else
free(patt);
#endif
mp_free(root->mempool, patt);
}
if(root->ac_pattable)
#ifdef USE_MPOOL
mpool_free(root->mempool, root->ac_pattable);
#else
free(root->ac_pattable);
#endif
mp_free(root->mempool, root->ac_pattable);
for(i = 0; i < root->ac_nodes; i++) {
if(!root->ac_nodetable[i]->leaf)
#ifdef USE_MPOOL
mpool_free(root->mempool, root->ac_nodetable[i]->trans);
mpool_free(root->mempool, root->ac_nodetable[i]);
#else
free(root->ac_nodetable[i]->trans);
free(root->ac_nodetable[i]);
#endif
mp_free(root->mempool, root->ac_nodetable[i]->trans);
mp_free(root->mempool, root->ac_nodetable[i]);
}
if(root->ac_nodetable)
#ifdef USE_MPOOL
mpool_free(root->mempool, root->ac_nodetable);
#else
free(root->ac_nodetable);
#endif
mp_free(root->mempool, root->ac_nodetable);
if(root->ac_root) {
#ifdef USE_MPOOL
mpool_free(root->mempool, root->ac_root->trans);
mpool_free(root->mempool, root->ac_root);
#else
free(root->ac_root->trans);
free(root->ac_root);
#endif
mp_free(root->mempool, root->ac_root->trans);
mp_free(root->mempool, root->ac_root);
}
}
@ -1189,11 +1102,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(strlen(hexsig) / 2 < root->ac_mindepth)
return CL_EPATSHORT;
#ifdef USE_MPOOL
if((new = (struct cli_ac_patt *) mpool_calloc(root->mempool, 1, sizeof(struct cli_ac_patt), NULL)) == NULL)
#else
if((new = (struct cli_ac_patt *) cli_calloc(1, sizeof(struct cli_ac_patt))) == NULL)
#endif
if((new = (struct cli_ac_patt *) mp_calloc(root->mempool, 1, sizeof(struct cli_ac_patt))) == NULL)
return CL_EMEM;
new->rtype = rtype;
@ -1213,11 +1122,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(strchr(hexsig, '[')) {
if(!(hexcpy = cli_strdup(hexsig))) {
#ifdef USE_MPOOL
mpool_free(root->mempool, new);
#else
free(new);
#endif
mp_free(root->mempool, new);
return CL_EMEM;
}
@ -1282,22 +1187,14 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(error) {
free(hexcpy);
#ifdef USE_MPOOL
mpool_free(root->mempool, new);
#else
free(new);
#endif
mp_free(root->mempool, new);
return error;
}
hex = cli_strdup(hex);
free(hexcpy);
if(!hex) {
#ifdef USE_MPOOL
mpool_free(root->mempool, new);
#else
free(new);
#endif
mp_free(root->mempool, new);
return CL_EMEM;
}
}
@ -1308,11 +1205,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(hex) {
hexcpy = hex;
} else if(!(hexcpy = cli_strdup(hexsig))) {
#ifdef USE_MPOOL
mpool_free(root->mempool, new);
#else
free(new);
#endif
mp_free(root->mempool, new);
return CL_EMEM;
}
@ -1340,11 +1233,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
}
*start++ = 0;
#ifdef USE_MPOOL
newalt = (struct cli_ac_alt *) mpool_calloc(root->mempool, 1, sizeof(struct cli_ac_alt), NULL);
#else
newalt = (struct cli_ac_alt *) cli_calloc(1, sizeof(struct cli_ac_alt));
#endif
newalt = (struct cli_ac_alt *) mp_calloc(root->mempool, 1, sizeof(struct cli_ac_alt));
if(!newalt) {
cli_errmsg("cli_ac_addsig: Can't allocate newalt\n");
error = CL_EMEM;
@ -1353,18 +1242,10 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
new->alt++;
#ifdef USE_MPOOL
newtable = (struct cli_ac_alt **) mpool_resize(root->mempool, new->alttable, new->alt * sizeof(struct cli_ac_alt *), NULL);
#else
newtable = (struct cli_ac_alt **) cli_realloc(new->alttable, new->alt * sizeof(struct cli_ac_alt *));
#endif
newtable = (struct cli_ac_alt **) mp_realloc(root->mempool, new->alttable, new->alt * sizeof(struct cli_ac_alt *));
if(!newtable) {
new->alt--;
#ifdef USE_MPOOL
mpool_free(root->mempool, newalt);
#else
free(newalt);
#endif
mp_free(root->mempool, newalt);
cli_errmsg("cli_ac_addsig: Can't realloc new->alttable\n");
error = CL_EMEM;
break;
@ -1384,11 +1265,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(3 * newalt->num - 1 == (uint16_t) strlen(pt)) {
newalt->chmode = 1;
#ifdef USE_MPOOL
newalt->str = (unsigned char *) mpool_alloc(root->mempool, newalt->num, NULL);
#else
newalt->str = (unsigned char *) cli_malloc(newalt->num);
#endif
newalt->str = (unsigned char *) mp_malloc(root->mempool, newalt->num);
if(!newalt->str) {
cli_errmsg("cli_ac_addsig: Can't allocate newalt->str\n");
error = CL_EMEM;
@ -1417,11 +1294,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
while(altpt->next)
altpt = altpt->next;
#ifdef USE_MPOOL
altpt->next = (struct cli_ac_alt *) mpool_calloc(root->mempool, 1, sizeof(struct cli_ac_alt), NULL);
#else
altpt->next = (struct cli_ac_alt *) cli_calloc(1, sizeof(struct cli_ac_alt));
#endif
altpt->next = (struct cli_ac_alt *) mp_calloc(root->mempool, 1, sizeof(struct cli_ac_alt));
if(!altpt->next) {
cli_errmsg("cli_ac_addsig: Can't allocate altpt->next\n");
error = CL_EMEM;
@ -1456,13 +1329,11 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
free(hex);
#ifdef USE_MPOOL
ac_free_alt(root->mempool, new);
}
mpool_free(root->mempool, new);
#else
ac_free_alt(new);
}
free(new);
#endif
}
mp_free(root->mempool, new);
return error;
}
}
@ -1472,7 +1343,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
unsigned int mpoolpattsz = (strlen(hex ? hex : hexsig) / 2 + 1) * sizeof(uint16_t);
uint16_t *mpoolpatt = cli_hex2ui(hex ? hex : hexsig);
if(mpoolpatt) {
if((new->pattern = mpool_alloc(root->mempool, mpoolpattsz, NULL)) != NULL)
if((new->pattern = mp_malloc(root->mempool, mpoolpattsz)) != NULL)
memcpy(new->pattern, mpoolpatt, mpoolpattsz);
free(mpoolpatt);
} else new->pattern = NULL;
@ -1485,11 +1356,10 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(new->alt)
#ifdef USE_MPOOL
ac_free_alt(root->mempool, new);
mpool_free(root->mempool, new);
#else
ac_free_alt(new);
free(new);
#endif
mp_free(root->mempool, new);
free(hex);
return CL_EMALFDB;
}
@ -1539,13 +1409,11 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
cli_errmsg("cli_ac_addsig: Can't find a static subpattern of length %u\n", root->ac_mindepth);
#ifdef USE_MPOOL
ac_free_alt(root->mempool, new);
mpool_free(root->mempool, new->pattern);
mpool_free(root->mempool, new);
#else
ac_free_alt(new);
free(new->pattern);
free(new);
#endif
mp_free(root->mempool, new->pattern);
mp_free(root->mempool, new);
return CL_EMALFDB;
}
@ -1566,7 +1434,7 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
{
char *mpoolvirname = cli_virname((char *) virname, options & CL_DB_OFFICIAL, 0);
if(mpoolvirname) {
if((new->virname = mpool_alloc(root->mempool, strlen(mpoolvirname) + 1, NULL)) != NULL)
if((new->virname = mp_malloc(root->mempool, strlen(mpoolvirname) + 1)) != NULL)
strcpy(new->virname, mpoolvirname);
free(mpoolvirname);
} else new->virname = NULL;
@ -1575,15 +1443,13 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
new->virname = cli_virname((char *) virname, options & CL_DB_OFFICIAL, 0);
#endif
if(!new->virname) {
mp_free(root->mempool, new->prefix ? new->prefix : new->pattern);
#ifdef USE_MPOOL
new->prefix ? mpool_free(root->mempool, new->prefix) : mpool_free(root->mempool, new->pattern);
ac_free_alt(root->mempool, new);
mpool_free(root->mempool, new);
#else
new->prefix ? free(new->prefix) : free(new->pattern);
ac_free_alt(new);
free(new);
#endif
mp_free(root->mempool, new);
return CL_EMEM;
}
@ -1592,43 +1458,35 @@ int cli_ac_addsig(struct cli_matcher *root, const char *virname, const char *hex
if(offset) {
#ifdef USE_MPOOL
if((new->offset = mpool_alloc(root->mempool, strlen(offset) + 1, NULL)))
if((new->offset = mp_malloc(root->mempool, strlen(offset) + 1)))
strcpy(new->offset, offset);
#else
new->offset = cli_strdup(offset);
#endif
if(!new->offset) {
mp_free(root->mempool, new->prefix ? new->prefix : new->pattern);
#ifdef USE_MPOOL
new->prefix ? mpool_free(root->mempool, new->prefix) : mpool_free(root->mempool, new->pattern);
ac_free_alt(root->mempool, new);
mpool_free(root->mempool, new->virname);
mpool_free(root->mempool, new);
#else
new->prefix ? free(new->prefix) : free(new->pattern);
ac_free_alt(new);
free(new->virname);
free(new);
#endif
mp_free(root->mempool, new->virname);
mp_free(root->mempool, new);
return CL_EMEM;
}
}
if((ret = cli_ac_addpatt(root, new))) {
mp_free(root->mempool, new->prefix ? new->prefix : new->pattern);
mp_free(root->mempool, new->virname);
#ifdef USE_MPOOL
new->prefix ? mpool_free(root->mempool, new->prefix) : mpool_free(root->mempool, new->pattern);
mpool_free(root->mempool, new->virname);
ac_free_alt(root->mempool, new);
if(new->offset)
mpool_free(root->mempool, new->offset);
mpool_free(root->mempool, new);
#else
new->prefix ? free(new->prefix) : free(new->pattern);
free(new->virname);
ac_free_alt(new);
if(new->offset)
free(new->offset);
free(new);
#endif
if(new->offset)
mp_free(root->mempool, new->offset);
mp_free(root->mempool, new);
return ret;
}

@ -32,9 +32,7 @@
#include "matcher-bm.h"
#include "filetypes.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
#define BM_MIN_LENGTH 3
#define BM_BLOCK_SIZE 3
@ -101,23 +99,11 @@ int cli_bm_init(struct cli_matcher *root)
{
uint16_t i, size = HASH(255, 255, 255) + 1;
#ifdef USE_MPOOL
if(!(root->bm_shift = (uint8_t *) mpool_calloc(root->mempool, size, sizeof(uint8_t), NULL)))
#else
if(!(root->bm_shift = (uint8_t *) cli_calloc(size, sizeof(uint8_t))))
#endif
if(!(root->bm_shift = (uint8_t *) mp_calloc(root->mempool, size, sizeof(uint8_t))))
return CL_EMEM;
#ifdef USE_MPOOL
if(!(root->bm_suffix = (struct cli_bm_patt **) mpool_calloc(root->mempool, size, sizeof(struct cli_bm_patt *), NULL))) {
#else
if(!(root->bm_suffix = (struct cli_bm_patt **) cli_calloc(size, sizeof(struct cli_bm_patt *)))) {
#endif
#ifdef USE_MPOOL
mpool_free(root->mempool, root->bm_shift);
#else
free(root->bm_shift);
#endif
if(!(root->bm_suffix = (struct cli_bm_patt **) mp_calloc(root->mempool, size, sizeof(struct cli_bm_patt *)))) {
mp_free(root->mempool, root->bm_shift);
return CL_EMEM;
}
@ -134,11 +120,7 @@ void cli_bm_free(struct cli_matcher *root)
if(root->bm_shift)
#ifdef USE_MPOOL
mpool_free(root->mempool, root->bm_shift);
#else
free(root->bm_shift);
#endif
mp_free(root->mempool, root->bm_shift);
if(root->bm_suffix) {
for(i = 0; i < size; i++) {
@ -147,42 +129,17 @@ void cli_bm_free(struct cli_matcher *root)
prev = patt;
patt = patt->next;
if(prev->prefix)
#ifdef USE_MPOOL
mpool_free(root->mempool, prev->prefix);
#else
free(prev->prefix);
#endif
mp_free(root->mempool, prev->prefix);
else
#ifdef USE_MPOOL
mpool_free(root->mempool, prev->pattern);
#else
free(prev->pattern);
#endif
mp_free(root->mempool, prev->pattern);
if(prev->virname)
#ifdef USE_MPOOL
mpool_free(root->mempool, prev->virname);
#else
free(prev->virname);
#endif
mp_free(root->mempool, prev->virname);
if(prev->offset)
#ifdef USE_MPOOL
mpool_free(root->mempool, prev->offset);
#else
free(prev->offset);
#endif
#ifdef USE_MPOOL
mpool_free(root->mempool, prev);
#else
free(prev);
#endif
mp_free(root->mempool, prev->offset);
mp_free(root->mempool, prev);
}
}
#ifdef USE_MPOOL
mpool_free(root->mempool, root->bm_suffix);
#else
free(root->bm_suffix);
#endif
mp_free(root->mempool, root->bm_suffix);
}
}

@ -34,9 +34,7 @@
#include "matcher-bm.h"
#include "hashtab.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
#define CLI_MATCH_WILDCARD 0xff00
#define CLI_MATCH_CHAR 0x0000
@ -59,7 +57,7 @@ struct cli_lsig_tdb {
const uint32_t *sectoff, *sectrva, *sectvsz, *sectraw, *sectrsz,
*secturva, *sectuvsz, *secturaw, *sectursz;
#ifdef USE_MPOOL
mpool_t *mempool;
mp_t *mempool;
#endif
};
@ -88,7 +86,7 @@ struct cli_matcher {
uint16_t maxpatlen;
uint8_t ac_only;
#ifdef USE_MPOOL
mpool_t *mempool;
mp_t *mempool;
#endif
};

@ -1,86 +0,0 @@
2006-05-31 Gray Watson <>
* Version 2.1.0 released.
* Added MPOOL_ERROR_PNT_OVER to distinguish between pointer
overwrite and mpool structure overwrite.
2005-05-20 Gray Watson <>
* Version 2.0.0 released.
* First external publication of library.
Thu Mar 4 10:14:21 1999 Gray Watson <>
* Reworked the way the blocks were split up.
* Removed the round arguments. Not used.
Wed Mar 3 19:29:38 1999 Gray Watson <>
* Moved to random(). Fucking rand() was hiding a lot of problems
from me.
* Added some additional sanity checks in free().
* Added mpool_set_max_pages to the library.
Thu Feb 25 12:41:51 1999 Gray Watson <>
* Added log_function transaction callback.
Thu Feb 25 09:53:33 1999 Gray Watson <>
* Changed the default page size to 16 * getpagesize.
Wed Feb 24 17:52:52 1999 Gray Watson <>
* Major reworking of internals to simplify structures.
Fri Feb 19 12:52:55 1999 Gray Watson <>
* Made a number of changes to the internals which removed the
addr_to_block as a performance pig.
Tue Feb 16 21:11:23 1999 Gray Watson <>
* Added ability for free to look up in the free bit lists for
memory to use.
* Added mpool_clear. Good idea.
Thu Feb 11 02:53:45 1999 Gray Watson <>
* Finally a working version. Looks much better.
* Added rounding sizes so it will allocate aligned memory.
* Added minimum size to the mpool_free function to speed it up.
Wed Feb 10 23:30:48 1999 Gray Watson <>
* Version 1 with new fine grained memory resolution almost
working.
Fri May 2 02:26:28 1997 Gray Watson <>
* Moved to MAP_PRIVATE from MAP_SHARED.
* Fixed the min/max handling.
* Added additional info to mpool_stat.
Thu May 1 16:51:06 1997 Gray Watson <>
* Added page-size information request.
* Added better no-memory errors.
Thu Apr 24 01:58:41 1997 Gray Watson <>
* Added handling of null for debugging purposes.
Mon Apr 14 03:31:26 1997 Gray Watson <>
* Started the mpool routines.

@ -1,56 +0,0 @@
#
# $Id: Makefile.all,v 1.1.1.1 2005/05/20 19:58:29 gray Exp $
#
HFLS = mpool.h
OBJS = mpool.o
CC = cc
CFLAGS = -g -I. $(DEFINES)
#CFLAGS = -g -I.
LDFLAGS =
RANLIB = ranlib
DESTDIR = /usr/local
TEST = mpool_t
LIBRARY = libmpool.a
all : $(LIBRARY) $(UTIL)
clean :
rm -f a.out core *.o *.t
rm -f $(LIBRARY) $(TEST)
install : $(HFLS) $(LIBRARY)
install -c -m 444 $(HFLS) $(DESTDIR)/include
install -c -m 444 $(LIBRARY) $(DESTDIR)/lib
$(RANLIB) $(DESTDIR)/libo/$(LIBRARY)
$(LIBRARY) : $(OBJS)
ar cr $(LIBRARY) $?
$(RANLIB) $@
tests : $(TEST)
$(TEST) : $(TEST).o $(LIBRARY)
rm -f $@
$(CC) $(LDFLAGS) $(TEST).o $(LIBRARY)
mv a.out $@
$(UTIL) : $(UTIL).o $(LIBRARY)
rm -f $@
$(CC) $(LDFLAGS) $(UTIL).o $(LIBRARY)
mv a.out $@
.c.o :
rm -f $@
$(CC) $(CFLAGS) -c $< -o $@
#
# Below are dependencies that are automatically generated by make
# depend. Please do not edit by hand.
#
mpool.o: mpool.c mpool.h mpool_loc.h
mpool_t.o: mpool_t.c mpool.h

@ -1,11 +0,0 @@
-------------------------------------------------------------------------------
$Id: NEWS,v 1.2 2006/05/31 20:28:31 gray Exp $
-------------------------------------------------------------------------------
Version 2.1.0:
* Added MPOOL_ERROR_PNT_OVER to show pointer overwrites.
Version 2.0.0:
* Initial external release of library after use since 1996.

@ -1,46 +0,0 @@
-------------------------------------------------------------------------------
$Id: README,v 1.2 2005/05/22 19:49:30 gray Exp $
-------------------------------------------------------------------------------
BACKGROUND:
This is a memory pool library which was written to allow a program to
have heaps that it could destroy without fragmenting memory. You can
have multiple heaps and reset them easily completely reclaiming the
memory (as opposed to standard heaps).
With it you can mpool_open() a new heap, then mpool_alloc(),
mpool_calloc(), mpool_realloc(), mpool_free() to your heart's content.
Once you are done with the memory-pool you can run mpool_clear() or
mpool_close() and completely remove the memory associated with the
pools. This is very handy if you are working with some large blocks
of memory and want to reset back to a clean state.
Check out the mpool.h file for more information. Sorry for minimal
docs.
-------------------------------------------------------------------------------
INSTALLATION:
1) Typing 'make' should be enough to build libskip.a.
2) Typing 'make tests' should make the mpool_t test program.
-------------------------------------------------------------------------------
REPOSITORY:
The newest versions of the library are available from:
http://256.com/sources/mpool/
-------------------------------------------------------------------------------
AUTHOR:
If you have any questions or problems feel free to send me mail.
Gray Watson
http://256.com/gray/
-------------------------------------------------------------------------------

File diff suppressed because it is too large Load Diff

@ -1,466 +0,0 @@
/*
* Memory pool defines.
*
* Copyright 1996 by Gray Watson.
*
* This file is part of the mpool package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: mpool.h,v 1.4 2006/05/31 20:26:11 gray Exp $
*/
#ifndef __MPOOL_H__
#define __MPOOL_H__
#include <sys/types.h>
/*
* mpool flags to mpool_alloc or mpool_set_attr
*/
/*
* Choose a best fit algorithm not first fit. This takes more CPU
* time but will result in a tighter heap.
*/
#define MPOOL_FLAG_BEST_FIT (1<<0)
/*
* By default the library adds 2 bytes onto all allocations to insert
* a magic number that it can look for to determine how large a freed
* memory chunk is. This flag indicates that few if any frees are
* going to be performed on the pool and to not waste memory on these
* bytes.
*/
#define MPOOL_FLAG_NO_FREE (1<<1)
/*
* This enables very heavy packing at the possible expense of CPU.
* This affects a number of parts of the library.
*
* By default the 1st page of memory is reserved for the main mpool
* structure. This flag will cause the rest of the 1st block to be
* available for use as user memory.
*
* By default the library looks through the memory when freed looking
* for a magic value. There is an internal max size that it will look
* and then it will give up. This flag forces it to look until it
* finds it.
*/
#define MPOOL_FLAG_HEAVY_PACKING (1<<2)
/*
* Use sbrk not mmap to allocate pages. This is not recommended for
* normal use.
*/
#define MPOOL_FLAG_USE_SBRK (1<<3)
/*
* Mpool error codes
*/
#define MPOOL_ERROR_NONE 1 /* no error */
#define MPOOL_ERROR_ARG_NULL 2 /* function argument is null */
#define MPOOL_ERROR_ARG_INVALID 3 /* function argument is invalid */
#define MPOOL_ERROR_PNT 4 /* invalid mpool pointer */
#define MPOOL_ERROR_POOL_OVER 5 /* mpool structure was overwritten */
#define MPOOL_ERROR_PAGE_SIZE 6 /* could not get system page-size */
#define MPOOL_ERROR_OPEN_ZERO 7 /* could not open /dev/zero */
#define MPOOL_ERROR_NO_MEM 8 /* no memory available */
#define MPOOL_ERROR_MMAP 9 /* problems with mmap */
#define MPOOL_ERROR_SIZE 10 /* error processing requested size */
#define MPOOL_ERROR_TOO_BIG 11 /* allocation exceeded max size */
#define MPOOL_ERROR_MEM 12 /* invalid memory address */
#define MPOOL_ERROR_MEM_OVER 13 /* memory lower bounds overwritten */
#define MPOOL_ERROR_NOT_FOUND 14 /* memory block not found in pool */
#define MPOOL_ERROR_IS_FREE 15 /* memory block already free */
#define MPOOL_ERROR_BLOCK_STAT 16 /* invalid internal block status */
#define MPOOL_ERROR_FREE_ADDR 17 /* invalid internal free address */
#define MPOOL_ERROR_SBRK_CONTIG 18 /* sbrk did not return contiguous mem*/
#define MPOOL_ERROR_NO_PAGES 19 /* ran out of pages in pool */
#define MPOOL_ERROR_ALLOC 20 /* calloc,malloc,free,realloc failed */
#define MPOOL_ERROR_PNT_OVER 21 /* pointer structure was overwritten */
/*
* Mpool function IDs for the mpool_log_func callback function.
*/
#define MPOOL_FUNC_CLOSE 1 /* mpool_close function called */
#define MPOOL_FUNC_CLEAR 2 /* mpool_clear function called */
#define MPOOL_FUNC_ALLOC 3 /* mpool_alloc function called */
#define MPOOL_FUNC_CALLOC 4 /* mpool_calloc function called */
#define MPOOL_FUNC_FREE 5 /* mpool_free function called */
#define MPOOL_FUNC_RESIZE 6 /* mpool_resize function called */
/*
* void mpool_log_func_t
*
* DESCRIPTION:
*
* Mpool transaction log function.
*
* RETURNS:
*
* None.
*
* ARGUMENT:
*
* mp_p -> Associated mpool address.
*
* func_id -> Integer function ID which identifies which mpool
* function is being called.
*
* byte_size -> Optionally specified byte size.
*
* ele_n -> Optionally specified element number. For mpool_calloc
* only.
*
* new_addr -> Optionally specified new address. For mpool_alloc,
* mpool_calloc, and mpool_resize only.
*
* old_addr -> Optionally specified old address. For mpool_resize and
* mpool_free only.
*
* old_byte_size -> Optionally specified old byte size. For
* mpool_resize only.
*/
typedef void (*mpool_log_func_t)(const void *mp_p,
const int func_id,
const unsigned long byte_size,
const unsigned long ele_n,
const void *old_addr, const void *new_addr,
const unsigned long old_byte_size);
#ifdef MPOOL_MAIN
#include "mpool_loc.h"
#else
/* generic mpool type */
typedef void mpool_t;
#endif
/*<<<<<<<<<< The below prototypes are auto-generated by fillproto */
/*
* mpool_t *mpool_open
*
* DESCRIPTION:
*
* Open/allocate a new memory pool.
*
* RETURNS:
*
* Success - Pool pointer which must be passed to mpool_close to
* deallocate.
*
* Failure - NULL
*
* ARGUMENTS:
*
* flags -> Flags to set attributes of the memory pool. See the top
* of mpool.h.
*
* page_size -> Set the internal memory page-size. This must be a
* multiple of the getpagesize() value. Set to 0 for the default.
*
* start_addr -> Starting address to try and allocate memory pools.
* This is ignored if the MPOOL_FLAG_USE_SBRK is enabled.
*
* error_p <- Pointer to integer which, if not NULL, will be set with
* a mpool error code.
*/
extern
mpool_t *mpool_open(const unsigned int flags, const unsigned int page_size,
void *start_addr, int *error_p);
/*
* int mpool_close
*
* DESCRIPTION:
*
* Close/free a memory allocation pool previously opened with
* mpool_open.
*
* RETURNS:
*
* Success - MPOOL_ERROR_NONE
*
* Failure - Mpool error code
*
* ARGUMENTS:
*
* mp_p <-> Pointer to our memory pool.
*/
extern
int mpool_close(mpool_t *mp_p);
/*
* int mpool_clear
*
* DESCRIPTION:
*
* Wipe an opened memory pool clean so we can start again.
*
* RETURNS:
*
* Success - MPOOL_ERROR_NONE
*
* Failure - Mpool error code
*
* ARGUMENTS:
*
* mp_p <-> Pointer to our memory pool.
*/
extern
int mpool_clear(mpool_t *mp_p);
/*
* void *mpool_alloc
*
* DESCRIPTION:
*
* Allocate space for bytes inside of an already open memory pool.
*
* RETURNS:
*
* Success - Pointer to the address to use.
*
* Failure - NULL
*
* ARGUMENTS:
*
* mp_p <-> Pointer to the memory pool. If NULL then it will do a
* normal malloc.
*
* byte_size -> Number of bytes to allocate in the pool. Must be >0.
*
* error_p <- Pointer to integer which, if not NULL, will be set with
* a mpool error code.
*/
extern
void *mpool_alloc(mpool_t *mp_p, const unsigned long byte_size,
int *error_p);
/*
* void *mpool_calloc
*
* DESCRIPTION:
*
* Allocate space for elements of bytes in the memory pool and zero
* the space afterwards.
*
* RETURNS:
*
* Success - Pointer to the address to use.
*
* Failure - NULL
*
* ARGUMENTS:
*
* mp_p <-> Pointer to the memory pool. If NULL then it will do a
* normal calloc.
*
* ele_n -> Number of elements to allocate.
*
* ele_size -> Number of bytes per element being allocated.
*
* error_p <- Pointer to integer which, if not NULL, will be set with
* a mpool error code.
*/
extern
void *mpool_calloc(mpool_t *mp_p, const unsigned long ele_n,
const unsigned long ele_size, int *error_p);
/*
* int mpool_free
*
* DESCRIPTION:
*
* Free an address from a memory pool.
*
* RETURNS:
*
* Success - MPOOL_ERROR_NONE
*
* Failure - Mpool error code
*
* ARGUMENTS:
*
* mp_p <-> Pointer to the memory pool. If NULL then it will do a
* normal free.
*
* addr <-> Address to free.
*
* size -> Size of the address being freed.
*/
extern
int mpool_free(mpool_t *mp_p, void *addr);
/*
* void *mpool_resize
*
* DESCRIPTION:
*
* Reallocate an address in a mmeory pool to a new size. This is
* different from realloc in that it needs the old address' size. If
* you don't have it then you need to allocate new space, copy the
* data, and free the old pointer yourself.
*
* RETURNS:
*
* Success - Pointer to the address to use.
*
* Failure - NULL
*
* ARGUMENTS:
*
* mp_p <-> Pointer to the memory pool. If NULL then it will do a
* normal realloc.
*
* old_addr -> Previously allocated address.
*
* old_byte_size -> Size of the old address. Must be known, cannot be
* 0.
*
* new_byte_size -> New size of the allocation.
*
* error_p <- Pointer to integer which, if not NULL, will be set with
* a mpool error code.
*/
extern
void *mpool_resize(mpool_t *mp_p, void *old_addr,
const unsigned long new_byte_size,
int *error_p);
extern
void *mpool_resize2(mpool_t *mp_p, void *old_addr,
const unsigned long new_byte_size,
int *error_p);
/*
* int mpool_stats
*
* DESCRIPTION:
*
* Return stats from the memory pool.
*
* RETURNS:
*
* Success - MPOOL_ERROR_NONE
*
* Failure - Mpool error code
*
* ARGUMENTS:
*
* mp_p -> Pointer to the memory pool.
*
* page_size_p <- Pointer to an unsigned integer which, if not NULL,
* will be set to the page-size of the pool.
*
* num_alloced_p <- Pointer to an unsigned long which, if not NULL,
* will be set to the number of pointers currently allocated in pool.
*
* user_alloced_p <- Pointer to an unsigned long which, if not NULL,
* will be set to the number of user bytes allocated in this pool.
*
* max_alloced_p <- Pointer to an unsigned long which, if not NULL,
* will be set to the maximum number of user bytes that have been
* allocated in this pool.
*
* tot_alloced_p <- Pointer to an unsigned long which, if not NULL,
* will be set to the total amount of space (including administrative
* overhead) used by the pool.
*/
extern
int mpool_stats(const mpool_t *mp_p, unsigned int *page_size_p,
unsigned long *num_alloced_p,
unsigned long *user_alloced_p,
unsigned long *max_alloced_p,
unsigned long *tot_alloced_p);
/*
* int mpool_set_log_func
*
* DESCRIPTION:
*
* Set a logging callback function to be called whenever there was a
* memory transaction. See mpool_log_func_t.
*
* RETURNS:
*
* Success - MPOOL_ERROR_NONE
*
* Failure - Mpool error code
*
* ARGUMENTS:
*
* mp_p <-> Pointer to the memory pool.
*
* log_func -> Log function (defined in mpool.h) which will be called
* with each mpool transaction.
*/
extern
int mpool_set_log_func(mpool_t *mp_p, mpool_log_func_t log_func);
/*
* int mpool_set_max_pages
*
* DESCRIPTION:
*
* Set the maximum number of pages that the library will use. Once it
* hits the limit it will return MPOOL_ERROR_NO_PAGES.
*
* NOTE: if the MPOOL_FLAG_HEAVY_PACKING is set then this max-pages
* value will include the page with the mpool header structure in it.
* If the flag is _not_ set then the max-pages will not include this
* first page.
*
* RETURNS:
*
* Success - MPOOL_ERROR_NONE
*
* Failure - Mpool error code
*
* ARGUMENTS:
*
* mp_p <-> Pointer to the memory pool.
*
* max_pages -> Maximum number of pages used by the library.
*/
extern
int mpool_set_max_pages(mpool_t *mp_p, const unsigned int max_pages);
/*
* const char *mpool_strerror
*
* DESCRIPTION:
*
* Return the corresponding string for the error number.
*
* RETURNS:
*
* Success - String equivalient of the error.
*
* Failure - String "invalid error code"
*
* ARGUMENTS:
*
* error -> Error number that we are converting.
*/
extern
const char *mpool_strerror(const int error);
/*<<<<<<<<<< This is end of the auto-generated output from fillproto. */
#endif /* ! __MPOOL_H__ */

@ -1,116 +0,0 @@
/*
* Memory pool local defines.
*
* Copyright 1996 by Gray Watson.
*
* This file is part of the mpool package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: mpool_loc.h,v 1.2 2005/05/20 20:08:54 gray Exp $
*/
#ifndef __MPOOL_LOC_H__
#define __MPOOL_LOC_H__
#define MPOOL_MAGIC 0xABACABA /* magic for struct */
#define BLOCK_MAGIC 0xB1B1007 /* magic for blocks */
#define FENCE_MAGIC0 (unsigned char)(0xFAU) /* 1st magic mem byte */
#define FENCE_MAGIC1 (unsigned char)(0xD3U) /* 2nd magic mem byte */
#define FENCE_SIZE 2 /* fence space */
#define MIN_ALLOCATION (sizeof(mpool_free_t)) /* min alloc */
#define MAX_FREE_SEARCH 10240 /* max size to search */
#define MAX_FREE_LIST_SEARCH 100 /* max looking for free mem */
/*
* bitflag tools for Variable and a Flag
*/
#define BIT_FLAG(x) (1 << (x))
#define BIT_SET(v,f) (v) |= (f)
#define BIT_CLEAR(v,f) (v) &= ~(f)
#define BIT_IS_SET(v,f) ((v) & (f))
#define BIT_TOGGLE(v,f) (v) ^= (f)
#define SET_POINTER(pnt, val) \
do { \
if ((pnt) != NULL) { \
(*(pnt)) = (val); \
} \
} while(0)
#define BLOCK_FLAG_USED BIT_FLAG(0) /* block is used */
#define BLOCK_FLAG_FREE BIT_FLAG(1) /* block is free */
#define DEFAULT_PAGE_MULT 16 /* pagesize = this * getpagesize*/
/* How many pages SIZE bytes resides in. We add in the block header. */
#define PAGES_IN_SIZE(mp_p, size) (((size) + sizeof(mpool_block_t) + \
(mp_p)->mp_page_size - 1) / \
(mp_p)->mp_page_size)
#define SIZE_OF_PAGES(mp_p, page_n) ((page_n) * (mp_p)->mp_page_size)
#define MAX_BITS 30 /* we only can allocate 1gb chunks */
#define MAX_BLOCK_USER_MEMORY(mp_p) ((mp_p)->mp_page_size - \
sizeof(mpool_block_t))
#define FIRST_ADDR_IN_BLOCK(block_p) (void *)((char *)(block_p) + \
sizeof(mpool_block_t))
#define MEMORY_IN_BLOCK(block_p) ((char *)(block_p)->mb_bounds_p - \
((char *)(block_p) + \
sizeof(mpool_block_t)))
typedef struct {
unsigned int mp_magic; /* magic number for struct */
unsigned int mp_flags; /* flags for the struct */
unsigned long mp_alloc_c; /* number of allocations */
unsigned long mp_user_alloc; /* user bytes allocated */
unsigned long mp_max_alloc; /* maximum user bytes allocated */
unsigned int mp_page_c; /* number of pages allocated */
unsigned int mp_max_pages; /* maximum number of pages to use */
unsigned int mp_page_size; /* page-size of our system */
int mp_fd; /* fd for /dev/zero if mmap-ing */
off_t mp_top; /* top of our allocations in fd */
mpool_log_func_t mp_log_func; /* log callback function */
void *mp_addr; /* current address for mmaping */
void *mp_min_p; /* min address in pool for checks */
void *mp_bounds_p; /* max address in pool for checks */
struct mpool_block_st *mp_first_p; /* first memory block we are using */
struct mpool_block_st *mp_last_p; /* last memory block we are using */
struct mpool_block_st *mp_free[MAX_BITS + 1]; /* free lists based on size */
unsigned int mp_magic2; /* upper magic for overwrite sanity */
} mpool_t;
/* for debuggers to be able to interrogate the generic type in the .h file */
typedef mpool_t mpool_ext_t;
/*
* Block header structure. This structure *MUST* be long-word
* aligned.
*/
typedef struct mpool_block_st {
unsigned int mb_magic; /* magic number for block header */
void *mb_bounds_p; /* block boundary location */
struct mpool_block_st *mb_next_p; /* linked list next pointer */
unsigned int mb_magic2; /* upper magic for overwrite sanity */
} mpool_block_t;
/*
* Free list structure.
*/
typedef struct {
void *mf_next_p; /* pointer to the next free address */
unsigned long mf_size; /* size of the free block */
} mpool_free_t;
#endif /* ! __MPOOL_LOC_H__ */

@ -1,914 +0,0 @@
/*
* Memory pool test program.
*
* Copyright 1996 by Gray Watson.
*
* This file is part of the mpool package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: mpool_t.c,v 1.2 2005/05/20 20:08:55 gray Exp $
*/
/*
* Test program for the malloc library. Current it is interactive although
* should be script based.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "mpool.h"
#ifdef __GNUC__
#ident "$Id: mpool_t.c,v 1.2 2005/05/20 20:08:55 gray Exp $"
#else
static char *rcs_id = "$Id: mpool_t.c,v 1.2 2005/05/20 20:08:55 gray Exp $";
#endif
#define DEFAULT_ITERATIONS 10000
#define MAX_POINTERS 1024
#define MAX_ALLOC (1024 * 1024)
#define MIN_AVAIL 10
#define RANDOM_VALUE(x) ((random() % ((x) * 10)) / 10)
/* pointer tracking structure */
struct pnt_info_st {
long pi_crc; /* crc of storage */
long pi_size; /* size of storage */
void *pi_pnt; /* pnt to storage */
struct pnt_info_st *pi_next; /* pnt to next */
};
typedef struct pnt_info_st pnt_info_t;
static pnt_info_t *pointer_grid;
/* argument variables */
static int best_fit_b = 0; /* set best fit flag */
static int heavy_pack_b = 0; /* set heavy pack flg*/
static int interactive_b = 0; /* interactive flag */
static int log_trxn_b = 0; /* log mem trxns */
static long max_alloc = MAX_ALLOC; /* amt of mem to use */
static int max_pages_n = 0; /* max # pages */
static int use_malloc_b = 0; /* use system alloc */
static int max_pointers = MAX_POINTERS; /* # of pnts to use */
static int no_free_b = 0; /* set no free flag */
static long page_size = 0; /* mpool pagesize */
static int use_sbrk_b = 0; /* use sbrk not mmap */
static unsigned int seed_random = 0; /* random seed */
static int default_iter_n = DEFAULT_ITERATIONS; /* # of iters */
static int verbose_b = 0; /* verbose flag */
/*
* static long hex_to_long
*
* DESCRIPTION:
*
* Hexadecimal string to integer translation.
*
* RETURNS:
*
* Long value of converted hex string.
*
* ARGUMENTS:
*
* str -> Hex string we are converting.
*/
static long hex_to_long(const char *str)
{
long ret;
const char *str_p = str;
/* strip off spaces */
for (; *str_p == ' ' || *str_p == '\t'; str_p++) {
}
/* skip a leading 0[xX] */
if (*str_p == '0' && (*(str_p + 1) == 'x' || *(str_p + 1) == 'X')) {
str_p += 2;
}
for (ret = 0;; str_p++) {
if (*str_p >= '0' && *str_p <= '9') {
ret = ret * 16 + (*str_p - '0');
}
else if (*str_p >= 'a' && *str_p <= 'f') {
ret = ret * 16 + (*str_p - 'a' + 10);
}
else if (*str_p >= 'A' && *str_p <= 'F') {
ret = ret * 16 + (*str_p - 'A' + 10);
}
else {
break;
}
}
return ret;
}
/*
* static void* get_address
*
* DESCRIPTION:
*
* Read an address from the user.
*
* RETURNS:
*
* Address read in from user.
*
* ARGUMENTS:
*
* None.
*/
static void *get_address(void)
{
char line[80];
void *pnt;
do {
(void)printf("Enter a hex address: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
return NULL;
}
} while (line[0] == '\0');
pnt = (void *)hex_to_long(line);
return pnt;
}
/*
* static void do_random
*
* DESCRIPTION:
*
* Try ITER_N random program iterations, returns 1 on success else 0
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* pool <-> Out memory pool.
*
* iter_n -> Number of iterations to run.
*/
static void do_random(mpool_t *pool, const int iter_n)
{
int iter_c, free_c, ret;
long max = max_alloc, amount;
char *chunk_p;
void *new_pnt;
pnt_info_t *free_p, *used_p = NULL;
pnt_info_t *pnt_p, *last_p;
if (use_malloc_b) {
pointer_grid = (pnt_info_t *)malloc(sizeof(pnt_info_t) * max_pointers);
}
else {
pointer_grid = (pnt_info_t *)mpool_alloc(pool,
sizeof(pnt_info_t) * max_pointers,
&ret);
}
if (pointer_grid == NULL) {
(void)printf("mpool_t: problems allocating %d pointer slots: %s\n",
max_pointers, strerror(errno));
return;
}
/* initialize free list */
free_p = pointer_grid;
for (pnt_p = pointer_grid; pnt_p < pointer_grid + max_pointers; pnt_p++) {
pnt_p->pi_size = 0;
pnt_p->pi_pnt = NULL;
pnt_p->pi_next = pnt_p + 1;
}
/* redo the last next pointer */
(pnt_p - 1)->pi_next = NULL;
free_c = max_pointers;
for (iter_c = 0; iter_c < iter_n;) {
int which;
/* special case when doing non-linear stuff, sbrk took all memory */
if (max < MIN_AVAIL && free_c == max_pointers) {
break;
}
if (free_c < max_pointers && used_p == NULL) {
(void)fprintf(stderr, "mpool_t: problem with test program free list\n");
exit(1);
}
/* decide whether to malloc a new pointer or free/realloc an existing */
which = RANDOM_VALUE(4);
/*
* < MIN_AVAIL means alloc as long as we have enough memory and
* there are free slots we do an allocation, else we free
*/
if (free_c == max_pointers
|| (free_c > 0 && which < 3 && max >= MIN_AVAIL)) {
while (1) {
amount = RANDOM_VALUE(max / 2);
if (amount > 0) {
break;
}
}
which = RANDOM_VALUE(9);
pnt_p = NULL;
switch (which) {
case 0: case 1: case 2:
pnt_p = free_p;
if (use_malloc_b) {
pnt_p->pi_pnt = malloc(amount);
}
else {
pnt_p->pi_pnt = mpool_alloc(pool, amount, &ret);
}
if (verbose_b) {
(void)printf("%d: malloc %ld (max %ld) into slot %d. got %#lx\n",
iter_c + 1, amount, max, pnt_p - pointer_grid,
(long)pnt_p->pi_pnt);
}
if (pnt_p->pi_pnt == NULL) {
(void)printf("malloc of %ld failed: %s\n",
amount,
(use_malloc_b ? strerror(errno) : mpool_strerror(ret)));
}
pnt_p->pi_size = amount;
break;
case 3: case 4: case 5:
pnt_p = free_p;
if (use_malloc_b) {
pnt_p->pi_pnt = calloc(amount, sizeof(char));
}
else {
pnt_p->pi_pnt = mpool_calloc(pool, amount, sizeof(char), &ret);
}
if (verbose_b) {
(void)printf("%d: calloc %ld (max %ld) into slot %d. got %#lx\n",
iter_c + 1, amount, max, pnt_p - pointer_grid,
(long)pnt_p->pi_pnt);
}
/* test the returned block to make sure that is has been cleared */
if (pnt_p->pi_pnt == NULL) {
(void)printf("calloc of %ld failed: %s\n",
amount,
(use_malloc_b ? strerror(errno) : mpool_strerror(ret)));
}
else {
for (chunk_p = pnt_p->pi_pnt;
chunk_p < (char *)pnt_p->pi_pnt + amount;
chunk_p++) {
if (*chunk_p != '\0') {
(void)printf("calloc of %ld not zeroed on iteration #%d\n",
amount, iter_c + 1);
break;
}
}
pnt_p->pi_size = amount;
}
break;
case 6: case 7: case 8:
if (free_c == max_pointers) {
continue;
}
which = RANDOM_VALUE(max_pointers - free_c);
for (pnt_p = used_p; which > 0; which--) {
pnt_p = pnt_p->pi_next;
}
if (use_malloc_b) {
new_pnt = realloc(pnt_p->pi_pnt, amount);
}
else {
new_pnt = mpool_resize(pool, pnt_p->pi_pnt, amount,
&ret);
}
if (verbose_b) {
(void)printf("%d: resize %#lx from %ld to %ld (max %ld) slot %d. "
"got %#lx\n",
iter_c + 1, (long)pnt_p->pi_pnt, pnt_p->pi_size, amount,
max, pnt_p - pointer_grid, (long)new_pnt);
}
if (new_pnt == NULL) {
(void)printf("resize of %#lx old size %ld new size %ld failed: %s\n",
(long)pnt_p->pi_pnt, pnt_p->pi_size, amount,
(use_malloc_b ? strerror(errno) : mpool_strerror(ret)));
pnt_p->pi_pnt = NULL;
pnt_p->pi_size = 0;
}
else {
/* we effectively freed the old memory */
max += pnt_p->pi_size;
pnt_p->pi_pnt = new_pnt;
pnt_p->pi_size = amount;
}
break;
default:
break;
}
if (pnt_p != NULL && pnt_p->pi_pnt != NULL) {
if (pnt_p == free_p) {
free_p = pnt_p->pi_next;
pnt_p->pi_next = used_p;
used_p = pnt_p;
free_c--;
}
max -= amount;
iter_c++;
}
continue;
}
/*
* choose a rand slot to free and make sure it is not a free-slot
*/
which = RANDOM_VALUE(max_pointers - free_c);
/* find pnt in the used list */
last_p = NULL;
for (pnt_p = used_p, last_p = NULL;
pnt_p != NULL && which > 0;
last_p = pnt_p, pnt_p = pnt_p->pi_next, which--) {
}
if (pnt_p == NULL) {
/* huh? error here */
abort();
}
if (last_p == NULL) {
used_p = pnt_p->pi_next;
}
else {
last_p->pi_next = pnt_p->pi_next;
}
if (use_malloc_b) {
free(pnt_p->pi_pnt);
}
else {
ret = mpool_free(pool, pnt_p->pi_pnt);
if (ret != MPOOL_ERROR_NONE) {
(void)printf("free error on pointer '%#lx' of size %ld: %s\n",
(long)pnt_p->pi_pnt, pnt_p->pi_size,
mpool_strerror(ret));
}
}
if (verbose_b) {
(void)printf("%d: free'd %ld bytes from slot %d (%#lx)\n",
iter_c + 1, pnt_p->pi_size, pnt_p - pointer_grid,
(long)pnt_p->pi_pnt);
}
pnt_p->pi_pnt = NULL;
pnt_p->pi_next = free_p;
free_p = pnt_p;
free_c++;
max += pnt_p->pi_size;
iter_c++;
}
/* free used pointers */
for (pnt_p = pointer_grid; pnt_p < pointer_grid + max_pointers; pnt_p++) {
if (pnt_p->pi_pnt != NULL) {
if (use_malloc_b) {
free(pnt_p->pi_pnt);
}
else {
ret = mpool_free(pool, pnt_p->pi_pnt);
if (ret != MPOOL_ERROR_NONE) {
(void)printf("free error on pointer '%#lx' of size %ld: %s\n",
(long)pnt_p->pi_pnt, pnt_p->pi_size,
mpool_strerror(ret));
}
}
}
}
if (use_malloc_b) {
free(pointer_grid);
}
else {
ret = mpool_free(pool, pointer_grid);
if (ret != MPOOL_ERROR_NONE) {
(void)printf("free error on grid pointer: %s\n", mpool_strerror(ret));
}
}
}
/*
* static void do_interactive
*
* DESCRIPTION:
*
* Run the interactive section of the program.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* pool <-> Out memory pool.
*/
static void do_interactive(mpool_t *pool)
{
int len, ret;
char line[128], *line_p;
void *pnt, *new_pnt;
(void)printf("Mpool test program. Type 'help' for assistance.\n");
for (;;) {
(void)printf("> ");
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
line_p = strchr(line, '\n');
if (line_p != NULL) {
*line_p = '\0';
}
len = strlen(line);
if (len == 0) {
continue;
}
if (strncmp(line, "?", len) == 0
|| strncmp(line, "help", len) == 0) {
(void)printf("\thelp - print this message\n\n");
(void)printf("\tmalloc - allocate memory\n");
(void)printf("\tcalloc - allocate/clear memory\n");
(void)printf("\tresize - resize memory\n");
(void)printf("\tfree - deallocate memory\n\n");
(void)printf("\tclear - clear the pool\n");
(void)printf("\toverwrite - overwrite some memory to test errors\n");
(void)printf("\trandom - randomly execute a number of [de] allocs\n");
(void)printf("\tquit - quit this test program\n");
continue;
}
if (strncmp(line, "quit", len) == 0) {
break;
}
if (strncmp(line, "malloc", len) == 0) {
int size;
(void)printf("How much to malloc: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
size = atoi(line);
pnt = mpool_alloc(pool, size, &ret);
if (pnt == NULL) {
(void)printf("malloc(%d) failed: %s\n", size, mpool_strerror(ret));
}
else {
(void)printf("malloc(%d) returned '%#lx'\n", size, (long)pnt);
}
continue;
}
if (strncmp(line, "calloc", len) == 0) {
int size;
(void)printf("How much to calloc: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
size = atoi(line);
pnt = mpool_calloc(pool, size, sizeof(char), &ret);
if (pnt == NULL) {
(void)printf("calloc(%d) failed: %s\n", size, mpool_strerror(ret));
}
else {
(void)printf("calloc(%d) returned '%#lx'\n", size, (long)pnt);
}
continue;
}
if (strncmp(line, "resize", len) == 0) {
int size, old_size;
pnt = get_address();
(void)printf("Old size of allocation: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
old_size = atoi(line);
(void)printf("New size of allocation: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
size = atoi(line);
new_pnt = mpool_resize(pool, pnt, size, &ret);
if (new_pnt == NULL) {
(void)printf("resize(%#lx, %d) failed: %s\n",
(long)pnt, size, mpool_strerror(ret));
}
else {
(void)printf("resize(%#lx, %d) returned '%#lx'\n",
(long)pnt, size, (long)new_pnt);
}
continue;
}
if (strncmp(line, "free", len) == 0) {
int old_size;
pnt = get_address();
(void)printf("Old minimum size we are freeing: ");
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
old_size = atoi(line);
ret = mpool_free(pool, pnt);
if (ret != MPOOL_ERROR_NONE) {
(void)fprintf(stderr, "free failed: %s\n", mpool_strerror(ret));
}
continue;
}
if (strncmp(line, "clear", len) == 0) {
ret = mpool_clear(pool);
if (ret == MPOOL_ERROR_NONE) {
(void)fprintf(stderr, "clear succeeded\n");
}
else {
(void)fprintf(stderr, "clear failed: %s\n", mpool_strerror(ret));
}
continue;
}
if (strncmp(line, "overwrite", len) == 0) {
char *overwrite = "OVERWRITTEN";
pnt = get_address();
memcpy((char *)pnt, overwrite, strlen(overwrite));
(void)printf("Done.\n");
continue;
}
/* do random heap hits */
if (strncmp(line, "random", len) == 0) {
int iter_n;
(void)printf("How many iterations[%d]: ", default_iter_n);
if (fgets(line, sizeof(line), stdin) == NULL) {
break;
}
if (line[0] == '\0' || line[0] == '\n') {
iter_n = default_iter_n;
}
else {
iter_n = atoi(line);
}
do_random(pool, iter_n);
continue;
}
(void)printf("Unknown command '%s'. Type 'help' for assistance.\n", line);
}
}
/*
* static void log_func
*
* DESCRIPTION:
*
* Mpool transaction log function.
*
* RETURNS:
*
* None.
*
* ARGUMENT:
*
* mp_p -> Associated mpool address.
*
* func_id -> Integer function ID which identifies which mpool
* function is being called.
*
* byte_size -> Optionally specified byte size.
*
* ele_n -> Optionally specified element number. For mpool_calloc
* only.
*
* new_addr -> Optionally specified new address. For mpool_alloc,
* mpool_calloc, and mpool_resize only.
*
* old_addr -> Optionally specified old address. For mpool_resize and
* mpool_free only.
*
* old_byte_size -> Optionally specified old byte size. For
* mpool_resize only.
*/
static void log_func(const void *mp_p, const int func_id,
const unsigned long byte_size,
const unsigned long ele_n,
const void *new_addr, const void *old_addr,
const unsigned long old_byte_size)
{
(void)printf("mp %#lx ", (long)mp_p);
switch (func_id) {
case MPOOL_FUNC_CLOSE:
(void)printf("close\n");
break;
case MPOOL_FUNC_CLEAR:
(void)printf("clear\n");
break;
case MPOOL_FUNC_ALLOC:
(void)printf("alloc %lu bytes got %#lx\n",
byte_size, (long)new_addr);
break;
case MPOOL_FUNC_CALLOC:
(void)printf("calloc %lu ele size, %lu ele number, got %#lx\n",
byte_size, ele_n, (long)new_addr);
break;
case MPOOL_FUNC_FREE:
(void)printf("free %#lx of %lu bytes\n", (long)old_addr, byte_size);
break;
case MPOOL_FUNC_RESIZE:
(void)printf("resize %#lx of %lu bytes to %lu bytes, got %#lx\n",
(long)old_addr, old_byte_size, byte_size,
(long)new_addr);
break;
default:
(void)printf("unknown function %d, %lu bytes\n", func_id, byte_size);
break;
}
}
/*
* static void usage
*
* DESCRIPTION:
*
* Print a usage message.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* None.
*/
static void usage(void)
{
(void)fprintf(stderr,
"Usage: mpool_t [-bhHilMnsv] [-m size] [-p number] "
"[-P size] [-S seed] [-t times]\n");
(void)fprintf(stderr,
" -b set MPOOL_FLAG_BEST_FIT\n"
" -h set MPOOL_FLAG_NO_FREE\n"
" -H use system heap not mpool\n"
" -i turn on interactive mode\n"
" -l log memory transactions\n"
" -M max number pages in mpool\n"
" -n set MPOOL_FLAG_NO_FREE\n"
" -s use sbrk instead of mmap\n"
" -v enable verbose messages\n"
" -m size maximum allocation to test\n"
" -p max-pnts number of pointers to test\n"
" -S seed-rand seed for random function\n"
" -t interations number of iterations to run\n");
exit(1);
}
/*
* static void process_args
*
* DESCRIPTION:
*
* Process our arguments.
*
* RETURNS:
*
* None.
*
* ARGUMENTS:
*
* None.
*/
static void process_args(int argc, char ** argv)
{
argc--, argv++;
/* process the args */
for (; *argv != NULL; argv++, argc--) {
if (**argv != '-') {
continue;
}
switch (*(*argv + 1)) {
case 'b':
best_fit_b = 1;
break;
case 'h':
heavy_pack_b = 1;
break;
case 'H':
use_malloc_b = 1;
break;
case 'i':
interactive_b = 1;
break;
case 'l':
log_trxn_b = 1;
break;
case 'm':
argv++, argc--;
if (argc <= 0) {
usage();
}
max_alloc = atoi(*argv);
break;
case 'M':
max_pages_n = 1;
break;
case 'n':
no_free_b = 1;
break;
case 'p':
argv++, argc--;
if (argc <= 0) {
usage();
}
max_pointers = atoi(*argv);
break;
case 'P':
argv++, argc--;
if (argc <= 0) {
usage();
}
page_size = atoi(*argv);
break;
case 'S':
argv++, argc--;
if (argc <= 0) {
usage();
}
seed_random = atoi(*argv);
break;
case 't':
argv++, argc--;
if (argc <= 0) {
usage();
}
default_iter_n = atoi(*argv);
break;
case 'v':
verbose_b = 1;
break;
default:
usage();
break;
}
}
}
/*
* Main routine
*/
int main(int argc, char **argv)
{
int ret;
unsigned int flags = 0, pool_page_size;
unsigned long num_alloced, user_alloced, max_alloced, tot_alloced;
mpool_t *pool;
process_args(argc, argv);
/* repeat until we get a non 0 seed */
while (seed_random == 0) {
seed_random = time(0) ^ getpid();
}
(void)srandom(seed_random);
(void)printf("Random seed is %u\n", seed_random);
if (best_fit_b) {
flags |= MPOOL_FLAG_BEST_FIT;
}
if (heavy_pack_b) {
flags |= MPOOL_FLAG_HEAVY_PACKING;
}
if (no_free_b) {
flags |= MPOOL_FLAG_NO_FREE;
}
if (use_sbrk_b) {
flags |= MPOOL_FLAG_USE_SBRK;
}
/* open our memory pool */
pool = mpool_open(flags, page_size, NULL, &ret);
if (pool == NULL) {
(void)fprintf(stderr, "Error in mpool_open: %s\n", mpool_strerror(ret));
exit(1);
}
/* are we logging transactions */
if (log_trxn_b) {
ret = mpool_set_log_func(pool, log_func);
if (ret != MPOOL_ERROR_NONE) {
(void)fprintf(stderr, "Error in mpool_set_log_func: %s\n",
mpool_strerror(ret));
}
}
if (max_pages_n > 0) {
ret = mpool_set_max_pages(pool, max_pages_n);
if (ret != MPOOL_ERROR_NONE) {
(void)fprintf(stderr, "Error in mpool_set_max_pages: %s\n",
mpool_strerror(ret));
}
}
if (interactive_b) {
do_interactive(pool);
}
else {
(void)printf("Running %d tests (use -i for interactive)...\n",
default_iter_n);
(void)fflush(stdout);
do_random(pool, default_iter_n);
}
/* get stats from the pool */
ret = mpool_stats(pool, &pool_page_size, &num_alloced, &user_alloced,
&max_alloced, &tot_alloced);
if (ret == MPOOL_ERROR_NONE) {
(void)printf("Pool page size = %d. Number active allocated = %lu\n",
pool_page_size, num_alloced);
(void)printf("User bytes allocated = %lu. Max space allocated = %lu\n",
user_alloced, max_alloced);
(void)printf("Total space allocated = %lu\n", tot_alloced);
}
else {
(void)fprintf(stderr, "Error in mpool_stats: %s\n", mpool_strerror(ret));
}
/* close the pool */
ret = mpool_close(pool);
if (ret != MPOOL_ERROR_NONE) {
(void)fprintf(stderr, "Error in mpool_close: %s\n", mpool_strerror(ret));
exit(1);
}
exit(0);
}

@ -0,0 +1,253 @@
/*
* Copyright (C) 2008 Sourcefire, Inc.
*
* Authors: Alberto Wu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
/* a naive pool allocator */
#if HAVE_CONFIG_H
#include "clamav-config.h"
#endif
#ifdef USE_MPOOL
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#include <sys/mman.h>
#include <stddef.h>
/* #define DEBUGMPOOL */
#ifdef DEBUGMPOOL
#include <stdio.h>
FILE *lfd = NULL;
#define spam(...) fprintf(lfd, __VA_ARGS__)
#else
#define spam
#endif
#include "mpool.h"
//#define MIN_FRAGSIZE 4096 /* 1m2.282s */
//#define MIN_FRAGSIZE 8192 /* 0m46.652s */
//#define MIN_FRAGSIZE 16384 /* 0m8.365s */
//#define MIN_FRAGSIZE 32768 /* 0m3.788s */
//#define MIN_FRAGSIZE 65536 /* 0m2.759s */
//#define MIN_FRAGSIZE 131072 /* 0m2.445s */
#define MIN_FRAGSIZE 262144 /* 0m2.343s */
//#define MIN_FRAGSIZE 524288 /* 0m2.387s */
//#define MIN_FRAGSIZE 1048576 /* 0m2.392s */
//#define MIN_FRAGSIZE 2097152 /* 0m2.402s */
struct FRAG {
struct FRAG *next;
unsigned int sbits;
void *fake;
};
#define FRAG_OVERHEAD (offsetof(struct FRAG, fake))
static unsigned int align_to_pagesize(struct MP *mp, unsigned int size) {
return (size / mp->psize + (size % mp->psize != 0)) * mp->psize;
}
static unsigned int align_to_voidptr(unsigned int size) {
return (size / sizeof(void *) + (size % sizeof(void *) != 0)) * sizeof(void *);
}
/* FIXME: use instead a bit2size table so we can spare bits and offer a better granularity */
static unsigned int to_bits(unsigned int size) {
unsigned int i;
for(i=0; i<32; i++)
if((unsigned int)1<<i >= size) return i;
return i; /* NOTREACHED */
}
static unsigned int from_bits(unsigned int bits) {
return 1<<bits;
}
struct MP *mp_create() {
struct MP mp, *mp_p;
memset(&mp, 0, sizeof(mp));
mp.psize = getpagesize();
#ifdef DEBUGMPOOL
lfd = fopen("mmpool_log", "w");
#endif
mp.fd = open("/dev/zero", O_RDWR, 0);
mp.top = align_to_pagesize(&mp, MIN_FRAGSIZE + 0*sizeof(mp));
mp.mpm.usize = align_to_voidptr(sizeof(struct MPMAP));
mp.mpm.size = mp.top - align_to_voidptr(sizeof(mp));
if ((mp_p = (struct MP *)mmap(NULL, mp.top, PROT_READ | PROT_WRITE, MAP_PRIVATE, mp.fd, 0)) == MAP_FAILED)
return NULL;
memcpy(mp_p, &mp, sizeof(mp));
spam("Map created @ %p->%p - size %u out of %u\n", mp_p, (void*)mp_p + mp.mpm.size, mp.mpm.usize, mp.mpm.size);
return mp_p;
}
void mp_destroy(struct MP *mp) {
struct MPMAP *mpm_next = mp->mpm.next, *mpm;
close(mp->fd);
while((mpm = mpm_next)) {
mpm_next = mpm->next;
munmap((void *)mpm, mpm->size);
}
munmap((void *)mp, mp->mpm.size + align_to_voidptr(sizeof(mp)));
spam("Map destroyed @ %p\n", mp);
}
void mp_flush(struct MP *mp) {
struct MPMAP *mpm_next = mp->mpm.next, *mpm;
while((mpm = mpm_next)) {
mpm_next = mpm->next;
munmap((void *)mpm + align_to_pagesize(mp, mpm->usize), mpm->size - align_to_pagesize(mp, mpm->usize));
mpm->size = mpm->usize = align_to_pagesize(mp, mpm->usize);
}
munmap(&mp->mpm + align_to_pagesize(mp, mp->mpm.usize + align_to_voidptr(sizeof(mp))), mp->mpm.size - align_to_pagesize(mp, mp->mpm.usize + align_to_voidptr(sizeof(mp))));
mp->mpm.size = mp->mpm.usize;
spam("Map flushed @ %p\n", mp);
}
void check_all(struct MP *mp) {
struct MPMAP *mpm = &mp->mpm;
while(mpm) {
volatile unsigned char *c = (unsigned char *)mpm;
unsigned int len = mpm->size;
spam("checking object %p - size %u\n", mpm, len);
while (len--) {
c[len];
}
mpm=mpm->next;
}
}
void *mp_malloc(struct MP *mp, size_t size) {
unsigned int i, j, needed = align_to_voidptr(size + FRAG_OVERHEAD);
const unsigned int sbits = to_bits(needed);
struct FRAG *f = NULL;
struct MPMAP *mpm = &mp->mpm;
// check_all(mp);
if (!size) return NULL;
j = sbits+2;
if (j<7) j = 7;
if (j > 32) j = 32;
for (i=sbits; i<j; i++)
if((f = mp->avail[i])) break;
/* Case 1: We have a free'd frag */
if(f) {
spam("malloc %p size %u (freed)\n"), f, size;
mp->avail[i] = f->next;
return &f->fake;
}
needed = from_bits(sbits);
/* Case 2: We have nuff room available for this frag already */
while(mpm) {
if(mpm->size - mpm->usize >= needed) {
f = (struct FRAG *)((void *)mpm + mpm->usize);
spam("malloc %p size %u (hole)\n", f, size);
mpm->usize += needed;
f->sbits = sbits;
return &f->fake;
}
mpm = mpm->next;
}
/* Case 3: We allocate more */
if (needed + align_to_voidptr(sizeof(*mpm)) > MIN_FRAGSIZE)
i = align_to_pagesize(mp, needed + align_to_voidptr(sizeof(*mpm)));
else
i = align_to_pagesize(mp, MIN_FRAGSIZE);
if ((mpm = (struct MPMAP *)mmap(NULL, i, PROT_READ | PROT_WRITE, MAP_PRIVATE, mp->fd, mp->top)) == MAP_FAILED) {
spam("failed to alloc %u bytes (%u requested)\n", i, size);
return NULL;
}
mp->top += i;
mpm->size = i;
mpm->usize = needed + align_to_voidptr(sizeof(*mpm));
mpm->next = mp->mpm.next;
mp->mpm.next = mpm;
f = (struct FRAG *)((void *)mpm + align_to_voidptr(sizeof(*mpm)));
spam("malloc %p size %u (new map)\n", f, size);
f->sbits = sbits;
return &f->fake;
}
void mp_free(struct MP *mp, void *ptr) {
struct FRAG *f = (struct FRAG *)(ptr - FRAG_OVERHEAD);
if (!ptr) return;
f->next = mp->avail[f->sbits];
mp->avail[f->sbits] = f;
spam("free @ %p\n", f);
}
void *mp_calloc(struct MP *mp, size_t nmemb, size_t size) {
unsigned int needed = nmemb*size;
void *ptr;
if(!needed) return NULL;
if((ptr = mp_malloc(mp, needed)))
memset(ptr, 0, needed);
return ptr;
}
void *mp_realloc(struct MP *mp, void *ptr, size_t size) {
struct FRAG *f = (struct FRAG *)(ptr - FRAG_OVERHEAD);
unsigned int csize;
void *new_ptr;
if (!ptr) return mp_malloc(mp, size);
spam("realloc @ %p (size %u -> %u))\n", f, from_bits(f->sbits), size);
csize = from_bits(f->sbits) - FRAG_OVERHEAD;
if (csize >= size) return ptr;
if (!(new_ptr = mp_malloc(mp, size)))
return NULL;
memcpy(new_ptr, ptr, csize);
mp_free(mp, ptr);
return new_ptr;
}
void *mp_realloc2(struct MP *mp, void *ptr, size_t size) {
struct FRAG *f = (struct FRAG *)(ptr - FRAG_OVERHEAD);
unsigned int csize;
void *new_ptr;
if (!ptr) return mp_malloc(mp, size);
spam("realloc @ %p (size %u -> %u))\n", f, from_bits(f->sbits), size);
csize = from_bits(f->sbits) - FRAG_OVERHEAD;
if (csize >= size) return ptr;
if ((new_ptr = mp_malloc(mp, size)))
memcpy(new_ptr, ptr, csize);
mp_free(mp, ptr);
return new_ptr;
}
#endif /* USE_MPOOL */

@ -0,0 +1,62 @@
/*
* Copyright (C) 2008 Sourcefire, Inc.
*
* Authors: Alberto Wu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#ifndef MPOOL_H
#define MPOOL_H
#ifdef USE_MPOOL
struct MPMAP {
struct MPMAP *next;
unsigned int size;
unsigned int usize;
};
struct MP {
int fd;
unsigned int psize;
unsigned int top;
struct FRAG *avail[32];
struct MPMAP mpm;
};
typedef struct MP mp_t;
mp_t *mp_create(void);
void mp_destroy(mp_t *mp);
void *mp_malloc(mp_t *mp, size_t size);
void mp_free(mp_t *mp, void *ptr);
void *mp_calloc(mp_t *mp, size_t nmemb, size_t size);
void *mp_realloc(mp_t *mp, void *ptr, size_t size);
void *mp_realloc2(mp_t *mp, void *ptr, size_t size);
void mp_flush(mp_t *mp);
#else /* USE_MPOOL */
#define mp_malloc(a, b) cli_malloc(b)
#define mp_free(a, b) free(b)
#define mp_calloc(a, b, c) cli_calloc(b, c)
#define mp_realloc(a, b, c) cli_realloc(b, c)
#define mp_realloc2(a, b, c) cli_realloc2(b, c)
#endif /* USE_MPOOL */
#endif

@ -45,9 +45,7 @@
#include "phish_whitelist.h"
#include "regex_list.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
int whitelist_match(const struct cl_engine* engine,char* real_url,const char* display_url,int hostOnly)
{
@ -59,11 +57,9 @@ int whitelist_match(const struct cl_engine* engine,char* real_url,const char* di
int init_whitelist(struct cl_engine* engine)
{
if(engine) {
engine->whitelist_matcher = (struct regex_matcher *) mp_malloc(engine->mempool, sizeof(struct regex_matcher));
#ifdef USE_MPOOL
engine->whitelist_matcher = (struct regex_matcher *) mpool_alloc(engine->mempool, sizeof(struct regex_matcher), NULL);
((struct regex_matcher *)(engine->whitelist_matcher))->mempool = engine->mempool;
#else
engine->whitelist_matcher = (struct regex_matcher *) cli_malloc(sizeof(struct regex_matcher));
#endif
if(!engine->whitelist_matcher)
return CL_EMEM;
@ -82,11 +78,7 @@ void whitelist_done(struct cl_engine* engine)
{
if(engine && engine->whitelist_matcher) {
regex_list_done(engine->whitelist_matcher);
#ifdef USE_MPOOL
mpool_free(engine->mempool, engine->whitelist_matcher);
#else
free(engine->whitelist_matcher);
#endif
mp_free(engine->mempool, engine->whitelist_matcher);
engine->whitelist_matcher = NULL;
}
}

@ -52,9 +52,7 @@
#include "md5.h"
#include <assert.h>
#ifdef USE_MPOOL
#include "mpool.h"
#endif
#define DOMAIN_REAL 1
#define DOMAIN_DISPLAY 0
@ -861,11 +859,7 @@ int phishing_init(struct cl_engine* engine)
{
struct phishcheck* pchk;
if(!engine->phishcheck) {
#ifdef USE_MPOOL
pchk = engine->phishcheck = mpool_alloc(engine->mempool, sizeof(struct phishcheck), NULL);
#else
pchk = engine->phishcheck = cli_malloc(sizeof(struct phishcheck));
#endif
pchk = engine->phishcheck = mp_malloc(engine->mempool, sizeof(struct phishcheck));
if(!pchk)
return CL_EMEM;
pchk->is_disabled=1;
@ -883,11 +877,7 @@ int phishing_init(struct cl_engine* engine)
cli_dbgmsg("Initializing phishcheck module\n");
if(build_regex(&pchk->preg_numeric,numeric_url_regex,1)) {
#ifdef USE_MPOOL
mpool_free(engine->mempool, pchk);
#else
free(pchk);
#endif
mp_free(engine->mempool, pchk);
engine->phishcheck = NULL;
return CL_EFORMAT;
}
@ -907,11 +897,7 @@ void phishing_done(struct cl_engine* engine)
domainlist_done(engine);
if(pchk) {
cli_dbgmsg("Freeing phishcheck struct\n");
#ifdef USE_MPOOL
mpool_free(engine->mempool, pchk);
#else
free(pchk);
#endif
mp_free(engine->mempool, pchk);
}
cli_dbgmsg("Phishcheck cleaned up\n");
}

@ -67,9 +67,7 @@
#include <stddef.h>
#endif
#ifdef USE_MPOOL
#include "mpool.h"
#endif
#ifdef CL_THREAD_SAFE
# include <pthread.h>
@ -283,11 +281,7 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
}
} else {
#ifdef USE_MPOOL
bm_new = (struct cli_bm_patt *) mpool_calloc(root->mempool, 1, sizeof(struct cli_bm_patt), NULL);
#else
bm_new = (struct cli_bm_patt *) cli_calloc(1, sizeof(struct cli_bm_patt));
#endif
bm_new = (struct cli_bm_patt *) mp_calloc(root->mempool, 1, sizeof(struct cli_bm_patt));
if(!bm_new)
return CL_EMEM;
@ -296,7 +290,7 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
unsigned char *mpoolhexsig = (unsigned char *) cli_hex2str(hexsig);
if(mpoolhexsig) {
unsigned int mpoolhexsigsz = strlen(hexsig) / 2 + 1;
if((bm_new->pattern = mpool_alloc(root->mempool, mpoolhexsigsz, NULL)))
if((bm_new->pattern = mp_malloc(root->mempool, mpoolhexsigsz)))
memcpy(bm_new->pattern, mpoolhexsig, mpoolhexsigsz);
free(mpoolhexsig);
} else bm_new->pattern = NULL;
@ -305,11 +299,7 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
bm_new->pattern = (unsigned char *) cli_hex2str(hexsig);
#endif
if(!bm_new->pattern) {
#ifdef USE_MPOOL
mpool_free(root->mempool, bm_new);
#else
free(bm_new);
#endif
mp_free(root->mempool, bm_new);
return CL_EMALFDB;
}
bm_new->length = strlen(hexsig) / 2;
@ -318,7 +308,7 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
{
char *mpoolvirname = cli_virname((char *) virname, options & CL_DB_OFFICIAL, 0);
if(mpoolvirname) {
if((bm_new->virname = mpool_alloc(root->mempool, strlen(mpoolvirname) + 1, NULL)))
if((bm_new->virname = mp_malloc(root->mempool, strlen(mpoolvirname) + 1)))
strcpy(bm_new->virname, mpoolvirname);
free(mpoolvirname);
} else bm_new->virname = NULL;
@ -327,33 +317,22 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
bm_new->virname = cli_virname((char *) virname, options & CL_DB_OFFICIAL, 0);
#endif
if(!bm_new->virname) {
#ifdef USE_MPOOL
mpool_free(root->mempool, bm_new->pattern);
mpool_free(root->mempool, bm_new);
#else
free(bm_new->pattern);
free(bm_new);
#endif
mp_free(root->mempool, bm_new->pattern);
mp_free(root->mempool, bm_new);
return CL_EMEM;
}
if(offset) {
#ifdef USE_MPOOL
if((bm_new->offset = mpool_alloc(root->mempool, strlen(offset) + 1, NULL)))
if((bm_new->offset = mp_malloc(root->mempool, strlen(offset) + 1)))
strcpy(bm_new->offset, offset);
#else
bm_new->offset = cli_strdup(offset);
#endif
if(!bm_new->offset) {
#ifdef USE_MPOOL
mpool_free(root->mempool, bm_new->pattern);
mpool_free(root->mempool, bm_new->virname);
mpool_free(root->mempool, bm_new);
#else
free(bm_new->pattern);
free(bm_new->virname);
free(bm_new);
#endif
mp_free(root->mempool, bm_new->pattern);
mp_free(root->mempool, bm_new->virname);
mp_free(root->mempool, bm_new);
return CL_EMEM;
}
}
@ -365,15 +344,9 @@ int cli_parse_add(struct cli_matcher *root, const char *virname, const char *hex
if((ret = cli_bm_addpatt(root, bm_new))) {
cli_errmsg("cli_parse_add(): Problem adding signature (4).\n");
#ifdef USE_MPOOL
mpool_free(root->mempool, bm_new->pattern);
mpool_free(root->mempool, bm_new->virname);
mpool_free(root->mempool, bm_new);
#else
free(bm_new->pattern);
free(bm_new->virname);
free(bm_new);
#endif
mp_free(root->mempool, bm_new->pattern);
mp_free(root->mempool, bm_new->virname);
mp_free(root->mempool, bm_new);
return ret;
}
}
@ -398,15 +371,12 @@ int cli_initengine(struct cl_engine **engine, unsigned int options)
(*engine)->refcount = 1;
#ifdef USE_MPOOL
if(!((*engine)->mempool = mpool_open(MPOOL_FLAG_HEAVY_PACKING | MPOOL_FLAG_BEST_FIT, 0, NULL, NULL))) {
if(!((*engine)->mempool = mp_create())) {
cli_errmsg("Can't allocate memory for memory pool!\n");
return CL_EMEM;
}
(*engine)->root = mpool_calloc((*engine)->mempool, CLI_MTARGETS, sizeof(struct cli_matcher *), NULL);
#else
(*engine)->root = cli_calloc(CLI_MTARGETS, sizeof(struct cli_matcher *));
#endif
(*engine)->root = mp_calloc((*engine)->mempool, CLI_MTARGETS, sizeof(struct cli_matcher *));
if(!(*engine)->root) {
/* no need to free previously allocated memory here */
cli_errmsg("Can't allocate memory for roots!\n");
@ -440,11 +410,9 @@ static int cli_initroots(struct cl_engine *engine, unsigned int options)
for(i = 0; i < CLI_MTARGETS; i++) {
if(!engine->root[i]) {
cli_dbgmsg("Initializing engine->root[%d]\n", i);
root = engine->root[i] = (struct cli_matcher *) mp_calloc(engine->mempool, 1, sizeof(struct cli_matcher));
#ifdef USE_MPOOL
root = engine->root[i] = (struct cli_matcher *) mpool_calloc(engine->mempool, 1, sizeof(struct cli_matcher), NULL);
root->mempool = engine->mempool;
#else
root = engine->root[i] = (struct cli_matcher *) cli_calloc(1, sizeof(struct cli_matcher));
#endif
if(!root) {
cli_errmsg("cli_initroots: Can't allocate memory for cli_matcher\n");
@ -867,11 +835,7 @@ static int lsigattribs(char *attribs, struct cli_lsig_tdb *tdb)
switch(apt->type) {
case CLI_TDB_UINT:
off[i] = cnt = tdb->cnt[CLI_TDB_UINT]++;
#ifdef USE_MPOOL
tdb->val = (uint32_t *) mpool_resize2(tdb->mempool, tdb->val, tdb->cnt[CLI_TDB_UINT] * sizeof(uint32_t), NULL);
#else
tdb->val = (uint32_t *) cli_realloc2(tdb->val, tdb->cnt[CLI_TDB_UINT] * sizeof(uint32_t));
#endif
tdb->val = (uint32_t *) mp_realloc2(tdb->mempool, tdb->val, tdb->cnt[CLI_TDB_UINT] * sizeof(uint32_t));
if(!tdb->val) {
tdb->cnt[CLI_TDB_UINT] = 0;
return -1;
@ -887,11 +851,7 @@ static int lsigattribs(char *attribs, struct cli_lsig_tdb *tdb)
*pt2++ = 0;
off[i] = cnt = tdb->cnt[CLI_TDB_RANGE];
tdb->cnt[CLI_TDB_RANGE] += 2;
#ifdef USE_MPOOL
tdb->range = (uint32_t *) mpool_resize2(tdb->mempool, tdb->range, tdb->cnt[CLI_TDB_RANGE] * sizeof(uint32_t), NULL);
#else
tdb->range = (uint32_t *) cli_realloc2(tdb->range, tdb->cnt[CLI_TDB_RANGE] * sizeof(uint32_t));
#endif
tdb->range = (uint32_t *) mp_realloc2(tdb->mempool, tdb->range, tdb->cnt[CLI_TDB_RANGE] * sizeof(uint32_t));
if(!tdb->range) {
tdb->cnt[CLI_TDB_RANGE] = 0;
return -1;
@ -907,11 +867,7 @@ static int lsigattribs(char *attribs, struct cli_lsig_tdb *tdb)
}
off[i] = cnt = tdb->cnt[CLI_TDB_RANGE];
tdb->cnt[CLI_TDB_RANGE] += 3;
#ifdef USE_MPOOL
tdb->range = (uint32_t *) mpool_resize2(tdb->mempool, tdb->range, tdb->cnt[CLI_TDB_RANGE] * sizeof(uint32_t), NULL);
#else
tdb->range = (uint32_t *) cli_realloc2(tdb->range, tdb->cnt[CLI_TDB_RANGE] * sizeof(uint32_t));
#endif
tdb->range = (uint32_t *) mp_realloc2(tdb->mempool, tdb->range, tdb->cnt[CLI_TDB_RANGE] * sizeof(uint32_t));
if(!tdb->range) {
tdb->cnt[CLI_TDB_RANGE] = 0;
return -1;
@ -928,11 +884,7 @@ static int lsigattribs(char *attribs, struct cli_lsig_tdb *tdb)
case CLI_TDB_STR:
off[i] = cnt = tdb->cnt[CLI_TDB_STR];
tdb->cnt[CLI_TDB_STR] += strlen(pt) + 1;
#ifdef USE_MPOOL
tdb->str = (char *) mpool_resize2(tdb->mempool, tdb->str, tdb->cnt[CLI_TDB_STR] * sizeof(char), NULL);
#else
tdb->str = (char *) cli_realloc2(tdb->str, tdb->cnt[CLI_TDB_STR] * sizeof(char));
#endif
tdb->str = (char *) mp_realloc2(tdb->mempool, tdb->str, tdb->cnt[CLI_TDB_STR] * sizeof(char));
if(!tdb->str) {
cli_errmsg("lsigattribs: Can't allocate memory for tdb->str\n");
return -1;
@ -974,24 +926,14 @@ static int lsigattribs(char *attribs, struct cli_lsig_tdb *tdb)
return 0;
}
#ifdef USE_MPOOL
#define FREE_TDB(x) do { \
if(x.cnt[CLI_TDB_UINT]) \
mpool_free(x.mempool, x.val); \
mp_free(x.mempool, x.val); \
if(x.cnt[CLI_TDB_RANGE]) \
mpool_free(x.mempool, x.range); \
mp_free(x.mempool, x.range); \
if(x.cnt[CLI_TDB_STR]) \
mpool_free(x.mempool, x.str); \
mp_free(x.mempool, x.str); \
} while(0);
#else
#define FREE_TDB(x) \
if(x.cnt[CLI_TDB_UINT]) \
free(x.val); \
if(x.cnt[CLI_TDB_RANGE]) \
free(x.range); \
if(x.cnt[CLI_TDB_STR]) \
free(x.str);
#endif
#define LDB_TOKENS 67
static int cli_loadldb(FILE *fs, struct cl_engine **engine, unsigned int *signo, unsigned int options, struct cli_dbio *dbio, const char *dbname)
@ -1093,11 +1035,8 @@ static int cli_loadldb(FILE *fs, struct cl_engine **engine, unsigned int *signo,
}
root = (*engine)->root[tdb.target[0]];
#ifdef USE_MPOOL
lsig = (struct cli_ac_lsig *) mpool_calloc((*engine)->mempool, 1, sizeof(struct cli_ac_lsig), NULL);
#else
lsig = (struct cli_ac_lsig *) cli_calloc(1, sizeof(struct cli_ac_lsig));
#endif
lsig = (struct cli_ac_lsig *) mp_calloc((*engine)->mempool, 1, sizeof(struct cli_ac_lsig));
if(!lsig) {
cli_errmsg("cli_loadldb: Can't allocate memory for lsig\n");
FREE_TDB(tdb);
@ -1105,7 +1044,7 @@ static int cli_loadldb(FILE *fs, struct cl_engine **engine, unsigned int *signo,
break;
}
#ifdef USE_MPOOL
if((lsig->logic = mpool_alloc((*engine)->mempool, strlen(logic)+1, NULL)))
if((lsig->logic = mp_malloc((*engine)->mempool, strlen(logic)+1)))
strcpy(lsig->logic, logic);
#else
lsig->logic = cli_strdup(logic);
@ -1114,11 +1053,7 @@ static int cli_loadldb(FILE *fs, struct cl_engine **engine, unsigned int *signo,
cli_errmsg("cli_loadldb: Can't allocate memory for lsig->logic\n");
FREE_TDB(tdb);
ret = CL_EMEM;
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, lsig);
#else
free(lsig);
#endif
mp_free((*engine)->mempool, lsig);
break;
}
@ -1126,20 +1061,12 @@ static int cli_loadldb(FILE *fs, struct cl_engine **engine, unsigned int *signo,
memcpy(&lsig->tdb, &tdb, sizeof(tdb));
root->ac_lsigs++;
#ifdef USE_MPOOL
newtable = (struct cli_ac_lsig **) mpool_resize((*engine)->mempool, root->ac_lsigtable, root->ac_lsigs * sizeof(struct cli_ac_lsig *), NULL);
#else
newtable = (struct cli_ac_lsig **) cli_realloc(root->ac_lsigtable, root->ac_lsigs * sizeof(struct cli_ac_lsig *));
#endif
newtable = (struct cli_ac_lsig **) mp_realloc((*engine)->mempool, root->ac_lsigtable, root->ac_lsigs * sizeof(struct cli_ac_lsig *));
if(!newtable) {
root->ac_lsigs--;
cli_errmsg("cli_loadldb: Can't realloc root->ac_lsigtable\n");
FREE_TDB(tdb);
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, lsig);
#else
free(lsig);
#endif
mp_free((*engine)->mempool, lsig);
ret = CL_EMEM;
break;
}
@ -1264,11 +1191,7 @@ static int cli_loadftm(FILE *fs, struct cl_engine **engine, unsigned int options
break;
} else if(atoi(tokens[0]) == 0) { /* memcmp() */
#ifdef USE_MPOOL
new = (struct cli_ftype *) mpool_alloc((*engine)->mempool, sizeof(struct cli_ftype), NULL);
#else
new = (struct cli_ftype *) cli_malloc(sizeof(struct cli_ftype));
#endif
new = (struct cli_ftype *) mp_malloc((*engine)->mempool, sizeof(struct cli_ftype));
if(!new) {
ret = CL_EMEM;
break;
@ -1280,7 +1203,7 @@ static int cli_loadftm(FILE *fs, struct cl_engine **engine, unsigned int options
unsigned char *mpoolmagic = cli_hex2str(tokens[2]);
if(mpoolmagic) {
unsigned int mpoolmagicsz = strlen(tokens[2]) / 2 + 1;
if((new->magic = mpool_alloc((*engine)->mempool, mpoolmagicsz, NULL)))
if((new->magic = mp_malloc((*engine)->mempool, mpoolmagicsz)))
memcpy(new->magic, mpoolmagic, mpoolmagicsz);
free(mpoolmagic);
} else new->magic = NULL;
@ -1291,28 +1214,19 @@ static int cli_loadftm(FILE *fs, struct cl_engine **engine, unsigned int options
if(!new->magic) {
cli_errmsg("cli_loadftm: Can't decode the hex string\n");
ret = CL_EMALFDB;
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new);
#else
free(new);
#endif
mp_free((*engine)->mempool, new);
break;
}
new->length = strlen(tokens[2]) / 2;
#ifdef USE_MPOOL
if((new->tname = mpool_alloc((*engine)->mempool, strlen(tokens[3])+1, NULL)))
if((new->tname = mp_malloc((*engine)->mempool, strlen(tokens[3])+1)))
strcpy(new->tname, tokens[3]);
#else
new->tname = cli_strdup(tokens[3]);
#endif
if(!new->tname) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->magic);
mpool_free((*engine)->mempool, new);
#else
free(new->magic);
free(new);
#endif
mp_free((*engine)->mempool, new->magic);
mp_free((*engine)->mempool, new);
ret = CL_EMEM;
break;
}
@ -1367,11 +1281,7 @@ static int cli_loadign(FILE *fs, struct cl_engine **engine, unsigned int options
while(cli_dbgets(buffer, FILEBUFF, fs, dbio)) {
line++;
cli_chomp(buffer);
#ifdef USE_MPOOL
new = (struct cli_ignsig *) mpool_calloc((*engine)->mempool, 1, sizeof(struct cli_ignsig), NULL);
#else
new = (struct cli_ignsig *) cli_calloc(1, sizeof(struct cli_ignsig));
#endif
new = (struct cli_ignsig *) mp_calloc((*engine)->mempool, 1, sizeof(struct cli_ignsig));
if(!new) {
ret = CL_EMEM;
break;
@ -1381,7 +1291,7 @@ static int cli_loadign(FILE *fs, struct cl_engine **engine, unsigned int options
{
unsigned char *mpooldbname = cli_strtok(buffer, 0, ":");
if(mpooldbname) {
if((new->dbname = mpool_alloc((*engine)->mempool, strlen(mpooldbname) + 1, NULL)))
if((new->dbname = mp_malloc((*engine)->mempool, strlen(mpooldbname) + 1)))
strcpy(new->dbname, mpooldbname);
free(mpooldbname);
} else new->dbname = NULL;
@ -1390,23 +1300,14 @@ static int cli_loadign(FILE *fs, struct cl_engine **engine, unsigned int options
new->dbname = cli_strtok(buffer, 0, ":");
#endif
if(!new->dbname) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new);
#else
free(new);
#endif
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
}
if(!(pt = cli_strtok(buffer, 1, ":"))) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->dbname);
mpool_free((*engine)->mempool, new);
#else
free(new->dbname);
free(new);
#endif
mp_free((*engine)->mempool, new->dbname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1421,7 +1322,7 @@ static int cli_loadign(FILE *fs, struct cl_engine **engine, unsigned int options
{
unsigned char *mpoolsigname = cli_strtok(buffer, 2, ":");
if(mpoolsigname) {
if((new->signame = mpool_alloc((*engine)->mempool, strlen(mpoolsigname) + 1, NULL)))
if((new->signame = mp_malloc((*engine)->mempool, strlen(mpoolsigname) + 1)))
strcpy(new->signame, mpoolsigname);
free(mpoolsigname);
} else new->signame = NULL;
@ -1430,13 +1331,8 @@ static int cli_loadign(FILE *fs, struct cl_engine **engine, unsigned int options
new->signame = cli_strtok(buffer, 2, ":");
#endif
if(!new->signame) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->dbname);
mpool_free((*engine)->mempool, new);
#else
free(new->dbname);
free(new);
#endif
mp_free((*engine)->mempool, new->dbname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
}
@ -1467,15 +1363,9 @@ static void cli_freeign(struct cl_engine *engine)
while(ignored->list) {
pt = ignored->list;
ignored->list = ignored->list->next;
#ifdef USE_MPOOL
mpool_free(engine->mempool, pt->dbname);
mpool_free(engine->mempool, pt->signame);
mpool_free(engine->mempool,pt);
#else
free(pt->dbname);
free(pt->signame);
free(pt);
#endif
mp_free(engine->mempool, pt->dbname);
mp_free(engine->mempool, pt->signame);
mp_free(engine->mempool,pt);
}
hashset_destroy(&ignored->hs);
free(engine->ignored);
@ -1499,23 +1389,11 @@ static int cli_md5db_init(struct cl_engine **engine, unsigned int mode)
if(mode == MD5_HDB) {
#ifdef USE_MPOOL
bm = (*engine)->md5_hdb = (struct cli_matcher *) mpool_calloc((*engine)->mempool, sizeof(struct cli_matcher), 1, NULL);
#else
bm = (*engine)->md5_hdb = (struct cli_matcher *) cli_calloc(sizeof(struct cli_matcher), 1);
#endif
bm = (*engine)->md5_hdb = (struct cli_matcher *) mp_calloc((*engine)->mempool, sizeof(struct cli_matcher), 1);
} else if(mode == MD5_MDB) {
#ifdef USE_MPOOL
bm = (*engine)->md5_mdb = (struct cli_matcher *) mpool_calloc((*engine)->mempool, sizeof(struct cli_matcher), 1, NULL);
#else
bm = (*engine)->md5_mdb = (struct cli_matcher *) cli_calloc(sizeof(struct cli_matcher), 1);
#endif
bm = (*engine)->md5_mdb = (struct cli_matcher *) mp_calloc((*engine)->mempool, sizeof(struct cli_matcher), 1);
} else {
#ifdef USE_MPOOL
bm = (*engine)->md5_fp = (struct cli_matcher *) mpool_calloc((*engine)->mempool, sizeof(struct cli_matcher), 1, NULL);
#else
bm = (*engine)->md5_fp = (struct cli_matcher *) cli_calloc(sizeof(struct cli_matcher), 1);
#endif
bm = (*engine)->md5_fp = (struct cli_matcher *) mp_calloc((*engine)->mempool, sizeof(struct cli_matcher), 1);
}
if(!bm)
@ -1578,22 +1456,14 @@ static int cli_loadmd5(FILE *fs, struct cl_engine **engine, unsigned int *signo,
if((*engine)->ignored && cli_chkign((*engine)->ignored, dbname, line, pt))
continue;
#ifdef USE_MPOOL
new = (struct cli_bm_patt *) mpool_calloc((*engine)->mempool, 1, sizeof(struct cli_bm_patt), NULL);
#else
new = (struct cli_bm_patt *) cli_calloc(1, sizeof(struct cli_bm_patt));
#endif
new = (struct cli_bm_patt *) mp_calloc((*engine)->mempool, 1, sizeof(struct cli_bm_patt));
if(!new) {
ret = CL_EMEM;
break;
}
if(!(pt = tokens[md5_field])) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new);
#else
free(new);
#endif
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
}
@ -1602,14 +1472,14 @@ static int cli_loadmd5(FILE *fs, struct cl_engine **engine, unsigned int *signo,
if(strlen(pt) == 32) {
unsigned char *mpoolhex = (unsigned char *) cli_hex2str(pt);
if(mpoolhex) {
if((new->pattern = mpool_alloc((*engine)->mempool, 17, NULL)))
if((new->pattern = mp_malloc((*engine)->mempool, 17)))
memcpy(new->pattern, mpoolhex, 17);
free(mpoolhex);
} else new->pattern = NULL;
} else new->pattern = NULL;
if(new->pattern == NULL) {
cli_errmsg("cli_loadmd5: Malformed MD5 string at line %u\n", line);
mpool_free((*engine)->mempool, new);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
}
@ -1624,13 +1494,8 @@ static int cli_loadmd5(FILE *fs, struct cl_engine **engine, unsigned int *signo,
new->length = 16;
if(!(pt = tokens[size_field])) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->pattern);
mpool_free((*engine)->mempool, new);
#else
free(new->pattern);
free(new);
#endif
mp_free((*engine)->mempool, new->pattern);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
}
@ -1640,7 +1505,7 @@ static int cli_loadmd5(FILE *fs, struct cl_engine **engine, unsigned int *signo,
{
char *mpoolvname = cli_virname((char *) tokens[2], options & CL_DB_OFFICIAL, 0);
if(mpoolvname) {
if((new->virname = mpool_alloc((*engine)->mempool, strlen(mpoolvname) + 1, NULL)))
if((new->virname = mp_malloc((*engine)->mempool, strlen(mpoolvname) + 1)))
strcpy(new->virname, mpoolvname);
free(mpoolvname);
} else new->virname = NULL;
@ -1649,29 +1514,17 @@ static int cli_loadmd5(FILE *fs, struct cl_engine **engine, unsigned int *signo,
new->virname = cli_virname((char *) tokens[2], options & CL_DB_OFFICIAL, 0);
#endif
if(!new->virname) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->pattern);
mpool_free((*engine)->mempool, new);
#else
free(new->pattern);
free(new);
#endif
mp_free((*engine)->mempool, new->pattern);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
}
MD5_DB;
if(!db && (ret = cli_md5db_init(engine, mode))) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->pattern);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
free(new->pattern);
free(new->virname);
free(new);
#endif
mp_free((*engine)->mempool, new->pattern);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
break;
} else {
MD5_DB;
@ -1679,16 +1532,9 @@ static int cli_loadmd5(FILE *fs, struct cl_engine **engine, unsigned int *signo,
if((ret = cli_bm_addpatt(db, new))) {
cli_errmsg("cli_loadmd5: Error adding BM pattern\n");
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->pattern);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
free(new->pattern);
free(new->virname);
free(new);
#endif
mp_free((*engine)->mempool, new->pattern);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
break;
}
@ -1740,11 +1586,7 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
cli_chomp(buffer);
#ifdef USE_MPOOL
new = (struct cli_meta_node *) mpool_calloc((*engine)->mempool, 1, sizeof(struct cli_meta_node), NULL);
#else
new = (struct cli_meta_node *) cli_calloc(1, sizeof(struct cli_meta_node));
#endif
new = (struct cli_meta_node *) mp_calloc((*engine)->mempool, 1, sizeof(struct cli_meta_node));
if(!new) {
ret = CL_EMEM;
break;
@ -1754,7 +1596,7 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
{
char *mpoolvname = cli_virname(cli_strtok(buffer, 0, ":"), options & CL_DB_OFFICIAL, 1);
if(mpoolvname) {
if((new->virname = mpool_alloc((*engine)->mempool, strlen(mpoolvname) + 1, NULL)))
if((new->virname = mp_malloc((*engine)->mempool, strlen(mpoolvname) + 1)))
strcpy(new->virname, mpoolvname);
free(mpoolvname);
} else new->virname = NULL;
@ -1763,34 +1605,20 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
new->virname = cli_virname(cli_strtok(buffer, 0, ":"), options & CL_DB_OFFICIAL, 1);
#endif
if(!new->virname) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new);
#else
free(new);
#endif
mp_free((*engine)->mempool, new);
ret = CL_EMEM;
break;
}
if((*engine)->ignored && cli_chkign((*engine)->ignored, dbname, line, new->virname)) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
free(new->virname);
free(new);
#endif
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
continue;
}
if(!(pt = cli_strtok(buffer, 1, ":"))) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
free(new->virname);
free(new);
#endif
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1802,7 +1630,7 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
{
char *mpoolfname = cli_strtok(buffer, 2, ":");
if(mpoolfname) {
if((new->filename = mpool_alloc((*engine)->mempool, strlen(mpoolfname) + 1, NULL)))
if((new->filename = mp_malloc((*engine)->mempool, strlen(mpoolfname) + 1)))
strcpy(new->filename, mpoolfname);
free(mpoolfname);
} else new->filename = NULL;
@ -1811,36 +1639,21 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
new->filename = cli_strtok(buffer, 2, ":");
#endif
if(!new->filename) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
free(new->virname);
free(new);
#endif
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
if(!strcmp(new->filename, "*")) {
#ifdef USE_MPOOL
mpool_free((*engine)->mempool, new->filename);
#else
free(new->filename);
#endif
mp_free((*engine)->mempool, new->filename);
new->filename = NULL;
}
}
if(!(pt = cli_strtok(buffer, 3, ":"))) {
#ifdef USE_MPOOL
if(new->filename) mpool_free((*engine)->mempool, new->filename);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
if(new->filename) free(new->filename);
free(new->virname);
free(new);
#endif
if(new->filename) mp_free((*engine)->mempool, new->filename);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1852,15 +1665,9 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
}
if(!(pt = cli_strtok(buffer, 4, ":"))) {
#ifdef USE_MPOOL
if(new->filename) mpool_free((*engine)->mempool, new->filename);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
if(new->filename) free(new->filename);
free(new->virname);
free(new);
#endif
if(new->filename) mp_free((*engine)->mempool, new->filename);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1872,15 +1679,9 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
}
if(!(pt = cli_strtok(buffer, 5, ":"))) {
#ifdef USE_MPOOL
if(new->filename) mpool_free((*engine)->mempool, new->filename);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
if(new->filename) free(new->filename);
free(new->virname);
free(new);
#endif
if(new->filename) mp_free((*engine)->mempool, new->filename);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1898,15 +1699,9 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
}
if(!(pt = cli_strtok(buffer, 6, ":"))) {
#ifdef USE_MPOOL
if(new->filename) mpool_free((*engine)->mempool, new->filename);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
if(new->filename) free(new->filename);
free(new->virname);
free(new);
#endif
if(new->filename) mp_free((*engine)->mempool, new->filename);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1918,15 +1713,9 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
}
if(!(pt = cli_strtok(buffer, 7, ":"))) {
#ifdef USE_MPOOL
if(new->filename) mpool_free((*engine)->mempool, new->filename);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
if(new->filename) free(new->filename);
free(new->virname);
free(new);
#endif
if(new->filename) mp_free((*engine)->mempool, new->filename);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -1938,15 +1727,9 @@ static int cli_loadmd(FILE *fs, struct cl_engine **engine, unsigned int *signo,
}
if(!(pt = cli_strtok(buffer, 8, ":"))) {
#ifdef USE_MPOOL
if(new->filename) mpool_free((*engine)->mempool, new->filename);
mpool_free((*engine)->mempool, new->virname);
mpool_free((*engine)->mempool, new);
#else
if(new->filename) free(new->filename);
free(new->virname);
free(new);
#endif
if(new->filename) mp_free((*engine)->mempool, new->filename);
mp_free((*engine)->mempool, new->virname);
mp_free((*engine)->mempool, new);
ret = CL_EMALFDB;
break;
} else {
@ -2487,9 +2270,6 @@ void cl_free(struct cl_engine *engine)
#ifdef CL_THREAD_SAFE
pthread_mutex_unlock(&cli_ref_mutex);
#endif
#ifdef USE_MPOOL
#define free(X) mpool_free(engine->mempool, X)
#endif
if(engine->root) {
for(i = 0; i < CLI_MTARGETS; i++) {
@ -2499,71 +2279,70 @@ void cl_free(struct cl_engine *engine)
cli_ac_free(root);
if(root->ac_lsigtable) {
for(j = 0; j < root->ac_lsigs; j++) {
free(root->ac_lsigtable[j]->logic);
mp_free(engine->mempool, root->ac_lsigtable[j]->logic);
FREE_TDB(root->ac_lsigtable[j]->tdb);
free(root->ac_lsigtable[j]);
mp_free(engine->mempool, root->ac_lsigtable[j]);
}
free(root->ac_lsigtable);
mp_free(engine->mempool, root->ac_lsigtable);
}
free(root);
mp_free(engine->mempool, root);
}
}
free(engine->root);
mp_free(engine->mempool, engine->root);
}
if((root = engine->md5_hdb)) {
cli_bm_free(root);
free(root);
mp_free(engine->mempool, root);
}
if((root = engine->md5_mdb)) {
cli_bm_free(root);
free(root->soff);
mp_free(engine->mempool, root->soff);
if(root->md5_sizes_hs.capacity) {
hashset_destroy(&root->md5_sizes_hs);
}
free(root);
mp_free(engine->mempool, root);
}
if((root = engine->md5_fp)) {
cli_bm_free(root);
free(root);
mp_free(engine->mempool, root);
}
metapt = engine->zip_mlist;
while(metapt) {
metah = metapt;
metapt = metapt->next;
free(metah->virname);
mp_free(engine->mempool, metah->virname);
if(metah->filename)
free(metah->filename);
free(metah);
mp_free(engine->mempool, metah->filename);
mp_free(engine->mempool, metah);
}
metapt = engine->rar_mlist;
while(metapt) {
metah = metapt;
metapt = metapt->next;
free(metah->virname);
mp_free(engine->mempool, metah->virname);
if(metah->filename)
free(metah->filename);
free(metah);
mp_free(engine->mempool, metah->filename);
mp_free(engine->mempool, metah);
}
if(((struct cli_dconf *) engine->dconf)->phishing & PHISHING_CONF_ENGINE)
phishing_done(engine);
if(engine->dconf)
free(engine->dconf);
mp_free(engine->mempool, engine->dconf);
if(engine->pua_cats)
free(engine->pua_cats);
mp_free(engine->mempool, engine->pua_cats);
cli_ftfree(engine);
cli_freeign(engine);
#ifdef USE_MPOOL
#undef free(X)
if(engine->mempool) mpool_close(engine->mempool);
#endif /* USE_MPOOL */
if(engine->mempool) mp_destroy(engine->mempool);
#endif
free(engine);
}
@ -2577,7 +2356,7 @@ static void cli_md5db_build(struct cli_matcher* root)
{
uint32_t *mpoolht;
unsigned int mpoolhtsz = root->md5_sizes_hs.count * sizeof(*mpoolht);
root->soff = mpool_alloc(root->mempool, mpoolhtsz, NULL);
root->soff = mp_malloc(root->mempool, mpoolhtsz);
root->soff_len = hashset_toarray(&root->md5_sizes_hs, &mpoolht);
memcpy(root->soff, mpoolht, mpoolhtsz);
free(mpoolht);

@ -58,9 +58,7 @@
#include "jsparse/textbuf.h"
#include "regex_suffix.h"
#ifdef USE_MPOOL
#include "mpool.h"
#endif
/* Prototypes */
static regex_t *new_preg(struct regex_matcher *matcher);
@ -348,7 +346,7 @@ int regex_list_match(struct regex_matcher* matcher,char* real_url,const char* di
int init_regex_list(struct regex_matcher* matcher)
{
#ifdef USE_MPOOL
mpool_t *mp = matcher->mempool;
mp_t *mp = matcher->mempool;
#endif
int rc;
@ -595,17 +593,9 @@ void regex_list_done(struct regex_matcher* matcher)
for(i=0;i<matcher->regex_cnt;i++) {
regex_t *r = matcher->all_pregs[i];
cli_regfree(r);
#ifdef USE_MPOOL
mpool_free(matcher->mempool, r);
#else
free(r);
#endif
mp_free(matcher->mempool, r);
}
#ifdef USE_MPOOL
mpool_free(matcher->mempool, matcher->all_pregs);
#else
free(matcher->all_pregs);
#endif
mp_free(matcher->mempool, matcher->all_pregs);
}
hashtab_free(&matcher->suffix_hash);
cli_bm_free(&matcher->md5_hashes);
@ -621,11 +611,7 @@ int is_regex_ok(struct regex_matcher* matcher)
static int add_newsuffix(struct regex_matcher *matcher, struct regex_list *info, const char *suffix, size_t len)
{
struct cli_matcher *root = &matcher->suffixes;
#ifdef USE_MPOOL
struct cli_ac_patt *new = mpool_calloc(matcher->mempool,1,sizeof(*new),NULL);
#else
struct cli_ac_patt *new = cli_calloc(1,sizeof(*new));
#endif
struct cli_ac_patt *new = mp_calloc(matcher->mempool,1,sizeof(*new));
size_t i;
int ret;
@ -647,17 +633,9 @@ static int add_newsuffix(struct regex_matcher *matcher, struct regex_list *info,
if(new->length > root->maxpatlen)
root->maxpatlen = new->length;
#ifdef USE_MPOOL
new->pattern = mpool_alloc(matcher->mempool, sizeof(new->pattern[0])*len, NULL);
#else
new->pattern = cli_malloc(sizeof(new->pattern[0])*len);
#endif
new->pattern = mp_malloc(matcher->mempool, sizeof(new->pattern[0])*len);
if(!new->pattern) {
#ifdef USE_MPOOL
mpool_free(matcher->mempool, new);
#else
free(new);
#endif
mp_free(matcher->mempool, new);
return CL_EMEM;
}
for(i=0;i<len;i++)
@ -666,13 +644,8 @@ static int add_newsuffix(struct regex_matcher *matcher, struct regex_list *info,
new->customdata = info;
new->virname = NULL;
if((ret = cli_ac_addpatt(root,new))) {
#ifdef USE_MPOOL
mpool_free(matcher->mempool, new->pattern);
mpool_free(matcher->mempool, new);
#else
free(new->pattern);
free(new);
#endif
mp_free(matcher->mempool, new->pattern);
mp_free(matcher->mempool, new);
return ret;
}
SO_preprocess_add(&matcher->filter, (const unsigned char*)suffix, len);
@ -743,18 +716,10 @@ static size_t reverse_string(char *pattern)
static regex_t *new_preg(struct regex_matcher *matcher)
{
regex_t *r;
#ifdef USE_MPOOL
matcher->all_pregs = mpool_resize(matcher->mempool, matcher->all_pregs, ++matcher->regex_cnt * sizeof(*matcher->all_pregs), NULL);
#else
matcher->all_pregs = cli_realloc(matcher->all_pregs, ++matcher->regex_cnt * sizeof(*matcher->all_pregs));
#endif
matcher->all_pregs = mp_realloc(matcher->mempool, matcher->all_pregs, ++matcher->regex_cnt * sizeof(*matcher->all_pregs));
if(!matcher->all_pregs)
return NULL;
#ifdef USE_MPOOL
r = mpool_alloc(matcher->mempool, sizeof(*r), NULL);
#else
r = cli_malloc(sizeof(*r));
#endif
r = mp_malloc(matcher->mempool, sizeof(*r));
if(!r)
return NULL;
matcher->all_pregs[matcher->regex_cnt-1] = r;

@ -29,9 +29,7 @@
#include "matcher.h"
#include <zlib.h> /* for gzFile */
#ifdef USE_MPOOL
#include "mpool.h"
#endif
struct filter {
uint32_t B[65536];
@ -56,7 +54,7 @@ struct regex_matcher {
struct filter md5_filter;
struct filter filter;
#ifdef USE_MPOOL
mpool_t *mempool;
mp_t *mempool;
#endif
int list_inited:2;
int list_loaded:2;

Loading…
Cancel
Save