Provided by: manpages-es-dev_4.13-4_all bug

NOMBRE

       __malloc_hook,  __malloc_initialize_hook,  __memalign_hook,  __free_hook,  __realloc_hook,
       __after_morecore_hook - variables de depuración de malloc

SINOPSIS

       #include <malloc.h>

       void *(*__malloc_hook)(size_t size, const void *caller);

       void *(*__realloc_hook)(void *ptr, size_t size, const void *caller);

       void *(*__memalign_hook)(size_t alignment, size_t size,
                                const void *caller);

       void (*__free_hook)(void *ptr, const void *caller);

       void (*__malloc_initialize_hook)(void);

       void (*__after_morecore_hook)(void);

DESCRIPCIÓN

       La biblioteca de C de GNU le permite modificar el comportamiento de malloc(3),  realloc(3)
       y  free(3)   especificando  funciones  de  «enganche»  (hook)  adecuadas. Puede usar estos
       enganches para que, por ejemplo, le ayuden  a  depurar  programas  que  usan  asignaciones
       dinámicas de memoria.

       La  variable  __malloc_initialize_hook  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:

           void (*__malloc_initialize_hook)(void) = my_init_hook;

       Ahora la función my_init_hook()  puede hacer la inicialización de todos los enganches.

       The   four   functions  pointed  to  by  __malloc_hook,  __realloc_hook,  __memalign_hook,
       __free_hook have a  prototype  like  the  functions  malloc(3),  realloc(3),  memalign(3),
       free(3),  respectively,  except  that  they  have  a  final argument caller that gives the
       address of the caller of malloc(3), etc.

       The variable __after_morecore_hook points at a function that is  called  each  time  after
       sbrk(2)  was asked for more memory.

CONFORME A

       These functions are GNU extensions.

NOTAS

       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.  Programmers should instead preempt calls to the relevant functions
       by defining and exporting functions such as "malloc" and "free".

EJEMPLOS

       A continuación tiene un pequeño ejemplo de cómo usar estas variables.

       #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\n",
                   size, caller, result);

           /* Restauramos nuestros enganches */
           __malloc_hook = my_malloc_hook;

           return result;
       }

VÉASE TAMBIÉN

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

COLOFÓN

       Esta página es parte de la versión 5.10 del proyecto Linux man-pages. 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/.

TRADUCCIÓN

       La traducción  al  español  de  esta  página  del  manual  fue  creada  por  Juan  Piernas
       <piernas@ditec.um.es>

       Esta  traducción  es  documentación  libre;  lea  la  GNU General Public License Version 3
       ⟨https://www.gnu.org/licenses/gpl-3.0.html⟩ o posterior con respecto a las condiciones  de
       copyright.  No existe NINGUNA RESPONSABILIDAD.

       Si  encuentra  algún  error  en  la  traducción de esta página del manual, envíe un correo
       electrónico a debian-l10n-spanish@lists.debian.org ⟨⟩.