tools/profiler/LulCommon.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/profiler/LulCommon.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +
     1.7 +// Copyright (c) 2011, 2013 Google Inc.
     1.8 +// All rights reserved.
     1.9 +//
    1.10 +// Redistribution and use in source and binary forms, with or without
    1.11 +// modification, are permitted provided that the following conditions are
    1.12 +// met:
    1.13 +//
    1.14 +//     * Redistributions of source code must retain the above copyright
    1.15 +// notice, this list of conditions and the following disclaimer.
    1.16 +//     * Redistributions in binary form must reproduce the above
    1.17 +// copyright notice, this list of conditions and the following disclaimer
    1.18 +// in the documentation and/or other materials provided with the
    1.19 +// distribution.
    1.20 +//     * Neither the name of Google Inc. nor the names of its
    1.21 +// contributors may be used to endorse or promote products derived from
    1.22 +// this software without specific prior written permission.
    1.23 +//
    1.24 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.25 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.26 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.27 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.28 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.29 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.30 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.31 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.32 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.33 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.34 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.35 +
    1.36 +// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
    1.37 +
    1.38 +
    1.39 +// This file is derived from the following files in
    1.40 +// toolkit/crashreporter/google-breakpad:
    1.41 +//   src/common/module.cc
    1.42 +//   src/common/unique_string.cc
    1.43 +
    1.44 +// There's no internal-only interface for LulCommon.  Hence include
    1.45 +// the external interface directly.
    1.46 +#include "LulCommonExt.h"
    1.47 +
    1.48 +#include <stdlib.h>
    1.49 +#include <string.h>
    1.50 +
    1.51 +#include <string>
    1.52 +#include <map>
    1.53 +
    1.54 +
    1.55 +namespace lul {
    1.56 +
    1.57 +using std::string;
    1.58 +
    1.59 +////////////////////////////////////////////////////////////////
    1.60 +// Module
    1.61 +//
    1.62 +Module::Module(const string &name, const string &os,
    1.63 +               const string &architecture, const string &id) :
    1.64 +    name_(name),
    1.65 +    os_(os),
    1.66 +    architecture_(architecture),
    1.67 +    id_(id) { }
    1.68 +
    1.69 +Module::~Module() {
    1.70 +}
    1.71 +
    1.72 +
    1.73 +////////////////////////////////////////////////////////////////
    1.74 +// UniqueString
    1.75 +//
    1.76 +class UniqueString {
    1.77 + public:
    1.78 +  UniqueString(string str) { str_ = strdup(str.c_str()); }
    1.79 +  ~UniqueString() { free(reinterpret_cast<void*>(const_cast<char*>(str_))); }
    1.80 +  const char* str_;
    1.81 +};
    1.82 +
    1.83 +class UniqueStringUniverse {
    1.84 +public:
    1.85 +  UniqueStringUniverse() {};
    1.86 +  const UniqueString* FindOrCopy(string str) {
    1.87 +    std::map<string, UniqueString*>::iterator it = map_.find(str);
    1.88 +    if (it == map_.end()) {
    1.89 +      UniqueString* ustr = new UniqueString(str);
    1.90 +      map_[str] = ustr;
    1.91 +      return ustr;
    1.92 +    } else {
    1.93 +      return it->second;
    1.94 +    }
    1.95 +  }
    1.96 +private:
    1.97 +  std::map<string, UniqueString*> map_;
    1.98 +};
    1.99 +
   1.100 +
   1.101 +const UniqueString* ToUniqueString(string str) {
   1.102 +  // For the initialisation of sUniverse to be threadsafe,
   1.103 +  // this relies on C++11's semantics.
   1.104 +  static UniqueStringUniverse sUniverse;
   1.105 +  return sUniverse.FindOrCopy(str);
   1.106 +}
   1.107 +
   1.108 +const char* const FromUniqueString(const UniqueString* ustr)
   1.109 +{
   1.110 +  return ustr->str_;
   1.111 +}
   1.112 +
   1.113 +}  // namespace lul

mercurial