...
|
...
|
@@ -366,12 +366,12 @@ static const unsigned int fragsz[] = {
|
366
|
366
|
|
367
|
367
|
struct MPMAP {
|
368
|
368
|
struct MPMAP *next;
|
369
|
|
- unsigned int size;
|
370
|
|
- unsigned int usize;
|
|
369
|
+ size_t size;
|
|
370
|
+ size_t usize;
|
371
|
371
|
};
|
372
|
372
|
|
373
|
373
|
struct MP {
|
374
|
|
- unsigned int psize;
|
|
374
|
+ size_t psize;
|
375
|
375
|
struct FRAG *avail[FRAGSBITS];
|
376
|
376
|
union {
|
377
|
377
|
struct MPMAP mpm;
|
...
|
...
|
@@ -397,23 +397,23 @@ struct FRAG {
|
397
|
397
|
};
|
398
|
398
|
#define FRAG_OVERHEAD (offsetof(struct FRAG, u.a.fake))
|
399
|
399
|
|
400
|
|
-static unsigned int align_to_pagesize(struct MP *mp, unsigned int size) {
|
|
400
|
+static size_t align_to_pagesize(struct MP *mp, size_t size) {
|
401
|
401
|
return (size / mp->psize + (size % mp->psize != 0)) * mp->psize;
|
402
|
402
|
}
|
403
|
403
|
|
404
|
|
-static unsigned int to_bits(unsigned int size) {
|
|
404
|
+static unsigned int to_bits(size_t size) {
|
405
|
405
|
unsigned int i;
|
406
|
406
|
for(i=0; i<FRAGSBITS; i++)
|
407
|
407
|
if(fragsz[i] >= size) return i;
|
408
|
408
|
return FRAGSBITS;
|
409
|
409
|
}
|
410
|
410
|
|
411
|
|
-static unsigned int from_bits(unsigned int bits) {
|
|
411
|
+static size_t from_bits(unsigned int bits) {
|
412
|
412
|
if (bits >= FRAGSBITS) return 0;
|
413
|
413
|
return fragsz[bits];
|
414
|
414
|
}
|
415
|
415
|
|
416
|
|
-static inline unsigned int alignof(unsigned int size)
|
|
416
|
+static inline unsigned int alignof(size_t size)
|
417
|
417
|
{
|
418
|
418
|
/* conservative estimate of alignment.
|
419
|
419
|
* A struct that needs alignment of 'align' is padded by the compiler
|
...
|
...
|
@@ -446,7 +446,7 @@ static inline size_t alignto(size_t p, size_t size)
|
446
|
446
|
|
447
|
447
|
struct MP *mpool_create() {
|
448
|
448
|
struct MP mp, *mpool_p;
|
449
|
|
- unsigned int sz;
|
|
449
|
+ size_t sz;
|
450
|
450
|
memset(&mp, 0, sizeof(mp));
|
451
|
451
|
mp.psize = cli_getpagesize();
|
452
|
452
|
sz = align_to_pagesize(&mp, MIN_FRAGSIZE);
|
...
|
...
|
@@ -470,13 +470,13 @@ struct MP *mpool_create() {
|
470
|
470
|
memset(mpool_p, ALLOCPOISON, sz);
|
471
|
471
|
#endif
|
472
|
472
|
memcpy(mpool_p, &mp, sizeof(mp));
|
473
|
|
- spam("Map created @%p->%p - size %u out of %u - voidptr=%u\n", mpool_p, (char *)mpool_p + mp.u.mpm.size, mp.u.mpm.usize, mp.u.mpm.size, SIZEOF_VOID_P);
|
|
473
|
+ spam("Map created @%p->%p - size %lu out of %lu - voidptr=%lu\n", mpool_p, (char *)mpool_p + mp.u.mpm.size, (unsigned long)mp.u.mpm.usize, (unsigned long)mp.u.mpm.size, (unsigned long)SIZEOF_VOID_P);
|
474
|
474
|
return mpool_p;
|
475
|
475
|
}
|
476
|
476
|
|
477
|
477
|
void mpool_destroy(struct MP *mp) {
|
478
|
478
|
struct MPMAP *mpm_next = mp->u.mpm.next, *mpm;
|
479
|
|
- unsigned int mpmsize;
|
|
479
|
+ size_t mpmsize;
|
480
|
480
|
|
481
|
481
|
while((mpm = mpm_next)) {
|
482
|
482
|
mpmsize = mpm->size;
|
...
|
...
|
@@ -541,7 +541,7 @@ void mpool_flush(struct MP *mp) {
|
541
|
541
|
}
|
542
|
542
|
used += mp->u.mpm.size;
|
543
|
543
|
cli_dbgmsg("pool memory used: %.3f MB\n", used/(1024*1024.0));
|
544
|
|
- spam("Map flushed @%p, in use: %lu\n", mp, used);
|
|
544
|
+ spam("Map flushed @%p, in use: %lu\n", mp, (unsigned long)used);
|
545
|
545
|
}
|
546
|
546
|
|
547
|
547
|
int mpool_getstats(const struct cl_engine *eng, size_t *used, size_t *total)
|
...
|
...
|
@@ -565,13 +565,13 @@ int mpool_getstats(const struct cl_engine *eng, size_t *used, size_t *total)
|
565
|
565
|
return 0;
|
566
|
566
|
}
|
567
|
567
|
|
568
|
|
-static inline unsigned align_increase(unsigned size, unsigned a)
|
|
568
|
+static inline size_t align_increase(size_t size, size_t a)
|
569
|
569
|
{
|
570
|
570
|
/* we must pad with at most a-1 bytes to align start of struct */
|
571
|
571
|
return size + a - 1;
|
572
|
572
|
}
|
573
|
573
|
|
574
|
|
-static void* allocate_aligned(struct MPMAP *mpm, unsigned long size, unsigned align, const char *dbg)
|
|
574
|
+static void* allocate_aligned(struct MPMAP *mpm, size_t size, unsigned align, const char *dbg)
|
575
|
575
|
{
|
576
|
576
|
/* We could always align the size to maxalign (8), however that wastes
|
577
|
577
|
* space.
|
...
|
...
|
@@ -584,8 +584,8 @@ static void* allocate_aligned(struct MPMAP *mpm, unsigned long size, unsigned al
|
584
|
584
|
unsigned p_aligned = alignto(p, align);
|
585
|
585
|
struct FRAG *f = (struct FRAG*)((char*)mpm + p_aligned - FRAG_OVERHEAD);
|
586
|
586
|
unsigned realneed = p_aligned + size - mpm->usize;
|
587
|
|
- unsigned sbits = to_bits(realneed);
|
588
|
|
- unsigned needed = from_bits(sbits);
|
|
587
|
+ unsigned int sbits = to_bits(realneed);
|
|
588
|
+ size_t needed = from_bits(sbits);
|
589
|
589
|
#ifdef CL_DEBUG
|
590
|
590
|
assert(p_aligned + size <= mpm->size);
|
591
|
591
|
#endif
|
...
|
...
|
@@ -596,7 +596,7 @@ static void* allocate_aligned(struct MPMAP *mpm, unsigned long size, unsigned al
|
596
|
596
|
#ifdef CL_DEBUG
|
597
|
597
|
assert(mpm->usize <= mpm->size);
|
598
|
598
|
#endif
|
599
|
|
- spam("malloc @%p size %u (%s) origsize %u overhead %u\n", f, realneed, dbg, size, needed - size);
|
|
599
|
+ spam("malloc @%p size %lu (%s) origsize %lu overhead %lu\n", f, (unsigned long)realneed, dbg, (unsigned long)size, (unsigned long)(needed - size));
|
600
|
600
|
#ifdef CL_DEBUG
|
601
|
601
|
f->magic = MPOOLMAGIC;
|
602
|
602
|
memset(&f->u.a.fake, ALLOCPOISON, size);
|
...
|
...
|
@@ -605,15 +605,15 @@ static void* allocate_aligned(struct MPMAP *mpm, unsigned long size, unsigned al
|
605
|
605
|
}
|
606
|
606
|
|
607
|
607
|
void *mpool_malloc(struct MP *mp, size_t size) {
|
608
|
|
- unsigned align = alignof(size);
|
609
|
|
- unsigned int i, needed = align_increase(size+FRAG_OVERHEAD, align);
|
|
608
|
+ size_t align = alignof(size);
|
|
609
|
+ size_t i, needed = align_increase(size+FRAG_OVERHEAD, align);
|
610
|
610
|
const unsigned int sbits = to_bits(needed);
|
611
|
611
|
struct FRAG *f = NULL;
|
612
|
612
|
struct MPMAP *mpm = &mp->u.mpm;
|
613
|
613
|
|
614
|
614
|
/* check_all(mp); */
|
615
|
615
|
if (!size || sbits == FRAGSBITS) {
|
616
|
|
- cli_errmsg("mpool_malloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long int) size);
|
|
616
|
+ cli_errmsg("mpool_malloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long) size);
|
617
|
617
|
return NULL;
|
618
|
618
|
}
|
619
|
619
|
|
...
|
...
|
@@ -629,12 +629,12 @@ void *mpool_malloc(struct MP *mp, size_t size) {
|
629
|
629
|
f->magic = MPOOLMAGIC;
|
630
|
630
|
memset(&f->u.a.fake, ALLOCPOISON, size);
|
631
|
631
|
#endif
|
632
|
|
- spam("malloc @%p size %u (freed) origsize %u overhead %u\n", f, f->u.a.padding + FRAG_OVERHEAD + size, size, needed - size);
|
|
632
|
+ spam("malloc @%p size %lu (freed) origsize %lu overhead %lu\n", f, (unsigned long)(f->u.a.padding + FRAG_OVERHEAD + size), (unsigned long)size, (unsigned long)(needed - size));
|
633
|
633
|
return &f->u.a.fake;
|
634
|
634
|
}
|
635
|
635
|
|
636
|
636
|
if (!(needed = from_bits(sbits))) {
|
637
|
|
- cli_errmsg("mpool_malloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long int) size);
|
|
637
|
+ cli_errmsg("mpool_malloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long) size);
|
638
|
638
|
return NULL;
|
639
|
639
|
}
|
640
|
640
|
|
...
|
...
|
@@ -656,8 +656,8 @@ void *mpool_malloc(struct MP *mp, size_t size) {
|
656
|
656
|
#else
|
657
|
657
|
if (!(mpm = (struct MPMAP *)VirtualAlloc(NULL, i, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))) {
|
658
|
658
|
#endif
|
659
|
|
- cli_errmsg("mpool_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int)i);
|
660
|
|
- spam("failed to alloc %u bytes (%u requested)\n", i, size);
|
|
659
|
+ cli_errmsg("mpool_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long)i);
|
|
660
|
+ spam("failed to alloc %lu bytes (%lu requested)\n", (unsigned long)i, (unsigned long)size);
|
661
|
661
|
return NULL;
|
662
|
662
|
}
|
663
|
663
|
#ifdef CL_DEBUG
|
...
|
...
|
@@ -699,7 +699,7 @@ void mpool_free(struct MP *mp, void *ptr) {
|
699
|
699
|
}
|
700
|
700
|
|
701
|
701
|
void *mpool_calloc(struct MP *mp, size_t nmemb, size_t size) {
|
702
|
|
- unsigned int needed = nmemb*size;
|
|
702
|
+ size_t needed = nmemb*size;
|
703
|
703
|
void *ptr;
|
704
|
704
|
|
705
|
705
|
if(!needed) return NULL;
|
...
|
...
|
@@ -710,18 +710,18 @@ void *mpool_calloc(struct MP *mp, size_t nmemb, size_t size) {
|
710
|
710
|
|
711
|
711
|
void *mpool_realloc(struct MP *mp, void *ptr, size_t size) {
|
712
|
712
|
struct FRAG *f = (struct FRAG *)((char *)ptr - FRAG_OVERHEAD);
|
713
|
|
- unsigned int csize;
|
|
713
|
+ size_t csize;
|
714
|
714
|
void *new_ptr;
|
715
|
715
|
if (!ptr) return mpool_malloc(mp, size);
|
716
|
716
|
|
717
|
717
|
if(!size || !(csize = from_bits(f->u.a.sbits))) {
|
718
|
|
- cli_errmsg("mpool_realloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long int) size);
|
|
718
|
+ cli_errmsg("mpool_realloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long) size);
|
719
|
719
|
return NULL;
|
720
|
720
|
}
|
721
|
721
|
csize -= FRAG_OVERHEAD + f->u.a.padding;
|
722
|
722
|
if (csize >= size && (!f->u.a.sbits || from_bits(f->u.a.sbits-1)-FRAG_OVERHEAD-f->u.a.padding < size)) {
|
723
|
723
|
spam("free @%p\n", f);
|
724
|
|
- spam("malloc @%p size %u (self) origsize %u overhead %u\n", f, size + FRAG_OVERHEAD + f->u.a.padding, size, csize-size+FRAG_OVERHEAD+f->u.a.padding);
|
|
724
|
+ spam("malloc @%p size %lu (self) origsize %lu overhead %lu\n", f, (unsigned long)(size + FRAG_OVERHEAD + f->u.a.padding), (unsigned long)size, (unsigned long)(csize-size+FRAG_OVERHEAD+f->u.a.padding));
|
725
|
725
|
return ptr;
|
726
|
726
|
}
|
727
|
727
|
if (!(new_ptr = mpool_malloc(mp, size)))
|
...
|
...
|
@@ -763,7 +763,7 @@ unsigned char *cli_mpool_hex2str(mpool_t *mp, const char *hex) {
|
763
|
763
|
|
764
|
764
|
char *cli_mpool_strdup(mpool_t *mp, const char *s) {
|
765
|
765
|
char *alloc;
|
766
|
|
- unsigned int strsz;
|
|
766
|
+ size_t strsz;
|
767
|
767
|
|
768
|
768
|
if(s == NULL) {
|
769
|
769
|
cli_errmsg("cli_mpool_strdup(): s == NULL. Please report to http://bugs.clamav.net\n");
|
...
|
...
|
@@ -773,7 +773,7 @@ char *cli_mpool_strdup(mpool_t *mp, const char *s) {
|
773
|
773
|
strsz = strlen(s) + 1;
|
774
|
774
|
alloc = mpool_malloc(mp, strsz);
|
775
|
775
|
if(!alloc)
|
776
|
|
- cli_errmsg("cli_mpool_strdup(): Can't allocate memory (%u bytes).\n", (unsigned int) strsz);
|
|
776
|
+ cli_errmsg("cli_mpool_strdup(): Can't allocate memory (%lu bytes).\n", (unsigned long) strsz);
|
777
|
777
|
else
|
778
|
778
|
memcpy(alloc, s, strsz);
|
779
|
779
|
return alloc;
|
...
|
...
|
@@ -820,12 +820,12 @@ char *cli_mpool_virname(mpool_t *mp, const char *virname, unsigned int official)
|
820
|
820
|
|
821
|
821
|
uint16_t *cli_mpool_hex2ui(mpool_t *mp, const char *hex) {
|
822
|
822
|
uint16_t *str;
|
823
|
|
- unsigned int len;
|
|
823
|
+ size_t len;
|
824
|
824
|
|
825
|
825
|
len = strlen(hex);
|
826
|
826
|
|
827
|
827
|
if(len % 2 != 0) {
|
828
|
|
- cli_errmsg("cli_mpool_hex2ui(): Malformed hexstring: %s (length: %u)\n", hex, len);
|
|
828
|
+ cli_errmsg("cli_mpool_hex2ui(): Malformed hexstring: %s (length: %lu)\n", hex, (unsigned long)len);
|
829
|
829
|
return NULL;
|
830
|
830
|
}
|
831
|
831
|
|
...
|
...
|
@@ -843,26 +843,26 @@ uint16_t *cli_mpool_hex2ui(mpool_t *mp, const char *hex) {
|
843
|
843
|
|
844
|
844
|
#ifdef DEBUGMPOOL
|
845
|
845
|
void mpool_stats(struct MP *mp) {
|
846
|
|
- unsigned int i=0, ta=0, tu=0;
|
|
846
|
+ size_t i=0, ta=0, tu=0;
|
847
|
847
|
struct MPMAP *mpm = &mp->u.mpm;
|
848
|
848
|
|
849
|
849
|
cli_warnmsg("MEMORY POOL STATISTICS\n map \tsize\tused\t%\n");
|
850
|
850
|
while(mpm) {
|
851
|
|
- cli_warnmsg("- %u\t%u\t%u\t%f%%\n", i, mpm->size, mpm->usize, (float)mpm->usize/(float)mpm->size*100);
|
|
851
|
+ cli_warnmsg("- %lu\t%lu\t%lu\t%f%%\n", (unsigned long)i, (unsigned long)(mpm->size), (unsigned long)(mpm->usize), (float)mpm->usize/(float)mpm->size*100);
|
852
|
852
|
ta+=mpm->size;
|
853
|
853
|
tu+=mpm->usize;
|
854
|
854
|
i++;
|
855
|
855
|
mpm = mpm->next;
|
856
|
856
|
}
|
857
|
|
- cli_warnmsg("MEMORY POOL SUMMARY\nMaps: %u\nTotal: %u\nUsed: %u (%f%%)\n", i, ta, tu, (float)tu/(float)ta*100);
|
|
857
|
+ cli_warnmsg("MEMORY POOL SUMMARY\nMaps: %lu\nTotal: %lu\nUsed: %lu (%f%%)\n", (unsigned long)i, (unsigned long)ta, (unsigned long)tu, (float)tu/(float)ta*100);
|
858
|
858
|
}
|
859
|
859
|
|
860
|
860
|
void check_all(struct MP *mp) {
|
861
|
861
|
struct MPMAP *mpm = &mp->u.mpm;
|
862
|
862
|
while(mpm) {
|
863
|
863
|
volatile unsigned char *c = (unsigned char *)mpm;
|
864
|
|
- unsigned int len = mpm->size;
|
865
|
|
- spam("checking object %p - size %u\n", mpm, len);
|
|
864
|
+ size_t len = mpm->size;
|
|
865
|
+ spam("checking object %p - size %lu\n", mpm, (unsigned long)len);
|
866
|
866
|
while (len--) {
|
867
|
867
|
c[len];
|
868
|
868
|
}
|
...
|
...
|
@@ -883,3 +883,4 @@ void mpool_calloc() {}
|
883
|
883
|
|
884
|
884
|
#endif /* USE_MPOOL */
|
885
|
885
|
|
|
886
|
+
|