Skip to content

Commit d1dde41

Browse files
committed
Fix zend_alloc memory extend (realloc with increased size) for Windows
1 parent 2c8f2e9 commit d1dde41

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

Zend/zend_alloc.c

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,40 @@ 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 (GetLastError() == ERROR_INVALID_ADDRESS) {
442+
zend_mm_munmap(ptr, size);
443+
return NULL;
444+
}
445+
#if ZEND_MM_ERROR
446+
stderr_last_error("VirtualAlloc(addr) failed");
447+
#endif
448+
return NULL;
449+
}
450+
EXPECTED(ptr == addr);
451+
return ptr;
423452
#else
424453
int flags = MAP_PRIVATE | MAP_ANON;
425454
#if defined(MAP_EXCL)
@@ -434,11 +463,7 @@ static void *zend_mm_mmap_fixed(void *addr, size_t size)
434463
#endif
435464
return NULL;
436465
} 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-
}
466+
zend_mm_munmap(ptr, size);
442467
return NULL;
443468
}
444469
return ptr;
@@ -450,10 +475,9 @@ static void *zend_mm_mmap(size_t size)
450475
{
451476
#ifdef _WIN32
452477
void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
453-
454478
if (ptr == NULL) {
455479
#if ZEND_MM_ERROR
456-
stderr_last_error("VirtualAlloc() failed");
480+
stderr_last_error("VirtualAlloc(NULL) failed");
457481
#endif
458482
return NULL;
459483
}
@@ -482,23 +506,6 @@ static void *zend_mm_mmap(size_t size)
482506
#endif
483507
}
484508

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-
502509
/***********/
503510
/* Bitmask */
504511
/***********/
@@ -1846,11 +1853,7 @@ static zend_mm_heap *zend_mm_init(void)
18461853

18471854
if (UNEXPECTED(chunk == NULL)) {
18481855
#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
1856+
fprintf(stderr, "\nCan't initialize heap\n");
18541857
#endif
18551858
return NULL;
18561859
}
@@ -2978,11 +2981,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
29782981
chunk = (zend_mm_chunk*)handlers->chunk_alloc(&tmp_storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
29792982
if (UNEXPECTED(chunk == NULL)) {
29802983
#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
2984+
fprintf(stderr, "\nCan't initialize heap\n");
29862985
#endif
29872986
return NULL;
29882987
}
@@ -3025,11 +3024,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
30253024
if (!storage) {
30263025
handlers->chunk_free(&tmp_storage, chunk, ZEND_MM_CHUNK_SIZE);
30273026
#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
3027+
fprintf(stderr, "\nCan't initialize heap\n");
30333028
#endif
30343029
return NULL;
30353030
}

0 commit comments

Comments
 (0)