changeset 2203:fca1cdc673b2

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.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 16 Jul 2007 00:08:35 +0000
parents 98e76ba7d5a4
children 9f64d06fa168
files src/stdlib/SDL_malloc.c
diffstat 1 files changed, 24 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/src/stdlib/SDL_malloc.c	Sun Jul 15 21:54:16 2007 +0000
+++ b/src/stdlib/SDL_malloc.c	Mon Jul 16 00:08:35 2007 +0000
@@ -2727,8 +2727,7 @@
             }
         }
         u = u->fd;
-    }
-    while (u != t);
+    } while (u != t);
     assert(head != 0);
 }
 
@@ -2784,8 +2783,7 @@
             do {
                 if (p == x)
                     return 1;
-            }
-            while ((p = p->fd) != b);
+            } while ((p = p->fd) != b);
         }
     } else {
         bindex_t tidx;
@@ -2802,8 +2800,7 @@
                 do {
                     if (u == (tchunkptr) x)
                         return 1;
-                }
-                while ((u = u->fd) != t);
+                } while ((u = u->fd) != t);
             }
         }
     }
@@ -3476,7 +3473,9 @@
         if (ss == 0) {          /* First time through or recovery */
             char *base = (char *) CALL_MORECORE(0);
             if (base != CMFAIL) {
-                asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
+                asize =
+                    granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT +
+                                      SIZE_T_ONE);
                 /* Adjust to end on a page boundary */
                 if (!is_page_aligned(base))
                     asize += (page_align((size_t) base) - (size_t) base);
@@ -3491,7 +3490,7 @@
             /* Subtract out existing available top space from MORECORE request. */
             asize =
                 granularity_align(nb - m->topsize + TOP_FOOT_SIZE +
-                                  SIZE_T_ONE);
+                                  MALLOC_ALIGNMENT + SIZE_T_ONE);
             /* Use mem here only if it did continuously extend old space */
             if (asize < HALF_MAX_SIZE_T &&
                 (br =
@@ -3507,7 +3506,8 @@
                     asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) {
                     size_t esize =
                         granularity_align(nb + TOP_FOOT_SIZE +
-                                          SIZE_T_ONE - asize);
+                                          MALLOC_ALIGNMENT + SIZE_T_ONE -
+                                          asize);
                     if (esize < HALF_MAX_SIZE_T) {
                         char *end = (char *) CALL_MORECORE(esize);
                         if (end != CMFAIL)
@@ -3530,7 +3530,7 @@
     }
 
     if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */
-        size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE;
+        size_t req = nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE;
         size_t rsize = granularity_align(req);
         if (rsize > nb) {       /* Fail if wraps around zero */
             char *mp = (char *) (CALL_MMAP(rsize));
@@ -3543,7 +3543,9 @@
     }
 
     if (HAVE_MORECORE && tbase == CMFAIL) {     /* Try noncontiguous MORECORE */
-        size_t asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE);
+        size_t asize =
+            granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT +
+                              SIZE_T_ONE);
         if (asize < HALF_MAX_SIZE_T) {
             char *br = CMFAIL;
             char *end = CMFAIL;
@@ -3679,9 +3681,8 @@
         if (m->topsize > pad) {
             /* Shrink top space in granularity-size units, keeping at least one */
             size_t unit = mparams.granularity;
-            size_t extra =
-                ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
-                 SIZE_T_ONE) * unit;
+            size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit -
+                            SIZE_T_ONE) * unit;
             msegmentptr sp = segment_holding(m, (char *) m->top);
 
             if (!is_extern_segment(sp)) {
@@ -3689,10 +3690,10 @@
                     if (HAVE_MMAP && sp->size >= extra && !has_segment_link(m, sp)) {   /* can't shrink if pinned */
                         size_t newsize = sp->size - extra;
                         /* Prefer mremap, fall back to munmap */
-                        if ((CALL_MREMAP
-                             (sp->base, sp->size, newsize, 0) != MFAIL)
-                            || (CALL_MUNMAP(sp->base + newsize, extra)
-                                == 0)) {
+                        if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) !=
+                             MFAIL)
+                            || (CALL_MUNMAP(sp->base + newsize, extra) ==
+                                0)) {
                             released = extra;
                         }
                     }
@@ -3962,11 +3963,10 @@
                    We've allocated enough total room so that this is always
                    possible.
                  */
-                char *br = (char *)
-                    mem2chunk((size_t)
-                              (((size_t)
-                                (mem + alignment -
-                                 SIZE_T_ONE)) & -alignment));
+                char *br = (char *) mem2chunk((size_t) (((size_t) (mem +
+                                                                   alignment -
+                                                                   SIZE_T_ONE))
+                                                        & -alignment));
                 char *pos =
                     ((size_t) (br - (char *) (p)) >=
                      MIN_CHUNK_SIZE) ? br : br + alignment;
@@ -5234,4 +5234,5 @@
 */
 
 #endif /* !HAVE_MALLOC */
+
 /* vi: set ts=4 sw=4 expandtab: */