static int doubleTest(void) { double *pointers[2] = {NULL,NULL,}; // At the beginning of allocation, we should have all of the bytes. expectHeapEmpty(); // Can't allocate 0 bytes! pointers[0] = heapAlloc(0); expect(pointers[0] == NULL); // The number of free bytes should still be the total heap. expectHeapEmpty(); // Allocating 1 double (8 bytes) should be no problem. // The heap should automatically initialize itself the first time we alloc. pointers[0] = (double *)heapAlloc(1 * sizeof(double)); expect(pointers[0] != NULL); pointers[0][0] = 12.345; expect(*pointers[0] == 12.345); // We should have 9 bytes missing from the heap, since allocations need one // extra byte for length purposes. expectHeapSize(HEAP_SIZE - 9); // If we free the memory, we should get back all of our bytes. heapFree(pointers[0]); expectHeapEmpty(); // How about if we free one 16 byte chunk and one 24 byte chunk. pointers[0] = NULL; pointers[0] = (double *)heapAlloc(2 * sizeof(double)); expect(pointers[0] != NULL); pointers[1] = (double *)heapAlloc(3 * sizeof(double)); expect(pointers[1] != NULL); // We should have 16 + 1 + 24 + 1 bytes missing. expectHeapSize(HEAP_SIZE - 16 - 1 - 24 - 1); // If we free the 24 byte chunk, we should have only 16 + 1 bytes missing. heapFree(pointers[1]); expectHeapSize(HEAP_SIZE - 16 - 1); // After we free the 16 byte chunk, we should be back to 0 bytes missing. heapFree(pointers[0]); expectHeapEmpty(); return 0; }
int spiMasterRequest(uint8_t busId, struct SpiDevice **dev_out) { int ret = 0; struct SpiDeviceState *state = heapAlloc(sizeof(*state)); if (!state) return -ENOMEM; struct SpiDevice *dev = &state->dev; ret = spiRequest(dev, busId); if (ret < 0) goto err_request; if (!dev->ops->masterRxTx) { ret = -EOPNOTSUPP; goto err_opsupp; } *dev_out = dev; return 0; err_opsupp: if (dev->ops->release) dev->ops->release(dev); err_request: heapFree(state); return ret; }
static void spiSlaveDone(struct SpiDeviceState *state) { struct SpiDevice *dev = &state->dev; if (dev->ops->release) dev->ops->release(dev); heapFree(state); }
// 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; }
void filtraeLiberaPagina(heap_t* h, char** page, int nr, strArray_t* save, int M) { int i; for (i = 0; i < nr; i++) { if (strlen(page[i]) <= M) { heapFree(h, page[i]); } else { strArrayPush(h, save, page[i]); } } }
static int stressTest(void) { unsigned char *pointers[3] = {NULL, NULL, NULL,}; // Initializing the heap should mean all the bytes are available. heapInit(); expectHeapEmpty(); // Let's allocate the whole heap with two chunks. One will be // (HEAP_SIZE / 4 - 1) bytes and the other will be // ((3 * HEAP_SIZE / 4) - 1) bytes. pointers[0] = (unsigned char *)heapAlloc((HEAP_SIZE / 4) - 1); expect(pointers[0] != NULL); pointers[1] = (unsigned char *)heapAlloc(((3 * HEAP_SIZE) / 4) - 1); expect(pointers[1] != NULL); // There should be no bytes left. expectHeapFull(); // Therefore, we should not be able to allocate anymore. pointers[2] = (unsigned char *)heapAlloc(1); expect(pointers[2] == NULL); // If we free the first guy, we should only be missing the second big chunk. heapFree(pointers[0]); expectHeapSize(HEAP_SIZE - (((3 * HEAP_SIZE) / 4) - 1) - 1); // If we try to allocate some memory that is equal to the bytes left, // then we should fail, because we need any extra byte to mark the end of // the reference. pointers[2] = heapAlloc(heapFreeBytesCount); expect(pointers[2] == NULL); // Make sure when we free the second chunk, we have a full heap available. heapFree(pointers[1]); expectHeapEmpty(); return 0; }
int spiMasterRelease(struct SpiDevice *dev) { struct SpiDeviceState *state = SPI_DEVICE_TO_STATE(dev); if (dev->ops->release) { int ret = dev->ops->release(dev); if (ret < 0) return ret; } heapFree(state); return 0; }
static int arrayTest(void) { int **matrix, i; heapInit(); expectHeapEmpty(); // Allocate room for 3 pointers. matrix = (int **)heapAlloc(3 * sizeof(int *)); expectHeapSize(HEAP_SIZE - (3 * (sizeof(int *))) - 1 // 3 pointers and 1 alloc byte - 0); // Allocate 5 integers in each array. for (i = 0; i < 3; i ++) { matrix[i] = (int *)heapAlloc(5 * sizeof(int)); } expectHeapSize(HEAP_SIZE // 3 pointers and 1 alloc byte - (3 * (sizeof(int *))) - 1 // 3 arrays of 5 int's plus an alloc byte each - 3 * ((5 * sizeof(int)) + 1) - 0); // Free the arrays. for (i = 0; i < 3; i ++) { heapFree(matrix[i]); } expectHeapSize(HEAP_SIZE - (3 * (sizeof(int *))) - 1 // 3 pointers and 1 alloc byte - 0); // Free the pointers. heapFree(matrix); expectHeapEmpty(); return 0; }
static void freeBlock( GCBlock *t ){ int flags=t->flags; int size=flags & ~15; int i=size/16; if( i>255 ){ clrMemBit( t ); heapFree( t,size ); }else{ t->flags=BBGC_MARKED; t->succ=freeBlocks[i]; freeBlocks[i]=t; } gc_alloced-=size; }
void strArrayPush(heap_t* h, strArray_t* sarray, char* s) { if (sarray->num + 1 >= sarray->allocSize) { /* rialloca */ int i; char** old = sarray->array; sarray->allocSize *= 2; if (sarray->allocSize == 0) sarray->allocSize = 2; sarray->array = heapAlloc(h, sarray->allocSize * (sizeof(char*))); if (old != NULL) { for (i = 0; i < sarray->num; i++) { sarray->array[i] = old[i]; } heapFree(h, old); } } sarray->array[sarray->num++] = s; }
int spiSlaveRequest(uint8_t busId, const struct SpiMode *mode, struct SpiDevice **dev_out) { int ret = 0; struct SpiDeviceState *state = heapAlloc(sizeof(*state)); if (!state) return -ENOMEM; struct SpiDevice *dev = &state->dev; ret = spiRequest(dev, busId); if (ret < 0) goto err_request; if (!dev->ops->slaveIdle || !dev->ops->slaveRxTx) { ret = -EOPNOTSUPP; goto err_opsupp; } state->mode = *mode; state->err = 0; ret = spiSlaveStart(state, mode); if (ret < 0) goto err_opsupp; *dev_out = dev; return 0; err_opsupp: if (dev->ops->release) dev->ops->release(dev); err_request: heapFree(state); return ret; }
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); }
void strArrayFree(heap_t* h, strArray_t* sarray) { if (sarray->allocSize > 0) { heapFree(h, sarray->array); } heapFree(h, sarray); }
static void SzFree(void *, void *address) { heapFree(address); }