Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Unified Diff: mojo/system/memory.h

Issue 25323003: Mojo: Optimize VerifyUserPointer(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/system/core_test_base.cc ('k') | mojo/system/memory.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/system/memory.h
diff --git a/mojo/system/memory.h b/mojo/system/memory.h
index 6a1dafcb8de818c2b5db788e944c2be8d9ba9676..cca3acab4697f3ca0b8cc95454375631da6c6c90 100644
--- a/mojo/system/memory.h
+++ b/mojo/system/memory.h
@@ -10,16 +10,32 @@
namespace mojo {
namespace system {
-// Verify that |count * size_each| bytes can be read from the user |pointer|
-// insofar as possible/necessary. |count| and |size_each| are specified
-// separately instead of a single size, since |count * size_each| may overflow a
-// |size_t|. |count| may be zero but |size_each| must be nonzero.
+// This is just forward-declared, with the definition and explicit
+// instantiations in the .cc file. This is used by |VerifyUserPointer<T>()|
+// below, and you should use that instead.
+template <size_t size>
+bool VerifyUserPointerForSize(const void* pointer, size_t count);
+
+// Verify that |count * sizeof(T)| bytes can be read from the user |pointer|
+// insofar as possible/necessary (note: this is done carefully since |count *
+// sizeof(T)| may overflow a |size_t|. |count| may be zero. If |T| is |void|,
+// then the size of each element is taken to be a single byte.
//
// For example, if running in kernel mode, this should be a full verification
// that the given memory is owned and readable by the user process. In user
// mode, if crashes are acceptable, this may do nothing at all (and always
// return true).
-bool VerifyUserPointer(const void* pointer, size_t count, size_t size_each);
+template <typename T>
+bool VerifyUserPointer(const T* pointer, size_t count) {
+ return VerifyUserPointerForSize<sizeof(T)>(pointer, count);
+}
+
+// Special-case |T| equals |void| so that the size is in bytes, as indicated
+// above.
+template <>
+inline bool VerifyUserPointer<void>(const void* pointer, size_t count) {
+ return VerifyUserPointerForSize<1>(pointer, count);
+}
} // namespace system
} // namespace mojo
« no previous file with comments | « mojo/system/core_test_base.cc ('k') | mojo/system/memory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698