Mercurial > sdl-ios-xcode
comparison src/stdlib/SDL_malloc.c @ 4077:a9df0628d256 SDL-1.2
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:07:02 +0000 |
parents | d634945c6172 |
children | a1b03ba2fcd0 |
comparison
equal
deleted
inserted
replaced
4076:9a7c62bbc8b3 | 4077:a9df0628d256 |
---|---|
3406 ACQUIRE_MORECORE_LOCK(); | 3406 ACQUIRE_MORECORE_LOCK(); |
3407 | 3407 |
3408 if (ss == 0) { /* First time through or recovery */ | 3408 if (ss == 0) { /* First time through or recovery */ |
3409 char* base = (char*)CALL_MORECORE(0); | 3409 char* base = (char*)CALL_MORECORE(0); |
3410 if (base != CMFAIL) { | 3410 if (base != CMFAIL) { |
3411 asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE); | 3411 asize = granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE); |
3412 /* Adjust to end on a page boundary */ | 3412 /* Adjust to end on a page boundary */ |
3413 if (!is_page_aligned(base)) | 3413 if (!is_page_aligned(base)) |
3414 asize += (page_align((size_t)base) - (size_t)base); | 3414 asize += (page_align((size_t)base) - (size_t)base); |
3415 /* Can't call MORECORE if size is negative when treated as signed */ | 3415 /* Can't call MORECORE if size is negative when treated as signed */ |
3416 if (asize < HALF_MAX_SIZE_T && | 3416 if (asize < HALF_MAX_SIZE_T && |
3420 } | 3420 } |
3421 } | 3421 } |
3422 } | 3422 } |
3423 else { | 3423 else { |
3424 /* Subtract out existing available top space from MORECORE request. */ | 3424 /* Subtract out existing available top space from MORECORE request. */ |
3425 asize = granularity_align(nb - m->topsize + TOP_FOOT_SIZE + SIZE_T_ONE); | 3425 asize = granularity_align(nb - m->topsize + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE); |
3426 /* Use mem here only if it did continuously extend old space */ | 3426 /* Use mem here only if it did continuously extend old space */ |
3427 if (asize < HALF_MAX_SIZE_T && | 3427 if (asize < HALF_MAX_SIZE_T && |
3428 (br = (char*)(CALL_MORECORE(asize))) == ss->base+ss->size) { | 3428 (br = (char*)(CALL_MORECORE(asize))) == ss->base+ss->size) { |
3429 tbase = br; | 3429 tbase = br; |
3430 tsize = asize; | 3430 tsize = asize; |
3433 | 3433 |
3434 if (tbase == CMFAIL) { /* Cope with partial failure */ | 3434 if (tbase == CMFAIL) { /* Cope with partial failure */ |
3435 if (br != CMFAIL) { /* Try to use/extend the space we did get */ | 3435 if (br != CMFAIL) { /* Try to use/extend the space we did get */ |
3436 if (asize < HALF_MAX_SIZE_T && | 3436 if (asize < HALF_MAX_SIZE_T && |
3437 asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) { | 3437 asize < nb + TOP_FOOT_SIZE + SIZE_T_ONE) { |
3438 size_t esize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE - asize); | 3438 size_t esize = granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE - asize); |
3439 if (esize < HALF_MAX_SIZE_T) { | 3439 if (esize < HALF_MAX_SIZE_T) { |
3440 char* end = (char*)CALL_MORECORE(esize); | 3440 char* end = (char*)CALL_MORECORE(esize); |
3441 if (end != CMFAIL) | 3441 if (end != CMFAIL) |
3442 asize += esize; | 3442 asize += esize; |
3443 else { /* Can't use; try to release */ | 3443 else { /* Can't use; try to release */ |
3457 | 3457 |
3458 RELEASE_MORECORE_LOCK(); | 3458 RELEASE_MORECORE_LOCK(); |
3459 } | 3459 } |
3460 | 3460 |
3461 if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ | 3461 if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ |
3462 size_t req = nb + TOP_FOOT_SIZE + SIZE_T_ONE; | 3462 size_t req = nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE; |
3463 size_t rsize = granularity_align(req); | 3463 size_t rsize = granularity_align(req); |
3464 if (rsize > nb) { /* Fail if wraps around zero */ | 3464 if (rsize > nb) { /* Fail if wraps around zero */ |
3465 char* mp = (char*)(CALL_MMAP(rsize)); | 3465 char* mp = (char*)(CALL_MMAP(rsize)); |
3466 if (mp != CMFAIL) { | 3466 if (mp != CMFAIL) { |
3467 tbase = mp; | 3467 tbase = mp; |
3470 } | 3470 } |
3471 } | 3471 } |
3472 } | 3472 } |
3473 | 3473 |
3474 if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ | 3474 if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ |
3475 size_t asize = granularity_align(nb + TOP_FOOT_SIZE + SIZE_T_ONE); | 3475 size_t asize = granularity_align(nb + TOP_FOOT_SIZE + MALLOC_ALIGNMENT + SIZE_T_ONE); |
3476 if (asize < HALF_MAX_SIZE_T) { | 3476 if (asize < HALF_MAX_SIZE_T) { |
3477 char* br = CMFAIL; | 3477 char* br = CMFAIL; |
3478 char* end = CMFAIL; | 3478 char* end = CMFAIL; |
3479 ACQUIRE_MORECORE_LOCK(); | 3479 ACQUIRE_MORECORE_LOCK(); |
3480 br = (char*)(CALL_MORECORE(asize)); | 3480 br = (char*)(CALL_MORECORE(asize)); |