File: UIDRegistry.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (119 lines) | stat: -rw-r--r-- 2,974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//===--- UIDRegistry.cpp --------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "SourceKit/Support/Concurrency.h"
#include "SourceKit/Support/UIdent.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/raw_ostream.h"
#include <mutex>
#include <vector>

using namespace SourceKit;
using llvm::StringRef;

namespace {
class UIDRegistryImpl {
  typedef llvm::StringMap<void *, llvm::BumpPtrAllocator> HashTableTy;
  typedef llvm::StringMapEntry<void *> EntryTy;
  HashTableTy HashTable;
  WorkQueue Queue{ WorkQueue::Dequeuing::Concurrent, "UIDRegistryImpl" };

public:

  void *get(StringRef Str);
  static StringRef getName(void *Ptr);
  static void setTag(void *Ptr, void *Tag);
  static void *getTag(void *Ptr);
};
} // end anonymous namespace

static UIDRegistryImpl *getGlobalRegistry() {
  static UIDRegistryImpl *GlobalRegistry = 0;
  if (!GlobalRegistry) {
    static std::once_flag flag;
    std::call_once(flag, [](){ GlobalRegistry = new UIDRegistryImpl(); });
  }
  return GlobalRegistry;
}

UIdent::UIdent(llvm::StringRef Str) {
  Ptr = getGlobalRegistry()->get(Str);
}

llvm::StringRef UIdent::getName() const {
  if (isInvalid())
    return StringRef();
  return UIDRegistryImpl::getName(Ptr);
}

const char *UIdent::c_str() const {
  if (isInvalid())
    return "";
  return getName().begin();
}

void UIdent::setTag(void *Tag) {
  assert(isValid());
  UIDRegistryImpl::setTag(Ptr, Tag);
}

void *UIdent::getTag() const {
  assert(isValid());
  return UIDRegistryImpl::getTag(Ptr);
}

void UIdent::dump() const {
  print(llvm::errs());
}

void UIdent::print(llvm::raw_ostream &OS) const {
  if (isInvalid())
    OS << "<<INVALID>>";
  else
    OS << getName();
}

void *UIDRegistryImpl::get(StringRef Str) {
  assert(!Str.empty());
  assert(!Str.contains(' '));
  EntryTy *Ptr = 0;
  Queue.dispatchSync([&]{
    HashTableTy::iterator It = HashTable.find(Str);
    if (It != HashTable.end())
      Ptr = &(*It);
  });

  if (Ptr == 0) {
    Queue.dispatchBarrierSync([&]{
      EntryTy &Entry = *HashTable.insert(std::make_pair(Str, nullptr)).first;
      Ptr = &Entry;
    });
  }

  return Ptr;
}

StringRef UIDRegistryImpl::getName(void *Ptr) {
  EntryTy *Entry = static_cast<EntryTy*>(Ptr);
  return Entry->getKey();
}

void UIDRegistryImpl::setTag(void *Ptr, void *Tag) {
  EntryTy *Entry = static_cast<EntryTy*>(Ptr);
  Entry->setValue(Tag);
}

void *UIDRegistryImpl::getTag(void *Ptr) {
  EntryTy *Entry = static_cast<EntryTy*>(Ptr);
  return Entry->getValue();
}