Mercurial > emacs
comparison src/ralloc.c @ 31498:17d4a8f3df67
Don't include string.h (redundant).
(MAP_ANON) [REL_ALLOC_MMAP]: Ensure it's defined.
[!MAP_ANON]: Include fcntl.h.
(mmap_fd) [REL_ALLOC_MMAP]: New variable.
(r_alloc, r_re_alloc, r_alloc_free)
(mmap_enlarge, mmap_set_vars): Use it.
(r_alloc_init_fd): New function.
(__morecore) [SYSTEM_MALLOC]: Don't declare.
(r_alloc_init): Call r_alloc_init_fd. Conditionalize stuff on
malloc type.
| author | Dave Love <fx@gnu.org> |
|---|---|
| date | Fri, 08 Sep 2000 13:46:27 +0000 |
| parents | 055accd6bf1d |
| children | 33510affffb3 |
comparison
equal
deleted
inserted
replaced
| 31497:03cff4e91e67 | 31498:17d4a8f3df67 |
|---|---|
| 43 | 43 |
| 44 #ifdef DOUG_LEA_MALLOC | 44 #ifdef DOUG_LEA_MALLOC |
| 45 #define M_TOP_PAD -2 | 45 #define M_TOP_PAD -2 |
| 46 extern int mallopt (); | 46 extern int mallopt (); |
| 47 #else /* not DOUG_LEA_MALLOC */ | 47 #else /* not DOUG_LEA_MALLOC */ |
| 48 #ifndef SYSTEM_MALLOC | |
| 48 extern int __malloc_extra_blocks; | 49 extern int __malloc_extra_blocks; |
| 50 #endif /* SYSTEM_MALLOC */ | |
| 49 #endif /* not DOUG_LEA_MALLOC */ | 51 #endif /* not DOUG_LEA_MALLOC */ |
| 50 | 52 |
| 51 #else /* not emacs */ | 53 #else /* not emacs */ |
| 52 | 54 |
| 53 #include <stddef.h> | 55 #include <stddef.h> |
| 55 typedef size_t SIZE; | 57 typedef size_t SIZE; |
| 56 typedef void *POINTER; | 58 typedef void *POINTER; |
| 57 | 59 |
| 58 #include <unistd.h> | 60 #include <unistd.h> |
| 59 #include <malloc.h> | 61 #include <malloc.h> |
| 60 #include <string.h> | |
| 61 | 62 |
| 62 #define safe_bcopy(x, y, z) memmove (y, x, z) | 63 #define safe_bcopy(x, y, z) memmove (y, x, z) |
| 63 #define bzero(x, len) memset (x, 0, len) | 64 #define bzero(x, len) memset (x, 0, len) |
| 64 | 65 |
| 65 #endif /* not emacs */ | 66 #endif /* not emacs */ |
| 1225 | 1226 |
| 1226 #ifdef REL_ALLOC_MMAP | 1227 #ifdef REL_ALLOC_MMAP |
| 1227 | 1228 |
| 1228 #include <sys/types.h> | 1229 #include <sys/types.h> |
| 1229 #include <sys/mman.h> | 1230 #include <sys/mman.h> |
| 1231 #ifndef MAP_ANON | |
| 1232 #ifdef MAP_ANONYMOUS | |
| 1233 #define MAP_ANON MAP_ANONYMOUS | |
| 1234 #else | |
| 1235 #define MAP_ANON 0 | |
| 1236 #endif | |
| 1237 #endif | |
| 1230 #include <stdio.h> | 1238 #include <stdio.h> |
| 1231 #include <errno.h> | 1239 #include <errno.h> |
| 1240 #if !MAP_ANON | |
| 1241 #include <fcntl.h> | |
| 1242 #endif | |
| 1232 | 1243 |
| 1233 /* Memory is allocated in regions which are mapped using mmap(2). | 1244 /* Memory is allocated in regions which are mapped using mmap(2). |
| 1234 The current implementation let's the system select mapped | 1245 The current implementation lets the system select mapped |
| 1235 addresses; we're not using MAP_FIXED in general, except when | 1246 addresses; we're not using MAP_FIXED in general, except when |
| 1236 trying to enlarge regions. | 1247 trying to enlarge regions. |
| 1237 | 1248 |
| 1238 Each mapped region starts with a mmap_region structure, the user | 1249 Each mapped region starts with a mmap_region structure, the user |
| 1239 area starts after that structure, aligned to MEM_ALIGN. | 1250 area starts after that structure, aligned to MEM_ALIGN. |
| 1270 | 1281 |
| 1271 /* Temporary storage for mmap_set_vars, see there. */ | 1282 /* Temporary storage for mmap_set_vars, see there. */ |
| 1272 | 1283 |
| 1273 static struct mmap_region *mmap_regions_1; | 1284 static struct mmap_region *mmap_regions_1; |
| 1274 | 1285 |
| 1286 /* File descriptor for mmap. If we don't have anonymous mapping, | |
| 1287 /dev/zero will be opened on it. */ | |
| 1288 | |
| 1289 static int mmap_fd = -1; | |
| 1290 | |
| 1275 /* Value is X rounded up to the next multiple of N. */ | 1291 /* Value is X rounded up to the next multiple of N. */ |
| 1276 | 1292 |
| 1277 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N)) | 1293 #define ROUND(X, N) (((X) + (N) - 1) / (N) * (N)) |
| 1278 | 1294 |
| 1279 /* Size of mmap_region structure plus padding. */ | 1295 /* Size of mmap_region structure plus padding. */ |
| 1300 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *)); | 1316 static struct mmap_region *mmap_find P_ ((POINTER_TYPE *, POINTER_TYPE *)); |
| 1301 POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t)); | 1317 POINTER_TYPE *r_alloc P_ ((POINTER_TYPE **, size_t)); |
| 1302 POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t)); | 1318 POINTER_TYPE *r_re_alloc P_ ((POINTER_TYPE **, size_t)); |
| 1303 void r_alloc_free P_ ((POINTER_TYPE **ptr)); | 1319 void r_alloc_free P_ ((POINTER_TYPE **ptr)); |
| 1304 | 1320 |
| 1321 | |
| 1322 void | |
| 1323 r_alloc_init_fd () | |
| 1324 { | |
| 1325 /* No anonymous mmap -- we need the file descriptor. */ | |
| 1326 mmap_fd = open ("/dev/zero", O_RDONLY); | |
| 1327 if (mmap_fd < 0) | |
| 1328 fatal ("cannot open /dev/zero"); | |
| 1329 } | |
| 1305 | 1330 |
| 1306 /* Return a region overlapping address range START...END, or null if | 1331 /* Return a region overlapping address range START...END, or null if |
| 1307 none. END is not including, i.e. the last byte in the range | 1332 none. END is not including, i.e. the last byte in the range |
| 1308 is at END - 1. */ | 1333 is at END - 1. */ |
| 1309 | 1334 |
| 1395 else | 1420 else |
| 1396 { | 1421 { |
| 1397 POINTER_TYPE *p; | 1422 POINTER_TYPE *p; |
| 1398 | 1423 |
| 1399 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE, | 1424 p = mmap (region_end, nbytes, PROT_READ | PROT_WRITE, |
| 1400 MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); | 1425 MAP_ANON | MAP_PRIVATE | MAP_FIXED, mmap_fd, 0); |
| 1401 if (p == MAP_FAILED) | 1426 if (p == MAP_FAILED) |
| 1402 { | 1427 { |
| 1403 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); | 1428 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); |
| 1404 success = 0; | 1429 success = 0; |
| 1405 } | 1430 } |
| 1435 void | 1460 void |
| 1436 mmap_set_vars (restore_p) | 1461 mmap_set_vars (restore_p) |
| 1437 int restore_p; | 1462 int restore_p; |
| 1438 { | 1463 { |
| 1439 struct mmap_region *r; | 1464 struct mmap_region *r; |
| 1465 static int fd; | |
| 1440 | 1466 |
| 1441 if (restore_p) | 1467 if (restore_p) |
| 1442 { | 1468 { |
| 1443 mmap_regions = mmap_regions_1; | 1469 mmap_regions = mmap_regions_1; |
| 1444 for (r = mmap_regions; r; r = r->next) | 1470 for (r = mmap_regions; r; r = r->next) |
| 1445 *r->var = MMAP_USER_AREA (r); | 1471 *r->var = MMAP_USER_AREA (r); |
| 1472 mmap_fd = fd; | |
| 1446 } | 1473 } |
| 1447 else | 1474 else |
| 1448 { | 1475 { |
| 1449 for (r = mmap_regions; r; r = r->next) | 1476 for (r = mmap_regions; r; r = r->next) |
| 1450 *r->var = NULL; | 1477 *r->var = NULL; |
| 1451 mmap_regions_1 = mmap_regions; | 1478 mmap_regions_1 = mmap_regions; |
| 1452 mmap_regions = NULL; | 1479 mmap_regions = NULL; |
| 1480 mmap_fd = -1; | |
| 1453 } | 1481 } |
| 1454 } | 1482 } |
| 1455 | 1483 |
| 1456 | 1484 |
| 1457 /* Return total number of bytes mapped. */ | 1485 /* Return total number of bytes mapped. */ |
| 1486 void *p; | 1514 void *p; |
| 1487 size_t map; | 1515 size_t map; |
| 1488 | 1516 |
| 1489 if (!r_alloc_initialized) | 1517 if (!r_alloc_initialized) |
| 1490 r_alloc_init (); | 1518 r_alloc_init (); |
| 1519 #if defined (REL_ALLOC_MMAP) && !MAP_ANON | |
| 1520 if (mmap_fd == -1) | |
| 1521 r_alloc_init_fd (); | |
| 1522 #endif | |
| 1491 | 1523 |
| 1492 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, page_size); | 1524 map = ROUND (nbytes + MMAP_REGION_STRUCT_SIZE, page_size); |
| 1493 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); | 1525 p = mmap (NULL, map, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, |
| 1526 mmap_fd, 0); | |
| 1494 | 1527 |
| 1495 if (p == MAP_FAILED) | 1528 if (p == MAP_FAILED) |
| 1496 { | 1529 { |
| 1497 if (errno != ENOMEM) | 1530 if (errno != ENOMEM) |
| 1498 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); | 1531 fprintf (stderr, "mmap: %s\n", emacs_strerror (errno)); |
| 1530 { | 1563 { |
| 1531 POINTER_TYPE *result; | 1564 POINTER_TYPE *result; |
| 1532 | 1565 |
| 1533 if (!r_alloc_initialized) | 1566 if (!r_alloc_initialized) |
| 1534 r_alloc_init (); | 1567 r_alloc_init (); |
| 1568 #if defined (REL_ALLOC_MMAP) && !MAP_ANON | |
| 1569 if (mmap_fd == -1) | |
| 1570 r_alloc_init_fd (); | |
| 1571 #endif | |
| 1535 | 1572 |
| 1536 if (*var == NULL) | 1573 if (*var == NULL) |
| 1537 result = r_alloc (var, nbytes); | 1574 result = r_alloc (var, nbytes); |
| 1538 else if (nbytes == 0) | 1575 else if (nbytes == 0) |
| 1539 { | 1576 { |
| 1599 r_alloc_free (var) | 1636 r_alloc_free (var) |
| 1600 POINTER_TYPE **var; | 1637 POINTER_TYPE **var; |
| 1601 { | 1638 { |
| 1602 if (!r_alloc_initialized) | 1639 if (!r_alloc_initialized) |
| 1603 r_alloc_init (); | 1640 r_alloc_init (); |
| 1641 #if defined (REL_ALLOC_MMAP) && !MAP_ANON | |
| 1642 if (mmap_fd == -1) | |
| 1643 r_alloc_init_fd (); | |
| 1644 #endif | |
| 1604 | 1645 |
| 1605 if (*var) | 1646 if (*var) |
| 1606 { | 1647 { |
| 1607 mmap_free (MMAP_REGION (*var)); | 1648 mmap_free (MMAP_REGION (*var)); |
| 1608 *var = NULL; | 1649 *var = NULL; |
| 1618 ***********************************************************************/ | 1659 ***********************************************************************/ |
| 1619 | 1660 |
| 1620 /* The hook `malloc' uses for the function which gets more space | 1661 /* The hook `malloc' uses for the function which gets more space |
| 1621 from the system. */ | 1662 from the system. */ |
| 1622 | 1663 |
| 1664 #ifndef SYSTEM_MALLOC | |
| 1623 extern POINTER (*__morecore) (); | 1665 extern POINTER (*__morecore) (); |
| 1666 #endif | |
| 1624 | 1667 |
| 1625 /* Initialize various things for memory allocation. */ | 1668 /* Initialize various things for memory allocation. */ |
| 1626 | 1669 |
| 1627 static void | 1670 static void |
| 1628 r_alloc_init () | 1671 r_alloc_init () |
| 1629 { | 1672 { |
| 1630 if (r_alloc_initialized) | 1673 if (r_alloc_initialized) |
| 1631 return; | 1674 return; |
| 1632 | 1675 |
| 1633 r_alloc_initialized = 1; | 1676 r_alloc_initialized = 1; |
| 1677 page_size = PAGE; | |
| 1678 #ifndef SYSTEM_MALLOC | |
| 1634 real_morecore = __morecore; | 1679 real_morecore = __morecore; |
| 1635 __morecore = r_alloc_sbrk; | 1680 __morecore = r_alloc_sbrk; |
| 1636 | 1681 #endif |
| 1682 | |
| 1683 #ifndef REL_ALLOC_MMAP | |
| 1637 first_heap = last_heap = &heap_base; | 1684 first_heap = last_heap = &heap_base; |
| 1638 first_heap->next = first_heap->prev = NIL_HEAP; | 1685 first_heap->next = first_heap->prev = NIL_HEAP; |
| 1639 first_heap->start = first_heap->bloc_start | 1686 first_heap->start = first_heap->bloc_start |
| 1640 = virtual_break_value = break_value = (*real_morecore) (0); | 1687 = virtual_break_value = break_value = (*real_morecore) (0); |
| 1641 if (break_value == NIL) | 1688 if (break_value == NIL) |
| 1642 abort (); | 1689 abort (); |
| 1643 | 1690 |
| 1644 page_size = PAGE; | |
| 1645 extra_bytes = ROUNDUP (50000); | 1691 extra_bytes = ROUNDUP (50000); |
| 1692 #endif | |
| 1646 | 1693 |
| 1647 #ifdef DOUG_LEA_MALLOC | 1694 #ifdef DOUG_LEA_MALLOC |
| 1648 mallopt (M_TOP_PAD, 64 * 4096); | 1695 mallopt (M_TOP_PAD, 64 * 4096); |
| 1649 #else | 1696 #else |
| 1697 #ifndef SYSTEM_MALLOC | |
| 1650 /* Give GNU malloc's morecore some hysteresis | 1698 /* Give GNU malloc's morecore some hysteresis |
| 1651 so that we move all the relocatable blocks much less often. */ | 1699 so that we move all the relocatable blocks much less often. */ |
| 1652 __malloc_extra_blocks = 64; | 1700 __malloc_extra_blocks = 64; |
| 1653 #endif | 1701 #endif |
| 1654 | 1702 #endif |
| 1703 | |
| 1704 #ifndef REL_ALLOC_MMAP | |
| 1655 first_heap->end = (POINTER) ROUNDUP (first_heap->start); | 1705 first_heap->end = (POINTER) ROUNDUP (first_heap->start); |
| 1656 | 1706 |
| 1657 /* The extra call to real_morecore guarantees that the end of the | 1707 /* The extra call to real_morecore guarantees that the end of the |
| 1658 address space is a multiple of page_size, even if page_size is | 1708 address space is a multiple of page_size, even if page_size is |
| 1659 not really the page size of the system running the binary in | 1709 not really the page size of the system running the binary in |
| 1667 /* Doubly true, with the additional call that explicitly adds the | 1717 /* Doubly true, with the additional call that explicitly adds the |
| 1668 rest of that page to the address space. */ | 1718 rest of that page to the address space. */ |
| 1669 bzero (first_heap->start, | 1719 bzero (first_heap->start, |
| 1670 (char *) first_heap->end - (char *) first_heap->start); | 1720 (char *) first_heap->end - (char *) first_heap->start); |
| 1671 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end; | 1721 virtual_break_value = break_value = first_heap->bloc_start = first_heap->end; |
| 1722 #endif | |
| 1672 use_relocatable_buffers = 1; | 1723 use_relocatable_buffers = 1; |
| 1673 } | 1724 r_alloc_init_fd (); |
| 1725 } |
