dynamic_string::dynamic_string(const char *p) { VOGL_ASSUME(cMaxDynamicStringBufSize <= cINT32_MAX); // This class assumes little endian byte order, because the low bits of m_dyn.m_pStr must alias m_small.m_flag VOGL_ASSUME(VOGL_LITTLE_ENDIAN_CPU); VOGL_ASSERT(p); set_to_empty_small_string(); set(p); }
void vogl_enable_fail_exceptions(bool enabled) { // Make sure off_t is ALWAYS 64-bits. VOGL_ASSUME(sizeof(off_t) == sizeof(uint64_t)); g_fail_exceptions = enabled; }
char *file_utils::get_exec_filename(char *pPath, size_t dest_len) { #if VOGL_HAS_PROC_FILESYSTEM ssize_t s = readlink("/proc/self/exe", pPath, dest_len); if (s >= 0) { pPath[s] = '\0'; return pPath; } VOGL_VERIFY(0); pPath[0] = '\0'; return pPath; #elif defined(VOGL_USE_OSX_API) char **argv = *_NSGetArgv(); strncpy(pPath, argv[0], dest_len); vogl_warning_printf("UNTESTED: file_utils::get_exec_filename returning [%s]\n", pPath); return pPath; #elif defined(VOGL_USE_WIN32_API) DWORD result = GetModuleFileNameA(0, pPath, safe_int_cast<DWORD>(dest_len)); VOGL_VERIFY(result != 0); return pPath; #else VOGL_ASSUME(!"Implement get_exec_filename for this platform."); #endif }
void semaphore::release(int32 releaseCount, int32 *pPreviousCount) { VOGL_ASSUME(sizeof(LONG) == sizeof(int32)); if (0 == ReleaseSemaphore(m_handle, releaseCount, (LPLONG)pPreviousCount)) { VOGL_FAIL("semaphore: ReleaseSemaphore() failed"); } }
size_t plat_rand_s(vogl::uint32* out_array, size_t out_array_length) { VOGL_ASSUME(sizeof(vogl::uint32) == sizeof(unsigned int)); size_t ret_values = 0; for (ret_values = 0; ret_values < out_array_length; ++ret_values) { if (FAILED(rand_s(&out_array[ret_values]))) return ret_values; } return ret_values; }
mutex::mutex(unsigned int spin_count) { VOGL_ASSUME(sizeof(mutex) >= sizeof(CRITICAL_SECTION)); void *p = m_buf; CRITICAL_SECTION &m_cs = *static_cast<CRITICAL_SECTION *>(p); BOOL status = true; status = InitializeCriticalSectionAndSpinCount(&m_cs, spin_count); if (!status) vogl_fail("mutex::mutex: InitializeCriticalSectionAndSpinCount failed", __FILE__, __LINE__); #ifdef VOGL_BUILD_DEBUG m_lock_count = 0; #endif }
char *file_utils::get_exec_filename(char *pPath, size_t dest_len) { #if VOGL_HAS_PROC_FILESYSTEM ssize_t s = readlink("/proc/self/exe", pPath, dest_len); if (s >= 0) { pPath[s] = '\0'; return pPath; } VOGL_VERIFY(0); pPath[0] = '\0'; return pPath; #elif defined(VOGL_USE_WIN32_API) DWORD result = GetModuleFileNameA(0, pPath, dest_len); VOGL_VERIFY(result != 0); return pPath; #else VOGL_ASSUME(!"Implement get_exec_filename for this platform."); #endif }
// pTrace_archive may be NULL. Takes ownership of pTrace_archive. // TODO: Get rid of the demarcation packet, etc. Make the initial sequence of packets more explicit. bool vogl_trace_file_writer::open(const char *pFilename, vogl_archive_blob_manager *pTrace_archive, bool delete_archive, bool write_demarcation_packet, uint pointer_sizes) { VOGL_FUNC_TRACER close(); if (!pFilename) return false; m_filename = pFilename; if (!m_stream.open(pFilename, cDataStreamWritable | cDataStreamSeekable, false)) { vogl_error_printf("%s: Failed opening trace file \"%s\"\n", VOGL_METHOD_NAME, pFilename); return false; } vogl_message_printf("%s: Prepping trace file \"%s\"\n", VOGL_METHOD_NAME, pFilename); m_sof_packet.init(); m_sof_packet.m_pointer_sizes = pointer_sizes; m_sof_packet.m_first_packet_offset = sizeof(m_sof_packet); md5_hash h(gen_uuid()); VOGL_ASSUME(sizeof(h) == sizeof(m_sof_packet.m_uuid)); memcpy(&m_sof_packet.m_uuid, &h, sizeof(h)); m_sof_packet.finalize(); VOGL_VERIFY(m_sof_packet.full_validation(sizeof(m_sof_packet))); if (m_stream.write(&m_sof_packet, sizeof(m_sof_packet)) != sizeof(m_sof_packet)) { vogl_error_printf("%s: Failed writing to trace file \"%s\"\n", VOGL_METHOD_NAME, pFilename); return false; } if (pTrace_archive) { m_pTrace_archive.reset(pTrace_archive); m_delete_archive = delete_archive; } else { m_pTrace_archive.reset(vogl_new(vogl_archive_blob_manager)); m_delete_archive = true; if (!m_pTrace_archive->init_file_temp(cBMFReadWrite, NULL)) { vogl_error_printf("%s: Failed opening temp archive!\n", VOGL_METHOD_NAME); m_pTrace_archive.reset(); return false; } } // TODO: The trace reader records the first offset right after SOF, I would like to do this after the demarcation packet. m_frame_file_offsets.reserve(10000); m_frame_file_offsets.resize(0); m_frame_file_offsets.push_back(m_stream.get_ofs()); write_ctypes_packet(); write_entrypoints_packet(); if (write_demarcation_packet) { vogl_write_glInternalTraceCommandRAD(m_stream, m_pCTypes, cITCRDemarcation, 0, NULL); } vogl_message_printf("%s: Finished opening trace file \"%s\"\n", VOGL_METHOD_NAME, pFilename); return true; }
bool semaphore::try_release(int32 releaseCount, int32 *pPreviousCount) { VOGL_ASSUME(sizeof(LONG) == sizeof(int32)); return ReleaseSemaphore(m_handle, releaseCount, (LPLONG)pPreviousCount) != 0; }
void spinlock::lock(uint32 max_spins, bool yielding) { if (g_number_of_processors <= 1) max_spins = 1; uint32 spinCount = 0; uint32 yieldCount = 0; for (;;) { VOGL_ASSUME(sizeof(long) == sizeof(int32)); if (!InterlockedExchange((volatile long *)&m_flag, TRUE)) break; YieldProcessor(); YieldProcessor(); YieldProcessor(); YieldProcessor(); YieldProcessor(); YieldProcessor(); YieldProcessor(); YieldProcessor(); spinCount++; if ((yielding) && (spinCount >= max_spins)) { switch (yieldCount) { case 0: { spinCount = 0; Sleep(0); yieldCount++; break; } case 1: { if (g_number_of_processors <= 1) spinCount = 0; else spinCount = max_spins / 2; Sleep(1); yieldCount++; break; } case 2: { if (g_number_of_processors <= 1) spinCount = 0; else spinCount = max_spins; Sleep(2); break; } } } } VOGL_MEMORY_IMPORT_BARRIER }