2016-05-02 03:26:32 +00:00
|
|
|
// Library for pooling common strings.
|
2016-05-18 22:30:21 +00:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2015-07-25 15:14:25 +00:00
|
|
|
#include <pthread.h>
|
2016-05-02 03:26:32 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <wchar.h>
|
2012-03-03 23:20:30 +00:00
|
|
|
#include <algorithm>
|
2016-04-21 06:00:54 +00:00
|
|
|
#include <memory>
|
2016-05-02 03:26:32 +00:00
|
|
|
#include <vector>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-05-02 03:26:32 +00:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "intern.h"
|
|
|
|
|
2016-05-02 03:26:32 +00:00
|
|
|
/// 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; }
|
2012-03-03 23:20:30 +00:00
|
|
|
};
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 03:26:32 +00:00
|
|
|
// A sorted vector ends up being a little more memory efficient than a std::set for the intern'd
|
|
|
|
// string table.
|
2012-03-03 23:20:30 +00:00
|
|
|
#define USE_SET 0
|
|
|
|
#if USE_SET
|
2016-05-02 03:26:32 +00:00
|
|
|
/// The table of intern'd strings.
|
2012-03-03 23:20:30 +00:00
|
|
|
typedef std::set<const wchar_t *, string_table_compare_t> string_table_t;
|
|
|
|
#else
|
2016-05-02 03:26:32 +00:00
|
|
|
/// The table of intern'd strings.
|
2012-07-17 19:47:01 +00:00
|
|
|
typedef std::vector<const wchar_t *> string_table_t;
|
2012-03-03 23:20:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static string_table_t string_table;
|
2006-11-11 10:54:52 +00:00
|
|
|
|
2016-05-02 03:26:32 +00:00
|
|
|
/// The lock to provide thread safety for intern'd strings.
|
2012-01-24 04:02:15 +00:00
|
|
|
static pthread_mutex_t intern_lock = PTHREAD_MUTEX_INITIALIZER;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 03:26:32 +00:00
|
|
|
static const wchar_t *intern_with_dup(const wchar_t *in, bool dup) {
|
|
|
|
if (!in) return NULL;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2016-05-29 05:28:26 +00:00
|
|
|
debug(5, L"intern %ls", in);
|
2016-07-21 05:30:58 +00:00
|
|
|
scoped_lock locker(intern_lock);
|
2012-01-24 04:02:15 +00:00
|
|
|
const wchar_t *result;
|
2012-11-18 10:23:22 +00:00
|
|
|
|
2012-03-03 23:20:30 +00:00
|
|
|
#if USE_SET
|
2012-01-24 04:02:15 +00:00
|
|
|
string_table_t::const_iterator iter = string_table.find(in);
|
2016-05-02 03:26:32 +00:00
|
|
|
if (iter != string_table.end()) {
|
2012-11-18 10:23:22 +00:00
|
|
|
result = *iter;
|
2016-05-02 03:26:32 +00:00
|
|
|
} else {
|
2012-01-24 04:02:15 +00:00
|
|
|
result = dup ? wcsdup(in) : in;
|
|
|
|
string_table.insert(result);
|
|
|
|
}
|
2012-03-03 23:20:30 +00:00
|
|
|
#else
|
2016-05-02 03:26:32 +00:00
|
|
|
string_table_t::iterator iter =
|
|
|
|
std::lower_bound(string_table.begin(), string_table.end(), in, string_table_compare_t());
|
|
|
|
if (iter != string_table.end() && wcscmp(*iter, in) == 0) {
|
2012-03-03 23:20:30 +00:00
|
|
|
result = *iter;
|
2016-05-02 03:26:32 +00:00
|
|
|
} else {
|
2012-03-03 23:20:30 +00:00
|
|
|
result = dup ? wcsdup(in) : in;
|
|
|
|
string_table.insert(iter, result);
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-24 04:02:15 +00:00
|
|
|
return result;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2016-05-02 03:26:32 +00:00
|
|
|
const wchar_t *intern(const wchar_t *in) { return intern_with_dup(in, true); }
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-02 03:26:32 +00:00
|
|
|
const wchar_t *intern_static(const wchar_t *in) { return intern_with_dup(in, false); }
|