VirtualBox

Changeset 1852 in kBuild for trunk/src/kmk/incdep.c


Ignore:
Timestamp:
Oct 13, 2008 12:16:34 AM (16 years ago)
Author:
bird
Message:

kmk/incdep: fixed the multithreaded heap issue on darwin. Only one worker thread for the pthread bunch, should suffice.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/incdep.c

    r1848 r1852  
    7676#endif
    7777
     78#ifdef __APPLE__
     79# include <malloc/malloc.h>
     80# define PARSE_IN_WORKER
     81#endif
     82
    7883
    7984/*******************************************************************************
     
    195200/* The handles to the worker threads. */
    196201#ifdef HAVE_PTHREAD
    197 static pthread_t incdep_threads[2];
     202static pthread_t incdep_threads[1];
    198203#elif defined (WINDOWS32)
    199204static HANDLE incdep_threads[2];
     
    206211static int volatile incdep_terminate;
    207212
     213#ifdef __APPLE__
     214/* malloc zone for the incdep threads. */
     215static malloc_zone_t *incdep_zone;
     216#endif
     217
    208218
    209219/*******************************************************************************
     
    211221*******************************************************************************/
    212222static void incdep_flush_it (struct floc *);
    213 static void eval_include_dep_file (struct incdep *, struct floc *, int);
     223static void eval_include_dep_file (struct incdep *, struct floc *);
     224
     225
     226/* xmalloc wrapper.
     227   For working around multithreaded performance problems found on Darwin,
     228   Linux (glibc), and possibly other systems. */
     229static void *incdep_xmalloc (struct incdep *cur, size_t size)
     230{
     231  void *ptr;
     232
     233#ifdef __APPLE__
     234  if (cur && cur->is_worker)
     235    {
     236      ptr = malloc_zone_malloc (incdep_zone, size);
     237      if (!ptr)
     238        fatal (NILF, _("virtual memory exhausted"));
     239    }
     240  else
     241    ptr = xmalloc (size);
     242#else
     243  ptr = xmalloc (size);
     244#endif
     245
     246  (void)cur;
     247  return ptr;
     248}
     249
     250/* memset(malloc(sz),'\0',sz) wrapper. */
     251static void *incdep_xcalloc (struct incdep *cur, size_t size)
     252{
     253  void *ptr;
     254
     255#ifdef __APPLE__
     256  if (cur && cur->is_worker)
     257    ptr = malloc_zone_calloc (incdep_zone, size, 1);
     258  else
     259    ptr = calloc (size, 1);
     260#else
     261  ptr = calloc (size, 1);
     262#endif
     263  if (!ptr)
     264    fatal (NILF, _("virtual memory exhausted"));
     265
     266  (void)cur;
     267  return ptr;
     268}
     269
     270/* free wrapper */
     271static void incdep_xfree (struct incdep *cur, void *ptr)
     272{
     273  /* free() *must* work for the allocation hacks above because
     274     of free_dep_chain. */
     275  free (ptr);
     276  (void)cur;
     277}
    214278
    215279
     
    347411  if (!fstat (fd, &st))
    348412    {
    349       cur->file_base = xmalloc (st.st_size + 1);
     413      cur->file_base = incdep_xmalloc (cur, st.st_size + 1);
    350414      if (read (fd, cur->file_base, st.st_size) == st.st_size)
    351415        {
     
    359423
    360424      error (f, "%s: read: %s", cur->name, strerror (errno));
    361       free (cur->file_base);
     425      incdep_xfree (cur, cur->file_base);
    362426    }
    363427  else
     
    371435/* Free the incdep structure. */
    372436static void
    373 incdep_free (struct incdep *cur)
     437incdep_freeit (struct incdep *cur)
    374438{
    375439#ifdef PARSE_IN_WORKER
     
    379443#endif
    380444
    381   free (cur->file_base);
     445  incdep_xfree (cur, cur->file_base);
    382446  free (cur);
    383447}
     
    408472
    409473      incdep_unlock ();
     474      cur->is_worker = 1;
    410475      incdep_read_file (cur, NILF);
    411476#ifdef PARSE_IN_WORKER
    412       eval_include_dep_file (cur, NILF, 1 /* is_worker */);
    413 #endif
     477      eval_include_dep_file (cur, NILF);
     478#endif
     479      cur->is_worker = 0;
    414480      incdep_lock ();
    415481
     
    475541  int tid;
    476542#endif
     543
     544  /* heap hacks */
     545
     546#ifdef __APPLE__
     547  incdep_zone = malloc_create_zone (0, 0);
     548  if (!incdep_zone)
     549    incdep_zone = malloc_default_zone ();
     550#endif
     551
    477552
    478553  /* create the mutex and two condition variables / event objects. */
     
    615690                                rec_vis->set,
    616691                                rec_vis->flocp);
    617         free (rec_vis->name);
     692        incdep_xfree (cur, rec_vis->name);
    618693        rec_vis = rec_vis->next;
    619         free (free_me);
     694        incdep_xfree (cur, free_me);
    620695      }
    621696    while (rec_vis);
     
    638713                                  rec_vd->flavor,
    639714                                  rec_vd->target_var);
    640         free (rec_vd->name);
     715        incdep_xfree (cur, rec_vd->name);
    641716        rec_vd = rec_vd->next;
    642         free (free_me);
     717        incdep_xfree (cur, free_me);
    643718      }
    644719    while (rec_vd);
     
    663738
    664739        newname = strcache_add (rec_f->filenames->name);
    665         free ((char *)rec_f->filenames->name);
     740        incdep_xfree (cur, (char *)rec_f->filenames->name);
    666741        rec_f->filenames->name = newname;
    667742
     
    677752
    678753        rec_f = rec_f->next;
    679         free (free_me);
     754        incdep_xfree (cur, free_me);
    680755      }
    681756    while (rec_f);
     
    717792      /* Duplicate the string. The other recorders knows which arguments
    718793         needs to be added to the string cache later. */
    719       char *newstr = xmalloc (len + 1);
     794      char *newstr = incdep_xmalloc (cur, len + 1);
    720795      memcpy (newstr, str, len);
    721796      newstr[len] = '\0';
     
    747822  else
    748823    {
    749       struct incdep_variable_in_set *rec = xmalloc (sizeof (*rec));
     824      struct incdep_variable_in_set *rec = incdep_xmalloc (cur, sizeof (*rec));
    750825      rec->name = (char *)name;
    751826      rec->name_length = name_length;
     
    786861  else
    787862    {
    788       struct incdep_variable_def *rec = xmalloc (sizeof (*rec));
     863      struct incdep_variable_def *rec = incdep_xmalloc (cur, sizeof (*rec));
    789864      rec->flocp = flocp;
    790865      rec->name = (char *)name;
     
    823898  else
    824899    {
    825       struct incdep_recorded_files *rec = xmalloc (sizeof (*rec));
     900      struct incdep_recorded_files *rec = incdep_xmalloc (cur, sizeof (*rec));
    826901
    827902      rec->filenames = filenames;
     
    866941   */
    867942static void
    868 eval_include_dep_file (struct incdep *curdep, struct floc *f, int is_worker)
     943eval_include_dep_file (struct incdep *curdep, struct floc *f)
    869944{
    870945  unsigned line_no = 1;
     
    876951  if (!cur)
    877952    return;
    878   curdep->is_worker = is_worker;
    879953
    880954  /* now parse the file. */
     
    9831057
    9841058          /* make a copy of the value, converting \r\n to \n, and define it. */
    985           value = xmalloc (value_len + 1);
     1059          value = incdep_xmalloc (curdep, value_len + 1);
    9861060          endp = memchr (value_start, '\r', value_len);
    9871061          if (endp)
     
    11421216              /* make a copy of the value, converting \r\n to \n, and define it. */
    11431217              value_len = value_end - value_start;
    1144               value = xmalloc (value_len + 1);
     1218              value = incdep_xmalloc (curdep, value_len + 1);
    11451219              if (!multi_line)
    11461220                  memcpy (value, value_start, value_len);
     
    12131287                  break;
    12141288                }
    1215               filenames = xmalloc (sizeof (struct nameseq));
     1289              filenames = incdep_xmalloc (curdep, sizeof (struct nameseq));
    12161290              memset (filenames, 0, sizeof (*filenames));
    12171291              filenames->name = incdep_record_strcache (curdep, cur, endp - cur);
     
    12531327
    12541328                  /* add it to the list. */
    1255                   *nextdep = dep = alloc_dep ();
     1329                  *nextdep = dep = incdep_xcalloc (curdep, sizeof (*dep));
    12561330                  dep->name = incdep_record_strcache (curdep, cur, endp - cur);
    12571331                  dep->includedep = 1;
     
    12691343
    12701344  /* free the file data */
    1271   if (is_worker)
    1272     {
    1273       free (curdep->file_base);
    1274       curdep->file_base = curdep->file_end = NULL;
    1275     }
     1345  incdep_xfree (curdep, curdep->file_base);
     1346  curdep->file_base = curdep->file_end = NULL;
    12761347}
    12771348
     
    12961367
    12971368          incdep_read_file (cur, f);
    1298           eval_include_dep_file (cur, f, 0);
    1299           incdep_free (cur);
     1369          eval_include_dep_file (cur, f);
     1370          incdep_freeit (cur);
    13001371
    13011372          incdep_lock ();
     
    13241395          incdep_flush_recorded_instructions (cur);
    13251396#else
    1326           eval_include_dep_file (cur, f, 0);
    1327 #endif
    1328           incdep_free (cur);
     1397          eval_include_dep_file (cur, f);
     1398#endif
     1399          incdep_freeit (cur);
    13291400          cur = next;
    13301401        }
     
    13521423  while ((name = find_next_token (&names_iterator, &name_len)) != 0)
    13531424    {
    1354        cur = xmalloc (sizeof (*cur) + name_len);
     1425       cur = xmalloc (sizeof (*cur) + name_len); /* not incdep_xmalloc here */
    13551426       cur->file_base = cur->file_end = NULL;
    13561427       memcpy (cur->name, name, name_len);
     
    13851456          struct incdep *next = cur->next;
    13861457          incdep_read_file (cur, f);
    1387           eval_include_dep_file (cur, f, 0); /* eats cur */
     1458          eval_include_dep_file (cur, f);
     1459          incdep_freeit (cur);
    13881460          cur = next;
    13891461        }
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette