/*Testing heap data stracure*/
int main(){

	int * head = heapCreate(15);
	int i;

	for(i=1;i<10;i++)
		heapInsert(i,head);

	heapPrint(head);

	printf("\nremove element : %d\n",heapRemove(head));
	printf("\n\n");
	heapPrint(head);

	printf("\nremove element : %d\n",heapRemove(head));
	printf("\n\n");

	heapInsert(120,head);
	heapInsert(6,head);
	heapInsert(5,head);

	heapPrint(head);

	int arr[] = {46, 109, 711, 275, 196, 367,545,256,789,267,552};

	int * head1= heapify(arr,11);

	printf("\n\n");	
	heapPrint(head1);

	return 0;
}
Beispiel #2
0
// Usage: ./client <fileIn> <fileOut> <fileOut2> <heapSize> <rowCount> <rowLength>
int main (int argc, char* argv[]) {

	char* nameFileIn = argv[1];
	char* nameFileOut = argv[2];
	char* nameFileOut2 = argv[3];
	char** page;
	FILE* fpI,* fpO;
	int nr;
	heap_t* heap;
	strArray_t* save;

	int D = atoi(argv[4]), N = atoi(argv[5]), M = atoi(argv[6]);

	printf("input file: %s\noutput file: %s\noutput file 2: %s\n",
			nameFileIn, nameFileOut, nameFileOut2);
	printf("D: %d - N: %d - M: %d\n", D, N, M);

	heap = heapCreate(D);

	save = strArrayInit(heap, 0); /* inizializza a vuoto */

	page = (char**)heapAlloc(heap, N * sizeof(char*));
	fpI = fopen(nameFileIn, "r");
	fpO = fopen(nameFileOut, "w");
	if (page == NULL || fpI == NULL || fpO == NULL) {
		printf("Errore in allocazione memoria o apertura file\n");
		exit(1);
	}

	while ((nr = leggiPagina(heap, page, fpI, N)) > 0) {
		ordinaPagina(page, nr);
		scriviPagina(page, fpO, nr);
		filtraeLiberaPagina(heap, page, nr, save, M);
	} 
	
	fclose(fpI);
	fclose(fpO);
	heapFree(heap, page);

	fpO = fopen(nameFileOut2, "w");
	if (fpO == NULL) {
		printf("Errore in apertura file\n");
		exit(1);
	}

	strArraySort(save);
	strArrayPrint(save, fpO);
	fclose(fpO);

	strArrayFree(heap, save);

	heapDestroy(heap);

	return 0;

}
Beispiel #3
0
Datei: gc.c Projekt: lborwell/gc
/*
* RUN THE COLLECTOR
* args
* Stack* s -- local variable stack
* heap** h -- current (pointer to pointer to) from-space
* int bd -- are we collecting BigData heap? (bool: 0 false, otherwise true)
*/
void collect(Stack* s, heap** h, int bd){
    heap* to = heapCreate();
    weakptrs = stackCreate();

    // If stack is empty, return empty heap
    if(!s){ 
        *h = to;
        return; 
    }

    //evacuate things pointed to from the stack, update stack references
    do{
        s->data = evac(s->data, *h, to);
    }while(s=s->next);

    // if collecting bigdata heap, no need to scavenge
    if(bd){
        *h = to;
        return;
    }

    //scavenge the to-space
    scavenge(*h,to);

    /*
    * Check weak pointer references. If data is already forwarded, we know
    * it's got another reference -- copy the forwarding pointer loc.
    * Otherwise, we collect the data.
    */
    int i;
    while((i = pop(&weakptrs)) != -1){
        if((*h)->heap[to->heap[i+1]] == FWD)
            to->heap[i+1] = (*h)->heap[to->heap[i+1]+1];
        else
            to->heap[i+1] = -1;
    }

    //Change reference of from-space to to-space
    free(*h);
    *h = to;

    //collect big data heap
    if(!(collectioncount % BIGDATACOLLECT)){
        if(!bigdataindex) return;

        collect(bigdataindex, &bigdataheap, 1);
        Stack* bdi = bigdataindex;
        do{
            (*h)->heap[bdi->bdloc] = bdi->data;
        }while(bdi=bdi->next);
    }
}
Beispiel #4
0
ArrayMaxHeap<ItemType>::
ArrayMaxHeap(const ItemType someArray[], const int arraySize):
             itemCount(arraySize), maxItems(2 * arraySize)
{
   // Allocate the array
   items = new ItemType[2 * arraySize];
   
   // Copy given values into the array
   for (int i = 0; i < itemCount; i++)
      items[i] = someArray[i];
   
   // Reorganize the array into a heap
   heapCreate();
} // end constructor
Beispiel #5
0
DWORD WINAPI tenkBackChannel(LPVOID lpvParam) {
	HANDLE		hPipe;
	TCHAR		buf[BUFSIZE+1];
	ULONG		bytesRead, bytesWritten;
	BOOL		fSuccess;
	ULONG64		funcAddr64;
	ULONG		*funcAddr, i;
	

	struct AllocateStruct	*aStruct = (struct AllocateStruct *)buf;
	struct ReallocateStruct	*rStruct = (struct ReallocateStruct *)buf;
	struct FreeStruct		*fStruct = (struct FreeStruct *)buf;
	struct CreateStruct		*cStruct = (struct CreateStruct *)buf;
	struct DestroyStruct	*dStruct = (struct DestroyStruct *)buf;
	struct CoalesceStruct	*cfbStruct = (struct CoalesceStruct *)buf;

	
	hPipe = (HANDLE) lpvParam;
	
	dprintf("[Byakugan] Waiting for named pipe connection...\n");

	for(;!ConnectNamedPipe(hPipe, NULL) == TRUE;) { dprintf("[B] waiting for connection...\n"); }

	dprintf("[Byakugan] Connected to back channel. :)\n");

	// Load addresses from symbols if possible for undoumented interfaces
	i = 0;
	while (undocdFunc[i] != NULL) {
		dprintf("[T] Sending address of %s\n", undocdFunc[i]);
		fSuccess = WriteFile(hPipe, &(undocdAddr[i]), sizeof(ULONG), &bytesWritten, NULL);
		if (!fSuccess || bytesWritten != sizeof(ULONG))
			dprintf("[T] Failed to send address of %s\n", undocdFunc[i]);
		i++;
	}
	//FlushFileBuffers(hPipe);
	dprintf("[T] Sent addresses of %d undocumented functions.\n", i);

	initializeHeapModel(&heapModel);

#undef THREEDHEAPFU_ENABLED //Place this in setup.bat

#if 0 //#ifdef THREEDHEAPFU_ENABLED
//Create a heap event proxy, play these back out to 3dheapfu
LPTSTR	lpszProxyPipename = TEXT("\\\\.\\pipe\\tenketsuProxy");
BOOL	fProxySuccess;
DWORD	dwProxyMode	= PIPE_READMODE_MESSAGE;
ULONG   bytesProxyWritten;
static BOOL	fDontAttemptProxyWrite = false;

HANDLE hProxyPipe = CreateFile(lpszProxyPipename,
								GENERIC_READ | GENERIC_WRITE,
								0,
								NULL,
								OPEN_EXISTING,
								0,
								NULL);

			if (hProxyPipe == INVALID_HANDLE_VALUE)
					dprintf("hProxyPipe == invalid handle\n");
			else
					dprintf("hProxyPipe == good\n");
			SetNamedPipeHandleState(hProxyPipe, &dwProxyMode, NULL, NULL); //?

#endif

	while (1) {
		fSuccess = ReadFile(	hPipe,
								buf,
								BUFSIZE*sizeof(TCHAR),
								&bytesRead,
								NULL);
		if (!fSuccess || bytesRead == 0) {
			dprintf("[Byakugan] ReadFile failed, or read 0 bytes.\n");
			continue;
		}
#if 0 //#ifdef THREEDHEAPFU_ENABLED
		//dprintf("jc: receieved an event of size %d. Forwarding on to ProxyPipe\n", bytesRead);
		//WriteFile(hPipe, &freeinfo, sizeof(struct FreeStruct), &bytesWritten, NULL);

		if (!fDontAttemptProxyWrite)
		{
			fProxySuccess = WriteFile(hProxyPipe, buf, bytesRead, &bytesProxyWritten, NULL); 
			if (bytesRead != bytesProxyWritten)
			{
				dprintf("Partial write to proxy on last event! ;(\n");
				dprintf("event size was %d. wrote %d\n", bytesRead, bytesProxyWritten);
				dprintf("Disabling message proxying until explicitly enabled.\n");
				fDontAttemptProxyWrite = true;
			}
		}

#endif
		switch ( *((BYTE *) buf) ) {
			case ALLOCATESTRUCT:
				//dprintf("[T] New Chunk @ 0x%08x\n", aStruct->ret);
				//dprintf("Heap: 0x%08x\tFlags: 0x%08x\tSize: 0x%08x\n\n", 
				//		aStruct->heapHandle, aStruct->flags, aStruct->size);
				if (heapModel.state & MODEL) heapAllocate(&heapModel, aStruct);
				if (heapModel.state & LOG) logAllocate(&heapModel, aStruct);
				break;

			case REALLOCATESTRUCT:
				//dprintf("[T] Realloc'd Chunk @ 0x%08x\n", rStruct->ret);
				//dprintf("Heap: 0x%08x\tFlags: 0x%08x\tSize: 0x%08x\n", 
				//		rStruct->heapHandle, rStruct->flags, rStruct->size);
				//if (rStruct->ret !=  (PVOID) rStruct->memoryPointer)
				//	dprintf("Replaces chunk @ 0x%08x\n", rStruct->memoryPointer);
				//dprintf("\n");
				if (heapModel.state & MODEL) heapReallocate(&heapModel, rStruct);
				if (heapModel.state & LOG) logReallocate(&heapModel, rStruct);
				break;

			case FREESTRUCT:
				//dprintf("[T] Free'd Chunk @ 0x%08x\n", fStruct->memoryPointer);
				//dprintf("Heap: 0x%08x\tFlags: 0x%08x\n\n", fStruct->heapHandle, fStruct->flags);
				if (heapModel.state & MODEL) heapFree(&heapModel, fStruct);
				if (heapModel.state & LOG) logFree(&heapModel, fStruct);
				break;

			case CREATESTRUCT:
				dprintf("[T] New Heap: 0x%08x\n", cStruct->ret);
				dprintf("Base: 0x%08x\tReserve: 0x%08x\tFlags: 0x%08x\n",
						cStruct->base, cStruct->reserve, cStruct->flags);
				//dprintf("Commit: 0x%08x\tLock: 0x%08x\n\n", cStruct->commit, cStruct->lock);
				if (heapModel.state & MODEL) heapCreate(&heapModel, cStruct);
				break;

			case DESTROYSTRUCT:
				dprintf("[T] Heap Destroyed: 0x%08x\n\n", dStruct->heapHandle);
				if (heapModel.state & MODEL) heapDestroy(&heapModel, dStruct);
				break;
			
			case COALESCESTRUCT:
				//dprintf("[T] Free Block Consolidation (returned 0x%08x)\n", cfbStruct->ret);
				//dprintf("Heap: 0x%08x\tArg2: 0x%08x\tArg3: 0x%08x\tArg4: 0x%08x\n\n",
				//		cfbStruct->heapHandle, cfbStruct->arg2, cfbStruct->arg3, cfbStruct->arg4);
				if (heapModel.state & MODEL) heapCoalesce(&heapModel, cfbStruct);
				break;

			default:
				dprintf("[Byakugan] Tenketsu: Unrecognized data was returned.\n");
		}

	}


	return (0);
}
Beispiel #6
0
/*
* Create heaps
*/
heap* initHeaps(){
    dataConsCreate();
    bigdataHeapCreate();
    bigdataindex = stackCreate();
    return heapCreate();
}