Guest User

Untitled

a guest
Jun 5th, 2020
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "page_locked_allocator.h"
  3. #include "run_time_error_helpers.h"
  4. #include <cassert>
  5. #include <Memoryapi.h>
  6. #include <iostream>
  7. void* page_locked_allocator_impl::allocation_impl(const size_t bytes)
  8. {
  9. const SIZE_T dwSize = bytes;
  10. const DWORD flAllocationType = MEM_COMMIT | MEM_RESERVE;
  11. const DWORD flProtect = PAGE_READWRITE;
  12. const auto lpAddress = VirtualAlloc(
  13. nullptr,
  14. dwSize,
  15. flAllocationType,
  16. flProtect
  17. );
  18. page_locked_increment(bytes);
  19. const auto success = VirtualLock(lpAddress, dwSize);
  20. if (lpAddress == NULL || success == NULL)
  21. {
  22. const auto some_error = GetLastError();
  23. const auto bad_alloc_message = "Bad Alloc: " + std::to_string(some_error);
  24. throw std::bad_alloc();
  25. //qli_runtime_error(bad_alloc_message);
  26. }
  27. return lpAddress;
  28. }
  29.  
  30. void page_locked_allocator_impl::page_locked_increment(const int inc)
  31. {
  32. const auto process_handle = GetCurrentProcess();
  33. SIZE_T minWorkingSet, maxWorkingSet;
  34. BOOL bRes = GetProcessWorkingSetSize(process_handle, &minWorkingSet, &maxWorkingSet);
  35. assert(bRes);
  36. SIZE_T newWorkingSetSize = maxWorkingSet + inc;
  37. bRes = SetProcessWorkingSetSize(process_handle, newWorkingSetSize, newWorkingSetSize);
  38. assert(bRes);
  39.  
  40. }
  41.  
  42. void page_locked_allocator_impl::deallocate_impl(void* p, size_t bytes)
  43. {
  44. //no throw lol
  45. VirtualFree(p, bytes, MEM_RELEASE);
  46. page_locked_increment((-1));
  47. }
Add Comment
Please, Sign In to add comment