Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / memory / memory_wedge.cc
blob10e885e7372b64dffd47275c0b90ec9861743310
1 // Copyright 2015 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.
5 #include "ios/chrome/browser/memory/memory_wedge.h"
7 #include <stdlib.h>
9 #include "base/logging.h"
11 namespace {
13 // Reference to the memory wedge. It is useful to:
14 // - make sure the compiler optimizations in Release don't skip |memset| and
15 // really end up putting the wedge in resident memory;
16 // - free it in tests.
17 static void* gMemoryWedge;
19 // The number of bytes in a megabyte.
20 const size_t kNumBytesInMB = 1024 * 1024;
22 } // namespace
24 namespace memory_wedge {
26 void AddWedge(size_t wedge_size_in_mb) {
27 DCHECK(!gMemoryWedge);
28 if (wedge_size_in_mb == 0)
29 return;
31 // Allocate a wedge and write to it to have it in resident memory.
32 const size_t wedge_size = wedge_size_in_mb * kNumBytesInMB;
33 gMemoryWedge = malloc(wedge_size);
34 memset(gMemoryWedge, -1, wedge_size);
37 void RemoveWedgeForTesting() {
38 DCHECK(gMemoryWedge);
39 free(gMemoryWedge);
40 gMemoryWedge = nullptr;
43 } // namespace memory_wedge