From: Lars Ingebrigtsen Date: Sun, 1 May 2022 09:51:35 +0000 (+0200) Subject: Add new function `malloc-trim' X-Git-Tag: emacs-29.0.90~1931^2~158 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=29f3d4d2c69a0e9d2ab0a13ee6952fa9cf4d6035;p=emacs.git Add new function `malloc-trim' * configure.ac (PGTK_LIBS): Check for malloc_trim. * src/alloc.c (Fmalloc_trim): Add new function (bug#45200). --- diff --git a/configure.ac b/configure.ac index 53e5779e2f7..b7189593a63 100644 --- a/configure.ac +++ b/configure.ac @@ -2939,6 +2939,8 @@ fi AC_SUBST(PGTK_OBJ) AC_SUBST(PGTK_LIBS) +AC_CHECK_FUNCS(malloc_trim) + dnl D-Bus has been tested under GNU/Linux only. Must be adapted for dnl other platforms. HAVE_DBUS=no diff --git a/etc/NEWS b/etc/NEWS index 3f22e0b04e9..371fbc2145a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -1544,6 +1544,11 @@ functions. * Lisp Changes in Emacs 29.1 +--- +** New function 'malloc-trim'. +This function allows returning unused memory back to the operating +system, and is mainly meant as a debugging tool. + --- ** 'x-show-tip' no longer hard-codes a timeout default. The new 'x-show-tooltip-timeout' variable allows the user to alter diff --git a/src/alloc.c b/src/alloc.c index b9712859c38..661f37dd5cc 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -7479,6 +7479,37 @@ arenas. */) } #endif +#ifdef HAVE_MALLOC_TRIM +DEFUN ("malloc-trim", Fmalloc_trim, Smalloc_trim, 0, 1, "", + doc: /* Release free memory from the heap. +This function asks libc to return unused memory back to the operating +system. This function isn't guaranteed to do anything, and is mainly +meant as a debugging tool. + +If LEAVE_PADDING is given, ask the system to leave that much unused +spaced in the heap. This should be an integer, and if not given, +defaults to 0. + +This function returns nil if no memory could be returned to the +system, and non-nil if some memory could be returned. */) + (Lisp_Object leave_padding) +{ + int pad = 0; + + if (! NILP (leave_padding)) + { + CHECK_FIXNAT (leave_padding); + pad = XFIXNUM (leave_padding); + } + + /* 1 means that memory was released to the system. */ + if (malloc_trim (pad) == 1) + return Qt; + else + return Qnil; +} +#endif + static bool symbol_uses_obj (Lisp_Object symbol, Lisp_Object obj) { @@ -7829,6 +7860,9 @@ N should be nonnegative. */); (__GLIBC__ > 2 || __GLIBC_MINOR__ >= 10) defsubr (&Smalloc_info); +#endif +#ifdef HAVE_MALLOC_TRIM + defsubr (&Smalloc_trim); #endif defsubr (&Ssuspicious_object);