diff options
| author | Dave Love | 2000-09-08 13:46:27 +0000 |
|---|---|---|
| committer | Dave Love | 2000-09-08 13:46:27 +0000 |
| commit | a2c23c923a6c98500895fc938c2c10157565a438 (patch) | |
| tree | 1d8b64801246f5b78ed096b505fc9f2843aa458d /src/ralloc.c | |
| parent | 3015eec0e8c1e7b24c8db35c764afd6d6971dd15 (diff) | |
| download | emacs-a2c23c923a6c98500895fc938c2c10157565a438.tar.gz emacs-a2c23c923a6c98500895fc938c2c10157565a438.zip | |
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.
Diffstat (limited to 'src/ralloc.c')
| -rw-r--r-- | src/ralloc.c | 62 |
1 files changed, 57 insertions, 5 deletions
diff --git a/src/ralloc.c b/src/ralloc.c index 0700bf9c664..f6ae887f4c1 100644 --- a/src/ralloc.c +++ b/src/ralloc.c | |||
| @@ -45,7 +45,9 @@ extern void safe_bcopy (); | |||
| 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 */ |
| @@ -57,7 +59,6 @@ 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) |
| @@ -1227,11 +1228,21 @@ r_alloc_check () | |||
| 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 | ||
| @@ -1272,6 +1283,11 @@ static struct mmap_region *mmap_regions; | |||
| 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)) |
| @@ -1303,6 +1319,15 @@ 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 | ||
| 1305 | 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 | } | ||
| 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. */ |
| @@ -1397,7 +1422,7 @@ mmap_enlarge (r, npages) | |||
| 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)); |
| @@ -1437,12 +1462,14 @@ 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 | { |
| @@ -1450,6 +1477,7 @@ mmap_set_vars (restore_p) | |||
| 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 | ||
| @@ -1488,9 +1516,14 @@ r_alloc (var, nbytes) | |||
| 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 | { |
| @@ -1532,6 +1565,10 @@ r_re_alloc (var, nbytes) | |||
| 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); |
| @@ -1601,6 +1638,10 @@ r_alloc_free (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 | { |
| @@ -1620,7 +1661,9 @@ r_alloc_free (var) | |||
| 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 | ||
| @@ -1631,9 +1674,13 @@ r_alloc_init () | |||
| 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; |
| 1681 | #endif | ||
| 1636 | 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 |
| @@ -1641,17 +1688,20 @@ r_alloc_init () | |||
| 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 |
| 1702 | #endif | ||
| 1654 | 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 |
| @@ -1669,5 +1719,7 @@ r_alloc_init () | |||
| 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; |
| 1724 | r_alloc_init_fd (); | ||
| 1673 | } | 1725 | } |