| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/system/data_pipe.h" | 5 #include "mojo/system/data_pipe.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // static | 22 // static |
| 23 const MojoCreateDataPipeOptions DataPipe::kDefaultCreateOptions = { | 23 const MojoCreateDataPipeOptions DataPipe::kDefaultCreateOptions = { |
| 24 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)), | 24 static_cast<uint32_t>(sizeof(MojoCreateDataPipeOptions)), |
| 25 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, | 25 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, |
| 26 1u, | 26 1u, |
| 27 static_cast<uint32_t>(kDefaultDataPipeCapacityBytes) | 27 static_cast<uint32_t>(kDefaultDataPipeCapacityBytes) |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 // static | 30 // static |
| 31 MojoResult DataPipe::ValidateCreateOptions( | 31 MojoResult DataPipe::ValidateCreateOptions( |
| 32 const MojoCreateDataPipeOptions* in_options, | 32 UserPointer<const MojoCreateDataPipeOptions> in_options, |
| 33 MojoCreateDataPipeOptions* out_options) { | 33 MojoCreateDataPipeOptions* out_options) { |
| 34 const MojoCreateDataPipeOptionsFlags kKnownFlags = | 34 const MojoCreateDataPipeOptionsFlags kKnownFlags = |
| 35 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD; | 35 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_MAY_DISCARD; |
| 36 | 36 |
| 37 *out_options = kDefaultCreateOptions; | 37 *out_options = kDefaultCreateOptions; |
| 38 if (!in_options) | 38 if (in_options.IsNull()) |
| 39 return MOJO_RESULT_OK; | 39 return MOJO_RESULT_OK; |
| 40 | 40 |
| 41 MojoResult result = | 41 UserOptionsReader<MojoCreateDataPipeOptions> reader(in_options); |
| 42 ValidateOptionsStructPointerSizeAndFlags<MojoCreateDataPipeOptions>( | 42 if (!reader.is_valid()) |
| 43 in_options, kKnownFlags, out_options); | 43 return MOJO_RESULT_INVALID_ARGUMENT; |
| 44 if (result != MOJO_RESULT_OK) | 44 |
| 45 return result; | 45 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, flags, reader)) |
| 46 return MOJO_RESULT_OK; |
| 47 if ((reader.options().flags & ~kKnownFlags)) |
| 48 return MOJO_RESULT_UNIMPLEMENTED; |
| 49 out_options->flags = reader.options().flags; |
| 46 | 50 |
| 47 // Checks for fields beyond |flags|: | 51 // Checks for fields beyond |flags|: |
| 48 | 52 |
| 49 if (!HAS_OPTIONS_STRUCT_MEMBER(MojoCreateDataPipeOptions, element_num_bytes, | 53 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, element_num_bytes, |
| 50 in_options)) | 54 reader)) |
| 51 return MOJO_RESULT_OK; | 55 return MOJO_RESULT_OK; |
| 52 if (in_options->element_num_bytes == 0) | 56 if (reader.options().element_num_bytes == 0) |
| 53 return MOJO_RESULT_INVALID_ARGUMENT; | 57 return MOJO_RESULT_INVALID_ARGUMENT; |
| 54 out_options->element_num_bytes = in_options->element_num_bytes; | 58 out_options->element_num_bytes = reader.options().element_num_bytes; |
| 55 | 59 |
| 56 if (!HAS_OPTIONS_STRUCT_MEMBER(MojoCreateDataPipeOptions, capacity_num_bytes, | 60 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateDataPipeOptions, capacity_num_bytes, |
| 57 in_options) || | 61 reader) || |
| 58 in_options->capacity_num_bytes == 0) { | 62 reader.options().capacity_num_bytes == 0) { |
| 59 // Round the default capacity down to a multiple of the element size (but at | 63 // Round the default capacity down to a multiple of the element size (but at |
| 60 // least one element). | 64 // least one element). |
| 61 out_options->capacity_num_bytes = std::max( | 65 out_options->capacity_num_bytes = std::max( |
| 62 static_cast<uint32_t>(kDefaultDataPipeCapacityBytes - | 66 static_cast<uint32_t>(kDefaultDataPipeCapacityBytes - |
| 63 (kDefaultDataPipeCapacityBytes % out_options->element_num_bytes)), | 67 (kDefaultDataPipeCapacityBytes % out_options->element_num_bytes)), |
| 64 out_options->element_num_bytes); | 68 out_options->element_num_bytes); |
| 65 return MOJO_RESULT_OK; | 69 return MOJO_RESULT_OK; |
| 66 } | 70 } |
| 67 if (in_options->capacity_num_bytes % out_options->element_num_bytes != 0) | 71 if (reader.options().capacity_num_bytes % out_options->element_num_bytes != 0) |
| 68 return MOJO_RESULT_INVALID_ARGUMENT; | 72 return MOJO_RESULT_INVALID_ARGUMENT; |
| 69 if (in_options->capacity_num_bytes > kMaxDataPipeCapacityBytes) | 73 if (reader.options().capacity_num_bytes > kMaxDataPipeCapacityBytes) |
| 70 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 74 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
| 71 out_options->capacity_num_bytes = in_options->capacity_num_bytes; | 75 out_options->capacity_num_bytes = reader.options().capacity_num_bytes; |
| 72 | 76 |
| 73 return MOJO_RESULT_OK; | 77 return MOJO_RESULT_OK; |
| 74 } | 78 } |
| 75 | 79 |
| 76 void DataPipe::ProducerCancelAllWaiters() { | 80 void DataPipe::ProducerCancelAllWaiters() { |
| 77 base::AutoLock locker(lock_); | 81 base::AutoLock locker(lock_); |
| 78 DCHECK(has_local_producer_no_lock()); | 82 DCHECK(has_local_producer_no_lock()); |
| 79 producer_waiter_list_->CancelAllWaiters(); | 83 producer_waiter_list_->CancelAllWaiters(); |
| 80 } | 84 } |
| 81 | 85 |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 element_num_bytes_(validated_options.element_num_bytes), | 396 element_num_bytes_(validated_options.element_num_bytes), |
| 393 capacity_num_bytes_(validated_options.capacity_num_bytes), | 397 capacity_num_bytes_(validated_options.capacity_num_bytes), |
| 394 producer_open_(true), | 398 producer_open_(true), |
| 395 consumer_open_(true), | 399 consumer_open_(true), |
| 396 producer_waiter_list_(has_local_producer ? new WaiterList() : NULL), | 400 producer_waiter_list_(has_local_producer ? new WaiterList() : NULL), |
| 397 consumer_waiter_list_(has_local_consumer ? new WaiterList() : NULL), | 401 consumer_waiter_list_(has_local_consumer ? new WaiterList() : NULL), |
| 398 producer_two_phase_max_num_bytes_written_(0), | 402 producer_two_phase_max_num_bytes_written_(0), |
| 399 consumer_two_phase_max_num_bytes_read_(0) { | 403 consumer_two_phase_max_num_bytes_read_(0) { |
| 400 // Check that the passed in options actually are validated. | 404 // Check that the passed in options actually are validated. |
| 401 MojoCreateDataPipeOptions unused ALLOW_UNUSED = { 0 }; | 405 MojoCreateDataPipeOptions unused ALLOW_UNUSED = { 0 }; |
| 402 DCHECK_EQ(ValidateCreateOptions(&validated_options, &unused), MOJO_RESULT_OK); | 406 DCHECK_EQ(ValidateCreateOptions(MakeUserPointer(&validated_options), &unused), |
| 407 MOJO_RESULT_OK); |
| 403 } | 408 } |
| 404 | 409 |
| 405 DataPipe::~DataPipe() { | 410 DataPipe::~DataPipe() { |
| 406 DCHECK(!producer_open_); | 411 DCHECK(!producer_open_); |
| 407 DCHECK(!consumer_open_); | 412 DCHECK(!consumer_open_); |
| 408 DCHECK(!producer_waiter_list_); | 413 DCHECK(!producer_waiter_list_); |
| 409 DCHECK(!consumer_waiter_list_); | 414 DCHECK(!consumer_waiter_list_); |
| 410 } | 415 } |
| 411 | 416 |
| 412 void DataPipe::AwakeProducerWaitersForStateChangeNoLock( | 417 void DataPipe::AwakeProducerWaitersForStateChangeNoLock( |
| 413 const HandleSignalsState& new_producer_state) { | 418 const HandleSignalsState& new_producer_state) { |
| 414 lock_.AssertAcquired(); | 419 lock_.AssertAcquired(); |
| 415 if (!has_local_producer_no_lock()) | 420 if (!has_local_producer_no_lock()) |
| 416 return; | 421 return; |
| 417 producer_waiter_list_->AwakeWaitersForStateChange(new_producer_state); | 422 producer_waiter_list_->AwakeWaitersForStateChange(new_producer_state); |
| 418 } | 423 } |
| 419 | 424 |
| 420 void DataPipe::AwakeConsumerWaitersForStateChangeNoLock( | 425 void DataPipe::AwakeConsumerWaitersForStateChangeNoLock( |
| 421 const HandleSignalsState& new_consumer_state) { | 426 const HandleSignalsState& new_consumer_state) { |
| 422 lock_.AssertAcquired(); | 427 lock_.AssertAcquired(); |
| 423 if (!has_local_consumer_no_lock()) | 428 if (!has_local_consumer_no_lock()) |
| 424 return; | 429 return; |
| 425 consumer_waiter_list_->AwakeWaitersForStateChange(new_consumer_state); | 430 consumer_waiter_list_->AwakeWaitersForStateChange(new_consumer_state); |
| 426 } | 431 } |
| 427 | 432 |
| 428 } // namespace system | 433 } // namespace system |
| 429 } // namespace mojo | 434 } // namespace mojo |
| OLD | NEW |