From 6db372133dab233f2db39e678c48ad1b04c075da Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 29 Jan 2017 21:19:39 -0800 Subject: [PATCH] Clean up and adopt owning_lock in intern.cpp --- src/intern.cpp | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/src/intern.cpp b/src/intern.cpp index 2ec335648..1f83b2f82 100644 --- a/src/intern.cpp +++ b/src/intern.cpp @@ -12,53 +12,29 @@ #include "fallback.h" // IWYU pragma: keep #include "intern.h" -/// Comparison function for intern'd strings. -class string_table_compare_t { - public: - bool operator()(const wchar_t *a, const wchar_t *b) const { return wcscmp(a, b) < 0; } -}; +bool string_less_than_string(const wchar_t *a, const wchar_t *b) { + return wcscmp(a, b) < 0; +} -// A sorted vector ends up being a little more memory efficient than a std::set for the intern'd -// string table. -#define USE_SET 0 -#if USE_SET /// The table of intern'd strings. -typedef std::set string_table_t; -#else -/// The table of intern'd strings. -typedef std::vector string_table_t; -#endif - -static string_table_t string_table; - -/// The lock to provide thread safety for intern'd strings. -static pthread_mutex_t intern_lock = PTHREAD_MUTEX_INITIALIZER; +owning_lock> string_table; static const wchar_t *intern_with_dup(const wchar_t *in, bool dup) { if (!in) return NULL; debug(5, L"intern %ls", in); - scoped_lock locker(intern_lock); - const wchar_t *result; + auto lock_string_table = string_table.acquire(); + std::vector &string_table = lock_string_table.value; -#if USE_SET - string_table_t::const_iterator iter = string_table.find(in); - if (iter != string_table.end()) { - result = *iter; - } else { - result = dup ? wcsdup(in) : in; - string_table.insert(result); - } -#else - string_table_t::iterator iter = - std::lower_bound(string_table.begin(), string_table.end(), in, string_table_compare_t()); + const wchar_t *result; + auto iter = std::lower_bound(string_table.begin(), string_table.end(), + in, string_less_than_string); if (iter != string_table.end() && wcscmp(*iter, in) == 0) { result = *iter; } else { result = dup ? wcsdup(in) : in; string_table.insert(iter, result); } -#endif return result; }