aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
authorStefan Monnier2003-07-14 02:51:08 +0000
committerStefan Monnier2003-07-14 02:51:08 +0000
commit19bcad1f59a3c3c02b461bac438b7c07c589580d (patch)
tree7448ed9e4b5089b5ecf38162cd4df14003810648 /src/alloc.c
parentec03b83e7df778bcf2d8de0d5be6d2fd4cbed36c (diff)
downloademacs-19bcad1f59a3c3c02b461bac438b7c07c589580d.tar.gz
emacs-19bcad1f59a3c3c02b461bac438b7c07c589580d.zip
(BLOCK_PADDING): Rename from ABLOCKS_PADDING. Update users.
(lisp_align_malloc): Use posix_memalign is available. (ABLOCKS_BASE): Use HAVE_POSIX_MEMALIGN as an optimization. (STRING_BLOCK_SIZE): Rename from STRINGS_IN_STRING_BLOCK for consistency. Update users.
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c50
1 files changed, 34 insertions, 16 deletions
diff --git a/src/alloc.c b/src/alloc.c
index 359904a775a..cffd0408e24 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -644,16 +644,22 @@ lisp_free (block)
644 644
645/* BLOCK_ALIGN has to be a power of 2. */ 645/* BLOCK_ALIGN has to be a power of 2. */
646#define BLOCK_ALIGN (1 << 10) 646#define BLOCK_ALIGN (1 << 10)
647#define BLOCK_BYTES \
648 (BLOCK_ALIGN - sizeof (struct alinged_block *) - ABLOCKS_PADDING)
649
650/* Internal data structures and constants. */
651 647
652/* Padding to leave at the end of a malloc'd block. This is to give 648/* Padding to leave at the end of a malloc'd block. This is to give
653 malloc a chance to minimize the amount of memory wasted to alignment. 649 malloc a chance to minimize the amount of memory wasted to alignment.
654 It should be tuned to the particular malloc library used. 650 It should be tuned to the particular malloc library used.
655 The current setting is based on glibc-2.3.2. */ 651 On glibc-2.3.2, malloc never tries to align, so a padding of 0 is best.
656#define ABLOCKS_PADDING 0 652 posix_memalign on the other hand would ideally prefer a value of 4
653 because otherwise, there's 1020 bytes wasted between each ablocks.
654 But testing shows that those 1020 will most of the time be efficiently
655 used by malloc to place other objects, so a value of 0 is still preferable
656 unless you have a lot of cons&floats and virtually nothing else. */
657#define BLOCK_PADDING 0
658#define BLOCK_BYTES \
659 (BLOCK_ALIGN - sizeof (struct aligned_block *) - BLOCK_PADDING)
660
661/* Internal data structures and constants. */
662
657#define ABLOCKS_SIZE 16 663#define ABLOCKS_SIZE 16
658 664
659/* An aligned block of memory. */ 665/* An aligned block of memory. */
@@ -676,8 +682,8 @@ struct ablock
676 struct ablocks *abase; 682 struct ablocks *abase;
677 /* The padding of all but the last ablock is unused. The padding of 683 /* The padding of all but the last ablock is unused. The padding of
678 the last ablock in an ablocks is not allocated. */ 684 the last ablock in an ablocks is not allocated. */
679#if ABLOCKS_PADDING 685#if BLOCK_PADDING
680 char padding[ABLOCKS_PADDING]; 686 char padding[BLOCK_PADDING];
681#endif 687#endif
682}; 688};
683 689
@@ -688,7 +694,7 @@ struct ablocks
688}; 694};
689 695
690/* Size of the block requested from malloc or memalign. */ 696/* Size of the block requested from malloc or memalign. */
691#define ABLOCKS_BYTES (sizeof (struct ablocks) - ABLOCKS_PADDING) 697#define ABLOCKS_BYTES (sizeof (struct ablocks) - BLOCK_PADDING)
692 698
693#define ABLOCK_ABASE(block) \ 699#define ABLOCK_ABASE(block) \
694 (((unsigned long) (block)->abase) <= (1 + 2 * ABLOCKS_SIZE) \ 700 (((unsigned long) (block)->abase) <= (1 + 2 * ABLOCKS_SIZE) \
@@ -699,8 +705,12 @@ struct ablocks
699#define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase) 705#define ABLOCKS_BUSY(abase) ((abase)->blocks[0].abase)
700 706
701/* Pointer to the (not necessarily aligned) malloc block. */ 707/* Pointer to the (not necessarily aligned) malloc block. */
708#ifdef HAVE_POSIX_MEMALIGN
709#define ABLOCKS_BASE(abase) (abase)
710#else
702#define ABLOCKS_BASE(abase) \ 711#define ABLOCKS_BASE(abase) \
703 (1 & (int) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1]) 712 (1 & (int) ABLOCKS_BUSY (abase) ? abase : ((void**)abase)[-1])
713#endif
704 714
705/* The list of free ablock. */ 715/* The list of free ablock. */
706static struct ablock *free_ablock; 716static struct ablock *free_ablock;
@@ -735,8 +745,15 @@ lisp_align_malloc (nbytes, type)
735 mallopt (M_MMAP_MAX, 0); 745 mallopt (M_MMAP_MAX, 0);
736#endif 746#endif
737 747
748#ifdef HAVE_POSIX_MEMALIGN
749 {
750 int err = posix_memalign (&base, BLOCK_ALIGN, ABLOCKS_BYTES);
751 abase = err ? (base = NULL) : base;
752 }
753#else
738 base = malloc (ABLOCKS_BYTES); 754 base = malloc (ABLOCKS_BYTES);
739 abase = ALIGN (base, BLOCK_ALIGN); 755 abase = ALIGN (base, BLOCK_ALIGN);
756#endif
740 757
741 aligned = (base == abase); 758 aligned = (base == abase);
742 if (!aligned) 759 if (!aligned)
@@ -757,6 +774,7 @@ lisp_align_malloc (nbytes, type)
757 } 774 }
758 ABLOCKS_BUSY (abase) = (struct ablocks *) aligned; 775 ABLOCKS_BUSY (abase) = (struct ablocks *) aligned;
759 776
777 eassert (0 == ((EMACS_UINT)abase) % BLOCK_ALIGN);
760 eassert (ABLOCK_ABASE (&abase->blocks[3]) == abase); /* 3 is arbitrary */ 778 eassert (ABLOCK_ABASE (&abase->blocks[3]) == abase); /* 3 is arbitrary */
761 eassert (ABLOCK_ABASE (&abase->blocks[0]) == abase); 779 eassert (ABLOCK_ABASE (&abase->blocks[0]) == abase);
762 eassert (ABLOCKS_BASE (abase) == base); 780 eassert (ABLOCKS_BASE (abase) == base);
@@ -1311,7 +1329,7 @@ struct sblock
1311/* Number of Lisp strings in a string_block structure. The 1020 is 1329/* Number of Lisp strings in a string_block structure. The 1020 is
1312 1024 minus malloc overhead. */ 1330 1024 minus malloc overhead. */
1313 1331
1314#define STRINGS_IN_STRING_BLOCK \ 1332#define STRING_BLOCK_SIZE \
1315 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String)) 1333 ((1020 - sizeof (struct string_block *)) / sizeof (struct Lisp_String))
1316 1334
1317/* Structure describing a block from which Lisp_String structures 1335/* Structure describing a block from which Lisp_String structures
@@ -1320,7 +1338,7 @@ struct sblock
1320struct string_block 1338struct string_block
1321{ 1339{
1322 struct string_block *next; 1340 struct string_block *next;
1323 struct Lisp_String strings[STRINGS_IN_STRING_BLOCK]; 1341 struct Lisp_String strings[STRING_BLOCK_SIZE];
1324}; 1342};
1325 1343
1326/* Head and tail of the list of sblock structures holding Lisp string 1344/* Head and tail of the list of sblock structures holding Lisp string
@@ -1515,14 +1533,14 @@ allocate_string ()
1515 string_blocks = b; 1533 string_blocks = b;
1516 ++n_string_blocks; 1534 ++n_string_blocks;
1517 1535
1518 for (i = STRINGS_IN_STRING_BLOCK - 1; i >= 0; --i) 1536 for (i = STRING_BLOCK_SIZE - 1; i >= 0; --i)
1519 { 1537 {
1520 s = b->strings + i; 1538 s = b->strings + i;
1521 NEXT_FREE_LISP_STRING (s) = string_free_list; 1539 NEXT_FREE_LISP_STRING (s) = string_free_list;
1522 string_free_list = s; 1540 string_free_list = s;
1523 } 1541 }
1524 1542
1525 total_free_strings += STRINGS_IN_STRING_BLOCK; 1543 total_free_strings += STRING_BLOCK_SIZE;
1526 } 1544 }
1527 1545
1528 /* Pop a Lisp_String off the free-list. */ 1546 /* Pop a Lisp_String off the free-list. */
@@ -1673,7 +1691,7 @@ sweep_strings ()
1673 1691
1674 next = b->next; 1692 next = b->next;
1675 1693
1676 for (i = 0; i < STRINGS_IN_STRING_BLOCK; ++i) 1694 for (i = 0; i < STRING_BLOCK_SIZE; ++i)
1677 { 1695 {
1678 struct Lisp_String *s = b->strings + i; 1696 struct Lisp_String *s = b->strings + i;
1679 1697
@@ -1728,8 +1746,8 @@ sweep_strings ()
1728 1746
1729 /* Free blocks that contain free Lisp_Strings only, except 1747 /* Free blocks that contain free Lisp_Strings only, except
1730 the first two of them. */ 1748 the first two of them. */
1731 if (nfree == STRINGS_IN_STRING_BLOCK 1749 if (nfree == STRING_BLOCK_SIZE
1732 && total_free_strings > STRINGS_IN_STRING_BLOCK) 1750 && total_free_strings > STRING_BLOCK_SIZE)
1733 { 1751 {
1734 lisp_free (b); 1752 lisp_free (b);
1735 --n_string_blocks; 1753 --n_string_blocks;