more cache manager issues

Eric Norum eric at cls.usask.ca
Tue Oct 31 14:34:07 UTC 2000


Till Straumann wrote:
> 
> Browsing through the cache manager implementation of
> `rtems-ss-20000929' caused me having some more questions
> / suggestions:
> 
>  - cache_aligned_malloc() (currently not called by any piece of code)
> must not
>      be used. Calling `free' on memory allocated by
> cache_aligned_malloc() results
>      in heap corruption.
> 

Here's my solution to the problem.  Note that a block of memory
allocated by rtems_cache_aligned_malloc can only be freed by calling
rtems_cache_aligned_free and that rtems_cache_aligned_free can be used
only to free a block of memory allocated by rtems_cache_aligned_malloc.

/*
 * Allocate a cache-line-aligned block of memory
 * The assumption here is that CPU_DATA_CACHE_ALIGNMENT is at least as
restrictive as any other data type alignment restriction.
 * DO NOT USE free() TO DEALLOCATE THIS MEMORY!
 */
void *rtems_cache_aligned_malloc (size_t nbytes)
{
#if defined(CPU_DATA_CACHE_ALIGNMENT)
        void *p, *pa;

        p = malloc (nbytes + 2 * sizeof (void *) +
CPU_DATA_CACHE_ALIGNMENT - 1);
        pa = (void *)((unsigned long)p + 2 * sizeof (void *) +
CPU_DATA_CACHE_ALIGNMENT - 1) & (CPU_DATA_CACHE_ALIGNMENT - 1));
        ((void **)pa)[-1] = (void *)0xCAFEFOOD; /* Sanity check */
        ((void **)pa)[-2] = p;
        return pa;
#else
        return malloc( nbytes );
#endif

}

/*
 * Free a block of memory allocated by rtems_cache_aligned_malloc
 */
void rtems_cache_aligned_free (void *pa)
{
#if defined(CPU_DATA_CACHE_ALIGNMENT)
        if (((void **)pa)[-1] == (void *)0xCAFEFOOD)
                free (((void **)pa)[-2]);
        else
                ????? error message?  panic?  do nothing and just live
with the memory leaks????
#else
        free (pa);
#endif
}

I don't have time to put these into the system so ... ``with failing
hands I throw the torch''.

-- 
Eric Norum                                 eric at cls.usask.ca
Canadian Light Source                      Phone: (306) 966-6308
University of Saskatchewan                 FAX:   (306) 966-6058
Saskatoon, Canada.



More information about the users mailing list