SHARE
TWEET

Untitled

a guest Jul 18th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FixedSizeQueue::FixedSizeQueue(const std::string& filename, size_t size)
  2.     : size_(size)
  3.     , head_(0)
  4.     , tail_(0)
  5. {
  6.     fopen_s(&file_, filename.c_str(), "w+");
  7.     InitializeCriticalSection(&critical_section_);
  8. }
  9.  
  10. void
  11. FixedSizeQueue::push_values(int* values, size_t count)
  12. {
  13.     Logger::report("Called push_values");
  14.     EnterCriticalSection(&critical_section_);
  15.     size_t free_items = (tail_ > head_) ? size_ - tail_ + head_ : size_ - head_ + tail_;
  16.     if (count > free_items)
  17.     {
  18.         Logger::report("Buffer is full, can't push new values.");
  19.         exit(1);
  20.     }
  21.  
  22.     size_t till_end = (tail_ >= head_) ? size_ - tail_ : head_ - tail_;
  23.  
  24.     if (count < till_end)
  25.     {
  26.         fseek(file_, tail_ * sizeof(int), SEEK_SET);
  27.         int g = fwrite(values, sizeof(int), count, file_);
  28.         assert(g == count);
  29.  
  30.         tail_ += count;
  31.     }
  32.     else
  33.     {
  34.         fseek(file_, tail_ * sizeof(int), SEEK_SET);
  35.         int h = fwrite(values, sizeof(int), till_end, file_);
  36.         assert(h == till_end);
  37.         fseek(file_, tail_ * sizeof(int), SEEK_SET);
  38.         h = fwrite(values + count, sizeof(int), count - till_end, file_);
  39.         assert(h == count - till_end);
  40.  
  41.         tail_ = count - till_end;
  42.     }
  43.     fflush(file_);
  44.     LeaveCriticalSection(&critical_section_);
  45. }
  46.  
  47. size_t
  48. FixedSizeQueue::get_values(int* values)
  49. {
  50.     Logger::report("Called get_values");
  51.     EnterCriticalSection(&critical_section_);
  52.     const size_t item_count = (tail_ >= head_) ? tail_ - head_ : size_ - head_ + tail_;
  53.     if (tail_ > head_)
  54.     {
  55.         fseek(file_, head_ * sizeof(int), SEEK_SET);
  56.         fread(values, sizeof(int), item_count, file_);
  57.     }
  58.     else
  59.     {
  60.         fseek(file_, (size_ - head_) * sizeof(int), SEEK_SET);
  61.         fread(values, sizeof(int), size_ - head_, file_);
  62.         fseek(file_, 0, SEEK_SET);
  63.         fread(values + size_ - head_, sizeof(int), tail_, file_);
  64.     }
  65.  
  66.     head_ = tail_ = 0;
  67.  
  68.     LeaveCriticalSection(&critical_section_);
  69.  
  70.     return item_count;
  71. }
  72.      
  73. fopen_s( path, "wc") // w - write mode, c - allow immediate commit to disk
  74.      
  75. _flushall()
  76.      
  77. fclose()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top