On Sat, 06 Oct 2012, Carlos R. Mafra escribió:

> On Sat,  6 Oct 2012 at 12:22:26 +0200, Rodolfo García Peñas wrote:
> > 
> > Some functions malloc an free are used. 
> > These functions should be replaced (always?) by wmalloc and wfree.
> 
> Why do you think they should be replaced?

Hi Carlos,

This is the function wfree:

void wfree(void *ptr)
{
        if (ptr)
#ifdef USE_BOEHM_GC
                /* This should eventually be removed, once the criss-cross
                 * of wmalloc()d memory being free()d, malloc()d memory being
                 * wfree()d, various misuses of calling wfree() on objects
                 * allocated by libc malloc() and calling libc free() on
                 * objects allocated by Boehm GC (think external libraries)
                 * is cleaned up.
                 */
                if (GC_base(ptr) != 0)
                        GC_FREE(ptr);
                else
                        free(ptr);
#else
                free(ptr);
#endif
        ptr = NULL;
}

It checks if the pointer exists, then free it and set it to NULL. IMO is better 
to avoid free a NULL pointer and get a crash with double free memory error.

About wmalloc:

void *wmalloc(size_t size)
{
        void *tmp;

        assert(size > 0);

#ifdef USE_BOEHM_GC
        tmp = GC_MALLOC(size);
#else
        tmp = malloc(size);
#endif
        if (tmp == NULL) {
                wwarning("malloc() failed. Retrying after 2s.");
                sleep(2);
#ifdef USE_BOEHM_GC
                tmp = GC_MALLOC(size);
#else
                tmp = malloc(size);
#endif
                if (tmp == NULL) {
                        if (Aborting) {
                                fputs("Really Bad Error: recursive malloc() 
failure.", stderr);
                                exit(-1);
                        } else {
                                wfatal("virtual memory exhausted");
                                Aborting = 1;
                                wAbort(False);
                        }
                }
        }
        memset(tmp, 0, size);
        return tmp;
}

Is better because:

1. If memory is not available, sleep and retry -> No crash.
2. If no memory is available at all, then do a "order" exit -> No crash.
3. Set the memory to "0". It is interesting for differet compilers (sometimes 
set the memory to 0, sometimes no). -> Memory always set to 0, less errors, no 
more memory initialization.
4. You can do "wwin = wmalloc(sizeof(wwin))" instead "wwin = (struct wwin*) 
malloc(wwin)"

Is my oppinion.

kix
-- 
||// //\\// Rodolfo "kix" Garcia
||\\// //\\ http://www.kix.es/


-- 
To unsubscribe, send mail to [email protected].

Reply via email to