Home | History | Annotate | Download | only in common
      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
      3 // found in the LICENSE file.
      4 
      5 #include "gpu/command_buffer/common/mailbox.h"
      6 
      7 #include <string.h>
      8 
      9 #include "base/logging.h"
     10 #include "base/rand_util.h"
     11 
     12 namespace gpu {
     13 
     14 Mailbox::Mailbox() {
     15   memset(name, 0, sizeof(name));
     16 }
     17 
     18 bool Mailbox::IsZero() const {
     19   for (size_t i = 0; i < arraysize(name); ++i) {
     20     if (name[i])
     21       return false;
     22   }
     23   return true;
     24 }
     25 
     26 void Mailbox::SetZero() {
     27   memset(name, 0, sizeof(name));
     28 }
     29 
     30 void Mailbox::SetName(const int8* n) {
     31   DCHECK(IsZero() || !memcmp(name, n, sizeof(name)));
     32   memcpy(name, n, sizeof(name));
     33 }
     34 
     35 Mailbox Mailbox::Generate() {
     36   Mailbox result;
     37   // Generates cryptographically-secure bytes.
     38   base::RandBytes(result.name, sizeof(result.name));
     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 
     59 }  // namespace gpu
     60