From: Spencer Baugh Date: Sun, 22 Nov 2020 05:08:28 +0000 (-0500) Subject: * src/alloc.c (Fgarbage_collect_maybe): New function X-Git-Tag: emacs-28.0.90~4919 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=39915c708435cefd1c3eaddeec54d3b365d36515;p=emacs.git * src/alloc.c (Fgarbage_collect_maybe): New function --- diff --git a/etc/NEWS b/etc/NEWS index 7f18f129461..525ed8b36ee 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1828,6 +1828,8 @@ ledit.el, lmenu.el, lucid.el and old-whitespace.el. * Lisp Changes in Emacs 28.1 +** New function `garbage-collect-maybe` to trigger GC early + --- ** 'defvar' detects the error of defining a variable currently lexically bound. Such mixes are always signs that the outer lexical binding was an diff --git a/src/alloc.c b/src/alloc.c index 34f822e589e..5d2d5bc4add 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -6209,6 +6209,30 @@ For further details, see Info node `(elisp)Garbage Collection'. */) return CALLMANY (Flist, total); } +DEFUN ("garbage-collect-maybe", Fgarbage_collect_maybe, +Sgarbage_collect_maybe, 1, 1, "", + doc: /* Call `garbage-collect' if enough allocation happened. +FACTOR determines what "enough" means here: +If FACTOR is a positive number N, it means to run GC if more than +1/Nth of the allocations needed to trigger automatic allocation took +place. +Therefore, as N gets higher, this is more likely to perform a GC. +Returns non-nil if GC happened, and nil otherwise. */) + (Lisp_Object factor) +{ + CHECK_FIXNAT (factor); + EMACS_INT fact = XFIXNAT (factor); + + EMACS_INT since_gc = gc_threshold - consing_until_gc; + if (fact >= 1 && since_gc > gc_threshold / fact) + { + garbage_collect (); + return Qt; + } + else + return Qnil; +} + /* Mark Lisp objects in glyph matrix MATRIX. Currently the only interesting objects referenced from glyphs are strings. */ @@ -7553,6 +7577,7 @@ N should be nonnegative. */); defsubr (&Smake_finalizer); defsubr (&Spurecopy); defsubr (&Sgarbage_collect); + defsubr (&Sgarbage_collect_maybe); defsubr (&Smemory_info); defsubr (&Smemory_use_counts); #ifdef GNU_LINUX diff --git a/src/lisp.h b/src/lisp.h index 9901f80b51c..416c9b0cac1 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -3798,6 +3798,7 @@ flush_stack_call_func (void (*func) (void *arg), void *arg) extern void garbage_collect (void); extern void maybe_garbage_collect (void); +extern bool maybe_garbage_collect_eagerly (EMACS_INT factor); extern const char *pending_malloc_warning; extern Lisp_Object zero_vector; extern EMACS_INT consing_until_gc;