int DefineList::add(const TCHAR *name, const TCHAR *value/*=_T("")*/) { int pos=SortedStringList<struct define>::add(name); if (pos == -1) { return 1; } TCHAR **newvalue=&(((struct define*) m_gr.get())[pos].value); size_t size_in_bytes = (_tcslen(value) + 1) * sizeof(TCHAR); *newvalue=(TCHAR*)malloc(size_in_bytes); if (!(*newvalue)) { extern int g_display_errors; extern void quit(); if (g_display_errors) { PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: DefineList malloc(%lu) failed.\n"), (unsigned long) size_in_bytes); } quit(); } _tcscpy(*newvalue,value); return 0; }
void *MMapFile::get(__int64 offset, int *sizep) const { if (!sizep) return NULL; assert(!m_pView); int size = *sizep; if (!m_iSize || offset + size > m_iSize) { extern void quit(); extern int g_display_errors; if (g_display_errors) { PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error mmapping file (%I64d, %d) is out of range.\n"), offset, size); } quit(); } // fix offset __int64 alignedoffset = offset - (offset % m_iAllocationGranularity); size += offset - alignedoffset; #ifdef _WIN32 assert(MAKEQWORD(LODWORD(alignedoffset),HIDWORD(alignedoffset)) == alignedoffset); m_pView = MapViewOfFile(m_hFileMap, m_bReadOnly ? FILE_MAP_READ : FILE_MAP_WRITE, HIDWORD(alignedoffset), LODWORD(alignedoffset), size); #else m_pView = mmap(0, size, m_bReadOnly ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, m_hFileDesc, alignedoffset); m_iMappedSize = *sizep = size; #endif #ifdef _WIN32 if (!m_pView) #else if (m_pView == MAP_FAILED) #endif { extern void quit(); extern int g_display_errors; if (g_display_errors) { PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error mmapping datablock to %d.\n"), size); } quit(); } return (void *)((char *)m_pView + offset - alignedoffset); }
void MMapFile::resize(__int64 newsize) { release(); if (newsize > m_iSize) { #ifdef _WIN32 if (m_hFileMap) CloseHandle(m_hFileMap); m_hFileMap = 0; #endif m_iSize = newsize; #ifdef _WIN32 if (m_hFile == INVALID_HANDLE_VALUE) { TCHAR buf[MAX_PATH], buf2[MAX_PATH]; GetTempPath(MAX_PATH, buf); GetTempFileName(buf, _T("nsd"), 0, buf2); m_hFile = CreateFile( buf2, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_SEQUENTIAL_SCAN, NULL ); m_bTempHandle = TRUE; } if (m_hFile != INVALID_HANDLE_VALUE) { m_hFileMap = CreateFileMapping( m_hFile, NULL, m_bReadOnly ? PAGE_READONLY : PAGE_READWRITE, HIDWORD(m_iSize), LODWORD(m_iSize), NULL ); } #else if (m_hFile == NULL) { m_hFile = tmpfile(); if (m_hFile != NULL) { m_hFileDesc = fileno(m_hFile); m_bTempHandle = TRUE; } } // resize if (m_hFileDesc != -1) { unsigned char c = 0; if (lseek(m_hFileDesc, m_iSize, SEEK_SET) != (off_t)-1) { if (read(m_hFileDesc, &c, 1) != -1) { if (lseek(m_hFileDesc, m_iSize, SEEK_SET) != (off_t)-1) { if (write(m_hFileDesc, &c, 1) != -1) { return; // no errors } } } } } m_hFileDesc = -1; // some error occurred, bail #endif #ifdef _WIN32 if (!m_hFileMap) #else if (m_hFileDesc == -1) #endif { extern void quit(); extern int g_display_errors; if (g_display_errors) { PrintColorFmtMsg_ERR(_T("\nInternal compiler error #12345: error creating mmap the size of %I64d.\n"), m_iSize); } quit(); } } }