Re: [AD] Addons and al_free and non-public functions

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On 2010-05-20, Peter Wang <novalazy@xxxxxxxxxx> wrote:
> 
> How about this?  The macros would be unconditionally defined:
> 
>    #define al_malloc(n) al_malloc_with_context(n, __LINE__, __FILE__, __func__)
>    #define al_free(n)   al_free_with_context(n, __LINE__, __FILE__, __func__)
>    #define al_realloc(p, n) ...
> 
> By default, Allegro won't do malloc debugging, so the context arguments
> will just be ignored.  Since they are only constants, there should not
> be any real overhead to passing them unconditionally.
> 
> al_*_with_context may be called with dummy arguments if necessary,
> e.g. for language bindings.
> 
> Once all the Allegro code base consistently uses al_malloc (post-5.0)
> the user would be allowed to override the vtable.

I have implemented this locally.  It turned out to be easy to
grep/replace all instances of malloc/_AL_MALLOC etc. in the library and
addons so we can add back the ability to override the memory management
functions.

The old al_set_memory_management_functions function had too many loose
arguments, so my proposal is:

    void al_set_memory_interface(ALLEGRO_MEMORY_INTERFACE *iface);

I have attached memory.h from my workspace.

Peter
/*         ______   ___    ___
 *        /\  _  \ /\_ \  /\_ \
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Memory management routines.
 *
 *      See readme.txt for copyright information.
 */

#ifndef ALLEGRO_MEMORY_H
#define ALLEGRO_MEMORY_H

#ifdef __cplusplus
   extern "C" {
#endif


/* Type: ALLEGRO_MEMORY_INTERFACE
 */
typedef struct ALLEGRO_MEMORY_INTERFACE ALLEGRO_MEMORY_INTERFACE;

struct ALLEGRO_MEMORY_INTERFACE {
   void *(*mi_malloc)(size_t n, int line, const char *file, const char *func);
   void (*mi_free)(void *ptr, int line, const char *file, const char *func);
   void *(*mi_realloc)(void *ptr, size_t n, int line, const char *file, const char *func);
   void *(*mi_calloc)(size_t count, size_t n, int line, const char *file, const char *func);
};

AL_FUNC(void, al_set_memory_interface, (ALLEGRO_MEMORY_INTERFACE *iface));


/* Function: al_malloc
 */
#define al_malloc(n) (al_malloc_with_context((n), __LINE__, __FILE__, __func__))

/* Function: al_free
 */
#define al_free(p) (al_free_with_context((p), __LINE__, __FILE__, __func__))

/* Function: al_realloc
 */
#define al_realloc(p, n) (al_realloc_with_context((p), (n), __LINE__, __FILE__, __func__))

/* Function: al_calloc
 */
#define al_calloc(c, n) (al_calloc_with_context((c), (n), __LINE__, __FILE__, __func__))


AL_FUNC(void *, al_malloc_with_context, (size_t n,
   int line, const char *file, const char *func));
AL_FUNC(void, al_free_with_context, (void *ptr,
   int line, const char *file, const char *func));
AL_FUNC(void *, al_realloc_with_context, (void *ptr, size_t n,
   int line, const char *file, const char *func));
AL_FUNC(void *, al_calloc_with_context, (size_t count, size_t n,
   int line, const char *file, const char *func));


#ifdef __cplusplus
   }
#endif

#endif          /* ifndef ALLEGRO_MEMORY_H */


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/