__malloc_hook(3) Library Functions Manual __malloc_hook(3)

__malloc_hook, __malloc_initialize_hook, __memalign_hook, __free_hook, __realloc_hook, __after_morecore_hook - malloc debugging variables (DEPRECATED)

Bibliothèque C standard (libc, -lc)

#include <malloc.h>
void *(*volatile __malloc_hook)(size_t size, const void *caller);
void *(*volatile __realloc_hook)(void *ptr, size_t size,
                         const void *caller);
void *(*volatile __memalign_hook)(size_t alignment, size_t size,
                         const void *caller);
void (*volatile __free_hook)(void *ptr, const void *caller);
void (*__malloc_initialize_hook)(void);
void (*volatile __after_morecore_hook)(void);

La bibliothèque C GNU vous permet de modifier le comportement de malloc(3), realloc(3) et free(3) en fixant les points d'entrée des routines. Vous pouvez utiliser ces points pour, par exemple, faciliter le débogage des programmes utilisant des allocations de mémoire dynamique.

La variable __malloc_initialize_hook pointe vers une fonction qui est appelée une seule fois à l'initialisation de malloc(). C'est une variable libre qui peut être modifiée par l'application avec une déclaration comme celle-ci :


void (*__malloc_initialize_hook)(void) = my_init_hook;

À présent, la fonction my_init_hook() pourra faire toute l'initialisation des routines.

Les quatre fonctions pointées par __malloc_hook, __realloc_hook, __memalign_hook et __free_hook ont des prototypes semblables à ceux des fonctions malloc(3), realloc(3), memalign(3) et free(3), respectivement, avec un argument final supplémentaire caller qui fournit l'adresse du code appelant malloc(3), etc.

La variable __after_morecore_hook pointe sur une fonction qui est invoquée à chaque fois que sbrk(2) a été appelée pour augmenter la mémoire.

Ces fonctions sont des extensions GNU.

The use of these hook functions is not safe in multithreaded programs, and they are now deprecated. From glibc 2.24 onwards, the __malloc_initialize_hook variable has been removed from the API, and from glibc 2.34 onwards, all the hook variables have been removed from the API. Programmers should instead preempt calls to the relevant functions by defining and exporting malloc(), free(), realloc(), and calloc().

Voici un court exemple d'utilisation de ces variables.

#include <stdio.h>
#include <malloc.h>
/* Prototypes de nos routines */
static void my_init_hook(void);
static void *my_malloc_hook(size_t, const void *);
/* Variables pour sauver les routines originelles */
static void *(*old_malloc_hook)(size_t, const void *);
/* Surcharge de la routine d'initialisation de la bibliothèque C */
void (*__malloc_initialize_hook)(void) = my_init_hook;
static void
my_init_hook(void)
{

old_malloc_hook = __malloc_hook;
__malloc_hook = my_malloc_hook; } static void * my_malloc_hook(size_t size, const void *caller) {
void *result;
/* Restaurer la routine originale */
__malloc_hook = old_malloc_hook;
/* Appeler récursivement */
result = malloc(size);
/* Sauver la routine originale */
old_malloc_hook = __malloc_hook;
/* printf() peut appeler malloc(), il faut donc la protéger également */
printf("malloc(%zu) appelée depuis %p renvoie %p\n",
size, caller, result);
/* Restaurer nos propres routines */
__malloc_hook = my_malloc_hook;
return result; }

mallinfo(3), malloc(3), mcheck(3), mtrace(3)

La traduction française de cette page de manuel a été créée par Christophe Blaess https://www.blaess.fr/christophe/, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org> et Grégoire Scano <gregoire.scano@malloc.fr>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org.

7 janvier 2023 Pages du manuel de Linux 6.03