Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Coding style conformance

...

Code Block
bgColor#FFcccc
langcpp
#include <Windows.h>
#include <new>
 
void *operator new(std::size_t size) noexcept(false) {
  static HANDLE Hh = ::HeapCreate(0, 0, 0); // Private, expandable heap
  if (Hh) {
    return ::HeapAlloc(Hh, 0, size);
  }
  throw std::bad_alloc();
}

...

Code Block
bgColor#ccccff
langcpp
#include <Windows.h>
#include <new>

class HeapAllocator {
  static HANDLE Hh;
  static bool Initinit;
 
public:
  static void *alloc(std::size_t size) noexcept(false) {
    if (!Initinit) {
      Hh = ::HeapCreate(0, 0, 0); // Private, expandable heap
      Initinit = true;
    }
 
    if (Hh) {
      return ::HeapAlloc(Hh, 0, size);
    }
    throw std::bad_alloc();
  }
 
  static void dealloc(void *ptr) noexcept {
    if (Hh) {
      (void)::HeapFree(Hh, 0, ptr);
    }
  }
};
 
HANDLE HeapAllocator::Hh = nullptr;
bool HeapAllocator::Initinit = false;

void *operator new(std::size_t size) noexcept(false) {
  return HeapAllocator::alloc(size);
}
 
void operator delete(void *ptr) noexcept {
  return HeapAllocator::dealloc(ptr);
}
Page properties
hiddentrue

This code has a race condition. We should (1) fix the race condition, and (2) point to a rule about preventing race conditions (that we don't currently have!).

Noncompliant Code Example

...