mirror of
https://github.com/neovim/neovim.git
synced 2026-05-04 21:15:09 +00:00
refactor(map): remove extra-allocating map_new/map_free functions
Note: the reason for removing them is not that there after this refactor is no use of them, but rather that having them available is an anti-pattern: they manange an _extra_ heap allocation which has nothing to do with the functionality of the map itself (khash manages the real buffers internally). In case there happens to be a reason to allocate the map structure itself later, this should be made explicit using xcalloc/xfree calls.
This commit is contained in:
@@ -68,7 +68,8 @@ typedef struct {
|
||||
}
|
||||
|
||||
#if __has_feature(address_sanitizer)
|
||||
PMap(handle_T) *nlua_ref_markers = NULL;
|
||||
static PMap(handle_T) nlua_ref_markers = MAP_INIT;
|
||||
static bool nlua_track_refs = false;
|
||||
# define NLUA_TRACK_REFS
|
||||
#endif
|
||||
|
||||
@@ -568,7 +569,7 @@ void nlua_init(void)
|
||||
#ifdef NLUA_TRACK_REFS
|
||||
const char *env = os_getenv("NVIM_LUA_NOTRACK");
|
||||
if (!env || !*env) {
|
||||
nlua_ref_markers = pmap_new(handle_T)();
|
||||
nlua_track_refs = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -599,10 +600,10 @@ void nlua_free_all_mem(void)
|
||||
fprintf(stderr, "%d lua references were leaked!", nlua_refcount);
|
||||
}
|
||||
|
||||
if (nlua_ref_markers) {
|
||||
if (nlua_track_refs) {
|
||||
// in case there are leaked luarefs, leak the associated memory
|
||||
// to get LeakSanitizer stacktraces on exit
|
||||
pmap_free(handle_T)(nlua_ref_markers);
|
||||
pmap_destroy(handle_T)(&nlua_ref_markers);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1001,9 +1002,9 @@ LuaRef nlua_ref(lua_State *lstate, int index)
|
||||
if (ref > 0) {
|
||||
nlua_refcount++;
|
||||
#ifdef NLUA_TRACK_REFS
|
||||
if (nlua_ref_markers) {
|
||||
if (nlua_track_refs) {
|
||||
// dummy allocation to make LeakSanitizer track our luarefs
|
||||
pmap_put(handle_T)(nlua_ref_markers, ref, xmalloc(3));
|
||||
pmap_put(handle_T)(&nlua_ref_markers, ref, xmalloc(3));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1017,8 +1018,8 @@ void nlua_unref(lua_State *lstate, LuaRef ref)
|
||||
nlua_refcount--;
|
||||
#ifdef NLUA_TRACK_REFS
|
||||
// NB: don't remove entry from map to track double-unref
|
||||
if (nlua_ref_markers) {
|
||||
xfree(pmap_get(handle_T)(nlua_ref_markers, ref));
|
||||
if (nlua_track_refs) {
|
||||
xfree(pmap_get(handle_T)(&nlua_ref_markers, ref));
|
||||
}
|
||||
#endif
|
||||
luaL_unref(lstate, LUA_REGISTRYINDEX, ref);
|
||||
|
||||
@@ -101,7 +101,7 @@ static struct luaL_Reg treecursor_meta[] = {
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static PMap(cstr_t) *langs;
|
||||
static PMap(cstr_t) langs = MAP_INIT;
|
||||
|
||||
static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)
|
||||
{
|
||||
@@ -119,8 +119,6 @@ static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)
|
||||
/// all global state is stored in the regirstry of the lua_State
|
||||
void tslua_init(lua_State *L)
|
||||
{
|
||||
langs = pmap_new(cstr_t)();
|
||||
|
||||
// type metatables
|
||||
build_meta(L, TS_META_PARSER, parser_meta);
|
||||
build_meta(L, TS_META_TREE, tree_meta);
|
||||
@@ -133,7 +131,7 @@ void tslua_init(lua_State *L)
|
||||
int tslua_has_language(lua_State *L)
|
||||
{
|
||||
const char *lang_name = luaL_checkstring(L, 1);
|
||||
lua_pushboolean(L, pmap_has(cstr_t)(langs, lang_name));
|
||||
lua_pushboolean(L, pmap_has(cstr_t)(&langs, lang_name));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -142,7 +140,7 @@ int tslua_add_language(lua_State *L)
|
||||
const char *path = luaL_checkstring(L, 1);
|
||||
const char *lang_name = luaL_checkstring(L, 2);
|
||||
|
||||
if (pmap_has(cstr_t)(langs, lang_name)) {
|
||||
if (pmap_has(cstr_t)(&langs, lang_name)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -185,7 +183,7 @@ int tslua_add_language(lua_State *L)
|
||||
TREE_SITTER_LANGUAGE_VERSION, lang_version);
|
||||
}
|
||||
|
||||
pmap_put(cstr_t)(langs, xstrdup(lang_name), lang);
|
||||
pmap_put(cstr_t)(&langs, xstrdup(lang_name), lang);
|
||||
|
||||
lua_pushboolean(L, true);
|
||||
return 1;
|
||||
@@ -195,7 +193,7 @@ int tslua_inspect_lang(lua_State *L)
|
||||
{
|
||||
const char *lang_name = luaL_checkstring(L, 1);
|
||||
|
||||
TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name);
|
||||
TSLanguage *lang = pmap_get(cstr_t)(&langs, lang_name);
|
||||
if (!lang) {
|
||||
return luaL_error(L, "no such language: %s", lang_name);
|
||||
}
|
||||
@@ -243,7 +241,7 @@ int tslua_push_parser(lua_State *L)
|
||||
// Gather language name
|
||||
const char *lang_name = luaL_checkstring(L, 1);
|
||||
|
||||
TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name);
|
||||
TSLanguage *lang = pmap_get(cstr_t)(&langs, lang_name);
|
||||
if (!lang) {
|
||||
return luaL_error(L, "no such language: %s", lang_name);
|
||||
}
|
||||
@@ -1127,7 +1125,7 @@ int tslua_parse_query(lua_State *L)
|
||||
}
|
||||
|
||||
const char *lang_name = lua_tostring(L, 1);
|
||||
TSLanguage *lang = pmap_get(cstr_t)(langs, lang_name);
|
||||
TSLanguage *lang = pmap_get(cstr_t)(&langs, lang_name);
|
||||
if (!lang) {
|
||||
return luaL_error(L, "no such language: %s", lang_name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user