Add base::UncheckedMalloc and associated utils to mini_chromium.
BUG=crashpad:158
Change-Id: Iab27c88981c862dbbe71c3374b3937ce950502fb
Reviewed-on: https://chromium-review.googlesource.com/438834
Reviewed-by: Scott Graham <scottmg@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
diff --git a/base/BUILD.gn b/base/BUILD.gn
index beba0cb..6b6b43d 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -47,6 +47,7 @@
"macros.h",
"memory/aligned_memory.cc",
"memory/aligned_memory.h",
+ "memory/free_deleter.h",
"memory/ptr_util.h",
"memory/scoped_policy.h",
"numerics/safe_conversions.h",
@@ -56,6 +57,8 @@
"posix/eintr_wrapper.h",
"posix/safe_strerror.cc",
"posix/safe_strerror.h",
+ "process/memory.cc",
+ "process/memory.h",
"rand_util.cc",
"rand_util.h",
"scoped_clear_errno.h",
diff --git a/base/base.gyp b/base/base.gyp
index f351d3b..9f0f535 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -71,6 +71,7 @@
'macros.h',
'memory/aligned_memory.cc',
'memory/aligned_memory.h',
+ 'memory/free_deleter.h',
'memory/ptr_util.h',
'memory/scoped_policy.h',
'numerics/safe_conversions.h',
@@ -80,6 +81,8 @@
'posix/eintr_wrapper.h',
'posix/safe_strerror.cc',
'posix/safe_strerror.h',
+ 'process/memory.cc',
+ 'process/memory.h',
'rand_util.cc',
'rand_util.h',
'scoped_clear_errno.h',
diff --git a/base/memory/free_deleter.h b/base/memory/free_deleter.h
new file mode 100644
index 0000000..ade7ed5
--- /dev/null
+++ b/base/memory/free_deleter.h
@@ -0,0 +1,25 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MINI_CHROMIUM_BASE_MEMORY_FREE_DELETER_H_
+#define MINI_CHROMIUM_BASE_MEMORY_FREE_DELETER_H_
+
+#include <stdlib.h>
+
+namespace base {
+
+// Function object which invokes 'free' on its parameter, which must be
+// a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
+//
+// std::unique_ptr<int, base::FreeDeleter> foo_ptr(
+// static_cast<int*>(malloc(sizeof(int))));
+struct FreeDeleter {
+ inline void operator()(void* ptr) const {
+ free(ptr);
+ }
+};
+
+} // namespace base
+
+#endif // MINI_CHROMIUM_BASE_MEMORY_FREE_DELETER_H_
diff --git a/base/process/memory.cc b/base/process/memory.cc
new file mode 100644
index 0000000..4cee1f2
--- /dev/null
+++ b/base/process/memory.cc
@@ -0,0 +1,16 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/process/memory.h"
+
+#include <stdlib.h>
+
+namespace base {
+
+bool UncheckedMalloc(size_t size, void** result) {
+ *result = malloc(size);
+ return *result != NULL;
+}
+
+} // namespace base
diff --git a/base/process/memory.h b/base/process/memory.h
new file mode 100644
index 0000000..fca8745
--- /dev/null
+++ b/base/process/memory.h
@@ -0,0 +1,22 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MINI_CHROMIUM_BASE_PROCESS_MEMORY_H_
+#define MINI_CHROMIUM_BASE_PROCESS_MEMORY_H_
+
+#include <stddef.h>
+
+#include "base/compiler_specific.h"
+
+namespace base {
+
+// Special allocator function for callers that want to check for OOM.
+// On success, *result will contain a pointer that should be dallocated with
+// free().
+WARN_UNUSED_RESULT bool UncheckedMalloc(size_t size,
+ void** result);
+
+} // namespace base
+
+#endif // MINI_CHROMIUM_BASE_PROCESS_MEMORY_H_