예제 #1
0
int main(int argc, const char* argv[])
{
    SHEAP_Block* blk;
    int r, j, i, n;
    HEAP heap;
    char* c;

    /* CORE_SetLOGFormatFlags(fLOG_None | fLOG_Level | fLOG_OmitNoteLevel); */
    CORE_SetLOGFILE(stderr, 0/*false*/);
    if (argc > 1)
        g_NCBI_ConnectRandomSeed = atoi(argv[1]);
    else
        g_NCBI_ConnectRandomSeed = (int) time(0) ^ NCBI_CONNECT_SRAND_ADDEND;
    CORE_LOGF(eLOG_Note, ("Using seed %d", g_NCBI_ConnectRandomSeed));
    HEAP_Options(eOff/*slow*/, eOn/*newalk*/);
    srand(g_NCBI_ConnectRandomSeed);
    for (j = 1;  j <= 3;  j++) {
        CORE_LOGF(eLOG_Note, ("Creating heap %d", j));
        if (!(heap = HEAP_Create(0, 0, 4096, s_Expand, 0)))
            CORE_LOG(eLOG_Error, "Cannot create heap");
        for (n = 0; n < 1000 && (rand() & 0xFFFF) != 0x1234; n++) {
            r = rand() & 7;
            if (r == 1  ||  r == 3) {
                int/*bool*/ fast = rand() & 1;
                i = rand() & 0xFF;
                if (i) {
                    CORE_LOGF(eLOG_Note,
                              ("Allocating%s data size %d",
                               fast ? " fast" : "", i));
                    blk = fast ? HEAP_AllocFast(heap, i) : HEAP_Alloc(heap, i);
                    if (blk) {
                        CORE_LOGF(eLOG_Note,
                                  ("Done @%u, size %u",
                                   HEAP_ADDR(blk, heap), blk->size));
                    } else
                        CORE_LOG(eLOG_Error, "Allocation failed");
                    c = (char*) blk + sizeof(*blk);
                    while (i--)
                        *c++ = rand();
                    s_Walk(heap, 0);
                } else {
                    assert(!(fast
                             ? HEAP_AllocFast(heap, i)
                             : HEAP_Alloc(heap, i)));
                }
            } else if (r == 2  ||  r == 4) {
                blk = 0;
                do {
                    if (!(blk = HEAP_Walk(heap, blk)))
                        break;
                } while (rand() & 7);
                if (blk  &&  (short) blk->flag) {
                    unsigned size = blk->size;
                    unsigned data_size = size - sizeof(*blk);
                    CORE_LOGF(eLOG_Note,
                              ("Freeing @%u, size %u, data size %u",
                               HEAP_ADDR(blk, heap), size, data_size));
                    assert(data_size);
                    HEAP_Free(heap, blk);
                    CORE_LOG(eLOG_Note, "Done");
                    s_Walk(heap, 0);
                }
            } else if (r == 5) {
                const SHEAP_Block* prev = 0;
                unsigned ok = 0;
                blk = 0;
                for (;;) {
                    if (!(blk = HEAP_Walk(heap, blk)))
                        break;
                    if ((short) blk->flag  &&  (rand() & 7)) {
                        char buf[32];
                        unsigned size = blk->size;
                        int/*bool*/ fast = rand() & 1;
                        unsigned data_size = size - sizeof(*blk);
                        if (!fast  ||  !prev)
                            *buf = '\0';
                        else
                            sprintf(buf, ", prev @%u", HEAP_ADDR(prev, heap));
                        CORE_LOGF(eLOG_Note,
                                  ("Freeing%s%s @%u%s in walk,"
                                   " size %u, data size %u",
                                   fast ? " fast" : "", ok ? " more" : "",
                                   HEAP_ADDR(blk,heap), buf, size, data_size));
                        assert(data_size);
                        if (fast)
                            HEAP_FreeFast(heap, blk, prev);
                        else
                            HEAP_Free(heap, blk);
                        CORE_LOG(eLOG_Note, "Done");
                        s_Walk(heap, 0);
                        ok = 1;
                        if (prev  &&  !((short) prev->flag))
                            continue;
                    }
                    prev = blk;
                }
                if (!ok)
                    s_Walk(heap, "the");
                else
                    CORE_LOG(eLOG_Note, "Done with freeing while walking");
            } else if (r == 6  ||  r == 7) {
                HEAP newheap;

                if (r == 6) {
                    int/*bool*/ fast = rand() & 1;
                    if (fast) {
                        CORE_LOG(eLOG_Note, "Attaching heap fast");
                        newheap = HEAP_AttachFast(HEAP_Base(heap),
                                                  HEAP_Size(heap), 0);
                    } else {
                        CORE_LOG(eLOG_Note, "Attaching heap");
                        newheap = HEAP_Attach(HEAP_Base(heap), 0);
                    }
                } else {
                    CORE_LOG(eLOG_Note, "Copying heap");
                    newheap = HEAP_Copy(heap, 0, 0);
                }

                if (!newheap) {
                    CORE_LOGF(eLOG_Error,
                              ("%s failed", r == 6 ? "Attach" : "Copy"));
                } else
                    s_Walk(newheap, r == 6 ? "attached" : "copied");
                HEAP_Detach(newheap);
            } else {
                TNCBI_Size size = HEAP_Size(heap);
                HEAP newheap;

                CORE_LOG(eLOG_Note, "Trimming heap");
                newheap = HEAP_Trim(heap);
                CORE_LOGF(eLOG_Note,
                          ("Heap %strimmed: %u -> %u", newheap ? "" : "NOT ",
                           size, HEAP_Size(newheap)));
                if (newheap) {
                    heap = newheap;
                    s_Walk(heap, "trimmed");
                }
            }
        }
        HEAP_Destroy(heap);
        CORE_LOGF(eLOG_Note, ("Heap %d done", j));
    }
    CORE_LOG(eLOG_Note, "Test completed");
    CORE_SetLOG(0);
    return 0;
}
예제 #2
0
PUBLIC void Main()
{
	if(!LoadRTL(0x40000000)) goto _Finish;

	//Alloc window
	window = XKY_WINDOW_Alloc();
	if(!window)	goto _Finish;

	//Set random seed
	SetSeed(XKY_RTC_Seconds() + XKY_TMR_GetTicks());
	
	//Set exception handler for memory failures
	//XKY_EXCEPTION_SetHandler(0x0E, MtTestExceptionHandler);

	//Set-up stacks so thread finds it's parameters on it
	PAINT_PARAMETERS* p1 = (PAINT_PARAMETERS*)HEAP_Alloc(sizeof(PAINT_PARAMETERS));
	p1->x		= 0;
	p1->y		= 0;
	p1->width	= XKY_WINDOW_GetWidth(window)/2;
	p1->height	= XKY_WINDOW_GetHeight(window)/2;
	p1->color	= SRGB(255, 0, 0);

	PAINT_PARAMETERS* p2 = (PAINT_PARAMETERS*)HEAP_Alloc(sizeof(PAINT_PARAMETERS));
	p2->x		= 0;
	p2->y		= XKY_WINDOW_GetHeight(window)/2;
	p2->width	= XKY_WINDOW_GetWidth(window)/2;
	p2->height	= XKY_WINDOW_GetHeight(window)/2;
	p2->color	= SRGB(0, 255, 0);

	PAINT_PARAMETERS* p3 = (PAINT_PARAMETERS*)HEAP_Alloc(sizeof(PAINT_PARAMETERS));
	p3->x		= XKY_WINDOW_GetWidth(window)/2;
	p3->y		= XKY_WINDOW_GetHeight(window)/2;
	p3->width	= XKY_WINDOW_GetWidth(window)/2;
	p3->height	= XKY_WINDOW_GetHeight(window)/2;
	p3->color	= SRGB(0, 0, 255);

	PAINT_PARAMETERS* p4 = (PAINT_PARAMETERS*)HEAP_Alloc(sizeof(PAINT_PARAMETERS));
	p4->x		= XKY_WINDOW_GetWidth(window)/2;
	p4->y		= 0;
	p4->width	= XKY_WINDOW_GetWidth(window)/2;
	p4->height	= XKY_WINDOW_GetHeight(window)/2;
	p4->color	= SRGB(255, 255, 255);


	//Create threads
	ThreadInit();
	HANDLE t1 = CreateThread(PaintThread, p1);
	HANDLE t2 = CreateThread(PaintThread, p2);
	HANDLE t3 = CreateThread(PaintThread, p3);
	HANDLE t4 = CreateThread(PaintThread, p4);

	//Init scheduler iterator
	scheduler = LIST_First(&threads);

	//Start scheduler
	//XKY_CPU_RegisterCallback(XKY_ADDRESS_SPACE_GetCurrent(), MtCpuTimerCallbackFCFS);
	XKY_CPU_RegisterCallback(XKY_ADDRESS_SPACE_GetCurrent(), MtCpuTimerCallbackRoundRobin);

	while(!ThreadHasFinished(t1) || !ThreadHasFinished(t2) || !ThreadHasFinished(t3) || !ThreadHasFinished(t4));

	//All threads finished
	dword width = XKY_WINDOW_GetWidth(window);
	dword height = XKY_WINDOW_GetHeight(window);

	for(dword x = 0; x < width; x++)
	{
		for(dword y = 0; y < height; y++)
		{
			XKY_WINDOW_SetPixel(window, x, y, TRGB(0, 0, 0));
		}
	}

	XKY_WINDOW_PrintText(window, 200, 200, SRGB(255, 0, 255), &DONE);

_Finish:
	XKY_OS_Finish();
}