Contextualize GetBuffer() Instead storing the buffer in a non-thread-safe static array, we move it to the context. Signed-off-by: Ran Benita <ran234@gmail.com>
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
diff --git a/src/context.c b/src/context.c
index bad4da0..53028a1 100644
--- a/src/context.c
+++ b/src/context.c
@@ -51,6 +51,10 @@ struct xkb_context {
unsigned file_id;
struct atom_table *atom_table;
+
+ /* Buffer for the *Text() functions. */
+ char text_buffer[1024];
+ size_t text_next;
};
/**
@@ -406,3 +410,20 @@ xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
{
ctx->user_data = user_data;
}
+
+char *
+xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
+{
+ char *rtrn;
+
+ if (size >= sizeof(ctx->text_buffer))
+ return NULL;
+
+ if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
+ ctx->text_next = 0;
+
+ rtrn = &ctx->text_buffer[ctx->text_next];
+ ctx->text_next += size;
+
+ return rtrn;
+}
diff --git a/src/context.h b/src/context.h
index fb3150a..16bd321 100644
--- a/src/context.h
+++ b/src/context.h
@@ -63,6 +63,9 @@ xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom);
const char *
xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
+char *
+xkb_context_get_buffer(struct xkb_context *ctx, size_t size);
+
ATTR_PRINTF(3, 4) void
xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
const char *fmt, ...);
diff --git a/src/text.c b/src/text.c
index 66f21b0..39fa83e 100644
--- a/src/text.c
+++ b/src/text.c
@@ -210,27 +210,6 @@ const LookupEntry symInterpretMatchMaskNames[] = {
{ "Exactly", MATCH_EXACTLY },
};
-#define BUFFER_SIZE 1024
-
-static char *
-GetBuffer(size_t size)
-{
- static char buffer[BUFFER_SIZE];
- static size_t next;
- char *rtrn;
-
- if (size >= BUFFER_SIZE)
- return NULL;
-
- if (BUFFER_SIZE - next <= size)
- next = 0;
-
- rtrn = &buffer[next];
- next += size;
-
- return rtrn;
-}
-
const char *
ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask)
{
@@ -238,7 +217,7 @@ ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask)
size_t len;
ssize_t rem;
char *str;
- char buf[BUFFER_SIZE];
+ char buf[1024];
const struct xkb_mod *mod;
if (mask == 0)
@@ -249,7 +228,7 @@ ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask)
str = buf;
buf[0] = '\0';
- rem = BUFFER_SIZE;
+ rem = sizeof(buf);
darray_enumerate(i, mod, keymap->mods) {
if (!(mask & (1 << i)))
continue;
@@ -266,10 +245,10 @@ ModMaskText(const struct xkb_keymap *keymap, xkb_mod_mask_t mask)
str = buf;
len = strlen(str);
- if (len >= BUFFER_SIZE)
- len = BUFFER_SIZE - 1;
+ if (len >= sizeof(buf))
+ len = sizeof(buf) - 1;
- return strcpy(GetBuffer(len + 1), str);
+ return strcpy(xkb_context_get_buffer(keymap->ctx, len + 1), str);
}
@@ -307,7 +286,7 @@ ActionTypeText(unsigned type)
}
const char *
-KeysymText(xkb_keysym_t sym)
+KeysymText(struct xkb_context *ctx, xkb_keysym_t sym)
{
static char buffer[64];
@@ -321,7 +300,7 @@ KeyNameText(struct xkb_context *ctx, xkb_atom_t name)
{
const char *sname = xkb_atom_text(ctx, name);
size_t len = strlen(sname) + 3;
- char *buf = GetBuffer(len);
+ char *buf = xkb_context_get_buffer(ctx, len);
snprintf(buf, len, "<%s>", sname);
return buf;
}
diff --git a/src/text.h b/src/text.h
index 32115c8..1d7b929 100644
--- a/src/text.h
+++ b/src/text.h
@@ -61,7 +61,7 @@ const char *
ActionTypeText(enum xkb_action_type type);
const char *
-KeysymText(xkb_keysym_t sym);
+KeysymText(struct xkb_context *ctx, xkb_keysym_t sym);
const char *
KeyNameText(struct xkb_context *ctx, xkb_atom_t name);
diff --git a/src/xkbcomp/compat.c b/src/xkbcomp/compat.c
index dc51a6e..9aca92f 100644
--- a/src/xkbcomp/compat.c
+++ b/src/xkbcomp/compat.c
@@ -277,7 +277,7 @@ siText(SymInterpInfo *si, CompatInfo *info)
return "default";
snprintf(buf, sizeof(buf), "%s+%s(%s)",
- KeysymText(si->interp.sym),
+ KeysymText(info->keymap->ctx, si->interp.sym),
SIMatchText(si->interp.match),
ModMaskText(info->keymap, si->interp.mods));
return buf;
diff --git a/src/xkbcomp/symbols.c b/src/xkbcomp/symbols.c
index 9900469..a99851b 100644
--- a/src/xkbcomp/symbols.c
+++ b/src/xkbcomp/symbols.c
@@ -463,7 +463,7 @@ AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
log_err(info->keymap->ctx,
"Symbol \"%s\" added to modifier map for multiple modifiers; "
"Using %s, ignoring %s\n",
- KeysymText(new->u.keySym),
+ KeysymText(info->keymap->ctx, new->u.keySym),
ModIndexText(info->keymap, use),
ModIndexText(info->keymap, ignore));
else
@@ -1568,7 +1568,7 @@ CopyModMapDef(SymbolsInfo *info, ModMapEntry *entry)
log_vrb(info->keymap->ctx, 5,
"Key \"%s\" not found in symbol map; "
"Modifier map entry for %s not updated\n",
- KeysymText(entry->u.keySym),
+ KeysymText(info->keymap->ctx, entry->u.keySym),
ModIndexText(info->keymap, entry->modifier));
return false;
}