StringSaver.cpp 882 B

1234567891011121314151617181920212223242526
  1. //===-- StringSaver.cpp ---------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/StringSaver.h"
  9. using namespace llvm;
  10. StringRef StringSaver::save(StringRef S) {
  11. char *P = Alloc.Allocate<char>(S.size() + 1);
  12. if (!S.empty())
  13. memcpy(P, S.data(), S.size());
  14. P[S.size()] = '\0';
  15. return StringRef(P, S.size());
  16. }
  17. StringRef UniqueStringSaver::save(StringRef S) {
  18. auto R = Unique.insert(S);
  19. if (R.second) // cache miss, need to actually save the string
  20. *R.first = Strings.save(S); // safe replacement with equal value
  21. return *R.first;
  22. }