On Fri, Sep 21, 2012 at 4:11 AM, Tommi Rantala <[email protected]> wrote:
> HIDDEN void *
> sos_alloc (size_t size)
> {
> - char *mem;
> -
> -#ifdef HAVE_CMPXCHG
> - char *old_mem;
> + size_t pos;
>
> size = UNW_ALIGN(size, MAX_ALIGN);
> - if (!sos_memp)
> - cmpxchg_ptr (&sos_memp, 0, sos_memory);
> - do
> - {
> - old_mem = sos_memp;
>
> - mem = (char *) UNW_ALIGN((unsigned long) old_mem, MAX_ALIGN);
> - mem += size;
> - assert (mem < sos_memory + sizeof (sos_memory));
> - }
> - while (!cmpxchg_ptr (&sos_memp, old_mem, mem));
> +#if defined(__GNUC__)
> + /* Assume `sos_memory' is suitably aligned. */
> + assert(((uintptr_t) &sos_memory[0] & (MAX_ALIGN-1)) == 0);
> +#endif
> +
> +#if defined(__GNUC__) && defined(HAVE_FETCH_AND_ADD)
> + pos = fetch_and_add (&sos_memory_freepos, size);
> #else
> static define_lock (sos_lock);
> intrmask_t saved_mask;
>
> - size = UNW_ALIGN(size, MAX_ALIGN);
> -
> lock_acquire (&sos_lock, saved_mask);
> {
> - if (!sos_memp)
> - sos_memp = sos_memory;
> -
> - mem = (char *) UNW_ALIGN((unsigned long) sos_memp, MAX_ALIGN);
> - mem += size;
> - assert (mem < sos_memory + sizeof (sos_memory));
> - sos_memp = mem;
> +# ifndef __GNUC__
> + /* No assumptions about `sos_memory' alignment. */
> + if (sos_memory_freepos == 0)
> + {
> + unsigned align = UNW_ALIGN((uintptr_t) &sos_memory[0], MAX_ALIGN)
> + - (uintptr_t) &sos_memory[0];
> + sos_memory_freepos = align;
> + }
> +# endif
> + pos = sos_memory_freepos;
> + sos_memory_freepos += size;
> }
> lock_release (&sos_lock, saved_mask);
> #endif
> - return mem;
> +
> + assert (((uintptr_t) &sos_memory[pos] & (MAX_ALIGN-1)) == 0);
> + assert ((pos+size) <= SOS_MEMORY_SIZE);
> +
> + return &sos_memory[pos];
> }
>
> /* Must be called while holding the mempool lock. */
That's a lot of ifdefs in machine independent code. I feel:
http://paste.ubuntu.com/1250227/
should suffice. This might pessimize the GNUC && !HAVE_FETCH_AND_ADD a
bit. Let me know if you think this is a problem.
-Arun
_______________________________________________
Libunwind-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/libunwind-devel