httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bh...@gensym.com (Ben Hyde)
Subject Re: mutex in palloc
Date Tue, 09 Dec 1997 10:29:25 GMT
Ben Laurie wrote:
>Ben Hyde wrote:
>> Is the critical region in palloc so narrow
>> because allocation in a given pool is never
>> done by more than a single thread?
>I haven't checked the code, but that seems like a rash assumption, if
>true.

This untested, uncompiled rewrite illustrates my concern.

  --- BEFORE  ---

API_EXPORT(void *) palloc(struct pool *a, int reqsize)
{
#ifdef ALLOC_USE_MALLOC
  ...
#else

    /* Round up requested size to an even number of alignment units (core clicks)
     */

    int nclicks = 1 + ((reqsize - 1) / CLICK_SZ);
    int size = nclicks * CLICK_SZ;
    char *result;

    if (reqsize <= 0)
      return NULL;
    block_alarms();
    (void)acquire_mutex(alloc_mutex);
    {
      /* First, see if we have space in the block most recently
       * allocated to this pool
       */
      union block_hdr *blok = a->last;
      char *first_avail = blok->h.first_avail;
      char *new_first_avail = first_avail + size;

      if (new_first_avail <= blok->h.endp) {
	debug_verify_filled(first_avail, blok->h.endp,
			    "Ouch!  Someone trounced past the end of their allocation!\n");
	blok->h.first_avail = new_first_avail;
	result = first_avail;
      } else {
	/* Nope --- get a new one that's guaranteed to be big enough */
	blok = new_block(size);
	a->last->h.next = blok;
	a->last = blok;
	result = blok->h.first_avail;
	blok->h.first_avail += size;
      }
    }
    (void)release_mutex(alloc_mutex);
    unblock_alarms();

    return (void *)result;
#endif
}

  --- AFTER ---

API_EXPORT(void *) palloc(struct pool *a, int reqsize)
{
#ifdef ALLOC_USE_MALLOC
   ...
#else
    int nclicks = 1 + ((reqsize - 1) / CLICK_SZ);
    int size = nclicks * CLICK_SZ;  /* Align size */
    char *result;

    if (reqsize <= 0)
      return NULL;
    block_alarms();
    (void)acquire_mutex(alloc_mutex);
    {
      union block_hdr *blok = a->last;
      char *first_avail = blok->h.first_avail;
      char *new_first_avail = first_avail + size;

      if (new_first_avail <= blok->h.endp) {
	debug_verify_filled(first_avail, blok->h.endp,
			    "Ouch!  Someone trounced past the end of their allocation!\n");
	blok->h.first_avail = new_first_avail;
	result = first_avail;
      } else {
	blok = new_block(size);
	a->last->h.next = blok;
	a->last = blok;
	result = blok->h.first_avail;
	blok->h.first_avail += size;
      }
    }
    (void)release_mutex(alloc_mutex);
    unblock_alarms();

    return (void *)result;
#endif
}

Mime
View raw message