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

Side by Side Diff: mojo/public/cpp/system/buffer.h

Issue 1773403006: [mojo-sdk] Replace assert() usage in bindings with DCHECK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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 unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/strong_binding.h ('k') | mojo/public/cpp/system/data_pipe.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file provides a C++ wrapping around the Mojo C API for shared buffers, 5 // This file provides a C++ wrapping around the Mojo C API for shared buffers,
6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more 6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more
7 // strongly-typed representations of |MojoHandle|s. 7 // strongly-typed representations of |MojoHandle|s.
8 // 8 //
9 // Please see "mojo/public/c/system/buffer.h" for complete documentation of the 9 // Please see "mojo/public/c/system/buffer.h" for complete documentation of the
10 // API. 10 // API.
11 11
12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ 12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
13 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ 13 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
14 14
15 #include <assert.h>
16 #include <stdint.h> 15 #include <stdint.h>
17 16
18 #include "base/compiler_specific.h" 17 #include "base/compiler_specific.h"
18 #include "base/logging.h"
19 #include "mojo/public/c/system/buffer.h" 19 #include "mojo/public/c/system/buffer.h"
20 #include "mojo/public/cpp/system/handle.h" 20 #include "mojo/public/cpp/system/handle.h"
21 #include "mojo/public/cpp/system/macros.h" 21 #include "mojo/public/cpp/system/macros.h"
22 22
23 namespace mojo { 23 namespace mojo {
24 24
25 // A strongly-typed representation of a |MojoHandle| referring to a shared 25 // A strongly-typed representation of a |MojoHandle| referring to a shared
26 // buffer. 26 // buffer.
27 class SharedBufferHandle : public Handle { 27 class SharedBufferHandle : public Handle {
28 public: 28 public:
29 SharedBufferHandle() {} 29 SharedBufferHandle() {}
30 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} 30 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {}
31 31
32 // Copying and assignment allowed. 32 // Copying and assignment allowed.
33 }; 33 };
34 34
35 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), 35 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle),
36 "Bad size for C++ SharedBufferHandle"); 36 "Bad size for C++ SharedBufferHandle");
37 37
38 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; 38 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle;
39 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), 39 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle),
40 "Bad size for C++ ScopedSharedBufferHandle"); 40 "Bad size for C++ ScopedSharedBufferHandle");
41 41
42 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete 42 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete
43 // documentation. 43 // documentation.
44 inline MojoResult CreateSharedBuffer( 44 inline MojoResult CreateSharedBuffer(
45 const MojoCreateSharedBufferOptions* options, 45 const MojoCreateSharedBufferOptions* options,
46 uint64_t num_bytes, 46 uint64_t num_bytes,
47 ScopedSharedBufferHandle* shared_buffer) { 47 ScopedSharedBufferHandle* shared_buffer) {
48 assert(shared_buffer); 48 DCHECK(shared_buffer);
49 SharedBufferHandle handle; 49 SharedBufferHandle handle;
50 MojoResult rv = 50 MojoResult rv =
51 MojoCreateSharedBuffer(options, num_bytes, handle.mutable_value()); 51 MojoCreateSharedBuffer(options, num_bytes, handle.mutable_value());
52 // Reset even on failure (reduces the chances that a "stale"/incorrect handle 52 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
53 // will be used). 53 // will be used).
54 shared_buffer->reset(handle); 54 shared_buffer->reset(handle);
55 return rv; 55 return rv;
56 } 56 }
57 57
58 // Duplicates a handle to a buffer, most commonly so that the buffer can be 58 // Duplicates a handle to a buffer, most commonly so that the buffer can be
59 // shared with other applications. See |MojoDuplicateBufferHandle()| for 59 // shared with other applications. See |MojoDuplicateBufferHandle()| for
60 // complete documentation. 60 // complete documentation.
61 // 61 //
62 // TODO(ggowan): Rename this to DuplicateBufferHandle since it is making another 62 // TODO(ggowan): Rename this to DuplicateBufferHandle since it is making another
63 // handle to the same buffer, not duplicating the buffer itself. 63 // handle to the same buffer, not duplicating the buffer itself.
64 // 64 //
65 // TODO(vtl): This (and also the functions below) are templatized to allow for 65 // TODO(vtl): This (and also the functions below) are templatized to allow for
66 // future/other buffer types. A bit "safer" would be to overload this function 66 // future/other buffer types. A bit "safer" would be to overload this function
67 // manually. (The template enforces that the in and out handles be of the same 67 // manually. (The template enforces that the in and out handles be of the same
68 // type.) 68 // type.)
69 template <class BufferHandleType> 69 template <class BufferHandleType>
70 inline MojoResult DuplicateBuffer( 70 inline MojoResult DuplicateBuffer(
71 BufferHandleType buffer, 71 BufferHandleType buffer,
72 const MojoDuplicateBufferHandleOptions* options, 72 const MojoDuplicateBufferHandleOptions* options,
73 ScopedHandleBase<BufferHandleType>* new_buffer) { 73 ScopedHandleBase<BufferHandleType>* new_buffer) {
74 assert(new_buffer); 74 DCHECK(new_buffer);
75 BufferHandleType handle; 75 BufferHandleType handle;
76 MojoResult rv = MojoDuplicateBufferHandle( 76 MojoResult rv = MojoDuplicateBufferHandle(
77 buffer.value(), options, handle.mutable_value()); 77 buffer.value(), options, handle.mutable_value());
78 // Reset even on failure (reduces the chances that a "stale"/incorrect handle 78 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
79 // will be used). 79 // will be used).
80 new_buffer->reset(handle); 80 new_buffer->reset(handle);
81 return rv; 81 return rv;
82 } 82 }
83 83
84 // Maps a part of a buffer (specified by |buffer|, |offset|, and |num_bytes|) 84 // Maps a part of a buffer (specified by |buffer|, |offset|, and |num_bytes|)
85 // into memory. See |MojoMapBuffer()| for complete documentation. 85 // into memory. See |MojoMapBuffer()| for complete documentation.
86 template <class BufferHandleType> 86 template <class BufferHandleType>
87 inline MojoResult MapBuffer(BufferHandleType buffer, 87 inline MojoResult MapBuffer(BufferHandleType buffer,
88 uint64_t offset, 88 uint64_t offset,
89 uint64_t num_bytes, 89 uint64_t num_bytes,
90 void** pointer, 90 void** pointer,
91 MojoMapBufferFlags flags) { 91 MojoMapBufferFlags flags) {
92 assert(buffer.is_valid()); 92 DCHECK(buffer.is_valid());
93 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags); 93 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags);
94 } 94 }
95 95
96 // Unmaps a part of a buffer that was previously mapped with |MapBuffer()|. 96 // Unmaps a part of a buffer that was previously mapped with |MapBuffer()|.
97 // See |MojoUnmapBuffer()| for complete documentation. 97 // See |MojoUnmapBuffer()| for complete documentation.
98 inline MojoResult UnmapBuffer(void* pointer) { 98 inline MojoResult UnmapBuffer(void* pointer) {
99 assert(pointer); 99 DCHECK(pointer);
100 return MojoUnmapBuffer(pointer); 100 return MojoUnmapBuffer(pointer);
101 } 101 }
102 102
103 // A wrapper class that automatically creates a shared buffer and owns the 103 // A wrapper class that automatically creates a shared buffer and owns the
104 // handle. 104 // handle.
105 class SharedBuffer { 105 class SharedBuffer {
106 public: 106 public:
107 explicit SharedBuffer(uint64_t num_bytes); 107 explicit SharedBuffer(uint64_t num_bytes);
108 SharedBuffer(uint64_t num_bytes, 108 SharedBuffer(uint64_t num_bytes,
109 const MojoCreateSharedBufferOptions& options); 109 const MojoCreateSharedBufferOptions& options);
110 ~SharedBuffer(); 110 ~SharedBuffer();
111 111
112 ScopedSharedBufferHandle handle; 112 ScopedSharedBufferHandle handle;
113 }; 113 };
114 114
115 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) { 115 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) {
116 MojoResult result = CreateSharedBuffer(nullptr, num_bytes, &handle); 116 MojoResult result = CreateSharedBuffer(nullptr, num_bytes, &handle);
117 ALLOW_UNUSED_LOCAL(result); 117 ALLOW_UNUSED_LOCAL(result);
118 assert(result == MOJO_RESULT_OK); 118 DCHECK_EQ(MOJO_RESULT_OK, result);
119 } 119 }
120 120
121 inline SharedBuffer::SharedBuffer( 121 inline SharedBuffer::SharedBuffer(
122 uint64_t num_bytes, 122 uint64_t num_bytes,
123 const MojoCreateSharedBufferOptions& options) { 123 const MojoCreateSharedBufferOptions& options) {
124 MojoResult result = CreateSharedBuffer(&options, num_bytes, &handle); 124 MojoResult result = CreateSharedBuffer(&options, num_bytes, &handle);
125 ALLOW_UNUSED_LOCAL(result); 125 ALLOW_UNUSED_LOCAL(result);
126 assert(result == MOJO_RESULT_OK); 126 DCHECK_EQ(MOJO_RESULT_OK, result);
127 } 127 }
128 128
129 inline SharedBuffer::~SharedBuffer() { 129 inline SharedBuffer::~SharedBuffer() {
130 } 130 }
131 131
132 } // namespace mojo 132 } // namespace mojo
133 133
134 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ 134 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/strong_binding.h ('k') | mojo/public/cpp/system/data_pipe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698