main() { int x; UC str[10]; SC aux[100]; S_INDEX si, so; while ( 1 ) { printf("Registro ? "); gets(aux); si.registro = atol(aux); if ( aux[0] == '.' ) return(0); printf("campo ? "); gets(aux); si.campo = atoi(aux); printf("paragrafo ? "); gets(aux); si.paragrafo = atoi(aux); printf("frase ? "); gets(aux); si.frase = atoi(aux); printf("sequencia ? "); gets(aux); si.sequencia = atoi(aux); x = to_bits(&si, str); printf("%d %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", x, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7], str[8]); to_struct( &so, str); x = 0; if (si.registro != so.registro ) { printf("si.registro diferente\n"); x=1; }; if (si.campo != so.campo ) { printf("si.campo diferente\n"); x=1; }; if (si.paragrafo != so.paragrafo ) { printf("si.paragrafo diferente\n"); x=1; }; if (si.frase != so.frase ) { printf("si.frase diferente\n"); x=1; }; if (si.sequencia != so.sequencia ) { printf("si.sequencia diferente\n"); x=1; }; if ( x ) { printf("Reg = %ld\nCampo = %d\nParagrafo = %d\nFrase = %d\nSequencia = %d\n", si.registro, si.campo, si.paragrafo, si.frase, si.sequencia); } } }
void *mpool_malloc(struct MP *mp, size_t size) { unsigned int i, 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 || sbits == FRAGSBITS) { cli_errmsg("mpool_malloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long int) size); return NULL; } /* Case 1: We have a free'd frag */ if((f = mp->avail[sbits])) { spam("malloc %p size %u (freed)\n", f, mpool_roundup(size)); mp->avail[sbits] = f->u.next; f->u.sbits = sbits; #ifdef CL_DEBUG f->magic = MPOOLMAGIC; memset(&f->fake, ALLOCPOISON, size); #endif return &f->fake; } if (!(needed = from_bits(sbits))) { cli_errmsg("mpool_malloc(): Attempt to allocate %lu bytes. Please report to http://bugs.clamav.net\n", (unsigned long int) size); return NULL; } /* Case 2: We have nuff room available for this frag already */ while(mpm) { if(mpm->size - mpm->usize >= needed) { f = (struct FRAG *)((char *)mpm + mpm->usize); spam("malloc %p size %u (hole)\n", f, mpool_roundup(size)); mpm->usize += needed; f->u.sbits = sbits; #ifdef CL_DEBUG f->magic = MPOOLMAGIC; memset(&f->fake, ALLOCPOISON, size); #endif 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|ANONYMOUS_MAP, -1, 0)) == MAP_FAILED) { cli_errmsg("mpool_malloc(): Can't allocate memory (%lu bytes).\n", (unsigned long int)i); spam("failed to alloc %u bytes (%u requested)\n", i, size); return NULL; } #ifdef CL_DEBUG memset(mpm, ALLOCPOISON, i); #endif mpm->size = i; mpm->usize = needed + align_to_voidptr(sizeof(*mpm)); mpm->next = mp->mpm.next; mp->mpm.next = mpm; f = (struct FRAG *)((char *)mpm + align_to_voidptr(sizeof(*mpm))); spam("malloc %p size %u (new map)\n", f, mpool_roundup(size)); f->u.sbits = sbits; #ifdef CL_DEBUG f->magic = MPOOLMAGIC; #endif return &f->fake; }