diff options
| author | Ken Brown | 2011-08-16 09:27:12 -0400 |
|---|---|---|
| committer | Ken Brown | 2011-08-16 09:27:12 -0400 |
| commit | a4579d332d432ea629a1ad2168ce8023d172d563 (patch) | |
| tree | 518252a17be98424e3744bc35dcc96fedf41c84a /src | |
| parent | 9adfcd0bf50066cc678ba65fb1b236d1c4ca0334 (diff) | |
| download | emacs-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')
| -rw-r--r-- | src/ChangeLog | 17 | ||||
| -rw-r--r-- | src/gmalloc.c | 58 | ||||
| -rw-r--r-- | src/unexcw.c | 5 |
3 files changed, 78 insertions, 2 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index b16606ce121..2364f71c6a4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -1,3 +1,20 @@ | |||
| 1 | 2011-08-16 Ken Brown <kbrown@cornell.edu> | ||
| 2 | |||
| 3 | Fix memory allocation problems in Cygwin build (Bug#9273). | ||
| 4 | |||
| 5 | * unexcw.c ( __malloc_initialized): Declare external variable. | ||
| 6 | (fixup_executable): Force the dumped emacs to reinitialize malloc. | ||
| 7 | |||
| 8 | * gmalloc.c [CYGWIN] (bss_sbrk_heapbase, bss_sbrk_heapinfo): New | ||
| 9 | variables. | ||
| 10 | (malloc_initialize_1) [CYGWIN]: Prepare for reinitializing the | ||
| 11 | dumped emacs. | ||
| 12 | (_free_internal_nolock) [CYGWIN]: Ignore requests to free storage | ||
| 13 | in the static heap. | ||
| 14 | [CYGWIN] (special_realloc): New function. | ||
| 15 | (_realloc_internal_nolock) [CYGWIN]: Use the new function on | ||
| 16 | requests to realloc storage in the static heap. | ||
| 17 | |||
| 1 | 2011-08-15 Paul Eggert <eggert@cs.ucla.edu> | 18 | 2011-08-15 Paul Eggert <eggert@cs.ucla.edu> |
| 2 | 19 | ||
| 3 | * bidi.c (bidi_initialize): Remove unused local. | 20 | * bidi.c (bidi_initialize): Remove unused local. |
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 | ||
| 356 | extern __ptr_t bss_sbrk PP ((ptrdiff_t __size)); | 361 | extern __ptr_t bss_sbrk PP ((ptrdiff_t __size)); |
| 357 | extern int bss_sbrk_did_unexec; | 362 | extern int bss_sbrk_did_unexec; |
| 363 | char *bss_sbrk_heapbase; /* _heapbase for static heap */ | ||
| 364 | malloc_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 | ||
| 1380 | special_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); |
diff --git a/src/unexcw.c b/src/unexcw.c index f643c196de0..62df82ec3bc 100644 --- a/src/unexcw.c +++ b/src/unexcw.c | |||
| @@ -33,6 +33,8 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ | |||
| 33 | 33 | ||
| 34 | extern int bss_sbrk_did_unexec; | 34 | extern int bss_sbrk_did_unexec; |
| 35 | 35 | ||
| 36 | extern int __malloc_initialized; | ||
| 37 | |||
| 36 | /* emacs symbols that indicate where bss and data end for emacs internals */ | 38 | /* emacs symbols that indicate where bss and data end for emacs internals */ |
| 37 | extern char my_endbss[]; | 39 | extern char my_endbss[]; |
| 38 | extern char my_edata[]; | 40 | extern char my_edata[]; |
| @@ -210,9 +212,12 @@ fixup_executable (int fd) | |||
| 210 | lseek (fd, (long) (exe_header->section_header[i].s_scnptr), | 212 | lseek (fd, (long) (exe_header->section_header[i].s_scnptr), |
| 211 | SEEK_SET); | 213 | SEEK_SET); |
| 212 | assert (ret != -1); | 214 | assert (ret != -1); |
| 215 | /* force the dumped emacs to reinitialize malloc */ | ||
| 216 | __malloc_initialized = 0; | ||
| 213 | ret = | 217 | ret = |
| 214 | write (fd, (char *) start_address, | 218 | write (fd, (char *) start_address, |
| 215 | my_endbss - (char *) start_address); | 219 | my_endbss - (char *) start_address); |
| 220 | __malloc_initialized = 1; | ||
| 216 | assert (ret == (my_endbss - (char *) start_address)); | 221 | assert (ret == (my_endbss - (char *) start_address)); |
| 217 | if (debug_unexcw) | 222 | if (debug_unexcw) |
| 218 | printf (" .bss, mem start 0x%08x mem length %d\n", | 223 | printf (" .bss, mem start 0x%08x mem length %d\n", |