Chromium Code Reviews| 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 "gpu/command_buffer/common/mailbox.h" | 5 #include "gpu/command_buffer/common/mailbox.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" | |
| 10 | 11 |
| 11 namespace gpu { | 12 namespace gpu { |
| 12 | 13 |
| 13 Mailbox::Mailbox() { | 14 Mailbox::Mailbox() { |
| 14 memset(name, 0, sizeof(name)); | 15 memset(name, 0, sizeof(name)); |
| 15 } | 16 } |
| 16 | 17 |
| 17 bool Mailbox::IsZero() const { | 18 bool Mailbox::IsZero() const { |
| 18 for (size_t i = 0; i < arraysize(name); ++i) { | 19 for (size_t i = 0; i < arraysize(name); ++i) { |
| 19 if (name[i]) | 20 if (name[i]) |
| 20 return false; | 21 return false; |
| 21 } | 22 } |
| 22 return true; | 23 return true; |
| 23 } | 24 } |
| 24 | 25 |
| 25 void Mailbox::SetZero() { | 26 void Mailbox::SetZero() { |
| 26 memset(name, 0, sizeof(name)); | 27 memset(name, 0, sizeof(name)); |
| 27 } | 28 } |
| 28 | 29 |
| 29 void Mailbox::SetName(const int8* n) { | 30 void Mailbox::SetName(const int8* n) { |
| 30 DCHECK(IsZero() || !memcmp(name, n, sizeof(name))); | 31 DCHECK(IsZero() || !memcmp(name, n, sizeof(name))); |
| 31 memcpy(name, n, sizeof(name)); | 32 memcpy(name, n, sizeof(name)); |
| 32 } | 33 } |
| 33 | 34 |
| 35 Mailbox Mailbox::Generate() { | |
| 36 Mailbox result; | |
| 37 // Generates cryptographically-secure bytes. | |
| 38 base::RandBytes(result.name, sizeof(result.name)); | |
|
awong
2014/02/26 19:19:22
Use crypto::RandBytes() instead?
| |
| 39 #if !defined(NDEBUG) | |
| 40 int8 value = 1; | |
| 41 for (size_t i = 1; i < sizeof(result.name); ++i) | |
| 42 value ^= result.name[i]; | |
| 43 result.name[0] = value; | |
| 44 #endif | |
| 45 return result; | |
| 46 } | |
| 47 | |
| 48 bool Mailbox::Verify() const { | |
| 49 #if !defined(NDEBUG) | |
| 50 int8 value = 1; | |
| 51 for (size_t i = 0; i < sizeof(name); ++i) | |
| 52 value ^= name[i]; | |
| 53 return value == 0; | |
| 54 #else | |
| 55 return true; | |
| 56 #endif | |
| 57 } | |
| 58 | |
| 34 } // namespace gpu | 59 } // namespace gpu |
| OLD | NEW |