.\" -*- coding: UTF-8 -*-
.\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de)
.\"
.\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
.\" Distributed under GPL
.\" %%%LICENSE_END
.\"
.\" Heavily based on glibc documentation
.\" Polished, added docs, removed glibc doc bug, 2002-07-20, aeb
.\"
.\"*******************************************************************
.\"
.\" This file was generated with po4a. Translate the source file.
.\"
.\"*******************************************************************
.TH MALLOC_HOOK 3 "1 Noviembre 2020" GNU "Manual del Programador de Linux"
.SH NOMBRE
__malloc_hook, __malloc_initialize_hook, __memalign_hook, __free_hook,
__realloc_hook, __after_morecore_hook \- variables de depuración de malloc
.SH SINOPSIS
.nf
\fB#include <malloc.h>\fP
.PP
\fBvoid *(*__malloc_hook)(size_t \fP\fIsize\fP\fB, const void *\fP\fIcaller\fP\fB);\fP
.PP
\fBvoid *(*__realloc_hook)(void *\fP\fIptr\fP\fB, size_t \fP\fIsize\fP\fB, const void *\fP\fIcaller\fP\fB);\fP
.PP
\fBvoid *(*__memalign_hook)(size_t \fP\fIalignment\fP\fB, size_t \fP\fIsize\fP\fB,\fP
\fB                         const void *\fP\fIcaller\fP\fB);\fP
.PP
\fBvoid (*__free_hook)(void *\fP\fIptr\fP\fB, const void *\fP\fIcaller\fP\fB);\fP
.PP
\fBvoid (*__malloc_initialize_hook)(void);\fP
.PP
\fBvoid (*__after_morecore_hook)(void);\fP
.fi
.SH DESCRIPCIÓN
La biblioteca de C de GNU le permite modificar el comportamiento de
\fBmalloc\fP(3), \fBrealloc\fP(3)  y \fBfree\fP(3)  especificando funciones de
«enganche» (\fIhook\fP) adecuadas. Puede usar estos enganches para que, por
ejemplo, le ayuden a depurar programas que usan asignaciones dinámicas de
memoria.
.PP
La variable \fB__malloc_initialize_hook\fP apunta a una función que se invoca
una única vez cuando se inicializa la implementación de malloc. Esta es una
varible normal, por lo que se puede redefinir en una aplicación de forma
parecida a la siguiente:
.PP
.in +4n
.EX
void (*__malloc_initialize_hook)(void) = my_init_hook;
.EE
.in
.PP
Ahora la función \fImy_init_hook\fP()  puede hacer la inicialización de todos
los enganches.
.PP
The four functions pointed to by \fB__malloc_hook\fP, \fB__realloc_hook\fP,
\fB__memalign_hook\fP, \fB__free_hook\fP have a prototype like the functions
\fBmalloc\fP(3), \fBrealloc\fP(3), \fBmemalign\fP(3), \fBfree\fP(3), respectively,
except that they have a final argument \fIcaller\fP that gives the address of
the caller of \fBmalloc\fP(3), etc.
.PP
The variable \fB__after_morecore_hook\fP points at a function that is called
each time after \fBsbrk\fP(2)  was asked for more memory.
.SH "CONFORME A"
Estas funciones son extensiones de GNU.
.SH NOTAS
.\" https://bugzilla.redhat.com/show_bug.cgi?id=450187
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=9957
The use of these hook functions is not safe in multithreaded programs, and
they are now deprecated.  From glibc 2.24 onwards, the
\fB__malloc_initialize_hook\fP variable has been removed from the API.
Programmers should instead preempt calls to the relevant functions by
defining and exporting functions such as "malloc" and "free".
.SH EJEMPLOS
A continuación tiene un pequeño ejemplo de cómo usar estas variables.
.PP
.EX
#include <stdio.h>
#include <malloc.h>

/* Prototipos para nuestros enganches.  */
static void my_init_hook(void);
static void *my_malloc_hook(size_t, const void *);

/* Variables para guardar los enganches originales. */
static void *(*old_malloc_hook)(size_t, const void *);

/* Redefinimos el enganche de inicialización de la biblioteca de 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;

    /* Restauramos todos los enganches originales */
    __malloc_hook = old_malloc_hook;

    /* Llamamos recursivamente a malloc */
    result = malloc(size);

    /* Guardamos los enganches originales */
    old_malloc_hook = __malloc_hook;

    /* printf() might call malloc(), so protect it too. */
    printf("malloc(%zu) called from %p returns %p\en",
            size, caller, result);

    /* Restauramos nuestros enganches */
    __malloc_hook = my_malloc_hook;

    return result;
}
.EE
.SH "VÉASE TAMBIÉN"
\fBmallinfo\fP(3), \fBmalloc\fP(3), \fBmcheck\fP(3), \fBmtrace\fP(3)
.SH COLOFÓN
Esta página es parte de la versión 5.10 del proyecto Linux
\fIman\-pages\fP. Puede encontrar una descripción del proyecto, información
sobre cómo informar errores y la última versión de esta página en
\%https://www.kernel.org/doc/man\-pages/.
.PP
.SH TRADUCCIÓN
La traducción al español de esta página del manual fue creada por
Juan Piernas <piernas@ditec.um.es>
.
.PP
Esta traducción es documentación libre; lea la
.UR https://www.gnu.org/licenses/gpl-3.0.html
GNU General Public License Version 3
.UE
o posterior con respecto a las condiciones de copyright.
No existe NINGUNA RESPONSABILIDAD.
.PP
Si encuentra algún error en la traducción de esta página del manual, envíe un
correo electrónico a
.MT debian-l10n-spanish@lists.debian.org
.ME .