void CMemTest::InvariantDataRealloc(bool aligned)
{
    size_t size, sizeMin;
    CountErrors=0;
    if (FullLog) REPORT("\nInvariant data by realloc....");
    UCHAR* pchar;
    sizeMin=size=rand()%MAX_SIZE+10;
    pchar = aligned?
        (UCHAR*)Taligned_realloc(NULL,size,choose_random_alignment())
        : (UCHAR*)Trealloc(NULL,size);
    if (NULL == pchar)
        return;
    for (size_t k=0; k<size; k++)
        pchar[k]=(UCHAR)k%255+1;
    for (int i=0; i<COUNTEXPERIMENT; i++)
    {
        size=rand()%MAX_SIZE+10;
        UCHAR *pcharNew = aligned?
            (UCHAR*)Taligned_realloc(pchar,size, choose_random_alignment())
            : (UCHAR*)Trealloc(pchar,size);
        if (NULL == pcharNew)
            continue;
        pchar = pcharNew;
        sizeMin=size<sizeMin ? size : sizeMin;
        for (size_t k=0; k<sizeMin; k++)
            if (pchar[k] != (UCHAR)k%255+1)
            {
                CountErrors++;
                if (ShouldReportError())
                {
                    REPORT("stand '%c', must stand '%c'\n",pchar[k],(UCHAR)k%255+1);
                    REPORT("error: data changed (at %llu, SizeMin=%llu)\n",
                           (long long unsigned)k,(long long unsigned)sizeMin);
                }
            }
    }
    if (aligned)
        Taligned_realloc(pchar,0,choose_random_alignment());
    else
        Trealloc(pchar,0);
    if (CountErrors) REPORT("%s\n",strError);
    else if (FullLog) REPORT("%s\n",strOk);
    error_occurred |= ( CountErrors>0 ) ;
    //REPORT("end check\n");
}
Example #2
0
void InvariantDataRealloc(bool aligned, size_t maxAllocSize, bool checkData)
{
    Harness::FastRandom fastRandom(1);
    size_t size = 0, start = 0;
    char *ptr = NULL,
        // master to create copies and compare ralloc result against it
        *master = (char*)Tmalloc(2*maxAllocSize);

    ASSERT(master, NULL);
    ASSERT(!(2*maxAllocSize%sizeof(unsigned short)),
           "The loop below expects that 2*maxAllocSize contains sizeof(unsigned short)");
    for (size_t k = 0; k<2*maxAllocSize; k+=sizeof(unsigned short))
        *(unsigned short*)(master+k) = fastRandom.get();

    for (int i=0; i<100; i++) {
        // don't want sizeNew==0 here
        const size_t sizeNew = fastRandom.get() % (maxAllocSize-1) + 1;
        char *ptrNew = aligned?
            (char*)Taligned_realloc(ptr, sizeNew, choose_random_alignment())
            : (char*)Trealloc(ptr, sizeNew);
        ASSERT(ptrNew, NULL);
        // check that old data not changed
        if (checkData)
            ASSERT(!memcmp(ptrNew, master+start, min(size, sizeNew)), "broken data");

        // prepare fresh data, copying them from random position in master
        size = sizeNew;
        ptr = ptrNew;
        if (checkData) {
            start = fastRandom.get() % maxAllocSize;
            memcpy(ptr, master+start, size);
        }
    }
    if (aligned)
        Taligned_realloc(ptr, 0, choose_random_alignment());
    else
        Trealloc(ptr, 0);
    Tfree(master);
}