memory.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/process/memory.h"
  5. #include "build/build_config.h"
  6. #if BUILDFLAG(IS_WIN)
  7. #include <windows.h>
  8. #else
  9. #include <unistd.h>
  10. #endif // BUILDFLAG(IS_WIN)
  11. #include <string.h>
  12. #include "base/allocator/buildflags.h"
  13. #include "base/cxx17_backports.h"
  14. #include "base/debug/alias.h"
  15. #include "base/immediate_crash.h"
  16. #include "base/logging.h"
  17. #if BUILDFLAG(USE_PARTITION_ALLOC)
  18. #include "base/allocator/partition_allocator/page_allocator.h"
  19. #endif
  20. #include "build/build_config.h"
  21. namespace base {
  22. // Defined in memory_mac.mm for macOS + use_allocator="none". In case of
  23. // USE_PARTITION_ALLOC_AS_MALLOC, no need to route the call to the system
  24. // default calloc of macOS.
  25. #if !BUILDFLAG(IS_APPLE) || BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  26. bool UncheckedCalloc(size_t num_items, size_t size, void** result) {
  27. const size_t alloc_size = num_items * size;
  28. // Overflow check
  29. if (size && ((alloc_size / size) != num_items)) {
  30. *result = nullptr;
  31. return false;
  32. }
  33. if (!UncheckedMalloc(alloc_size, result))
  34. return false;
  35. memset(*result, 0, alloc_size);
  36. return true;
  37. }
  38. #endif // !BUILDFLAG(IS_APPLE) || BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  39. namespace internal {
  40. bool ReleaseAddressSpaceReservation() {
  41. #if BUILDFLAG(USE_PARTITION_ALLOC)
  42. return partition_alloc::ReleaseReservation();
  43. #else
  44. return false;
  45. #endif
  46. }
  47. } // namespace internal
  48. } // namespace base