aboutsummaryrefslogtreecommitdiffstats
path: root/src/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/alloc.c')
-rw-r--r--src/alloc.c80
1 files changed, 52 insertions, 28 deletions
diff --git a/src/alloc.c b/src/alloc.c
index 8fcc6f91df9..0c18fca1755 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -467,7 +467,7 @@ display_malloc_warning (void)
467/* Called if we can't allocate relocatable space for a buffer. */ 467/* Called if we can't allocate relocatable space for a buffer. */
468 468
469void 469void
470buffer_memory_full (void) 470buffer_memory_full (EMACS_INT nbytes)
471{ 471{
472 /* If buffers use the relocating allocator, no need to free 472 /* If buffers use the relocating allocator, no need to free
473 spare_memory, because we may have plenty of malloc space left 473 spare_memory, because we may have plenty of malloc space left
@@ -477,7 +477,7 @@ buffer_memory_full (void)
477 malloc. */ 477 malloc. */
478 478
479#ifndef REL_ALLOC 479#ifndef REL_ALLOC
480 memory_full (); 480 memory_full (nbytes);
481#endif 481#endif
482 482
483 /* This used to call error, but if we've run out of memory, we could 483 /* This used to call error, but if we've run out of memory, we could
@@ -673,7 +673,7 @@ xmalloc (size_t size)
673 MALLOC_UNBLOCK_INPUT; 673 MALLOC_UNBLOCK_INPUT;
674 674
675 if (!val && size) 675 if (!val && size)
676 memory_full (); 676 memory_full (size);
677 return val; 677 return val;
678} 678}
679 679
@@ -694,7 +694,8 @@ xrealloc (POINTER_TYPE *block, size_t size)
694 val = (POINTER_TYPE *) realloc (block, size); 694 val = (POINTER_TYPE *) realloc (block, size);
695 MALLOC_UNBLOCK_INPUT; 695 MALLOC_UNBLOCK_INPUT;
696 696
697 if (!val && size) memory_full (); 697 if (!val && size)
698 memory_full (size);
698 return val; 699 return val;
699} 700}
700 701
@@ -787,7 +788,7 @@ lisp_malloc (size_t nbytes, enum mem_type type)
787 788
788 MALLOC_UNBLOCK_INPUT; 789 MALLOC_UNBLOCK_INPUT;
789 if (!val && nbytes) 790 if (!val && nbytes)
790 memory_full (); 791 memory_full (nbytes);
791 return val; 792 return val;
792} 793}
793 794
@@ -934,7 +935,7 @@ lisp_align_malloc (size_t nbytes, enum mem_type type)
934 if (base == 0) 935 if (base == 0)
935 { 936 {
936 MALLOC_UNBLOCK_INPUT; 937 MALLOC_UNBLOCK_INPUT;
937 memory_full (); 938 memory_full (ABLOCKS_BYTES);
938 } 939 }
939 940
940 aligned = (base == abase); 941 aligned = (base == abase);
@@ -960,7 +961,7 @@ lisp_align_malloc (size_t nbytes, enum mem_type type)
960 lisp_malloc_loser = base; 961 lisp_malloc_loser = base;
961 free (base); 962 free (base);
962 MALLOC_UNBLOCK_INPUT; 963 MALLOC_UNBLOCK_INPUT;
963 memory_full (); 964 memory_full (SIZE_MAX);
964 } 965 }
965 } 966 }
966#endif 967#endif
@@ -2792,7 +2793,7 @@ allocate_vectorlike (EMACS_INT len)
2792 int word_size = sizeof p->contents[0]; 2793 int word_size = sizeof p->contents[0];
2793 2794
2794 if ((SIZE_MAX - header_size) / word_size < len) 2795 if ((SIZE_MAX - header_size) / word_size < len)
2795 memory_full (); 2796 memory_full (SIZE_MAX);
2796 2797
2797 MALLOC_BLOCK_INPUT; 2798 MALLOC_BLOCK_INPUT;
2798 2799
@@ -3270,35 +3271,58 @@ make_event_array (register int nargs, Lisp_Object *args)
3270 ************************************************************************/ 3271 ************************************************************************/
3271 3272
3272 3273
3273/* Called if malloc returns zero. */ 3274/* Called if malloc (NBYTES) returns zero. If NBYTES == SIZE_MAX,
3275 there may have been size_t overflow so that malloc was never
3276 called, or perhaps malloc was invoked successfully but the
3277 resulting pointer had problems fitting into a tagged EMACS_INT. In
3278 either case this counts as memory being full even though malloc did
3279 not fail. */
3274 3280
3275void 3281void
3276memory_full (void) 3282memory_full (size_t nbytes)
3277{ 3283{
3278 int i; 3284 /* Do not go into hysterics merely because a large request failed. */
3285 int enough_free_memory = 0;
3286 if (SPARE_MEMORY < nbytes)
3287 {
3288 void *p = malloc (SPARE_MEMORY);
3289 if (p)
3290 {
3291 if (spare_memory[0])
3292 free (p);
3293 else
3294 spare_memory[0] = p;
3295 enough_free_memory = 1;
3296 }
3297 }
3279 3298
3280 Vmemory_full = Qt; 3299 if (! enough_free_memory)
3300 {
3301 int i;
3281 3302
3282 memory_full_cons_threshold = sizeof (struct cons_block); 3303 Vmemory_full = Qt;
3283 3304
3284 /* The first time we get here, free the spare memory. */ 3305 memory_full_cons_threshold = sizeof (struct cons_block);
3285 for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++) 3306
3286 if (spare_memory[i]) 3307 /* The first time we get here, free the spare memory. */
3287 { 3308 for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++)
3288 if (i == 0) 3309 if (spare_memory[i])
3289 free (spare_memory[i]); 3310 {
3290 else if (i >= 1 && i <= 4) 3311 if (i == 0)
3291 lisp_align_free (spare_memory[i]); 3312 free (spare_memory[i]);
3292 else 3313 else if (i >= 1 && i <= 4)
3293 lisp_free (spare_memory[i]); 3314 lisp_align_free (spare_memory[i]);
3294 spare_memory[i] = 0; 3315 else
3295 } 3316 lisp_free (spare_memory[i]);
3317 spare_memory[i] = 0;
3318 }
3296 3319
3297 /* Record the space now used. When it decreases substantially, 3320 /* Record the space now used. When it decreases substantially,
3298 we can refill the memory reserve. */ 3321 we can refill the memory reserve. */
3299#if !defined SYSTEM_MALLOC && !defined SYNC_INPUT 3322#if !defined SYSTEM_MALLOC && !defined SYNC_INPUT
3300 bytes_used_when_full = BYTES_USED; 3323 bytes_used_when_full = BYTES_USED;
3301#endif 3324#endif
3325 }
3302 3326
3303 /* This used to call error, but if we've run out of memory, we could 3327 /* This used to call error, but if we've run out of memory, we could
3304 get infinite recursion trying to build the string. */ 3328 get infinite recursion trying to build the string. */