blob: f7ccfb97ea79877ad8db036ed1cfc0db8bbc08dd [file] [log] [blame]
Rafael Espindola454adf62015-06-13 12:49:52 +00001//===-- StringSaver.cpp ---------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Rafael Espindola454adf62015-06-13 12:49:52 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/StringSaver.h"
10
11using namespace llvm;
12
Mehdi Aminiec4fb5b2016-10-05 01:32:41 +000013StringRef StringSaver::save(StringRef S) {
Rafael Espindola454adf62015-06-13 12:49:52 +000014 char *P = Alloc.Allocate<char>(S.size() + 1);
Haojian Wu54829bb2018-08-20 13:12:54 +000015 if (!S.empty())
16 memcpy(P, S.data(), S.size());
Rafael Espindola454adf62015-06-13 12:49:52 +000017 P[S.size()] = '\0';
Mehdi Aminiec4fb5b2016-10-05 01:32:41 +000018 return StringRef(P, S.size());
Rafael Espindola454adf62015-06-13 12:49:52 +000019}
Sam McCall4bb7883d2018-07-23 10:44:40 +000020
21StringRef UniqueStringSaver::save(StringRef S) {
Eric Christopherdad2e922020-05-14 19:16:45 -070022 auto R = Unique.insert(S);
23 if (R.second) // cache miss, need to actually save the string
24 *R.first = Strings.save(S); // safe replacement with equal value
25 return *R.first;
Sam McCall4bb7883d2018-07-23 10:44:40 +000026}