Debian Codegraph @

src/atlas/runtime/trace/CallStack.cc

raw
1 /*
2 * (C) Copyright 2013 ECMWF.
3 *
4 * This software is licensed under the terms of the Apache Licence Version 2.0
5 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6 * In applying this licence, ECMWF does not waive the privileges and immunities
7 * granted to it by virtue of its status as an intergovernmental organisation
8 * nor does it submit to any jurisdiction.
9 */
10
11 #include "CallStack.h"
12
13 #include <functional>
14
15 #include "atlas/runtime/trace/CodeLocation.h"
16
17 namespace atlas {
18 namespace runtime {
19 namespace trace {
20
21 namespace {
22 static size_t hash_cstr(const char *s) {
23 // http://www.cse.yorku.ca/~oz/hash.html
24 size_t h = 5381;
25 int c;
26 while ((c = *s++)) {
27 h = ((h << 5) + h) + c;
28 }
29 return h;
30 }
31 static size_t hash_combine(size_t h1, size_t h2) {
32 return h1 ^ (h2 << 1);
33 };
34 static size_t hash_codelocation( const CodeLocation& loc ) {
35 return hash_combine( hash_cstr(loc.file()), std::hash<int>{}(loc.line()) );
36 }
37 }
38
39 void CallStack::push(const CodeLocation& loc, const std::string& id) {
40 if (stack_.size() == size_) {
41 stack_.resize(2 * size_);
42 }
43 auto hash = hash_codelocation(loc);
44 if( ! id.empty() ) {
45 hash = hash_combine( hash, std::hash<std::string>{}(id) );
46 }
47 stack_[size_++] = hash;
48 }
49
50 void CallStack::pop() {
51 --size_;
52 }
53
54 size_t CallStack::hash() const {
55 if (hash_) {
56 return hash_;
57 }
58 for (long i = size_ - 1; i >= 0; --i) {
59 auto h = stack_[i];
60 hash_ ^= (h << 1);
61 }
62 return hash_;
63 }
64
65 } // namespace trace
66 } // namespace runtime
67 } // namespace atlas