Skip to content

Commit f708b7c

Browse files
committed
Unify zend_alloc error handling
1 parent 2c8f2e9 commit f708b7c

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

Zend/zend_alloc.c

Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,38 @@ stderr_last_error(char *msg)
415415
/* OS Allocation */
416416
/*****************/
417417

418+
static void zend_mm_munmap(void *addr, size_t size)
419+
{
420+
#ifdef _WIN32
421+
if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
422+
#if ZEND_MM_ERROR
423+
stderr_last_error("VirtualFree() failed");
424+
#endif
425+
}
426+
#else
427+
if (munmap(addr, size) != 0) {
428+
#if ZEND_MM_ERROR
429+
fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
430+
#endif
431+
}
432+
#endif
433+
}
434+
418435
#ifndef HAVE_MREMAP
419436
static void *zend_mm_mmap_fixed(void *addr, size_t size)
420437
{
421438
#ifdef _WIN32
422-
return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
439+
void *ptr = VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
440+
if (ptr == NULL) {
441+
#if ZEND_MM_ERROR
442+
stderr_last_error("VirtualAlloc(addr) failed");
443+
#endif
444+
return NULL;
445+
} else if (ptr != addr) {
446+
zend_mm_munmap(ptr, size);
447+
return NULL;
448+
}
449+
return ptr;
423450
#else
424451
int flags = MAP_PRIVATE | MAP_ANON;
425452
#if defined(MAP_EXCL)
@@ -434,11 +461,7 @@ static void *zend_mm_mmap_fixed(void *addr, size_t size)
434461
#endif
435462
return NULL;
436463
} else if (ptr != addr) {
437-
if (munmap(ptr, size) != 0) {
438-
#if ZEND_MM_ERROR
439-
fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
440-
#endif
441-
}
464+
zend_mm_munmap(ptr, size);
442465
return NULL;
443466
}
444467
return ptr;
@@ -450,10 +473,9 @@ static void *zend_mm_mmap(size_t size)
450473
{
451474
#ifdef _WIN32
452475
void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
453-
454476
if (ptr == NULL) {
455477
#if ZEND_MM_ERROR
456-
stderr_last_error("VirtualAlloc() failed");
478+
stderr_last_error("VirtualAlloc(NULL) failed");
457479
#endif
458480
return NULL;
459481
}
@@ -482,23 +504,6 @@ static void *zend_mm_mmap(size_t size)
482504
#endif
483505
}
484506

485-
static void zend_mm_munmap(void *addr, size_t size)
486-
{
487-
#ifdef _WIN32
488-
if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
489-
#if ZEND_MM_ERROR
490-
stderr_last_error("VirtualFree() failed");
491-
#endif
492-
}
493-
#else
494-
if (munmap(addr, size) != 0) {
495-
#if ZEND_MM_ERROR
496-
fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
497-
#endif
498-
}
499-
#endif
500-
}
501-
502507
/***********/
503508
/* Bitmask */
504509
/***********/
@@ -1846,11 +1851,7 @@ static zend_mm_heap *zend_mm_init(void)
18461851

18471852
if (UNEXPECTED(chunk == NULL)) {
18481853
#if ZEND_MM_ERROR
1849-
#ifdef _WIN32
1850-
stderr_last_error("Can't initialize heap");
1851-
#else
1852-
fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
1853-
#endif
1854+
fprintf(stderr, "\nCan't initialize heap\n");
18541855
#endif
18551856
return NULL;
18561857
}
@@ -2978,11 +2979,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
29782979
chunk = (zend_mm_chunk*)handlers->chunk_alloc(&tmp_storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
29792980
if (UNEXPECTED(chunk == NULL)) {
29802981
#if ZEND_MM_ERROR
2981-
#ifdef _WIN32
2982-
stderr_last_error("Can't initialize heap");
2983-
#else
2984-
fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
2985-
#endif
2982+
fprintf(stderr, "\nCan't initialize heap\n");
29862983
#endif
29872984
return NULL;
29882985
}
@@ -3025,11 +3022,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
30253022
if (!storage) {
30263023
handlers->chunk_free(&tmp_storage, chunk, ZEND_MM_CHUNK_SIZE);
30273024
#if ZEND_MM_ERROR
3028-
#ifdef _WIN32
3029-
stderr_last_error("Can't initialize heap");
3030-
#else
3031-
fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
3032-
#endif
3025+
fprintf(stderr, "\nCan't initialize heap\n");
30333026
#endif
30343027
return NULL;
30353028
}

0 commit comments

Comments
 (0)