Apply clang-format across the whole tree
Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs, Allman functions) over every source file so the tree is formatter-clean. Whitespace only -- no behavior change; full x64 + x86 suites pass. Also set SortIncludes: false in .clang-format. Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h / dinput.h; winsock2.h must precede windows.h), and the default alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build break. Leaving order alone keeps the manual, correct grouping.
This commit is contained in:
@@ -15,8 +15,7 @@
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace coop
|
||||
{
|
||||
namespace coop {
|
||||
|
||||
// 'CLOG' little-endian.
|
||||
inline constexpr std::uint32_t kLogRingMagic = 0x474F4C43u;
|
||||
@@ -25,33 +24,30 @@ inline constexpr std::uint32_t kLogRingVersion = 1;
|
||||
// Per-pid mapping name, mirroring the other channels: coop_log_<pid>.
|
||||
inline constexpr wchar_t kLogRingPrefix[] = L"Local\\coop_log_";
|
||||
|
||||
inline constexpr std::uint32_t kLogMsgLen = 192; // chars per line (incl. NUL)
|
||||
inline constexpr std::uint32_t kLogMsgLen = 192; // chars per line (incl. NUL)
|
||||
inline constexpr std::uint32_t kLogCapacity = 1024; // ring records
|
||||
|
||||
// Severity of a log line; drives the host Log window's colour. Stored in
|
||||
// LogRecord::level. Info is 0 so existing/zero-filled records read as Info.
|
||||
enum LogLevel : std::uint32_t
|
||||
{
|
||||
enum LogLevel : std::uint32_t {
|
||||
LogLevel_Info = 0,
|
||||
LogLevel_Warn = 1,
|
||||
LogLevel_Error = 2,
|
||||
};
|
||||
|
||||
struct LogRecord
|
||||
{
|
||||
struct LogRecord {
|
||||
std::atomic<std::uint64_t> seq; // 0 = empty; else (global index + 1) once written
|
||||
std::uint32_t pid;
|
||||
std::uint32_t level; // LogLevel
|
||||
std::uint32_t level; // LogLevel
|
||||
std::uint64_t millis; // producer timestamp (GetTickCount64)
|
||||
char text[kLogMsgLen];
|
||||
};
|
||||
|
||||
struct LogRing
|
||||
{
|
||||
struct LogRing {
|
||||
std::uint32_t magic;
|
||||
std::uint32_t version;
|
||||
std::uint32_t capacity; // number of records
|
||||
std::uint32_t msg_len; // kLogMsgLen (sanity)
|
||||
std::uint32_t capacity; // number of records
|
||||
std::uint32_t msg_len; // kLogMsgLen (sanity)
|
||||
std::atomic<std::uint64_t> write_index; // total records ever claimed (free-running)
|
||||
std::uint8_t reserved[32];
|
||||
// LogRecord records[capacity] follows immediately.
|
||||
@@ -83,13 +79,11 @@ inline void log_ring_init(LogRing& r, std::uint32_t capacity)
|
||||
|
||||
inline bool log_ring_valid(const LogRing& r)
|
||||
{
|
||||
return r.magic == kLogRingMagic && r.version == kLogRingVersion && r.capacity != 0 &&
|
||||
r.msg_len == kLogMsgLen;
|
||||
return r.magic == kLogRingMagic && r.version == kLogRingVersion && r.capacity != 0 && r.msg_len == kLogMsgLen;
|
||||
}
|
||||
|
||||
// Producer (hook): append a line at severity `level` (LogLevel). Multi-producer safe.
|
||||
inline void log_ring_push(LogRing& r, std::uint32_t pid, std::uint32_t level, std::uint64_t millis,
|
||||
const char* text)
|
||||
inline void log_ring_push(LogRing& r, std::uint32_t pid, std::uint32_t level, std::uint64_t millis, const char* text)
|
||||
{
|
||||
const std::uint64_t idx = r.write_index.fetch_add(1, std::memory_order_acq_rel);
|
||||
LogRecord& rec = log_ring_records(&r)[idx % r.capacity];
|
||||
@@ -115,23 +109,19 @@ template <typename F>
|
||||
inline void log_ring_drain(LogRing& r, std::uint64_t& cursor, F&& emit)
|
||||
{
|
||||
const std::uint64_t w = r.write_index.load(std::memory_order_acquire);
|
||||
if (w <= cursor)
|
||||
{
|
||||
if (w <= cursor) {
|
||||
return;
|
||||
}
|
||||
const std::uint64_t lo = (w > r.capacity) ? (w - r.capacity) : 0;
|
||||
std::uint64_t i = cursor < lo ? lo : cursor; // skip records already overwritten
|
||||
LogRecord* recs = log_ring_records(&r);
|
||||
for (; i < w; ++i)
|
||||
{
|
||||
for (; i < w; ++i) {
|
||||
LogRecord& rec = recs[i % r.capacity];
|
||||
const std::uint64_t s1 = rec.seq.load(std::memory_order_acquire);
|
||||
if (s1 <= i)
|
||||
{
|
||||
if (s1 <= i) {
|
||||
break; // generation i not written yet (in-flight, or being overwritten); retry next call
|
||||
}
|
||||
if (s1 != i + 1)
|
||||
{
|
||||
if (s1 != i + 1) {
|
||||
continue; // s1 > i+1: overwritten by a later generation before we got here; lost, skip
|
||||
}
|
||||
// Seqlock read: copy the record out, then re-check seq. A producer overwriting this slot stores
|
||||
@@ -142,8 +132,7 @@ inline void log_ring_drain(LogRing& r, std::uint64_t& cursor, F&& emit)
|
||||
snap.millis = rec.millis;
|
||||
std::memcpy(snap.text, rec.text, kLogMsgLen);
|
||||
std::atomic_thread_fence(std::memory_order_acquire);
|
||||
if (rec.seq.load(std::memory_order_relaxed) == i + 1)
|
||||
{
|
||||
if (rec.seq.load(std::memory_order_relaxed) == i + 1) {
|
||||
emit(snap); // consistent snapshot
|
||||
}
|
||||
// else: overwritten while we copied -> skip (lost)
|
||||
|
||||
Reference in New Issue
Block a user