EventRingBuffer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // EventRingBuffer.cpp - 环形缓冲实现
  2. #include <ntddk.h>
  3. #include <wdf.h>
  4. #include "EventRingBuffer.h"
  5. namespace commkit_driver {
  6. EventRingBuffer::EventRingBuffer()
  7. : buffer_(nullptr), head_(0), tail_(0), count_(0) {
  8. KeInitializeSpinLock(&lock_);
  9. }
  10. EventRingBuffer::~EventRingBuffer() {
  11. Cleanup();
  12. }
  13. NTSTATUS EventRingBuffer::Initialize() {
  14. // 分配非分页内存(可在 DISPATCH_LEVEL 访问)
  15. ULONG size = sizeof(COMMKIT_EVENT) * COMMKIT_RING_CAPACITY;
  16. buffer_ = (COMMKIT_EVENT*)ExAllocatePool2(
  17. POOL_FLAG_NON_PAGED, size, 'BCMK');
  18. if (!buffer_) {
  19. return STATUS_INSUFFICIENT_RESOURCES;
  20. }
  21. RtlZeroMemory(buffer_, size);
  22. head_ = tail_ = count_ = 0;
  23. return STATUS_SUCCESS;
  24. }
  25. void EventRingBuffer::Cleanup() {
  26. if (buffer_) {
  27. ExFreePoolWithTag(buffer_, 'BCMK');
  28. buffer_ = nullptr;
  29. }
  30. count_ = head_ = tail_ = 0;
  31. }
  32. void EventRingBuffer::Push(ULONG sequence, DOUBLE timestamp,
  33. ULONG com_number, ULONG event_type,
  34. ULONG data_size, PVOID data) {
  35. KIRQL old_irql;
  36. KeAcquireSpinLock(&lock_, &old_irql);
  37. if (!buffer_) {
  38. KeReleaseSpinLock(&lock_, old_irql);
  39. return;
  40. }
  41. // 截断超长数据
  42. ULONG copy_size = data_size;
  43. if (copy_size > COMMKIT_MAX_DATA) {
  44. copy_size = COMMKIT_MAX_DATA;
  45. }
  46. // 写入 head 位置
  47. COMMKIT_EVENT* slot = &buffer_[head_];
  48. slot->Sequence = (INT32)sequence;
  49. slot->TimeStamp = timestamp;
  50. slot->ComNumber = com_number;
  51. slot->EventType = event_type;
  52. slot->DataSize = copy_size;
  53. if (copy_size > 0 && data) {
  54. RtlCopyMemory(slot->Data, data, copy_size);
  55. }
  56. // 推进 head
  57. head_ = (head_ + 1) % COMMKIT_RING_CAPACITY;
  58. if (count_ == COMMKIT_RING_CAPACITY) {
  59. // 满了,覆盖最旧:tail 跟随 head
  60. tail_ = head_;
  61. } else {
  62. count_++;
  63. }
  64. KeReleaseSpinLock(&lock_, old_irql);
  65. }
  66. LONG EventRingBuffer::PopBatch(PCOMMKIT_EVENT out_buf, LONG max_count) {
  67. if (!out_buf || max_count <= 0) return 0;
  68. KIRQL old_irql;
  69. KeAcquireSpinLock(&lock_, &old_irql);
  70. if (!buffer_) {
  71. KeReleaseSpinLock(&lock_, old_irql);
  72. return 0;
  73. }
  74. LONG to_pop = count_ < max_count ? count_ : max_count;
  75. for (LONG i = 0; i < to_pop; ++i) {
  76. RtlCopyMemory(&out_buf[i], &buffer_[tail_], sizeof(COMMKIT_EVENT));
  77. tail_ = (tail_ + 1) % COMMKIT_RING_CAPACITY;
  78. }
  79. count_ -= to_pop;
  80. KeReleaseSpinLock(&lock_, old_irql);
  81. return to_pop;
  82. }
  83. void EventRingBuffer::Clear() {
  84. KIRQL old_irql;
  85. KeAcquireSpinLock(&lock_, &old_irql);
  86. head_ = tail_ = count_ = 0;
  87. KeReleaseSpinLock(&lock_, old_irql);
  88. }
  89. } // namespace commkit_driver