Fixed bug #428
authorSam Lantinga <slouken@libsdl.org>
Mon, 16 Jul 2007 00:08:35 +0000
changeset 2203fca1cdc673b2
parent 2202 98e76ba7d5a4
child 2204 9f64d06fa168
Fixed bug #428

This fix is overkill, but approved by Doug Lea, and he'll be releasing a
new version of his malloc.c sometime next month.
src/stdlib/SDL_malloc.c
     1.1 --- a/src/stdlib/SDL_malloc.c	Sun Jul 15 21:54:16 2007 +0000
     1.2 +++ b/src/stdlib/SDL_malloc.c	Mon Jul 16 00:08:35 2007 +0000
     1.3 @@ -2727,8 +2727,7 @@
     1.4              }
     1.5          }
     1.6          u = u->fd;
     1.7 -    }
     1.8 -    while (u != t);
     1.9 +    } while (u != t);
    1.10      assert(head != 0);
    1.11  }
    1.12  
    1.13 @@ -2784,8 +2783,7 @@
    1.14              do {
    1.15                  if (p == x)
    1.16                      return 1;
    1.17 -            }
    1.18 -            while ((p = p->fd) != b);
    1.19 +            } while ((p = p->fd) != b);
    1.20          }
    1.21      } else {
    1.22          bindex_t tidx;
    1.23 @@ -2802,8 +2800,7 @@
    1.24                  do {
    1.25                      if (u == (tchunkptr) x)
    1.26                          return 1;
    1.27 -                }
    1.28 -                while ((u = u->fd) != t);
    1.29 +                } while ((u = u->fd) != t);
    1.30              }
    1.31          }
    1.32      }
    1.33 @@ -3476,7 +3473,9 @@
    1.34          if (ss == 0) {          /* First time through or recovery */
    1.35              char *base = (char *) CALL_MORECORE(0);
    1.36              if (base != CMFAIL) {
    1.37 -                asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
    1.38 +                asize =
    1.39 +                    granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT +
    1.40 +                                      SIZE_T_ONE);
    1.41                  /* Adjust to end on a page boundary */
    1.42                  if (!is_page_aligned(base))
    1.43                      asize += (page_align((size_t) base) - (size_t) base);
    1.44 @@ -3491,7 +3490,7 @@
    1.45              /* Subtract out existing available top space from MORECORE request. */
    1.46              asize =
    1.47                  granularity_align(nb - m->topsize + TOP_FOOT_SIZE +
    1.48 -                                  SIZE_T_ONE);
    1.49 +                                  MALLOC_ALIGNMENT + SIZE_T_ONE);
    1.50              /* Use mem here only if it did continuously extend old space */
    1.51              if (asize < HALF_MAX_SIZE_T &&
    1.52                  (br =
    1.53 @@ -3507,7 +3506,8 @@
    1.54                      asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) {
    1.55                      size_t esize =
    1.56                          granularity_align(nb + TOP_FOOT_SIZE +
    1.57 -                                          SIZE_T_ONE - asize);
    1.58 +                                          MALLOC_ALIGNMENT + SIZE_T_ONE -
    1.59 +                                          asize);
    1.60                      if (esize < HALF_MAX_SIZE_T) {
    1.61                          char *end = (char *) CALL_MORECORE(esize);
    1.62                          if (end != CMFAIL)
    1.63 @@ -3530,7 +3530,7 @@
    1.64      }
    1.65  
    1.66      if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */
    1.67 -        size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
    1.68 +        size_t req = nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE;
    1.69          size_t rsize = granularity_align(req);
    1.70          if (rsize > nb) {       /* Fail if wraps around zero */
    1.71              char *mp = (char *) (CALL_MMAP(rsize));
    1.72 @@ -3543,7 +3543,9 @@
    1.73      }
    1.74  
    1.75      if (HAVE_MORECORE && tbase == CMFAIL) {     /* Try noncontiguous MORECORE */
    1.76 -        size_t asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
    1.77 +        size_t asize =
    1.78 +            granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT +
    1.79 +                              SIZE_T_ONE);
    1.80          if (asize < HALF_MAX_SIZE_T) {
    1.81              char *br = CMFAIL;
    1.82              char *end = CMFAIL;
    1.83 @@ -3679,9 +3681,8 @@
    1.84          if (m->topsize > pad) {
    1.85              /* Shrink top space in granularity-size units, keeping at least one */
    1.86              size_t unit = mparams.granularity;
    1.87 -            size_t extra =
    1.88 -                ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
    1.89 -                 SIZE_T_ONE) * unit;
    1.90 +            size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
    1.91 +                            SIZE_T_ONE) * unit;
    1.92              msegmentptr sp = segment_holding(m, (char *) m->top);
    1.93  
    1.94              if (!is_extern_segment(sp)) {
    1.95 @@ -3689,10 +3690,10 @@
    1.96                      if (HAVE_MMAP && sp->size >= extra && !has_segment_link(m, sp)) {   /* can't shrink if pinned */
    1.97                          size_t newsize = sp->size - extra;
    1.98                          /* Prefer mremap, fall back to munmap */
    1.99 -                        if ((CALL_MREMAP
   1.100 -                             (sp->base, sp->size, newsize, 0) != MFAIL)
   1.101 -                            || (CALL_MUNMAP(sp->base + newsize, extra)
   1.102 -                                == 0)) {
   1.103 +                        if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) !=
   1.104 +                             MFAIL)
   1.105 +                            || (CALL_MUNMAP(sp->base + newsize, extra) ==
   1.106 +                                0)) {
   1.107                              released = extra;
   1.108                          }
   1.109                      }
   1.110 @@ -3962,11 +3963,10 @@
   1.111                     We've allocated enough total room so that this is always
   1.112                     possible.
   1.113                   */
   1.114 -                char *br = (char *)
   1.115 -                    mem2chunk((size_t)
   1.116 -                              (((size_t)
   1.117 -                                (mem + alignment -
   1.118 -                                 SIZE_T_ONE)) & -alignment));
   1.119 +                char *br = (char *) mem2chunk((size_t) (((size_t) (mem +
   1.120 +                                                                   alignment -
   1.121 +                                                                   SIZE_T_ONE))
   1.122 +                                                        & -alignment));
   1.123                  char *pos =
   1.124                      ((size_t) (br - (char *) (p)) >=
   1.125                       MIN_CHUNK_SIZE) ? br : br + alignment;
   1.126 @@ -5234,4 +5234,5 @@
   1.127  */
   1.128  
   1.129  #endif /* !HAVE_MALLOC */
   1.130 +
   1.131  /* vi: set ts=4 sw=4 expandtab: */