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

Side by Side Diff: mojo/edk/system/channel.cc

Issue 2772373002: [draft] Patch to check potential memory leak (Closed)
Patch Set: thread-safe Created 3 years, 8 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 | « base/trace_event/heap_profiler_allocation_context.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 #include "mojo/edk/system/channel.h" 5 #include "mojo/edk/system/channel.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <limits> 11 #include <limits>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/aligned_memory.h" 15 #include "base/memory/aligned_memory.h"
16 #include "base/process/process_handle.h" 16 #include "base/process/process_handle.h"
17 #include "mojo/edk/embedder/platform_handle.h" 17 #include "mojo/edk/embedder/platform_handle.h"
18 18
19 #if defined(OS_MACOSX) && !defined(OS_IOS) 19 #if defined(OS_MACOSX) && !defined(OS_IOS)
20 #include "base/mac/mach_logging.h" 20 #include "base/mac/mach_logging.h"
21 #elif defined(OS_WIN) 21 #elif defined(OS_WIN)
22 #include "base/win/win_util.h" 22 #include "base/win/win_util.h"
23 #endif 23 #endif
24 24
25 #include "base/atomicops.h"
26
25 namespace mojo { 27 namespace mojo {
26 namespace edk { 28 namespace edk {
27 29
28 namespace { 30 namespace {
29 31
30 static_assert( 32 static_assert(
31 IsAlignedForChannelMessage(sizeof(Channel::Message::LegacyHeader)), 33 IsAlignedForChannelMessage(sizeof(Channel::Message::LegacyHeader)),
32 "Invalid LegacyHeader size."); 34 "Invalid LegacyHeader size.");
33 35
34 static_assert(IsAlignedForChannelMessage(sizeof(Channel::Message::Header)), 36 static_assert(IsAlignedForChannelMessage(sizeof(Channel::Message::Header)),
(...skipping 10 matching lines...) Expand all
45 "message_type should be at the same offset in both Header " 47 "message_type should be at the same offset in both Header "
46 "structs."); 48 "structs.");
47 49
48 } // namespace 50 } // namespace
49 51
50 const size_t kReadBufferSize = 4096; 52 const size_t kReadBufferSize = 4096;
51 const size_t kMaxUnusedReadBufferCapacity = 4096; 53 const size_t kMaxUnusedReadBufferCapacity = 4096;
52 const size_t kMaxChannelMessageSize = 256 * 1024 * 1024; 54 const size_t kMaxChannelMessageSize = 256 * 1024 * 1024;
53 const size_t kMaxAttachedHandles = 128; 55 const size_t kMaxAttachedHandles = 128;
54 56
57 static base::subtle::Atomic32 message_count_ = 0;
58
55 Channel::Message::Message(size_t payload_size, size_t max_handles) 59 Channel::Message::Message(size_t payload_size, size_t max_handles)
56 #if defined(MOJO_EDK_LEGACY_PROTOCOL) 60 #if defined(MOJO_EDK_LEGACY_PROTOCOL)
57 : Message(payload_size, max_handles, MessageType::NORMAL_LEGACY) { 61 : Message(payload_size, max_handles, MessageType::NORMAL_LEGACY) {
erikchen 2017/03/27 19:10:35 this constructor calls another constructor, so we
62 base::subtle::Barrier_AtomicIncrement(&message_count_, 1);
63 if ((message_count_ % 1024) == 0) {
64 LOG(ERROR) << "Message: " << message_count_ << " ["
65 << base::GetCurrentProcId() << "]";
66 }
58 } 67 }
59 #else 68 #else
60 : Message(payload_size, max_handles, MessageType::NORMAL) { 69 : Message(payload_size, max_handles, MessageType::NORMAL) {
70 base::subtle::Barrier_AtomicIncrement(&message_count_, 1);
71 if ((message_count_ % 1024) == 0) {
72 LOG(ERROR) << "Message: " << message_count_ << " ["
73 << base::GetCurrentProcId() << "]";
74 }
61 } 75 }
62 #endif 76 #endif
63 77
64 Channel::Message::Message(size_t payload_size, 78 Channel::Message::Message(size_t payload_size,
65 size_t max_handles, 79 size_t max_handles,
66 MessageType message_type) 80 MessageType message_type)
67 : max_handles_(max_handles) { 81 : max_handles_(max_handles) {
68 DCHECK_LE(max_handles_, kMaxAttachedHandles); 82 DCHECK_LE(max_handles_, kMaxAttachedHandles);
83 base::subtle::Barrier_AtomicIncrement(&message_count_, 1);
84 if ((message_count_ % 1024) == 0) {
85 LOG(ERROR) << "Message: " << message_count_ << " ["
86 << base::GetCurrentProcId() << "]";
87 }
69 88
70 const bool is_legacy_message = (message_type == MessageType::NORMAL_LEGACY); 89 const bool is_legacy_message = (message_type == MessageType::NORMAL_LEGACY);
71 size_t extra_header_size = 0; 90 size_t extra_header_size = 0;
72 #if defined(OS_WIN) 91 #if defined(OS_WIN)
73 // On Windows we serialize HANDLEs into the extra header space. 92 // On Windows we serialize HANDLEs into the extra header space.
74 extra_header_size = max_handles_ * sizeof(HandleEntry); 93 extra_header_size = max_handles_ * sizeof(HandleEntry);
75 #elif defined(OS_MACOSX) && !defined(OS_IOS) 94 #elif defined(OS_MACOSX) && !defined(OS_IOS)
76 // On OSX, some of the platform handles may be mach ports, which are 95 // On OSX, some of the platform handles may be mach ports, which are
77 // serialised into the message buffer. Since there could be a mix of fds and 96 // serialised into the message buffer. Since there could be a mix of fds and
78 // mach ports, we store the mach ports as an <index, port> pair (of uint32_t), 97 // mach ports, we store the mach ports as an <index, port> pair (of uint32_t),
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 // Initialize all handles to invalid values. 148 // Initialize all handles to invalid values.
130 for (size_t i = 0; i < max_handles_; ++i) { 149 for (size_t i = 0; i < max_handles_; ++i) {
131 mach_ports_header_->entries[i] = 150 mach_ports_header_->entries[i] =
132 {0, static_cast<uint32_t>(MACH_PORT_NULL)}; 151 {0, static_cast<uint32_t>(MACH_PORT_NULL)};
133 } 152 }
134 #endif 153 #endif
135 } 154 }
136 } 155 }
137 156
138 Channel::Message::~Message() { 157 Channel::Message::~Message() {
158 base::subtle::Barrier_AtomicIncrement(&message_count_, -1);
139 base::AlignedFree(data_); 159 base::AlignedFree(data_);
140 } 160 }
141 161
142 // static 162 // static
143 Channel::MessagePtr Channel::Message::Deserialize(const void* data, 163 Channel::MessagePtr Channel::Message::Deserialize(const void* data,
144 size_t data_num_bytes) { 164 size_t data_num_bytes) {
145 if (data_num_bytes < sizeof(LegacyHeader)) 165 if (data_num_bytes < sizeof(LegacyHeader))
146 return nullptr; 166 return nullptr;
147 167
148 const LegacyHeader* legacy_header = 168 const LegacyHeader* legacy_header =
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 694
675 bool Channel::OnControlMessage(Message::MessageType message_type, 695 bool Channel::OnControlMessage(Message::MessageType message_type,
676 const void* payload, 696 const void* payload,
677 size_t payload_size, 697 size_t payload_size,
678 ScopedPlatformHandleVectorPtr handles) { 698 ScopedPlatformHandleVectorPtr handles) {
679 return false; 699 return false;
680 } 700 }
681 701
682 } // namespace edk 702 } // namespace edk
683 } // namespace mojo 703 } // namespace mojo
OLDNEW
« no previous file with comments | « base/trace_event/heap_profiler_allocation_context.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698