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