/** * @date 2012/01/03 * * Frees previously allocated data. * ******************************************************************************/ void* MemAlignProbe::MemAlign(size_t i_Boundary, size_t i_Size, const CallStack& Callers) { uint8_t *Data = NULL; uint32_t Padding = MemProbe::GetAlignmentPadding(i_Boundary, sizeof(HeapEntry)); if(AlignFunc != NULL) { if(GetHeap()->Lock() == 0) { Data = (uint8_t*)AlignFunc(i_Boundary, i_Size + sizeof(HeapEntry) + Padding); if(Data != NULL) { ExeContext *p_Context = ExeContext::Get(Callers); HeapEntry *Entry = new (Data + Padding) HeapEntry(i_Size, p_Context); Entry->Start = Data; GetHeap()->GetEntryList()->AppendItem(Entry); if(p_Context != NULL) p_Context->UpdateMemory(i_Size); Data = Data + sizeof(HeapEntry) + Padding; } } else { Data = (uint8_t*)AlignFunc(i_Boundary, i_Size); } GetHeap()->Unlock(); } return(Data); }
SharedPkt::~SharedPkt() { if (_allocation != nullptr) { GetHeap().Release(*this); _allocation = nullptr; // getting a wierd case were the destructor for one object was called twice...? Unclear why } }
void ZapImage::CopyWin32VersionResource() { #ifndef FEATURE_PAL // Copy the version resource over so it is easy to see in the dumps where the ngened module came from COUNT_T cbResourceData; PVOID pResourceData = m_ModuleDecoder.GetWin32Resource(MAKEINTRESOURCE(1), RT_VERSION, &cbResourceData); if (!pResourceData || !cbResourceData) return; ZapBlob * pVersionData = new (GetHeap()) ZapBlobPtr(pResourceData, cbResourceData); ZapVersionResource * pVersionResource = new (GetHeap()) ZapVersionResource(pVersionData); m_pWin32ResourceSection->Place(pVersionResource); m_pWin32ResourceSection->Place(pVersionData); SetDirectoryEntry(IMAGE_DIRECTORY_ENTRY_RESOURCE, m_pWin32ResourceSection); #endif }
SharedPkt::SharedPkt(const SharedPkt& cloneFrom) : Allocation(cloneFrom), _size(cloneFrom._size) { GetHeap().AddRef(*this); }
static void init_usd(ext2fs_st *st) { USD_PartitionInfo info; Type_Any any; IDCOffer_clp partition; USDTRC(printf("ext2fs: Creating USDCallbacks\n")); { IDCTransport_clp shmt; IDCOffer_clp offer; IDCService_clp service; L1_st *l1_st; /* Get a heap writable by the USD domain */ st->disk.heap = Gatekeeper$GetHeap(Pvs(gkpr), IDCOffer$PDID(st->disk.drive_offer), 0, SET_ELEM(Stretch_Right_Read) | SET_ELEM(Stretch_Right_Write), True); /* The L1 callback executes there */ l1_st = Heap$Malloc(st->disk.heap, sizeof(L1_st)); l1_st->ext2fs_st = st; /* our state is read-only in USD domain */ st->disk.l1_callback = &(l1_st->l1_callback); CLP_INIT(st->disk.l1_callback, &L1_ms, l1_st); /* Create an offer for the L2 Callback */ CL_INIT(st->disk.l2_callback, &L2_ms, st); ANY_INIT(&any,USDCallback_clp, &st->disk.l2_callback); shmt = NAME_FIND ("modules>ShmTransport", IDCTransport_clp); offer = IDCTransport$Offer (shmt, &any, NULL, Pvs(heap), Pvs(gkpr), Pvs(entry), &service); /* Put it in the L1 callback's state record */ l1_st->l2_offer = offer; } USDTRC(printf("ext2fs: Getting USDCtl offer\n")); if(!USDDrive$GetPartition(st->disk.usddrive, st->disk.partition, st->disk.l1_callback, &partition)) { return; } USDTRC(printf("ext2fs: Binding to USDCtl offer\n")); /* Bind to the USD. Do this explicitly rather than through the object table; we don't want anybody else getting hold of this one. */ st->disk.binding = IDCOffer$Bind(partition, Pvs(gkpr), &any); st->disk.usdctl = NARROW(&any, USDCtl_clp); /* Find out some information */ USDTRC(printf("ext2fs: fetching partition info\n")); if(!USDDrive$GetPartitionInfo(st->disk.usddrive, st->disk.partition, &info)) { return; } st->disk.phys_block_size = info.blocksize; st->disk.partition_size = info.size; DBO(printf("ext2fs: blocksize %d, partition size %d blocks,\n" " partition type %s\n", st->disk.phys_block_size, st->disk.partition_size, info.osname)); FREE(info.osname); return; }
void ZapImage::CopyDebugDirEntry() { // Insert an NGEN PDB debug directory entry *before* the IL PDB debug directory entry // (if one exists), so that we don't break tools that look for an IL PDB. { // This entry is initially empty. It is filled in ZapImage::GenerateFile. RSDS rsds = {0}; m_pNGenPdbDebugData = ZapBlob::NewBlob(static_cast<ZapWriter *>(this), &rsds, sizeof rsds); } // IL PDB entry: copy of the (first of possibly many) IMAGE_DEBUG_DIRECTORY entry // in the IL image DWORD nDebugEntry = 0; PIMAGE_DEBUG_DIRECTORY pDebugDir = NULL; ZapNode **ppDebugData = NULL; if (m_ModuleDecoder.HasDirectoryEntry(IMAGE_DIRECTORY_ENTRY_DEBUG)) { COUNT_T debugEntrySize; TADDR pDebugEntry = m_ModuleDecoder.GetDirectoryEntryData(IMAGE_DIRECTORY_ENTRY_DEBUG, &debugEntrySize); if (debugEntrySize != 0) { if (debugEntrySize < sizeof(IMAGE_DEBUG_DIRECTORY) || 0 != (debugEntrySize % sizeof(IMAGE_DEBUG_DIRECTORY))) { m_zapper->Warning(W("IMAGE_DIRECTORY_ENTRY_DEBUG size (%d) should be a multiple of %d\n"), debugEntrySize, sizeof(IMAGE_DEBUG_DIRECTORY)); } else { // Since pDebugEntry is an array of IMAGE_DEBUG_DIRECTORYs, debugEntrySize // should be a multiple of sizeof(IMAGE_DEBUG_DIRECTORY). _ASSERTE(0 == (debugEntrySize % sizeof(IMAGE_DEBUG_DIRECTORY))); nDebugEntry = DWORD(debugEntrySize / sizeof(IMAGE_DEBUG_DIRECTORY)); pDebugDir = new (GetHeap()) IMAGE_DEBUG_DIRECTORY[nDebugEntry]; memcpy(pDebugDir, (const void *)pDebugEntry, sizeof(IMAGE_DEBUG_DIRECTORY) * nDebugEntry); ppDebugData = new (GetHeap()) ZapNode*[nDebugEntry]; memset(ppDebugData, 0, nDebugEntry * sizeof(ZapNode*)); for (DWORD i = 0; i < nDebugEntry; i++) { // Some compilers set PointerToRawData but not AddressOfRawData as they put the // data at the end of the file in an unmapped part of the file RVA rvaOfRawData = (pDebugDir[i].AddressOfRawData != NULL) ? pDebugDir[i].AddressOfRawData : m_ModuleDecoder.OffsetToRva(pDebugDir[i].PointerToRawData); ULONG cbDebugData = pDebugDir[i].SizeOfData; if (cbDebugData != 0) { if (!m_ModuleDecoder.CheckRva(rvaOfRawData, cbDebugData)) m_zapper->Warning(W("IMAGE_DIRECTORY_ENTRY_DEBUG points to bad data\n")); else ppDebugData[i] = new (GetHeap()) ZapBlobPtr((PVOID)m_ModuleDecoder.GetRvaData(rvaOfRawData), cbDebugData); } } } } } ZapDebugDirectory * pDebugDirectory = new (GetHeap()) ZapDebugDirectory(m_pNGenPdbDebugData, nDebugEntry, pDebugDir, ppDebugData); m_pDebugSection->Place(pDebugDirectory); m_pDebugSection->Place(m_pNGenPdbDebugData); if (ppDebugData) { for (DWORD i = 0; i < nDebugEntry; i++) { if (ppDebugData[i] != nullptr) m_pDebugSection->Place(ppDebugData[i]); } } SetDirectoryEntry(IMAGE_DIRECTORY_ENTRY_DEBUG, pDebugDirectory); }
void ZapImage::CopyDebugDirEntry() { // Insert an NGEN PDB debug directory entry *before* the IL PDB debug directory entry // (if one exists), so that we don't break tools that look for an IL PDB. { // This entry is initially empty. It is filled in ZapImage::GenerateFile. RSDS rsds = {0}; m_pNGenPdbDebugData = ZapBlob::NewBlob(static_cast<ZapWriter *>(this), &rsds, sizeof rsds); } // IL PDB entry: copy of the (first of possibly many) IMAGE_DEBUG_DIRECTORY entry // in the IL image PIMAGE_DEBUG_DIRECTORY pDebugDir = NULL; ZapBlob *pDebugData = NULL; if (m_ModuleDecoder.HasDirectoryEntry(IMAGE_DIRECTORY_ENTRY_DEBUG)) { COUNT_T debugEntrySize; TADDR pDebugEntry = m_ModuleDecoder.GetDirectoryEntryData(IMAGE_DIRECTORY_ENTRY_DEBUG, &debugEntrySize); if (debugEntrySize != 0) { if (debugEntrySize < sizeof(IMAGE_DEBUG_DIRECTORY) || 0 != (debugEntrySize % sizeof(IMAGE_DEBUG_DIRECTORY))) { m_zapper->Warning(W("IMAGE_DIRECTORY_ENTRY_DEBUG size (%d) should be a multiple of %d\n"), debugEntrySize, sizeof(IMAGE_DEBUG_DIRECTORY)); } else { // Since pDebugEntry is an array of IMAGE_DEBUG_DIRECTORYs, debugEntrySize // should be a multiple of sizeof(IMAGE_DEBUG_DIRECTORY). _ASSERTE(0 == (debugEntrySize % sizeof(IMAGE_DEBUG_DIRECTORY))); // @TODO: pDebugEntry is an array of IMAGE_DEBUG_DIRECTORYs. Some tools // (like ibcmerge) add an extra dummy IMAGE_DEBUG_DIRECTORY to indicate // that the image was modified post-link. // We need to copy all the IMAGE_DEBUG_DIRECTORYs. For now, we only copy // the first one as it holds the relevant debug information. pDebugDir = PIMAGE_DEBUG_DIRECTORY(pDebugEntry); // Some compilers set PointerToRawData but not AddressOfRawData as they put the // data at the end of the file in an unmapped part of the file RVA rvaOfRawData = (pDebugDir->AddressOfRawData != NULL) ? pDebugDir->AddressOfRawData : m_ModuleDecoder.OffsetToRva(pDebugDir->PointerToRawData); ULONG cbDebugData = pDebugDir->SizeOfData; if (cbDebugData != 0) { if (!m_ModuleDecoder.CheckRva(rvaOfRawData, cbDebugData)) m_zapper->Warning(W("IMAGE_DIRECTORY_ENTRY_DEBUG points to bad data\n")); else pDebugData = new (GetHeap()) ZapBlobPtr((PVOID)m_ModuleDecoder.GetRvaData(rvaOfRawData), cbDebugData); } } } } ZapDebugDirectory * pDebugDirectory = new (GetHeap()) ZapDebugDirectory(m_pNGenPdbDebugData, pDebugData ? pDebugDir : NULL, pDebugData); m_pDebugSection->Place(pDebugDirectory); m_pDebugSection->Place(m_pNGenPdbDebugData); if (pDebugData) m_pDebugSection->Place(pDebugData); SetDirectoryEntry(IMAGE_DIRECTORY_ENTRY_DEBUG, pDebugDirectory); }
static void *internal_Allocate(int size, int flags, char *file, int line){ #else static void *internal_Allocate(int size, int flags){ #endif // assert(size!=48); //int log=1; //int tmp = size-1; //while((tmp>>=1)!=0) log++; //printf("AllocateSize %d,%d\n",size,log); if(!initialized){ heap_sem = new Semaphore(); heap_sem->Acquire(); #ifdef MEMORY_STATS thread_sem = new Semaphore(); thread_sem->Acquire(); unsigned long dwThreadId; CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ticker, 0, 0, &dwThreadId); // _beginthread(&ticker,4096,0); thread_sem->Acquire(); // Sleep(100000); #endif HANDLE file = CreateFileMapping((HANDLE)-1,0,PAGE_READWRITE,0,HEAP_SIZE+sizeof(Heap),0); printf("Total shared heap space allocated = %d bytes\n",heap_bytes+=HEAP_SIZE); Heap *h = (Heap*)MapViewOfFile(file,FILE_MAP_WRITE,0,0,0); assert(h); first_heap = h; h->Next = 0; h->File = file; memset(h->Mask,0,HEAP_SIZE/BLOCK_SIZE/8); memset(h->Type,0,HEAP_SIZE/BLOCK_SIZE/8); memset(h->Size,-1,HEAP_SIZE/BLOCK_SIZE*sizeof(int)); h->Blocks = (void*)(int(h)+sizeof(Heap)); initialized = true; // Assume we get no errors #ifdef _DEBUG void *block = internal_Allocate(BLOCK_SIZE-sizeof(MemHeader),flags,0,0); #else void *block = internal_Allocate(BLOCK_SIZE-sizeof(MemHeader),flags); #endif assert(block); if(!block){ heap_sem->Release(); printf("Failed!\n"); return 0; } int _size = 32*sizeof(Fragment*); // fixme: not 32 bits int log=1; int tmp = _size-1; while((tmp>>=1)!=0) log++; h = GetHeap(block); int blockn = h->GetBlock(block); h->Size[blockn] = size; h->Type[blockn/8]|=1<<(blockn&7); Fragments = (Fragment**)block; memset(Fragments,0,32*sizeof(Fragment*)); // fixme: not 32 bits Fragments[log] = (Fragment*)(int(block)+(2<<log)); Fragments[log]->Next = 0; Fragments[log]->Prev = 0; for(int n=3; n<(BLOCK_SIZE-sizeof(MemHeader))>>log; n++){ Fragment *frag = (Fragment*)(int(block)+(n<<log)); Fragments[log]->Next = frag; frag->Next = 0; frag->Prev = Fragments[log]; Fragments[log] = frag; } heap_sem->Release(); #ifdef MEMORY_STATS total_used_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0); total_size_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0); total_frag_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0); total_frags_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0); total_blocks_history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0); total_used_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY); total_size_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY); total_frag_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY); total_frags_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY); total_blocks_history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY); #endif } heap_sem->Acquire(); int orgsize = size; size+=sizeof(MemHeader); if(size<sizeof(Fragment)) size = sizeof(Fragment); if(size<=BLOCK_SIZE/2){ int log=1; int tmp = size-1; while((tmp>>=1)!=0) log++; //printf("Frag %d!\n",log); if(!Fragments[log]){ //printf("Frag %d empty!\n",log); #ifdef _DEBUG void *block = internal_Allocate(BLOCK_SIZE-sizeof(MemHeader),flags,0,0); #else void *block = internal_Allocate(BLOCK_SIZE-sizeof(MemHeader),flags); #endif assert(block); if(!block){ heap_sem->Release(); assert(false); return 0; } Heap *h = GetHeap(block); int blockn = h->GetBlock(block); h->Size[blockn] = log; //printf("h->Size[3] = %d\n",h->Size[3]); h->Type[blockn/8]|=1<<(blockn&7); Fragments[log] = (Fragment*)block; Fragments[log]->Next = 0; Fragments[log]->Prev = 0; for(int n=2; n<(BLOCK_SIZE-sizeof(MemHeader))>>log; n++){ Fragment *frag = (Fragment*)(int(block)+(n<<log)); Fragments[log]->Next = frag; frag->Next = 0; frag->Prev = Fragments[log]; Fragments[log] = frag; } } // int frags = get_frag_count(); MemHeader *header = (MemHeader*)Fragments[log]; //printf("Alloc: %d\n",log); if(Fragments[log]->Prev) Fragments[log]->Prev->Next = Fragments[log]->Next; if(Fragments[log]->Next) Fragments[log]->Next->Prev = Fragments[log]->Prev; Fragments[log] = Fragments[log]->Prev; //DumpStuff(); //GetHeap(data); //#ifdef MEMORY_STATISTICS // heap_used+=orgsize; //#endif // frags = frags-get_frag_count(); //printf("Alloc: %d\n",frags); // assert(frags==1); #ifdef _DEBUG AddToFile(header,file,line,orgsize); //1<<log); #endif //printf("Alloc at %x\n",data); //printf("Alloc(%d) %x\n",orgsize,header); header->size = orgsize; //printf("a\n"); #ifdef MEMORY_STATS if(file&&line){ header->line = line; if(file){ header->file = (char*)internal_Allocate(strlen(file)+1,flags,0,0); sprintf(header->file,"%s",file); }else header->file = 0; //printf("b\n"); MemHeader *ent = list; while(ent){ if(ent->line==header->line){ if(ent->file&&header->file){ if(!strcmp(ent->file,header->file)){ header->listent = ent; ent->size+=orgsize; break; } }else if(!ent->file&&!!header->file){ header->listent = ent; ent->size+=orgsize; break; } } ent = ent->Next; } //printf("c\n"); if(!ent){ MemHeader *ent = (MemHeader*)internal_Allocate(sizeof(MemHeader),flags,0,0); ent->size = orgsize; ent->file = file; ent->line = line; ent->history = (DynamicArray*)internal_Allocate(sizeof(DynamicArray),flags,0,0); ent->history->DynamicArray::DynamicArray(DARRAY_SHARED_MEMORY); header->listent = ent; ent->Next = list; list = ent; } }else header->listent = 0; #endif //printf("d\n"); heap_used+=orgsize; //printf("Allocated %d bytes!\n",orgsize); heap_sem->Release(); //printf("e\n"); return (void*)(int(header)+sizeof(MemHeader)); }else{
static void Deallocate(void *p, size_t/* size*/) { ::HeapFree(GetHeap(), 0, p); }
static void *Allocate(size_t size) { return ::HeapAlloc(GetHeap(), HEAP_ZERO_MEMORY, size);; }