aboutsummaryrefslogtreecommitdiffstats
path: root/src/gmalloc.c
diff options
context:
space:
mode:
authorKen Brown2011-08-16 09:27:12 -0400
committerKen Brown2011-08-16 09:27:12 -0400
commita4579d332d432ea629a1ad2168ce8023d172d563 (patch)
tree518252a17be98424e3744bc35dcc96fedf41c84a /src/gmalloc.c
parent9adfcd0bf50066cc678ba65fb1b236d1c4ca0334 (diff)
downloademacs-a4579d332d432ea629a1ad2168ce8023d172d563.tar.gz
emacs-a4579d332d432ea629a1ad2168ce8023d172d563.zip
Fix memory allocation problems in Cygwin build (Bug#9273).
* src/gmalloc.c [CYGWIN] (bss_sbrk_heapbase, bss_sbrk_heapinfo): New variables. (malloc_initialize_1) [CYGWIN]: Prepare for reinitializing the dumped emacs. (_free_internal_nolock) [CYGWIN]: Ignore requests to free storage in the static heap. [CYGWIN] (special_realloc): New function. (_realloc_internal_nolock) [CYGWIN]: Use the new function on requests to realloc storage in the static heap. * src/unexcw.c ( __malloc_initialized): Declare external variable. (fixup_executable): Force the dumped emacs to reinitialize malloc.
Diffstat (limited to 'src/gmalloc.c')
-rw-r--r--src/gmalloc.c58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/gmalloc.c b/src/gmalloc.c
index 916bb300fe1..61046ad9d1b 100644
--- a/src/gmalloc.c
+++ b/src/gmalloc.c
@@ -351,10 +351,17 @@ Fifth Floor, Boston, MA 02110-1301, USA.
351#endif 351#endif
352#include <errno.h> 352#include <errno.h>
353 353
354/* How to really get more memory. */ 354/* On Cygwin there are two heaps. temacs uses the static heap
355#if defined(CYGWIN) 355 (defined in sheap.c and managed with bss_sbrk), and the dumped
356 emacs uses the Cygwin heap (managed with sbrk). When emacs starts
357 on Cygwin, it reinitializes malloc, and we save the old info for
358 use by free and realloc if they're called with a pointer into the
359 static heap. */
360#ifdef CYGWIN
356extern __ptr_t bss_sbrk PP ((ptrdiff_t __size)); 361extern __ptr_t bss_sbrk PP ((ptrdiff_t __size));
357extern int bss_sbrk_did_unexec; 362extern int bss_sbrk_did_unexec;
363char *bss_sbrk_heapbase; /* _heapbase for static heap */
364malloc_info *bss_sbrk_heapinfo; /* _heapinfo for static heap */
358#endif 365#endif
359__ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)) = __default_morecore; 366__ptr_t (*__morecore) PP ((__malloc_ptrdiff_t __size)) = __default_morecore;
360 367
@@ -584,6 +591,16 @@ malloc_initialize_1 ()
584 mcheck (NULL); 591 mcheck (NULL);
585#endif 592#endif
586 593
594#ifdef CYGWIN
595 if (bss_sbrk_did_unexec)
596 /* we're reinitializing the dumped emacs */
597 {
598 bss_sbrk_heapbase = _heapbase;
599 bss_sbrk_heapinfo = _heapinfo;
600 memset (_fraghead, 0, BLOCKLOG * sizeof (struct list));
601 }
602#endif
603
587 if (__malloc_initialize_hook) 604 if (__malloc_initialize_hook)
588 (*__malloc_initialize_hook) (); 605 (*__malloc_initialize_hook) ();
589 606
@@ -1054,6 +1071,12 @@ _free_internal_nolock (ptr)
1054 if (ptr == NULL) 1071 if (ptr == NULL)
1055 return; 1072 return;
1056 1073
1074#ifdef CYGWIN
1075 if (ptr < _heapbase)
1076 /* We're being asked to free something in the static heap. */
1077 return;
1078#endif
1079
1057 PROTECT_MALLOC_STATE (0); 1080 PROTECT_MALLOC_STATE (0);
1058 1081
1059 LOCK_ALIGNED_BLOCKS (); 1082 LOCK_ALIGNED_BLOCKS ();
@@ -1349,6 +1372,31 @@ Fifth Floor, Boston, MA 02110-1301, USA.
1349 1372
1350#define min(A, B) ((A) < (B) ? (A) : (B)) 1373#define min(A, B) ((A) < (B) ? (A) : (B))
1351 1374
1375/* On Cygwin the dumped emacs may try to realloc storage allocated in
1376 the static heap. We just malloc space in the new heap and copy the
1377 data. */
1378#ifdef CYGWIN
1379__ptr_t
1380special_realloc (ptr, size)
1381 __ptr_t ptr;
1382 __malloc_size_t size;
1383{
1384 __ptr_t result;
1385 int type;
1386 __malloc_size_t block, oldsize;
1387
1388 block = ((char *) ptr - bss_sbrk_heapbase) / BLOCKSIZE + 1;
1389 type = bss_sbrk_heapinfo[block].busy.type;
1390 oldsize =
1391 type == 0 ? bss_sbrk_heapinfo[block].busy.info.size * BLOCKSIZE
1392 : (__malloc_size_t) 1 << type;
1393 result = _malloc_internal_nolock (size);
1394 if (result != NULL)
1395 memcpy (result, ptr, min (oldsize, size));
1396 return result;
1397}
1398#endif
1399
1352/* Debugging hook for realloc. */ 1400/* Debugging hook for realloc. */
1353__ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size)); 1401__ptr_t (*__realloc_hook) PP ((__ptr_t __ptr, __malloc_size_t __size));
1354 1402
@@ -1375,6 +1423,12 @@ _realloc_internal_nolock (ptr, size)
1375 else if (ptr == NULL) 1423 else if (ptr == NULL)
1376 return _malloc_internal_nolock (size); 1424 return _malloc_internal_nolock (size);
1377 1425
1426#ifdef CYGWIN
1427 if (ptr < _heapbase)
1428 /* ptr points into the static heap */
1429 return special_realloc (ptr, size);
1430#endif
1431
1378 block = BLOCK (ptr); 1432 block = BLOCK (ptr);
1379 1433
1380 PROTECT_MALLOC_STATE (0); 1434 PROTECT_MALLOC_STATE (0);