malloc_get_state(3)
β¨ malloc_get_state β bestie edition β¨
okay so real talk, this is malloc_get_state(3) from section 3 of the manual (Library Functions Manual, iykyk) β and yes she's a removed girlie, so buckle up π
π NAME
malloc_get_state, malloc_set_state β record and restore state of malloc implementation
π LIBRARY
Standard C library (libc, -lc)
π«Ά SYNOPSIS
#include <malloc.h>
void *malloc_get_state(void);
int malloc_set_state(void *state);
πΈ DESCRIPTION
Note: these functions are removed in glibc 2.25. π like, gone gone. not "deprecated," removed.
So. The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables β but NOT the actual contents of the heap, and NOT the state of malloc_hook(3) function pointers, okay, don't get it twisted π.
The state gets recorded into a system-dependent opaque data structure that's dynamically allocated via malloc(3), and a pointer to that data structure is what you get back as the function result. β¨ And bestie, heads up: it is the caller's responsibility to free(3) this memory. she does not clean up after you π
The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values recorded in the opaque data structure pointed to by state.
π₯ RETURN VALUE
- On success,
malloc_get_state()returns a pointer to a newly allocated opaque data structure. On error (for example, memory could not be allocated for the data structure),malloc_get_state()returnsNULL. - On success,
malloc_set_state()returns0. - If the implementation detects that state does not point to a correctly formed data structure,
malloc_set_state()returns-1. - If the implementation detects that the version of the data structure referred to by state is a more recent version than this implementation knows about,
malloc_set_state()returns-2. (she said "that's a future version, not my business" π)
π ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
- Interface:
malloc_get_state(),malloc_set_state()β Attribute: Thread safety β Value:MT-Safeπ¦
π STANDARDS
GNU.
π§ NOTES
These functions are useful when you're using this malloc(3) implementation as part of a shared library, and the heap contents are saved/restored via some other method. This technique is used by GNU Emacs to implement its "dumping" function. (main character behavior fr fr π)
Hook function pointers are never saved or restored by these functions, with exactly two exceptions:
- if malloc checking (see
mallopt(3)) was in use whenmalloc_get_state()was called, thenmalloc_set_state()resets malloc checking hooks if possible; - if malloc checking was not in use in the recorded state, but the caller has requested malloc checking, then the hooks are reset to
0.
π SEE ALSO
that's the whole page bestie, no cap πΈ this was Linux man-pages 6.20, dated 2026-02-08 β mwah, slay, periodt π β¨π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 2012, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms