Mercurial > emacs
comparison src/ralloc.c @ 31515:92dec3ff92a1
(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.
| author | Gerd Moellmann <gerd@gnu.org> |
|---|---|
| date | Fri, 08 Sep 2000 19:52:38 +0000 |
| parents | 6e606fa28569 |
| children | 5b50ac5d207d |
comparison
equal
deleted
inserted
replaced
| 31514:2c900086fb10 | 31515:92dec3ff92a1 |
|---|---|
| 1226 | 1226 |
| 1227 #ifdef REL_ALLOC_MMAP | 1227 #ifdef REL_ALLOC_MMAP |
| 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 |
| 1234 #else | 1235 #else |
| 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 |
| 1246 | |
| 1243 | 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 |
| 1247 trying to enlarge regions. | 1251 trying to enlarge regions. |
| 1277 | 1281 |
| 1278 /* Doubly-linked list of mmap'd regions. */ | 1282 /* Doubly-linked list of mmap'd regions. */ |
| 1279 | 1283 |
| 1280 static struct mmap_region *mmap_regions; | 1284 static struct mmap_region *mmap_regions; |
| 1281 | 1285 |
| 1282 /* Temporary storage for mmap_set_vars, see there. */ | |
| 1283 | |
| 1284 static 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 |
| 1289 static int mmap_fd = -1; | 1289 static int mmap_fd; |
| 1290 | |
| 1291 /* Temporary storage for mmap_set_vars, see there. */ | |
| 1292 | |
| 1293 static struct mmap_region *mmap_regions_1; | |
| 1294 static 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 |
| 1293 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N)) | 1298 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N)) |
| 1294 | 1299 |
| 1315 static int mmap_enlarge P_ ((struct mmap_region *, int)); | 1320 static int mmap_enlarge P_ ((struct mmap_region *, int)); |
| 1316 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *)); | 1321 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *)); |
| 1317 POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t)); | 1322 POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t)); |
| 1318 POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t)); | 1323 POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t)); |
| 1319 void r_alloc_free P_ ((POINTER_TYPE **ptr)); | 1324 void r_alloc_free P_ ((POINTER_TYPE **ptr)); |
| 1320 void 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 |
| 1324 is at END - 1. */ | 1329 is at END - 1. */ |
| 1325 | 1330 |
| 1451 void | 1456 void |
| 1452 mmap_set_vars (restore_p) | 1457 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 { |
| 1467 for (r = mmap_regions; r; r = r->next) | 1471 for (r = mmap_regions; r; r = r->next) |
| 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 } |
| 1474 | 1479 |
| 1475 | 1480 |
| 1503 size_t nbytes; | 1508 size_t nbytes; |
| 1504 { | 1509 { |
| 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, |
| 1517 mmap_fd, 0); | 1517 mmap_fd, 0); |
| 1518 | 1518 |
| 1552 POINTER_TYPE **var; | 1552 POINTER_TYPE **var; |
| 1553 size_t nbytes; | 1553 size_t 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); |
| 1566 else if (nbytes == 0) | 1561 else if (nbytes == 0) |
| 1567 { | 1562 { |
| 1625 | 1620 |
| 1626 void | 1621 void |
| 1627 r_alloc_free (var) | 1622 r_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)); |
| 1640 *var = NULL; | 1630 *var = NULL; |
| 1641 } | 1631 } |
| 1654 | 1644 |
| 1655 #ifndef SYSTEM_MALLOC | 1645 #ifndef SYSTEM_MALLOC |
| 1656 extern POINTER (*__morecore) (); | 1646 extern POINTER (*__morecore) (); |
| 1657 #endif | 1647 #endif |
| 1658 | 1648 |
| 1659 /* Set up the file descriptor for non-anonymous mmapping. */ | |
| 1660 | |
| 1661 void | |
| 1662 r_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 |
| 1676 static void | 1652 static void |
| 1677 r_alloc_init () | 1653 r_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; |
| 1686 __morecore = r_alloc_sbrk; | 1677 __morecore = r_alloc_sbrk; |
| 1687 | 1678 |
| 1722 rest of that page to the address space. */ | 1713 rest of that page to the address space. */ |
| 1723 bzero (first_heap->start, | 1714 bzero (first_heap->start, |
| 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 (); | 1720 } |
| 1729 } |
