void Profiler::EndProfile(char const *_name) { CoreAssert(this != NULL); if (!IsMasterThread()) return; //CoreAssert(m_currentElement->m_wasExpanded == m_currentElement->m_parent->m_isExpanded); if (m_currentElement && m_currentElement->m_parent ) { if (m_currentElement->m_parent->m_isExpanded) { if (m_doGlFinish && m_inRenderSection) { #ifdef ENABLE_OPENGL glFinish(); #endif } CoreAssert(m_currentElement != m_rootElement); CoreAssert(CrissCross::Data::Compare(_name, (const char *)m_currentElement->m_name) == 0); m_currentElement->End(); } CoreAssert(CrissCross::Data::Compare(m_currentElement->m_name, m_currentElement->m_parent->m_name) != 0); m_currentElement = m_currentElement->m_parent; } }
int64_t CoreIOReader::Position() { CoreAssert(this != NULL); CoreAssert(IsOpen()); #ifdef HAS_FPOS64 fpos64_t lastpos; #if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \ (defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS)) lastpos = _ftelli64(m_fileInputPointer); return lastpos; #elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \ defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD) || \ defined (TARGET_COMPILER_MINGW) fgetpos(m_fileInputPointer, &lastpos); return lastpos; #else fgetpos64(m_fileInputPointer, &lastpos); return lastpos.__pos; #endif #else fpos_t lastpos; lastpos = ftell(m_fileInputPointer); return lastpos; #endif }
Uint32 OpenGLGraphics::CreateFont(const char *_fontFace, int _height, bool _bold, bool _italic) { #ifdef ENABLE_FONTS char fontpath[512]; MemMappedFile *file = NULL; sprintf(fontpath, "fonts/%s.ttf", _fontFace); file = g_app->m_resource->GetUncompressedFile(fontpath); if ( !file ) { #ifdef TARGET_OS_WINDOWS char windir[128]; GetWindowsDirectory(windir, 128); sprintf(fontpath, "%s/fonts/%s.ttf", windir, _fontFace); #elif defined(TARGET_OS_MACOSX) sprintf(fontpath, "/Library/Fonts/%s.ttf", windir, _fontFace); #endif file = g_app->m_resource->GetUncompressedFile(fontpath); } CoreAssert(file); OpenGLFont *font = new OpenGLFont(file->m_data, file->m_size); CoreAssert(font); font->SetFontSize(_height); font->SetBold(_italic); font->SetItalic(_italic); return m_fonts.insert(font); #else return 0; #endif }
void Profiler::StartProfile(char const *_name) { CoreAssert(this != NULL); if (!IsMasterThread()) return; ProfiledElement *pe = m_currentElement->m_children.GetData(_name); if (!pe) { pe = new ProfiledElement(_name, m_currentElement, this); m_currentElement->m_children.PutData(_name, pe); } CoreAssert(m_rootElement->m_isExpanded); bool wasExpanded = m_currentElement->m_isExpanded; if (m_currentElement->m_isExpanded) { if (m_doGlFinish && m_inRenderSection) { #ifdef ENABLE_OPENGL glFinish(); #endif } pe->Start(); } m_currentElement = pe; m_currentElement->m_wasExpanded = wasExpanded; }
Mutex::Mutex(MutexType _type) : m_type(_type) { m_lockCount = 0; #ifdef TARGET_OS_WINDOWS InitializeCriticalSection(&m_mutex); #else int error; error = pthread_mutexattr_init(&m_mutexAttr); CoreAssert(error == 0); int ptype = 0; switch (m_type) { case MUTEX_TYPE_NORMAL: ptype = PTHREAD_MUTEX_NORMAL; break; case MUTEX_TYPE_ERRORCHECK: ptype = PTHREAD_MUTEX_ERRORCHECK; break; case MUTEX_TYPE_RECURSIVE: default: ptype = PTHREAD_MUTEX_RECURSIVE; break; } error = pthread_mutexattr_settype(&m_mutexAttr, ptype); CoreAssert(error == 0); error = pthread_mutex_init(&m_mutex, &m_mutexAttr); CoreAssert(error == 0); #endif }
void Mutex::Lock() { CoreAssert(this != NULL); #ifdef TARGET_OS_WINDOWS EnterCriticalSection(&m_mutex); #else int error = pthread_mutex_lock(&m_mutex); CoreAssert(error == 0); #endif m_lockCount++; }
Mutex::~Mutex() { CoreAssert(this != NULL); CoreAssert(m_lockCount == 0); #ifdef TARGET_OS_WINDOWS DeleteCriticalSection(&m_mutex); #else pthread_mutex_destroy(&m_mutex); pthread_mutexattr_destroy(&m_mutexAttr); #endif }
void Mutex::Unlock() { CoreAssert(this != NULL); CoreAssert(m_lockCount > 0); m_lockCount--; #ifdef TARGET_OS_WINDOWS LeaveCriticalSection(&m_mutex); #else int error = pthread_mutex_unlock(&m_mutex); CoreAssert(error == 0); #endif }
Mutex::Mutex() { m_lockCount = 0; #ifdef TARGET_OS_WINDOWS InitializeCriticalSection(&m_criticalSection); #else int error; error = pthread_mutexattr_init(&m_mutexAttr); CoreAssert(error == 0); error = pthread_mutexattr_settype(&m_mutexAttr, PTHREAD_MUTEX_RECURSIVE); CoreAssert(error == 0); error = pthread_mutex_init(&m_hMutex, &m_mutexAttr); CoreAssert(error == 0); #endif }
double ProfiledElement::GetMaxChildTime() { CoreAssert(this != NULL); double rv = 0.0; size_t first = m_children.StartOrderedWalk(); if (first == -1) { return 0.0; } size_t i = first; while (i != -1) { double val = m_children[i]->m_historyTotalTime; if (val > rv) { rv = val; } i = m_children.GetNextOrderedIndex(); } return rv / m_children[first]->m_historyNumSeconds; }
void Profiler::Advance() { CoreAssert(this != NULL); double timeNow = CrissCross::System::GetHighResTime(); if (timeNow > m_endOfSecond) { m_lengthOfLastSecond = timeNow - (m_endOfSecond - 1.0); m_endOfSecond = timeNow + 1.0; m_maxFound = 0.0f; m_rootElement->Advance(); } if( m_lastFrameStart >= 0 ) { double lastFrameTime = timeNow - m_lastFrameStart; m_frameTimes.insert_front( int( lastFrameTime * 1000 ) ); } while( m_frameTimes.valid(200) ) m_frameTimes.remove(200); m_lastFrameStart = timeNow; }
CrissCross::Errors CoreIOWriter::Write(const char *_format, ...) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; if (_format == NULL) return CC_ERR_BADPARAMETER; #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif va_list args; va_start(args, _format); /* Print out the string */ if (vfprintf(m_fileOutputPointer, _format, args) < 0) return CC_ERR_WRITE; fflush(m_fileOutputPointer); va_end(args); return CC_ERR_NONE; }
CrissCross::Errors CoreIOWriter::SetLineEndings(LineEndingType _ending) { CoreAssert(this != NULL); if (_ending == CC_LN_NATIVE) { #if defined (TARGET_OS_WINDOWS) _ending = CC_LN_CRLF; #elif defined (TARGET_OS_LINUX) || defined (TARGET_OS_MACOSX) || defined (TARGET_OS_FREEBSD) || \ defined (TARGET_OS_NETBSD) || defined (TARGET_OS_OPENBSD) || defined (TARGET_OS_NDSFIRMWARE) || \ defined (TARGET_OS_HAIKU) _ending = CC_LN_LF; #else #error You are not using a supported OS. #endif } switch (_ending) { case CC_LN_CR: sprintf(m_lineEnding, "\r"); break; case CC_LN_LF: sprintf(m_lineEnding, "\n"); break; case CC_LN_CRLF: sprintf(m_lineEnding, "\r\n"); break; default: return CC_ERR_BADPARAMETER; } return CC_ERR_NONE; }
void CPUID::DetectCacheInfo(int processor) { CoreAssert(this != NULL); if (proc[processor]->Manufacturer) { if (strcmp(proc[processor]->Manufacturer, "GenuineIntel") == 0) { int ntlb = 255, i; for (i = 0; i < ntlb; i++) { ntlb = Std[2].eax & 0xff; AddIntelCacheData(processor, Std[2].eax >> 8); AddIntelCacheData(processor, Std[2].eax >> 16); AddIntelCacheData(processor, Std[2].eax >> 24); if ((Std[2].ebx & 0x80000000) == 0) { AddIntelCacheData(processor, Std[2].ebx); AddIntelCacheData(processor, Std[2].ebx >> 8); AddIntelCacheData(processor, Std[2].ebx >> 16); AddIntelCacheData(processor, Std[2].ebx >> 24); } if ((Std[2].ecx & 0x80000000) == 0) { AddIntelCacheData(processor, Std[2].ecx); AddIntelCacheData(processor, Std[2].ecx >> 8); AddIntelCacheData(processor, Std[2].ecx >> 16); AddIntelCacheData(processor, Std[2].ecx >> 24); } if ((Std[2].edx & 0x80000000) == 0) { AddIntelCacheData(processor, Std[2].edx); AddIntelCacheData(processor, Std[2].edx >> 8); AddIntelCacheData(processor, Std[2].edx >> 16); AddIntelCacheData(processor, Std[2].edx >> 24); }
bool SortingHashTable<T>::erase(size_t _index) { bool retval = false; /* */ /* Remove data */ if (this->m_keys[_index] != (char *)-1) { retval = true; delete [] this->m_keys[_index]; } this->m_keys[_index] = (char *)-1; this->m_slotsFree++; /* */ /* Remove from ordered list */ short prevIndex = -1; short index = m_firstOrderedIndex; while (m_orderedIndices[index] != _index) { CoreAssert(index != -1); prevIndex = index; index = m_orderedIndices[index]; } if (prevIndex == -1) { m_firstOrderedIndex = -1; } else { m_orderedIndices[prevIndex] = m_orderedIndices[index]; } return retval; }
/* TODO: This function uses fgetc() which incurs unnecessary function call overhead. Find a suitable replacement. */ int CoreIOReader::ReadLine(std::string &_string) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif char c = (char)fgetc(m_fileInputPointer); if (c == (char)EOF) return 0; std::string buffer; while (c != (char)EOF && c != '\n') { buffer += c; c = (char)fgetc(m_fileInputPointer); } int len = (int)buffer.length(); if (len && buffer[len - 1] == '\r') buffer.resize(len - 1); _string = buffer; return (int)_string.length() * sizeof(char); }
void CPUID::DetectProcessorName(int processor) { CoreAssert(this != NULL); char *processorname = new char[(4 * 12) + 1]; char *_proc = &processorname[0]; memcpy(_proc, &Ext[2].eax, 4); _proc += 4; memcpy(_proc, &Ext[2].ebx, 4); _proc += 4; memcpy(_proc, &Ext[2].ecx, 4); _proc += 4; memcpy(_proc, &Ext[2].edx, 4); _proc += 4; memcpy(_proc, &Ext[3].eax, 4); _proc += 4; memcpy(_proc, &Ext[3].ebx, 4); _proc += 4; memcpy(_proc, &Ext[3].ecx, 4); _proc += 4; memcpy(_proc, &Ext[3].edx, 4); _proc += 4; memcpy(_proc, &Ext[4].eax, 4); _proc += 4; memcpy(_proc, &Ext[4].ebx, 4); _proc += 4; memcpy(_proc, &Ext[4].ecx, 4); _proc += 4; memcpy(_proc, &Ext[4].edx, 4); _proc += 4; *_proc = '\x0'; proc[processor]->ProcessorName = processorname; }
CrissCross::Errors CoreIOWriter::WriteLine(const char *_format, ...) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; if (_format == NULL) return CC_ERR_BADPARAMETER; #ifndef __GNUC__ m_ioMutex.Lock(); #endif va_list args; va_start(args, _format); /* Print out the string */ vfprintf(m_fileOutputPointer, _format, args); if (fprintf(m_fileOutputPointer, "%s", m_lineEnding) < 0) return CC_ERR_WRITE; va_end(args); Flush(); #ifndef __GNUC__ m_ioMutex.Unlock(); #endif return CC_ERR_NONE; }
/* * Read a directory entry, and return a pointer to a dirent structure * containing the name of the entry in d_name field. Individual directory * entries returned by this very function include regular files, * sub-directories, pseudo-directories "." and "..", but also volume labels, * hidden files and system files may be returned. */ struct dirent *readdir(DIR *dirp) { CoreAssert(dirp); if (dirp->search_handle == INVALID_HANDLE_VALUE) { /* directory stream was opened/rewound incorrectly or it ended normally */ return NULL; } /* get next directory entry */ if (dirp->cached != 0) { /* a valid directory entry already in memory */ dirp->cached = 0; } else { /* read next directory entry from disk */ if (FindNextFileA(dirp->search_handle, &dirp->current.data) == FALSE) { /* the very last file has been processed or an error occured */ FindClose(dirp->search_handle); dirp->search_handle = INVALID_HANDLE_VALUE; return NULL; } } /* copy as a multibyte character string */ strncpy(dirp->current.d_name, dirp->current.data.cFileName, sizeof(dirp->current.d_name)); dirp->current.d_name[MAX_PATH] = '\0'; return &dirp->current; }
CrissCross::Errors FileReader::Open(const char *_file) { CoreAssert(this != NULL); Close(); int _filePathLength = 0; if (_file == NULL) return CC_ERR_BADPARAMETER; if ((_filePathLength = (int)strlen(_file)) < 1) return CC_ERR_BADPARAMETER; delete [] (char *)m_filePath; m_filePath = new char[_filePathLength + 1]; strcpy((char *)m_filePath, _file); m_fileInputPointer = fopen(m_filePath, "rb"); if (m_fileInputPointer == NULL) return CC_ERR_FILE_OPEN; else return CC_ERR_NONE; }
int CoreIOReader::ReadU64(uint64_t *_buffer) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; if (!_buffer) return CC_ERR_INVALID_BUFFER; #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif size_t retval; retval = fread(_buffer, sizeof(uint64_t), 1, m_fileInputPointer); switch (m_endianness) { case CC_ENDIAN_LITTLE: *_buffer = CC_SwapLE64(*_buffer); break; case CC_ENDIAN_BIG: *_buffer = CC_SwapBE64(*_buffer); break; case CC_ENDIAN_NATIVE: /* Do nothing */ break; } return retval; }
RWLockHolder::RWLockHolder(RWLockHolder const &_lock) { CoreAssert(this != &_lock); _lock.m_copied = true; m_lock = _lock.m_lock; m_type = _lock.m_type; }
RWLockHolder::RWLockHolder(ReadWriteLock *_lock, RWLockHolderType _type) : m_lock(_lock), m_type(_type), m_copied(false) { CoreAssert(_lock); switch(_type) { case RW_LOCK_READ: m_lock->LockRead(); break; case RW_LOCK_WRITE: m_lock->LockWrite(); break; default: CoreAssert(_type != RW_LOCK_READ && _type != RW_LOCK_WRITE); } }
int CoreIOReader::ReadLine(char *_buffer, size_t _bufferLength) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; if (!_buffer) return CC_ERR_INVALID_BUFFER; if (!_bufferLength) return CC_ERR_INVALID_BUFFER; #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif /* We use fgets because it detects line endings. */ _buffer[0] = '\x0'; char *ret = fgets(_buffer, (int)_bufferLength, m_fileInputPointer); if (ret != _buffer) return -1; /* Detect line endings. */ char *endl = NULL; char *cr = strchr(_buffer, '\r'); char *lf = strchr(_buffer, '\n'); char *crlf = strstr(_buffer, "\r\n"); if (crlf) { SetLineEndings(CC_LN_CRLF); endl = crlf; } else if (cr) { SetLineEndings(CC_LN_CR); endl = cr; } else if (lf) { SetLineEndings(CC_LN_LF); endl = lf; } if (endl) *endl = '\x0'; return (int)strlen(_buffer); }
size_t SortingHashTable<T>::insert(char const *_key, T const &_data) { /* */ /* Make sure the table is big enough */ if (this->m_slotsFree * 2 <= this->m_size) { grow(); } /* */ /* Do the main insert */ size_t index = HashTable<T>::findInsertIndex(_key); CoreAssert(this->m_keys[index] == NULL || this->m_keys[index] == (char*)-1); this->m_keys[index] = cc_strdup(_key); this->m_data[index] = _data; this->m_slotsFree--; /* */ /* Insert us into the alphabetically order index list */ size_t i = findPrevKey(_key); if (i == -1) { /* Handle the case when the table is empty, or the new element is going */ /* to be the new first alphabetical element */ m_orderedIndices[index] = m_firstOrderedIndex; m_firstOrderedIndex = index; } else { m_orderedIndices[index] = m_orderedIndices[i]; m_orderedIndices[i] = index; } return index; }
int CoreIOReader::ReadBlock(void *_buffer, size_t _count) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; if (!_buffer) return CC_ERR_INVALID_BUFFER; if (!_count) return CC_ERR_INVALID_BUFFER; size_t retval; CoreAssert(_buffer != NULL); CoreAssert(_count > 0); #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif retval = fread(_buffer, _count, 1, m_fileInputPointer); return (int)retval; }
int64_t CoreIOReader::Length() { CoreAssert(this != NULL); CoreAssert(IsOpen()); #ifndef __GNUC__ MutexHolder mh(&m_ioMutex); #endif #ifdef HAS_FPOS64 fpos64_t lastpos, endpos; #if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \ (defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS)) lastpos = _ftelli64(m_fileInputPointer); _fseeki64(m_fileInputPointer, 0, SEEK_END); endpos = _ftelli64(m_fileInputPointer); _fseeki64(m_fileInputPointer, lastpos, SEEK_SET); #elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \ defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD) || \ defined (TARGET_COMPILER_MINGW) fgetpos(m_fileInputPointer, &lastpos); fseek(m_fileInputPointer, 0, SEEK_END); fgetpos(m_fileInputPointer, &endpos); fsetpos(m_fileInputPointer, &lastpos); #else fgetpos64(m_fileInputPointer, &lastpos); fseeko64(m_fileInputPointer, 0, SEEK_END); fgetpos64(m_fileInputPointer, &endpos); fsetpos64(m_fileInputPointer, &lastpos); #endif #else fpos_t lastpos, endpos; lastpos = ftell(m_fileInputPointer); fseek(m_fileInputPointer, 0, SEEK_END); endpos = ftell(m_fileInputPointer); fseek(m_fileInputPointer, lastpos, SEEK_SET); #endif #if defined (TARGET_OS_WINDOWS) || defined (TARGET_OS_MACOSX) || defined (TARGET_OS_FREEBSD) || \ defined (TARGET_OS_NETBSD) || defined (TARGET_OS_OPENBSD) || defined (TARGET_COMPILER_CYGWIN) || \ defined (TARGET_OS_NDSFIRMWARE) return endpos; #elif defined (TARGET_OS_LINUX) return endpos.__pos; #endif }
int CoreIOReader::Forward(int64_t _position) { CoreAssert(this != NULL); if (!IsOpen()) return CC_ERR_INVALID_BUFFER; int res = Seek(_position, SEEK_CUR); return (res == 0); }
void TigerHash::Reset() { CoreAssert(this != NULL); delete [] m_hash; m_hash = NULL; delete [] m_hashString; m_hashString = NULL; tiger_init(&m_state); }
void TigerHash::Finalize() { CoreAssert(this != NULL); if (m_hash) delete [] m_hash; m_hash = new unsigned char[TIGER_DIGEST_SIZE]; tiger_final(m_hash, &m_state); }