static void test_HeapCreate(void) { SYSTEM_INFO sysInfo; ULONG memchunk; HANDLE heap; LPVOID mem1,mem1a,mem3; UCHAR *mem2,*mem2a; UINT i; BOOL error; DWORD dwSize; /* Retrieve the page size for this system */ GetSystemInfo(&sysInfo); ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n"); /* Create a Heap with a minimum and maximum size */ /* Note that Windows and Wine seem to behave a bit differently with respect to memory allocation. In Windows, you can't access all the memory specified in the heap (due to overhead), so choosing a reasonable maximum size for the heap was done mostly by trial-and-error on Win2k. It may need more tweaking for otherWindows variants. */ memchunk=10*sysInfo.dwPageSize; heap=HeapCreate(0,2*memchunk,5*memchunk); ok( !((ULONG_PTR)heap & 0xffff), "heap %p not 64K aligned\n", heap ); /* Check that HeapCreate allocated the right amount of ram */ mem1=HeapAlloc(heap,0,5*memchunk+1); ok(mem1==NULL,"HeapCreate allocated more Ram than it should have\n"); HeapFree(heap,0,mem1); /* Check that a normal alloc works */ mem1=HeapAlloc(heap,0,memchunk); ok(mem1!=NULL,"HeapAlloc failed\n"); if(mem1) { ok(HeapSize(heap,0,mem1)>=memchunk, "HeapAlloc should return a big enough memory block\n"); } /* Check that a 'zeroing' alloc works */ mem2=HeapAlloc(heap,HEAP_ZERO_MEMORY,memchunk); ok(mem2!=NULL,"HeapAlloc failed\n"); if(mem2) { ok(HeapSize(heap,0,mem2)>=memchunk,"HeapAlloc should return a big enough memory block\n"); error=FALSE; for(i=0;i<memchunk;i++) { if(mem2[i]!=0) { error=TRUE; } } ok(!error,"HeapAlloc should have zeroed out its allocated memory\n"); } /* Check that HeapAlloc returns NULL when requested way too much memory */ mem3=HeapAlloc(heap,0,5*memchunk); ok(mem3==NULL,"HeapAlloc should return NULL\n"); if(mem3) { ok(HeapFree(heap,0,mem3),"HeapFree didn't pass successfully\n"); } /* Check that HeapRealloc works */ mem2a=HeapReAlloc(heap,HEAP_ZERO_MEMORY,mem2,memchunk+5*sysInfo.dwPageSize); ok(mem2a!=NULL,"HeapReAlloc failed\n"); if(mem2a) { ok(HeapSize(heap,0,mem2a)>=memchunk+5*sysInfo.dwPageSize,"HeapReAlloc failed\n"); error=FALSE; for(i=0;i<5*sysInfo.dwPageSize;i++) { if(mem2a[memchunk+i]!=0) { error=TRUE; } } ok(!error,"HeapReAlloc should have zeroed out its allocated memory\n"); } /* Check that HeapRealloc honours HEAP_REALLOC_IN_PLACE_ONLY */ error=FALSE; mem1a=HeapReAlloc(heap,HEAP_REALLOC_IN_PLACE_ONLY,mem1,memchunk+sysInfo.dwPageSize); if(mem1a!=NULL) { if(mem1a!=mem1) { error=TRUE; } } ok(mem1a==NULL || !error,"HeapReAlloc didn't honour HEAP_REALLOC_IN_PLACE_ONLY\n"); /* Check that HeapFree works correctly */ if(mem1a) { ok(HeapFree(heap,0,mem1a),"HeapFree failed\n"); } else { ok(HeapFree(heap,0,mem1),"HeapFree failed\n"); } if(mem2a) { ok(HeapFree(heap,0,mem2a),"HeapFree failed\n"); } else { ok(HeapFree(heap,0,mem2),"HeapFree failed\n"); } /* 0-length buffer */ mem1 = HeapAlloc(heap, 0, 0); ok(mem1 != NULL, "Reserved memory\n"); dwSize = HeapSize(heap, 0, mem1); /* should work with 0-length buffer */ ok(dwSize < 0xFFFFFFFF, "The size of the 0-length buffer\n"); ok(HeapFree(heap, 0, mem1), "Freed the 0-length buffer\n"); /* Check that HeapDestroy works */ ok(HeapDestroy(heap),"HeapDestroy failed\n"); }
static void test_heap(void) { LPVOID mem; LPVOID msecond; DWORD res; UINT flags; HGLOBAL gbl; HGLOBAL hsecond; SIZE_T size, size2; const SIZE_T max_size = 1024, init_size = 10; /* Heap*() functions */ mem = HeapAlloc(GetProcessHeap(), 0, 0); ok(mem != NULL, "memory not allocated for size 0\n"); HeapFree(GetProcessHeap(), 0, mem); mem = HeapReAlloc(GetProcessHeap(), 0, NULL, 10); ok(mem == NULL, "memory allocated by HeapReAlloc\n"); for (size = 0; size <= 256; size++) { SIZE_T heap_size; mem = HeapAlloc(GetProcessHeap(), 0, size); heap_size = HeapSize(GetProcessHeap(), 0, mem); ok(heap_size == size || heap_size == resize_9x(size), "HeapSize returned %lu instead of %lu or %lu\n", heap_size, size, resize_9x(size)); HeapFree(GetProcessHeap(), 0, mem); } /* test some border cases of HeapAlloc and HeapReAlloc */ mem = HeapAlloc(GetProcessHeap(), 0, 0); ok(mem != NULL, "memory not allocated for size 0\n"); msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0 - 7); ok(msecond == NULL, "HeapReAlloc(~0 - 7) should have failed\n"); msecond = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, ~(SIZE_T)0); ok(msecond == NULL, "HeapReAlloc(~0) should have failed\n"); HeapFree(GetProcessHeap(), 0, mem); mem = HeapAlloc(GetProcessHeap(), 0, ~(SIZE_T)0); ok(mem == NULL, "memory allocated for size ~0\n"); /* large blocks must be 16-byte aligned */ mem = HeapAlloc(GetProcessHeap(), 0, 512 * 1024); ok( mem != NULL, "failed for size 512K\n" ); ok( (ULONG_PTR)mem % 16 == 0 || broken((ULONG_PTR)mem % 16) /* win9x */, "512K block not 16-byte aligned\n" ); HeapFree(GetProcessHeap(), 0, mem); /* Global*() functions */ gbl = GlobalAlloc(GMEM_MOVEABLE, 0); ok(gbl != NULL, "global memory not allocated for size 0\n"); gbl = GlobalReAlloc(gbl, 10, GMEM_MOVEABLE); ok(gbl != NULL, "Can't realloc global memory\n"); size = GlobalSize(gbl); ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size); gbl = GlobalReAlloc(gbl, 0, GMEM_MOVEABLE); ok(gbl != NULL, "GlobalReAlloc should not fail on size 0\n"); size = GlobalSize(gbl); ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size); ok(GlobalFree(gbl) == NULL, "Memory not freed\n"); size = GlobalSize(gbl); ok(size == 0, "Memory should have been freed, size=%ld\n", size); gbl = GlobalReAlloc(0, 10, GMEM_MOVEABLE); ok(gbl == NULL, "global realloc allocated memory\n"); /* GlobalLock / GlobalUnlock with a valid handle */ gbl = GlobalAlloc(GMEM_MOVEABLE, 256); SetLastError(MAGIC_DEAD); mem = GlobalLock(gbl); /* #1 */ ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError()); SetLastError(MAGIC_DEAD); flags = GlobalFlags(gbl); ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); msecond = GlobalLock(gbl); /* #2 */ ok( msecond == mem, "returned %p with %d (expected '%p')\n", msecond, GetLastError(), mem); SetLastError(MAGIC_DEAD); flags = GlobalFlags(gbl); ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); SetLastError(MAGIC_DEAD); res = GlobalUnlock(gbl); /* #1 */ ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError()); SetLastError(MAGIC_DEAD); flags = GlobalFlags(gbl); ok( flags , "returned 0x%04x with %d (expected '!= 0')\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); res = GlobalUnlock(gbl); /* #0 */ /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */ ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)), "returned %d with %d (expected '0' with: ERROR_SUCCESS or " "MAGIC_DEAD)\n", res, GetLastError()); SetLastError(MAGIC_DEAD); flags = GlobalFlags(gbl); ok( !flags , "returned 0x%04x with %d (expected '0')\n", flags, GetLastError()); /* Unlock an already unlocked Handle */ SetLastError(MAGIC_DEAD); res = GlobalUnlock(gbl); /* NT: ERROR_NOT_LOCKED, 9x: untouched */ ok( !res && ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)), "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or " "MAGIC_DEAD)\n", res, GetLastError()); GlobalFree(gbl); /* invalid handles are caught in windows: */ SetLastError(MAGIC_DEAD); hsecond = GlobalFree(gbl); /* invalid handle: free memory twice */ ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE), "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n", hsecond, GetLastError(), gbl); SetLastError(MAGIC_DEAD); flags = GlobalFlags(gbl); ok( (flags == GMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE), "returned 0x%04x with 0x%08x (expected GMEM_INVALID_HANDLE with " "ERROR_INVALID_HANDLE)\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); size = GlobalSize(gbl); ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE), "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n", size, GetLastError()); SetLastError(MAGIC_DEAD); mem = GlobalLock(gbl); ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE), "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n", mem, GetLastError()); /* documented on MSDN: GlobalUnlock() return FALSE on failure. Win9x and wine return FALSE with ERROR_INVALID_HANDLE, but on NT 3.51 and XPsp2, TRUE with ERROR_INVALID_HANDLE is returned. The similar Test for LocalUnlock() works on all Systems */ SetLastError(MAGIC_DEAD); res = GlobalUnlock(gbl); ok(GetLastError() == ERROR_INVALID_HANDLE, "returned %d with %d (expected ERROR_INVALID_HANDLE)\n", res, GetLastError()); gbl = GlobalAlloc(GMEM_DDESHARE, 100); /* first free */ mem = GlobalFree(gbl); ok(mem == NULL, "Expected NULL, got %p\n", mem); /* invalid free */ if (sizeof(void *) != 8) /* crashes on 64-bit Vista */ { SetLastError(MAGIC_DEAD); mem = GlobalFree(gbl); ok(mem == gbl || broken(mem == NULL) /* nt4 */, "Expected gbl, got %p\n", mem); if (mem == gbl) ok(GetLastError() == ERROR_INVALID_HANDLE || GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */ "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); } /* GMEM_FIXED block expands in place only without flags */ for (size = 1; size <= max_size; size <<= 1) { gbl = GlobalAlloc(GMEM_FIXED, init_size); SetLastError(MAGIC_DEAD); hsecond = GlobalReAlloc(gbl, size + init_size, 0); ok(hsecond == gbl || (hsecond == NULL && GetLastError() == ERROR_NOT_ENOUGH_MEMORY), "got %p with %x (expected %p or NULL) @%ld\n", hsecond, GetLastError(), gbl, size); GlobalFree(gbl); } /* GMEM_FIXED block can be relocated with GMEM_MOVEABLE */ for (size = 1; size <= max_size; size <<= 1) { gbl = GlobalAlloc(GMEM_FIXED, init_size); SetLastError(MAGIC_DEAD); hsecond = GlobalReAlloc(gbl, size + init_size, GMEM_MOVEABLE); ok(hsecond != NULL, "got %p with %x (expected non-NULL) @%ld\n", hsecond, GetLastError(), size); mem = GlobalLock(hsecond); ok(mem == hsecond, "got %p (expected %p) @%ld\n", mem, hsecond, size); GlobalFree(hsecond); } gbl = GlobalAlloc(GMEM_DDESHARE, 100); res = GlobalUnlock(gbl); ok(res == 1 || broken(res == 0), /* win9x */ "Expected 1 or 0, got %d\n", res); res = GlobalUnlock(gbl); ok(res == 1 || broken(res == 0), /* win9x */ "Expected 1 or 0, got %d\n", res); GlobalFree(gbl); gbl = GlobalAlloc(GMEM_FIXED, 100); SetLastError(0xdeadbeef); res = GlobalUnlock(gbl); ok(res == 1 || broken(res == 0), /* win9x */ "Expected 1 or 0, got %d\n", res); ok(GetLastError() == 0xdeadbeef, "got %d\n", GetLastError()); GlobalFree(gbl); /* GlobalSize on an invalid handle */ if (sizeof(void *) != 8) /* crashes on 64-bit Vista */ { SetLastError(MAGIC_DEAD); size = GlobalSize((HGLOBAL)0xc042); ok(size == 0, "Expected 0, got %ld\n", size); ok(GetLastError() == ERROR_INVALID_HANDLE || GetLastError() == ERROR_INVALID_PARAMETER, /* win9x */ "Expected ERROR_INVALID_HANDLE or ERROR_INVALID_PARAMETER, got %d\n", GetLastError()); } gbl = GlobalAlloc( GMEM_FIXED, 0 ); SetLastError(0xdeadbeef); size = GlobalSize( gbl ); ok( size == 1, "wrong size %lu\n", size ); GlobalFree( gbl ); /* ####################################### */ /* Local*() functions */ gbl = LocalAlloc(LMEM_MOVEABLE, 0); ok(gbl != NULL, "local memory not allocated for size 0\n"); gbl = LocalReAlloc(gbl, 10, LMEM_MOVEABLE); ok(gbl != NULL, "Can't realloc local memory\n"); size = LocalSize(gbl); ok(size >= 10 && size <= 16, "Memory not resized to size 10, instead size=%ld\n", size); gbl = LocalReAlloc(gbl, 0, LMEM_MOVEABLE); ok(gbl != NULL, "LocalReAlloc should not fail on size 0\n"); size = LocalSize(gbl); ok(size == 0, "Memory not resized to size 0, instead size=%ld\n", size); ok(LocalFree(gbl) == NULL, "Memory not freed\n"); size = LocalSize(gbl); ok(size == 0, "Memory should have been freed, size=%ld\n", size); gbl = LocalReAlloc(0, 10, LMEM_MOVEABLE); ok(gbl == NULL, "local realloc allocated memory\n"); /* LocalLock / LocalUnlock with a valid handle */ gbl = LocalAlloc(LMEM_MOVEABLE, 256); SetLastError(MAGIC_DEAD); mem = LocalLock(gbl); /* #1 */ ok(mem != NULL, "returned %p with %d (expected '!= NULL')\n", mem, GetLastError()); SetLastError(MAGIC_DEAD); flags = LocalFlags(gbl); ok( flags == 1, "returned 0x%04x with %d (expected '0x0001')\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); msecond = LocalLock(gbl); /* #2 */ ok( msecond == mem, "returned %p with %d (expected '%p')\n", msecond, GetLastError(), mem); SetLastError(MAGIC_DEAD); flags = LocalFlags(gbl); ok( flags == 2, "returned 0x%04x with %d (expected '0x0002')\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); SetLastError(MAGIC_DEAD); res = LocalUnlock(gbl); /* #1 */ ok(res, "returned %d with %d (expected '!= 0')\n", res, GetLastError()); SetLastError(MAGIC_DEAD); flags = LocalFlags(gbl); ok( flags , "returned 0x%04x with %d (expected '!= 0')\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); res = LocalUnlock(gbl); /* #0 */ /* NT: ERROR_SUCCESS (documented on MSDN), 9x: untouched */ ok(!res && ((GetLastError() == ERROR_SUCCESS) || (GetLastError() == MAGIC_DEAD)), "returned %d with %d (expected '0' with: ERROR_SUCCESS or " "MAGIC_DEAD)\n", res, GetLastError()); SetLastError(MAGIC_DEAD); flags = LocalFlags(gbl); ok( !flags , "returned 0x%04x with %d (expected '0')\n", flags, GetLastError()); /* Unlock an already unlocked Handle */ SetLastError(MAGIC_DEAD); res = LocalUnlock(gbl); /* NT: ERROR_NOT_LOCKED, 9x: untouched */ ok( !res && ((GetLastError() == ERROR_NOT_LOCKED) || (GetLastError() == MAGIC_DEAD)), "returned %d with %d (expected '0' with: ERROR_NOT_LOCKED or " "MAGIC_DEAD)\n", res, GetLastError()); LocalFree(gbl); /* invalid handles are caught in windows: */ SetLastError(MAGIC_DEAD); hsecond = LocalFree(gbl); /* invalid handle: free memory twice */ ok( (hsecond == gbl) && (GetLastError() == ERROR_INVALID_HANDLE), "returned %p with 0x%08x (expected %p with ERROR_INVALID_HANDLE)\n", hsecond, GetLastError(), gbl); SetLastError(MAGIC_DEAD); flags = LocalFlags(gbl); ok( (flags == LMEM_INVALID_HANDLE) && (GetLastError() == ERROR_INVALID_HANDLE), "returned 0x%04x with 0x%08x (expected LMEM_INVALID_HANDLE with " "ERROR_INVALID_HANDLE)\n", flags, GetLastError()); SetLastError(MAGIC_DEAD); size = LocalSize(gbl); ok( (size == 0) && (GetLastError() == ERROR_INVALID_HANDLE), "returned %ld with 0x%08x (expected '0' with ERROR_INVALID_HANDLE)\n", size, GetLastError()); SetLastError(MAGIC_DEAD); mem = LocalLock(gbl); ok( (mem == NULL) && (GetLastError() == ERROR_INVALID_HANDLE), "returned %p with 0x%08x (expected NULL with ERROR_INVALID_HANDLE)\n", mem, GetLastError()); /* This Test works the same on all Systems (GlobalUnlock() is different) */ SetLastError(MAGIC_DEAD); res = LocalUnlock(gbl); ok(!res && (GetLastError() == ERROR_INVALID_HANDLE), "returned %d with %d (expected '0' with ERROR_INVALID_HANDLE)\n", res, GetLastError()); /* LMEM_FIXED block expands in place only without flags */ for (size = 1; size <= max_size; size <<= 1) { gbl = LocalAlloc(LMEM_FIXED, init_size); SetLastError(MAGIC_DEAD); hsecond = LocalReAlloc(gbl, size + init_size, 0); ok(hsecond == gbl || (hsecond == NULL && GetLastError() == ERROR_NOT_ENOUGH_MEMORY), "got %p with %x (expected %p or NULL) @%ld\n", hsecond, GetLastError(), gbl, size); LocalFree(gbl); } /* LMEM_FIXED memory can be relocated with LMEM_MOVEABLE */ for (size = 1; size <= max_size; size <<= 1) { gbl = LocalAlloc(LMEM_FIXED, init_size); SetLastError(MAGIC_DEAD); hsecond = LocalReAlloc(gbl, size + init_size, LMEM_MOVEABLE); ok(hsecond != NULL, "got %p with %x (expected non-NULL) @%ld\n", hsecond, GetLastError(), size); mem = LocalLock(hsecond); ok(mem == hsecond, "got %p (expected %p) @%ld\n", mem, hsecond, size); LocalFree(hsecond); } /* trying to unlock pointer from LocalAlloc */ gbl = LocalAlloc(LMEM_FIXED, 100); SetLastError(0xdeadbeef); res = LocalUnlock(gbl); ok(res == 0, "Expected 0, got %d\n", res); ok(GetLastError() == ERROR_NOT_LOCKED || broken(GetLastError() == 0xdeadbeef) /* win9x */, "got %d\n", GetLastError()); LocalFree(gbl); gbl = LocalAlloc( LMEM_FIXED, 0 ); SetLastError(0xdeadbeef); size = LocalSize( gbl ); ok( !size || broken(size == 1), /* vistau64 */ "wrong size %lu\n", size ); LocalFree( gbl ); /* trying to lock empty memory should give an error */ gbl = GlobalAlloc(GMEM_MOVEABLE|GMEM_ZEROINIT,0); ok(gbl != NULL, "returned NULL\n"); SetLastError(MAGIC_DEAD); mem = GlobalLock(gbl); /* NT: ERROR_DISCARDED, 9x: untouched */ ok( (mem == NULL) && ((GetLastError() == ERROR_DISCARDED) || (GetLastError() == MAGIC_DEAD)), "returned %p with 0x%x/%d (expected 'NULL' with: ERROR_DISCARDED or " "MAGIC_DEAD)\n", mem, GetLastError(), GetLastError()); GlobalFree(gbl); /* trying to get size from data pointer (GMEM_MOVEABLE) */ gbl = GlobalAlloc(GMEM_MOVEABLE, 0x123); ok(gbl != NULL, "returned NULL\n"); mem = GlobalLock(gbl); ok(mem != NULL, "returned NULL.\n"); ok(gbl != mem, "unexpectedly equal.\n"); size = GlobalSize(gbl); size2 = GlobalSize(mem); ok(size == 0x123, "got %lu\n", size); ok(size2 == 0x123, "got %lu\n", size2); GlobalFree(gbl); /* trying to get size from data pointer (GMEM_FIXED) */ gbl = GlobalAlloc(GMEM_FIXED, 0x123); ok(gbl != NULL, "returned NULL\n"); mem = GlobalLock(gbl); ok(mem != NULL, "returned NULL.\n"); ok(gbl == mem, "got %p, %p.\n", gbl, mem); size = GlobalSize(gbl); ok(size == 0x123, "got %lu\n", size); GlobalFree(gbl); size = GlobalSize((void *)0xdeadbee0); ok(size == 0, "got %lu\n", size); }
// --------------------------------------------------------------------------- void __fastcall FtpControl::Execute() { DWORD nopwait = 0; DWORD nopcmdtm = GetTickCount(); DWORD secondtm = GetTickCount(); NameThreadForDebugging("FtpControl"); fdeb("FtpControl thread %x", GetCurrentThreadId()); // Priority = tpIdle; fce = new TEvent(false); while (true) { Sleep(50); if (GetTickCount() - secondtm >= 1000) { secondtm = GetTickCount(); call_events(E_SECOND, (LPVOID)rand()); } // // if (GetTickCount() - nopcmdtm >= nopwait) // { // nopwait = 23400 + (rand() % 13400); // // if (!FtpSocket || processing_disk_event) // continue; // // char *cmds[] = // { // "NOp", "fEAT", "NooP", "STAT", "HELP", "TYPE", "SYST", "PWD" // }; // // int num = rand() % 8; // // ftplock(); // // ftpcmd(cmds[num], 0, 0); // // char b[11111]; // ftprl(b, timeoutst, 0); // // while (ftphasdata()) // ftprl(b, timeoutst, 0); // // ftpunlock(); // // // ftpunlock(); // // nopcmdtm = GetTickCount(); // } if (!ccs) continue; ccs->Enter(); for (vector<CACHE>::iterator it = cache.begin(); it != cache.end(); it++) { if (GetTickCount() - (*it).lastaccessed >= 10000) { try { if ((SIZE_T)HeapSize(GetProcessHeap(), 0, (*it).buf) != (SIZE_T) - 1) { // fdeb(E_CACHEMSG, "cache: freeing %7lu bytes (%7lu in records) of '%s' @ %08X", // HeapSize(GetProcessHeap(), 0, (*it).buf), (*it).size, (*it).fn, (*it).buf); int numcaches = ((int)cache.size()) - 1; unsigned long heapsiz; heapsiz = (unsigned long)HeapSize(GetProcessHeap(), 0, (*it).buf); HeapFree(GetProcessHeap(), 0, (*it).buf); it = cache.erase(it); call_events(E_CACHEFREED, (LPVOID)(*it).buf, (LPVOID)(*it).fn, (LPVOID)heapsiz, (LPVOID)(*it).offset, (LPVOID)numcaches); if (it == cache.end()) break; } else { deb(E_FTPCONTROL, "cache block @ %p for '%s' size: %lu is not heap allocated!", (*it).buf, (*it).fn, (*it).size); } } catch(...) { exp; } } // // // decrease blocks // unsigned long maxcachebufsize = precacheminbytes; // 8 * 1024 * 1024; // if ((*it).bufsize > maxcachebufsize) // { // unsigned long newsiz = maxcachebufsize - 512000; // memcpy((*it).buf, (void*)((unsigned long)(*it).buf + (unsigned long)(*it).size - // (unsigned long)maxcachebufsize), newsiz); // // (*it).buf = HeapReAlloc(GetProcessHeap(), 0, (*it).buf, newsiz); // cache_list(); // deb(E_CACHEMSG, "cache block %p truncated from %lu to %lu", (*it).buf, (*it).bufsize, // newsiz); // // // (*it).offset += ((*it).bufsize - newsiz); // if (!(*it).buf) // ds; // (*it).size = newsiz; // (*it).bufsize = newsiz; // } } ccs->Leave(); // fdeb(E_CACHEMSG,"check for cache"); files_sort_diskevent(); int seqreads; seqreads = cache_seq_reads(); for (vector<FATFILE>::iterator it = files.begin(); it != files.end(); it++) { if (GetTickCount() - (*it).lastaccessed >= 2000) { (*it).sequential_reads = 0; } if ((*it).lastlen && GetTickCount() - (*it).lastdisktime <= 1500) { // fdeb("checking %s", (*it).fn); unsigned long precachesize = 0; unsigned long readoffset = 0; unsigned long flen = 0; unsigned long stofs = 0; // (*it).lastoffset + (*it).lastlen; if (!(*it).sequential_reads) { // fdeb(E_CACHEMSG, "have another %lu bytes @ %lu", (*it).lastlen * 10, // stofs - (*it).lastoffset + (*it).lastlen); // fdeb( "skip sequential_read"); continue; } else { // fdeb(E_CACHEMSG, "sequential_reads %lu, cache %lu @ %lu", (*it).sequential_reads, // (*it).nextcachelen, (*it).nextcacheoffset); if (in_cache((*it).dosfn, (*it).lastoffset, (*it).nextcachelen)) { // fdeb("skip precache"); continue; } } // stofs = highcacheoffset((*it).dosfn); // stofs = seqcacheoffset((*it).dosfn); stofs = (*it).nextcacheoffset; if (stofs <= 65535) stofs = 0; deb("nextcachelen=%lu", (*it).nextcachelen); flen = (*it).nextcachelen; if (!flen) flen = (*it).nextcachelen ; if (!flen) flen = 512000; (*it).nowcaching = true; (*it).nowcachingoffset = stofs; (*it).nowcachinglen = flen; strcpy(nowcachingdosfn, (*it).dosfn); // fdeb(E_CACHEMSG, "seqcacheoffset %lu", stofs); if (!ftptrylock()) continue; char curdir[255]; int code = 250; strncpy(curdir, ftpcurdir, sizeof(curdir)); fdeb("allocating %lu bytes", flen); char *buf = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, flen); fdeb("flen %lu, buf @ %p %lu", flen, buf, HeapSize(GetProcessHeap(), 0, buf)); // ftpcs->Enter(); // fdeb(" if (strcmpi((*it).ftppath, ftpcurdir) != 0) { ftpcd("/"); code = 250; if (strlen((*it).ftppath)) code = ftpcd((*it).ftppath); if (code != 250) { ftpcd("/"); // ecs->Leave(); // processing_disk_event = false; // ftpcs->Leave(); ftpunlock(); continue; } strcpy(ftpcurdir, (*it).ftppath); } fdeb("caching %lu '%s' @ %lu ", flen, (*it).fn, stofs); call_events(E_STARTPRECACHE, (LPVOID)(*it).fn, (LPVOID)flen, (LPVOID)stofs); fdeb("ftpgetfile(%-15s, %8lu, %8p, %8lu)", (*it).fn, (unsigned long)stofs, buf, (unsigned long)flen); call_events(E_FTPPREGETFILE, (LPVOID)(*it).ftppath, (LPVOID)(*it).fn, (LPVOID)stofs, (LPVOID)buf, (LPVOID)flen); DWORD ftpgettm = GetTickCount(); unsigned long nrd = ftpgetfile((*it).fn, (unsigned long)stofs, (char*)buf, flen, (*it).dosfn, 65535, stofs); ftpunlock(); // ftpcs->Leave(); HeapFree(GetProcessHeap(), 0, buf); (*it).nowcaching = false; ftpgettm = GetTickCount() - ftpgettm; // fdeb(E_CACHEMSG, "precacheclstrs [%lu]", (*it).precacheclstrs); fdeb("ftpgetfile(%-15s, %8lu, %8p, %8lu) = %lu", (*it).fn, (unsigned long)stofs, buf, (unsigned long)flen, nrd); call_events(E_FTPGETFILE, (LPVOID)(*it).ftppath, (LPVOID)(*it).fn, (LPVOID)stofs, (LPVOID)buf, (LPVOID)flen, (LPVOID)ftpcontroltime, (LPVOID)ftpdatatime, (LPVOID)nrd); if (nrd > 0) { // cache_save((*it).ftppath, (*it).dosfn, ((unsigned long)stofs), nrd, buf); (*it).nextcacheoffset = stofs + flen; // (*it).lastoffset = stofs; } else { (*it).lastlen = 0; fdeb("zero download %lu @ %lu fsize %lu!", flen, stofs, (*it).size); // ds; } nopcmdtm = GetTickCount(); } } } }
std::size_t TLMemory::Platform::MemSize(void* pMem) { return HeapSize( g_MemHeap, 0x0, pMem ); }
static HRESULT assemble_shader(const char *preproc_shader, ID3DBlob **shader_blob, ID3DBlob **error_messages) { struct bwriter_shader *shader; char *messages = NULL; HRESULT hr; DWORD *res; LPD3DBLOB buffer; int size; char *pos; shader = SlAssembleShader(preproc_shader, &messages); if (messages) { TRACE("Assembler messages:\n"); TRACE("%s", messages); TRACE("Shader source:\n"); TRACE("%s\n", debugstr_a(preproc_shader)); if (error_messages) { const char *preproc_messages = *error_messages ? ID3D10Blob_GetBufferPointer(*error_messages) : NULL; size = strlen(messages) + (preproc_messages ? strlen(preproc_messages) : 0) + 1; hr = D3DCreateBlob(size, &buffer); if (FAILED(hr)) { HeapFree(GetProcessHeap(), 0, messages); if (shader) SlDeleteShader(shader); return hr; } pos = ID3D10Blob_GetBufferPointer(buffer); if (preproc_messages) { CopyMemory(pos, preproc_messages, strlen(preproc_messages) + 1); pos += strlen(preproc_messages); } CopyMemory(pos, messages, strlen(messages) + 1); if (*error_messages) ID3D10Blob_Release(*error_messages); *error_messages = buffer; } HeapFree(GetProcessHeap(), 0, messages); } if (shader == NULL) { ERR("Asm reading failed\n"); return D3DXERR_INVALIDDATA; } hr = SlWriteBytecode(shader, 9, &res); SlDeleteShader(shader); if (FAILED(hr)) { ERR("SlWriteBytecode failed with 0x%08x\n", hr); return D3DXERR_INVALIDDATA; } if (shader_blob) { size = HeapSize(GetProcessHeap(), 0, res); hr = D3DCreateBlob(size, &buffer); if (FAILED(hr)) { HeapFree(GetProcessHeap(), 0, res); return hr; } CopyMemory(ID3D10Blob_GetBufferPointer(buffer), res, size); *shader_blob = buffer; } HeapFree(GetProcessHeap(), 0, res); return S_OK; }
size_t DefaultMovable::size(const void* ptr) { return HeapSize(GetProcessHeap(), 0, ptr); }
static size_t win32memsize(void *p) { return HeapSize(win32heap, 0, p); }
//вернуть длину строки без завершающего нулевого символа int Length(wchar_t *str) { if (str == NULL) return 0; HANDLE hHeap = GetProcessHeap(); return ((int)HeapSize(hHeap, 0, str) / sizeof(wchar_t)) - 1; }
static void test_heap_checks( DWORD flags ) { BYTE old, *p, *p2; BOOL ret; SIZE_T i, size, large_size = 3000 * 1024 + 37; if (flags & HEAP_PAGE_ALLOCS) return; /* no tests for that case yet */ trace( "testing heap flags %08x\n", flags ); p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 ); ok( p != NULL, "HeapAlloc failed\n" ); ret = HeapValidate( GetProcessHeap(), 0, p ); ok( ret, "HeapValidate failed\n" ); size = HeapSize( GetProcessHeap(), 0, p ); ok( size == 17, "Wrong size %lu\n", size ); ok( p[14] == 0, "wrong data %x\n", p[14] ); ok( p[15] == 0, "wrong data %x\n", p[15] ); ok( p[16] == 0, "wrong data %x\n", p[16] ); if (flags & HEAP_TAIL_CHECKING_ENABLED) { ok( p[17] == 0xab, "wrong padding %x\n", p[17] ); ok( p[18] == 0xab, "wrong padding %x\n", p[18] ); ok( p[19] == 0xab, "wrong padding %x\n", p[19] ); } p2 = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, p, 14 ); if (p2 == p) { if (flags & HEAP_TAIL_CHECKING_ENABLED) { ok( p[14] == 0xab, "wrong padding %x\n", p[14] ); ok( p[15] == 0xab, "wrong padding %x\n", p[15] ); ok( p[16] == 0xab, "wrong padding %x\n", p[16] ); } else { ok( p[14] == 0, "wrong padding %x\n", p[14] ); ok( p[15] == 0, "wrong padding %x\n", p[15] ); } } else skip( "realloc in place failed\n"); ret = HeapFree( GetProcessHeap(), 0, p ); ok( ret, "HeapFree failed\n" ); p = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 17 ); ok( p != NULL, "HeapAlloc failed\n" ); old = p[17]; p[17] = 0xcc; if (flags & HEAP_TAIL_CHECKING_ENABLED) { ret = HeapValidate( GetProcessHeap(), 0, p ); ok( !ret, "HeapValidate succeeded\n" ); /* other calls only check when HEAP_VALIDATE is set */ if (flags & HEAP_VALIDATE) { size = HeapSize( GetProcessHeap(), 0, p ); ok( size == ~(SIZE_T)0 || broken(size == ~0u), "Wrong size %lu\n", size ); p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 ); ok( p2 == NULL, "HeapReAlloc succeeded\n" ); ret = HeapFree( GetProcessHeap(), 0, p ); ok( !ret || broken(sizeof(void*) == 8), /* not caught on xp64 */ "HeapFree succeeded\n" ); } p[17] = old; size = HeapSize( GetProcessHeap(), 0, p ); ok( size == 17, "Wrong size %lu\n", size ); p2 = HeapReAlloc( GetProcessHeap(), 0, p, 14 ); ok( p2 != NULL, "HeapReAlloc failed\n" ); p = p2; } ret = HeapFree( GetProcessHeap(), 0, p ); ok( ret, "HeapFree failed\n" ); p = HeapAlloc( GetProcessHeap(), 0, 37 ); ok( p != NULL, "HeapAlloc failed\n" ); memset( p, 0xcc, 37 ); ret = HeapFree( GetProcessHeap(), 0, p ); ok( ret, "HeapFree failed\n" ); if (flags & HEAP_FREE_CHECKING_ENABLED) { ok( p[16] == 0xee, "wrong data %x\n", p[16] ); ok( p[17] == 0xfe, "wrong data %x\n", p[17] ); ok( p[18] == 0xee, "wrong data %x\n", p[18] ); ok( p[19] == 0xfe, "wrong data %x\n", p[19] ); ret = HeapValidate( GetProcessHeap(), 0, NULL ); ok( ret, "HeapValidate failed\n" ); old = p[16]; p[16] = 0xcc; ret = HeapValidate( GetProcessHeap(), 0, NULL ); ok( !ret, "HeapValidate succeeded\n" ); p[16] = old; ret = HeapValidate( GetProcessHeap(), 0, NULL ); ok( ret, "HeapValidate failed\n" ); } /* now test large blocks */ p = HeapAlloc( GetProcessHeap(), 0, large_size ); ok( p != NULL, "HeapAlloc failed\n" ); ret = HeapValidate( GetProcessHeap(), 0, p ); ok( ret, "HeapValidate failed\n" ); size = HeapSize( GetProcessHeap(), 0, p ); ok( size == large_size, "Wrong size %lu\n", size ); ok( p[large_size - 2] == 0, "wrong data %x\n", p[large_size - 2] ); ok( p[large_size - 1] == 0, "wrong data %x\n", p[large_size - 1] ); if (flags & HEAP_TAIL_CHECKING_ENABLED) { /* Windows doesn't do tail checking on large blocks */ ok( p[large_size] == 0xab || broken(p[large_size] == 0), "wrong data %x\n", p[large_size] ); ok( p[large_size+1] == 0xab || broken(p[large_size+1] == 0), "wrong data %x\n", p[large_size+1] ); ok( p[large_size+2] == 0xab || broken(p[large_size+2] == 0), "wrong data %x\n", p[large_size+2] ); if (p[large_size] == 0xab) { p[large_size] = 0xcc; ret = HeapValidate( GetProcessHeap(), 0, p ); ok( !ret, "HeapValidate succeeded\n" ); /* other calls only check when HEAP_VALIDATE is set */ if (flags & HEAP_VALIDATE) { size = HeapSize( GetProcessHeap(), 0, p ); ok( size == ~(SIZE_T)0, "Wrong size %lu\n", size ); p2 = HeapReAlloc( GetProcessHeap(), 0, p, large_size - 3 ); ok( p2 == NULL, "HeapReAlloc succeeded\n" ); ret = HeapFree( GetProcessHeap(), 0, p ); ok( !ret, "HeapFree succeeded\n" ); } p[large_size] = 0xab; } } ret = HeapFree( GetProcessHeap(), 0, p ); ok( ret, "HeapFree failed\n" ); /* test block sizes when tail checking */ if (flags & HEAP_TAIL_CHECKING_ENABLED) { for (size = 0; size < 64; size++) { p = HeapAlloc( GetProcessHeap(), 0, size ); for (i = 0; i < 32; i++) if (p[size + i] != 0xab) break; ok( i >= 8, "only %lu tail bytes for size %lu\n", i, size ); HeapFree( GetProcessHeap(), 0, p ); } } }
LPVOID GetURL_NoProxy(LPCSTR lpszURL, DWORD *lpdwMemLength) { char szHostName[MAX_PATH]; char szExtraInfo[MAX_PATH]; char szScheme[MAX_PATH]; char szUrlPath[MAX_PATH]; URL_COMPONENTS UrlComponents = {}; UrlComponents.lpszScheme = szScheme; UrlComponents.dwSchemeLength = MAX_PATH; UrlComponents.lpszHostName = szHostName; UrlComponents.dwHostNameLength = MAX_PATH; UrlComponents.lpszUrlPath = szUrlPath; UrlComponents.dwUrlPathLength = MAX_PATH; UrlComponents.lpszExtraInfo = szExtraInfo; UrlComponents.dwExtraInfoLength = MAX_PATH; UrlComponents.dwStructSize = sizeof(URL_COMPONENTS); if (NULL == InternetCrackUrl(lpszURL, 0, 0, &UrlComponents)) return NULL; CHAR szHeaders[1024]; sprintf(szHeaders, "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*\r\n" "Accept-Language: en-us\r\n" "Proxy-Connection: Keep-Alive\r\n" "Pragma: no-cache" ); HINTERNET hInternet = InternetOpen("Mozilla/4.0 (compatible; MSIE 6.0)", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); if (NULL == hInternet) return NULL; HINTERNET hConnect = InternetConnect(hInternet, UrlComponents.lpszHostName, UrlComponents.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL); if (NULL == hConnect) { InternetCloseHandle(hInternet); return NULL; } CHAR szObjectName[MAX_PATH]; sprintf(szObjectName, "%s%s", UrlComponents.lpszUrlPath, UrlComponents.lpszExtraInfo); HINTERNET hRequest = HttpOpenRequest(hConnect, "GET", szObjectName, NULL, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, NULL ); if (NULL == hRequest) { InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } DWORD dwTimeout = 60000; InternetSetOption(hRequest, INTERNET_OPTION_CONNECT_TIMEOUT, &dwTimeout, sizeof(DWORD)); InternetSetOption(hRequest, INTERNET_OPTION_CONTROL_SEND_TIMEOUT, &dwTimeout, sizeof(DWORD)); InternetSetOption(hRequest, INTERNET_OPTION_RECEIVE_TIMEOUT, &dwTimeout, sizeof(DWORD)); BOOL bRet = HttpSendRequest(hRequest, szHeaders, strlen(szHeaders), NULL, 0); DWORD dwStatusCode; DWORD dwBufferLength = sizeof(DWORD); HttpQueryInfo(hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatusCode, &dwBufferLength, 0); if (FALSE == bRet || 200 != dwStatusCode) { InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } LPVOID lpMem = NULL; DWORD dwReadSize = 0; while (1) { if (NULL != lpMem && HeapSize(GetProcessHeap(), 0, lpMem) - dwReadSize < 0x2000) { if (HeapSize(GetProcessHeap(), 0, lpMem) - dwReadSize < 0x2000) { LPVOID lpNewMem = HeapReAlloc(GetProcessHeap(), 8, lpMem, HeapSize(GetProcessHeap(), 0, lpMem) + 0x2000); if (NULL == lpNewMem) break; lpMem = lpNewMem; } } else { lpMem = HeapAlloc(GetProcessHeap(), 8, 0x2000); if (NULL == lpMem) goto END; } DWORD dwRead = 0; if (!InternetReadFile(hRequest, ((char *)lpMem) + dwReadSize, 0x2000, &dwRead)) break; if (0 == dwRead) goto END; dwReadSize += dwRead; *lpdwMemLength = dwReadSize; } HeapFree(GetProcessHeap(), 0, lpMem); lpMem = NULL; END: InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return lpMem; }
void* __cdecl xf_realloc ( void * _Memory, size_t _Size #ifdef TRACK_MEMORY_ALLOCATIONS , LPCSTR lpszFileName, int nLine #endif ) { _ASSERTE(ghHeap); _ASSERTE(_Size>0); if (!_Memory) { return xf_malloc(_Size #ifdef TRACK_MEMORY_ALLOCATIONS , lpszFileName, nLine #endif ); } #ifdef TRACK_MEMORY_ALLOCATIONS xf_mem_block* pOld = ((xf_mem_block*)_Memory)-1; size_t _Size1 = HeapSize(ghHeap, 0, pOld); _ASSERTE(_Size1 < (_Size+sizeof(xf_mem_block)+8)); size_t _Size2 = 0; if (pOld->bBlockUsed == TRUE) { int nCCcmp = memcmp(((LPBYTE)_Memory)+pOld->nBlockSize, "\xCC\xCC\xCC\xCC\xCC\xCC\xCC\xCC", 8); _ASSERTE(nCCcmp == 0); _ASSERTE(_Size1 == (pOld->nBlockSize+sizeof(xf_mem_block)+8)); _Size2 = pOld->nBlockSize; } else { _ASSERTE(pOld->bBlockUsed == TRUE); if (_Size1 > (sizeof(xf_mem_block)+8)) _Size2 = _Size1 - (sizeof(xf_mem_block)+8); } xf_mem_block* p = (xf_mem_block*)HeapReAlloc(ghHeap, 0, pOld, _Size+sizeof(xf_mem_block)+8); if (p) { p->bBlockUsed = TRUE; p->nBlockSize = _Size; msprintf(p->sCreatedFrom, countof(p->sCreatedFrom), "%s:%i", _PointToName(lpszFileName), nLine); #ifdef _DEBUG if (_Size > _Size2) memset(((LPBYTE)(p+1))+_Size2, 0xFD, _Size - _Size2); #endif memset(((LPBYTE)(p+1))+_Size, 0xCC, 8); } else { _ASSERTE(p!=NULL); } return p?(p+1):p; #else void* p = HeapReAlloc(ghHeap, HEAP_ZERO_MEMORY, _Memory, _Size); return p; #endif }