forked from fish-shell/fish-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intern.cpp
84 lines (68 loc) · 1.94 KB
/
intern.cpp
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
/** \file intern.c
Library for pooling common strings
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <set>
#include <algorithm>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "common.h"
#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;
}
};
/* 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<const wchar_t *, string_table_compare_t> string_table_t;
#else
/** The table of intern'd strings */
typedef std::vector<const wchar_t *> 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;
static const wchar_t *intern_with_dup( const wchar_t *in, bool dup )
{
if( !in )
return NULL;
// debug( 0, L"intern %ls", in );
scoped_lock lock(intern_lock);
const wchar_t *result;
#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());
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;
}
const wchar_t *intern( const wchar_t *in )
{
return intern_with_dup(in, true);
}
const wchar_t *intern_static( const wchar_t *in )
{
return intern_with_dup(in, false);
}