aboutsummaryrefslogtreecommitdiffstats
path: root/src/ralloc.c
diff options
context:
space:
mode:
authorGerd Moellmann2000-09-08 19:52:57 +0000
committerGerd Moellmann2000-09-08 19:52:57 +0000
commitd1a02881509c0cb2210ab7f219e50f43e9067c54 (patch)
tree1c6d188d7fe00b52ee9a48ee5b53457a7cf320cd /src/ralloc.c
parent119d3665182175d1900c7da8bd0311b99e0df11c (diff)
downloademacs-d1a02881509c0cb2210ab7f219e50f43e9067c54.tar.gz
emacs-d1a02881509c0cb2210ab7f219e50f43e9067c54.zip
(mmap_fd): Remove initializer which can make it
read-only in a dumped Emacs. (mmap_fd_1): New variable. (mmap_set_vars): Remove local `fd'. Save mmap_fd in mmap_fd_1, restore it from there. (r_alloc, r_re_alloc, r_alloc_free): Call r_alloc_init unconditionally so that mmap_fd can be initialized there. (r_alloc_init_fd): Open-coded in r_alloc_init; function removed. (r_alloc_init) [REL_ALLOC_MMAP && !MAP_ANON]: Open /dev/zero. (r_alloc_init) [REL_ALLOC_MMAP && MAP_ANON]: Set mmap_fd to -1.
Diffstat (limited to 'src/ralloc.c')
-rw-r--r--src/ralloc.c79
1 files changed, 35 insertions, 44 deletions
diff --git a/src/ralloc.c b/src/ralloc.c
index 23ff1dcb930..a90b6514799 100644
--- a/src/ralloc.c
+++ b/src/ralloc.c
@@ -1228,6 +1228,7 @@ r_alloc_check ()
1228 1228
1229#include <sys/types.h> 1229#include <sys/types.h>
1230#include <sys/mman.h> 1230#include <sys/mman.h>
1231
1231#ifndef MAP_ANON 1232#ifndef MAP_ANON
1232#ifdef MAP_ANONYMOUS 1233#ifdef MAP_ANONYMOUS
1233#define MAP_ANON MAP_ANONYMOUS 1234#define MAP_ANON MAP_ANONYMOUS
@@ -1235,12 +1236,15 @@ r_alloc_check ()
1235#define MAP_ANON 0 1236#define MAP_ANON 0
1236#endif 1237#endif
1237#endif 1238#endif
1239
1238#include <stdio.h> 1240#include <stdio.h>
1239#include <errno.h> 1241#include <errno.h>
1240#if !MAP_ANON 1242
1243#if MAP_ANON == 0
1241#include <fcntl.h> 1244#include <fcntl.h>
1242#endif 1245#endif
1243 1246
1247
1244/* Memory is allocated in regions which are mapped using mmap(2). 1248/* Memory is allocated in regions which are mapped using mmap(2).
1245 The current implementation lets the system select mapped 1249 The current implementation lets the system select mapped
1246 addresses; we're not using MAP_FIXED in general, except when 1250 addresses; we're not using MAP_FIXED in general, except when
@@ -1279,14 +1283,15 @@ struct mmap_region
1279 1283
1280static struct mmap_region *mmap_regions; 1284static struct mmap_region *mmap_regions;
1281 1285
1282/* Temporary storage for mmap_set_vars, see there. */
1283
1284static struct mmap_region *mmap_regions_1;
1285
1286/* File descriptor for mmap. If we don't have anonymous mapping, 1286/* File descriptor for mmap. If we don't have anonymous mapping,
1287 /dev/zero will be opened on it. */ 1287 /dev/zero will be opened on it. */
1288 1288
1289static int mmap_fd = -1; 1289static int mmap_fd;
1290
1291/* Temporary storage for mmap_set_vars, see there. */
1292
1293static struct mmap_region *mmap_regions_1;
1294static int mmap_fd_1;
1290 1295
1291/* Value is X rounded up to the next multiple of N. */ 1296/* Value is X rounded up to the next multiple of N. */
1292 1297
@@ -1317,7 +1322,7 @@ static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *));
1317POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t)); 1322POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t));
1318POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t)); 1323POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t));
1319void r_alloc_free P_ ((POINTER_TYPE **ptr)); 1324void r_alloc_free P_ ((POINTER_TYPE **ptr));
1320void r_alloc_init_fd (); 1325
1321 1326
1322/* Return a region overlapping address range START...END, or null if 1327/* Return a region overlapping address range START...END, or null if
1323 none. END is not including, i.e. the last byte in the range 1328 none. END is not including, i.e. the last byte in the range
@@ -1453,14 +1458,13 @@ mmap_set_vars (restore_p)
1453 int restore_p; 1458 int restore_p;
1454{ 1459{
1455 struct mmap_region *r; 1460 struct mmap_region *r;
1456 static int fd;
1457 1461
1458 if (restore_p) 1462 if (restore_p)
1459 { 1463 {
1460 mmap_regions = mmap_regions_1; 1464 mmap_regions = mmap_regions_1;
1465 mmap_fd = mmap_fd_1;
1461 for (r = mmap_regions; r; r = r->next) 1466 for (r = mmap_regions; r; r = r->next)
1462 *r->var = MMAP_USER_AREA (r); 1467 *r->var = MMAP_USER_AREA (r);
1463 mmap_fd = fd;
1464 } 1468 }
1465 else 1469 else
1466 { 1470 {
@@ -1468,6 +1472,7 @@ mmap_set_vars (restore_p)
1468 *r->var = NULL; 1472 *r->var = NULL;
1469 mmap_regions_1 = mmap_regions; 1473 mmap_regions_1 = mmap_regions;
1470 mmap_regions = NULL; 1474 mmap_regions = NULL;
1475 mmap_fd_1 = mmap_fd;
1471 mmap_fd = -1; 1476 mmap_fd = -1;
1472 } 1477 }
1473} 1478}
@@ -1505,12 +1510,7 @@ r_alloc (var, nbytes)
1505 void *p; 1510 void *p;
1506 size_t map; 1511 size_t map;
1507 1512
1508 if (!r_alloc_initialized) 1513 r_alloc_init ();
1509 r_alloc_init ();
1510#if defined (REL_ALLOC_MMAP) && !MAP_ANON
1511 if (mmap_fd == -1)
1512 r_alloc_init_fd ();
1513#endif
1514 1514
1515 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, page_size); 1515 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, page_size);
1516 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 1516 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
@@ -1554,12 +1554,7 @@ r_re_alloc (var, nbytes)
1554{ 1554{
1555 POINTER_TYPE *result; 1555 POINTER_TYPE *result;
1556 1556
1557 if (!r_alloc_initialized) 1557 r_alloc_init ();
1558 r_alloc_init ();
1559#if defined (REL_ALLOC_MMAP) && !MAP_ANON
1560 if (mmap_fd == -1)
1561 r_alloc_init_fd ();
1562#endif
1563 1558
1564 if (*var == NULL) 1559 if (*var == NULL)
1565 result = r_alloc (var, nbytes); 1560 result = r_alloc (var, nbytes);
@@ -1627,13 +1622,8 @@ void
1627r_alloc_free (var) 1622r_alloc_free (var)
1628 POINTER_TYPE **var; 1623 POINTER_TYPE **var;
1629{ 1624{
1630 if (!r_alloc_initialized) 1625 r_alloc_init ();
1631 r_alloc_init (); 1626
1632#if defined (REL_ALLOC_MMAP) && !MAP_ANON
1633 if (mmap_fd == -1)
1634 r_alloc_init_fd ();
1635#endif
1636
1637 if (*var) 1627 if (*var)
1638 { 1628 {
1639 mmap_free (MMAP_REGION (*var)); 1629 mmap_free (MMAP_REGION (*var));
@@ -1656,30 +1646,31 @@ r_alloc_free (var)
1656extern POINTER (*__morecore) (); 1646extern POINTER (*__morecore) ();
1657#endif 1647#endif
1658 1648
1659/* Set up the file descriptor for non-anonymous mmapping. */
1660
1661void
1662r_alloc_init_fd ()
1663{
1664#ifdef REL_ALLOC_MMAP
1665#if !MAP_ANON
1666 /* No anonymous mmap -- we need the file descriptor. */
1667 mmap_fd = open ("/dev/zero", O_RDONLY);
1668 if (mmap_fd < 0)
1669 fatal ("cannot open /dev/zero");
1670#endif
1671#endif
1672}
1673 1649
1674/* Initialize various things for memory allocation. */ 1650/* Initialize various things for memory allocation. */
1675 1651
1676static void 1652static void
1677r_alloc_init () 1653r_alloc_init ()
1678{ 1654{
1655#if defined REL_ALLOC_MMAP && MAP_ANON == 0
1656 /* The value of mmap_fd is initially 0 in temacs, and -1
1657 in a dumped Emacs. */
1658 if (mmap_fd <= 0)
1659 {
1660 /* No anonymous mmap -- we need the file descriptor. */
1661 mmap_fd = open ("/dev/zero", O_RDONLY);
1662 if (mmap_fd == -1)
1663 fatal ("Cannot open /dev/zero: %s", emacs_strerror (errno));
1664 }
1665#endif /* REL_ALLOC_MMAP && MAP_ANON == 0 */
1666
1679 if (r_alloc_initialized) 1667 if (r_alloc_initialized)
1680 return; 1668 return;
1681
1682 r_alloc_initialized = 1; 1669 r_alloc_initialized = 1;
1670#if defined REL_ALLOC_MMAP && MAP_ANON != 0
1671 mmap_fd = -1;
1672#endif
1673
1683 page_size = PAGE; 1674 page_size = PAGE;
1684#ifndef SYSTEM_MALLOC 1675#ifndef SYSTEM_MALLOC
1685 real_morecore = __morecore; 1676 real_morecore = __morecore;
@@ -1724,6 +1715,6 @@ r_alloc_init ()
1724 (char *) first_heap->end - (char *) first_heap->start); 1715 (char *) first_heap->end - (char *) first_heap->start);
1725 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end; 1716 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end;
1726#endif 1717#endif
1718
1727 use_relocatable_buffers = 1; 1719 use_relocatable_buffers = 1;
1728 r_alloc_init_fd ();
1729} 1720}