@@ -1099,22 +1099,53 @@ func_free(ufunc_T *fp, int force)
10991099 vim_free (fp );
11001100}
11011101
1102+ /*
1103+ * There are two kinds of function names:
1104+ * 1. ordinary names, function defined with :function
1105+ * 2. numbered functions and lambdas
1106+ * For the first we only count the name stored in func_hashtab as a reference,
1107+ * using function() does not count as a reference, because the function is
1108+ * looked up by name.
1109+ */
1110+ static int
1111+ func_name_refcount (char_u * name )
1112+ {
1113+ return isdigit (* name ) || * name == '<' ;
1114+ }
1115+
11021116#if defined(EXITFREE ) || defined(PROTO )
11031117 void
11041118free_all_functions (void )
11051119{
11061120 hashitem_T * hi ;
1121+ ufunc_T * fp ;
1122+ long_u skipped = 0 ;
1123+ long_u todo ;
11071124
11081125 /* Need to start all over every time, because func_free() may change the
11091126 * hash table. */
1110- while (func_hashtab .ht_used > 0 )
1111- for (hi = func_hashtab .ht_array ; ; ++ hi )
1127+ while (func_hashtab .ht_used > skipped )
1128+ {
1129+ todo = func_hashtab .ht_used ;
1130+ for (hi = func_hashtab .ht_array ; todo > 0 ; ++ hi )
11121131 if (!HASHITEM_EMPTY (hi ))
11131132 {
1114- func_free (HI2UF (hi ), TRUE);
1115- break ;
1133+ -- todo ;
1134+ /* Only free functions that are not refcounted, those are
1135+ * supposed to be freed when no longer referenced. */
1136+ fp = HI2UF (hi );
1137+ if (func_name_refcount (fp -> uf_name ))
1138+ ++ skipped ;
1139+ else
1140+ {
1141+ func_free (fp , TRUE);
1142+ skipped = 0 ;
1143+ break ;
1144+ }
11161145 }
1117- hash_clear (& func_hashtab );
1146+ }
1147+ if (skipped == 0 )
1148+ hash_clear (& func_hashtab );
11181149}
11191150#endif
11201151
@@ -1668,20 +1699,6 @@ trans_function_name(
16681699 return name ;
16691700}
16701701
1671- /*
1672- * There are two kinds of function names:
1673- * 1. ordinary names, function defined with :function
1674- * 2. numbered functions and lambdas
1675- * For the first we only count the name stored in func_hashtab as a reference,
1676- * using function() does not count as a reference, because the function is
1677- * looked up by name.
1678- */
1679- static int
1680- func_name_refcount (char_u * name )
1681- {
1682- return isdigit (* name ) || * name == '<' ;
1683- }
1684-
16851702/*
16861703 * ":function"
16871704 */
0 commit comments