Blame libdw/libdw_alloc.c

Packit 032894
/* Memory handling for libdw.
Packit 032894
   Copyright (C) 2003, 2004, 2006 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2003.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <errno.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include "libdwP.h"
Packit 032894
#include "system.h"
Packit 032894
#include "atomics.h"
Packit 032894
#if USE_VG_ANNOTATIONS == 1
Packit 032894
#include <helgrind.h>
Packit 032894
#else
Packit 032894
#define ANNOTATE_HAPPENS_BEFORE(X)
Packit 032894
#define ANNOTATE_HAPPENS_AFTER(X)
Packit 032894
#endif
Packit 032894
Packit 032894
#define THREAD_ID_UNSET ((size_t) -1)
Packit 032894
static __thread size_t thread_id = THREAD_ID_UNSET;
Packit 032894
static atomic_size_t next_id = ATOMIC_VAR_INIT(0);
Packit 032894
Packit 032894
struct libdw_memblock *
Packit 032894
__libdw_alloc_tail (Dwarf *dbg)
Packit 032894
{
Packit 032894
  if (thread_id == THREAD_ID_UNSET)
Packit 032894
    thread_id = atomic_fetch_add (&next_id, 1);
Packit 032894
Packit 032894
  pthread_rwlock_rdlock (&dbg->mem_rwl);
Packit 032894
  if (thread_id >= dbg->mem_stacks)
Packit 032894
    {
Packit 032894
      pthread_rwlock_unlock (&dbg->mem_rwl);
Packit 032894
      pthread_rwlock_wrlock (&dbg->mem_rwl);
Packit 032894
Packit 032894
      /* Another thread may have already reallocated. In theory using an
Packit 032894
         atomic would be faster, but given that this only happens once per
Packit 032894
         thread per Dwarf, some minor slowdown should be fine.  */
Packit 032894
      if (thread_id >= dbg->mem_stacks)
Packit 032894
        {
Packit 032894
          dbg->mem_tails = realloc (dbg->mem_tails, (thread_id+1)
Packit 032894
                                    * sizeof (struct libdw_memblock *));
Packit 032894
          if (dbg->mem_tails == NULL)
Packit 032894
            {
Packit 032894
              pthread_rwlock_unlock (&dbg->mem_rwl);
Packit 032894
              dbg->oom_handler();
Packit 032894
            }
Packit 032894
          for (size_t i = dbg->mem_stacks; i <= thread_id; i++)
Packit 032894
            dbg->mem_tails[i] = NULL;
Packit 032894
          dbg->mem_stacks = thread_id + 1;
Packit 032894
          ANNOTATE_HAPPENS_BEFORE (&dbg->mem_tails);
Packit 032894
        }
Packit 032894
Packit 032894
      pthread_rwlock_unlock (&dbg->mem_rwl);
Packit 032894
      pthread_rwlock_rdlock (&dbg->mem_rwl);
Packit 032894
    }
Packit 032894
Packit 032894
  /* At this point, we have an entry in the tail array.  */
Packit 032894
  ANNOTATE_HAPPENS_AFTER (&dbg->mem_tails);
Packit 032894
  struct libdw_memblock *result = dbg->mem_tails[thread_id];
Packit 032894
  if (result == NULL)
Packit 032894
    {
Packit 032894
      result = malloc (dbg->mem_default_size);
Packit Service 35cfd5
      if (result == NULL)
Packit Service 35cfd5
	{
Packit Service 35cfd5
	  pthread_rwlock_unlock (&dbg->mem_rwl);
Packit Service 35cfd5
	  dbg->oom_handler();
Packit Service 35cfd5
	}
Packit 032894
      result->size = dbg->mem_default_size
Packit 032894
                     - offsetof (struct libdw_memblock, mem);
Packit 032894
      result->remaining = result->size;
Packit 032894
      result->prev = NULL;
Packit 032894
      dbg->mem_tails[thread_id] = result;
Packit 032894
    }
Packit 032894
  pthread_rwlock_unlock (&dbg->mem_rwl);
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
/* Can only be called after a allocation for this thread has already
Packit 032894
   been done, to possibly undo it.  */
Packit 032894
struct libdw_memblock *
Packit 032894
__libdw_thread_tail (Dwarf *dbg)
Packit 032894
{
Packit 032894
  struct libdw_memblock *result;
Packit 032894
  pthread_rwlock_rdlock (&dbg->mem_rwl);
Packit 032894
  result = dbg->mem_tails[thread_id];
Packit 032894
  pthread_rwlock_unlock (&dbg->mem_rwl);
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
void *
Packit 032894
__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
Packit 032894
{
Packit 032894
  size_t size = MAX (dbg->mem_default_size,
Packit 032894
		     (align - 1 +
Packit 032894
		      2 * minsize + offsetof (struct libdw_memblock, mem)));
Packit 032894
  struct libdw_memblock *newp = malloc (size);
Packit 032894
  if (newp == NULL)
Packit 032894
    dbg->oom_handler ();
Packit 032894
Packit 032894
  uintptr_t result = ((uintptr_t) newp->mem + align - 1) & ~(align - 1);
Packit 032894
Packit 032894
  newp->size = size - offsetof (struct libdw_memblock, mem);
Packit 032894
  newp->remaining = (uintptr_t) newp + size - (result + minsize);
Packit 032894
Packit 032894
  pthread_rwlock_rdlock (&dbg->mem_rwl);
Packit 032894
  newp->prev = dbg->mem_tails[thread_id];
Packit 032894
  dbg->mem_tails[thread_id] = newp;
Packit 032894
  pthread_rwlock_unlock (&dbg->mem_rwl);
Packit 032894
Packit 032894
  return (void *) result;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
Dwarf_OOM
Packit 032894
dwarf_new_oom_handler (Dwarf *dbg, Dwarf_OOM handler)
Packit 032894
{
Packit 032894
  Dwarf_OOM old = dbg->oom_handler;
Packit 032894
  dbg->oom_handler = handler;
Packit 032894
  return old;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
void
Packit 032894
__attribute ((noreturn)) attribute_hidden
Packit 032894
__libdw_oom (void)
Packit 032894
{
Packit 032894
  while (1)
Packit 032894
    error (EXIT_FAILURE, ENOMEM, "libdw");
Packit 032894
}