comparison src/alloc.c @ 83397:693e794b57bf

Merged from miles@gnu.org--gnu-2005 (patch 149-151, 629-641) Patches applied: * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-629 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-630 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-631 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-632 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-633 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-634 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-635 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-636 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-637 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-638 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-639 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-640 Update from CVS * miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-641 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-149 Merge from emacs--cvs-trunk--0 * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-150 Update from CVS * miles@gnu.org--gnu-2005/gnus--rel--5.10--patch-151 Update from CVS git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-437
author Karoly Lorentey <lorentey@elte.hu>
date Mon, 07 Nov 2005 14:56:19 +0000
parents 2a679c81f552 6ab8d86f8a2b
children 03934708f1e9
comparison
equal deleted inserted replaced
83396:201f610eb492 83397:693e794b57bf
136 136
137 /* Value of _bytes_used, when spare_memory was freed. */ 137 /* Value of _bytes_used, when spare_memory was freed. */
138 138
139 static __malloc_size_t bytes_used_when_full; 139 static __malloc_size_t bytes_used_when_full;
140 140
141 static __malloc_size_t bytes_used_when_reconsidered;
142
141 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer 143 /* Mark, unmark, query mark bit of a Lisp string. S must be a pointer
142 to a struct Lisp_String. */ 144 to a struct Lisp_String. */
143 145
144 #define MARK_STRING(S) ((S)->size |= ARRAY_MARK_FLAG) 146 #define MARK_STRING(S) ((S)->size |= ARRAY_MARK_FLAG)
145 #define UNMARK_STRING(S) ((S)->size &= ~ARRAY_MARK_FLAG) 147 #define UNMARK_STRING(S) ((S)->size &= ~ARRAY_MARK_FLAG)
180 182
181 EMACS_INT gc_relative_threshold; 183 EMACS_INT gc_relative_threshold;
182 184
183 static Lisp_Object Vgc_cons_percentage; 185 static Lisp_Object Vgc_cons_percentage;
184 186
187 /* Minimum number of bytes of consing since GC before next GC,
188 when memory is full. */
189
190 EMACS_INT memory_full_cons_threshold;
191
185 /* Nonzero during GC. */ 192 /* Nonzero during GC. */
186 193
187 int gc_in_progress; 194 int gc_in_progress;
188 195
189 /* Nonzero means abort if try to GC. 196 /* Nonzero means abort if try to GC.
211 static int total_conses, total_markers, total_symbols, total_vector_size; 218 static int total_conses, total_markers, total_symbols, total_vector_size;
212 static int total_free_conses, total_free_markers, total_free_symbols; 219 static int total_free_conses, total_free_markers, total_free_symbols;
213 static int total_free_floats, total_floats; 220 static int total_free_floats, total_floats;
214 221
215 /* Points to memory space allocated as "spare", to be freed if we run 222 /* Points to memory space allocated as "spare", to be freed if we run
216 out of memory. */ 223 out of memory. We keep one large block, four cons-blocks, and
217 224 two string blocks. */
218 static char *spare_memory; 225
219 226 char *spare_memory[7];
220 /* Amount of spare memory to keep in reserve. */ 227
228 /* Amount of spare memory to keep in large reserve block. */
221 229
222 #define SPARE_MEMORY (1 << 14) 230 #define SPARE_MEMORY (1 << 14)
223 231
224 /* Number of extra blocks malloc should get when it needs more core. */ 232 /* Number of extra blocks malloc should get when it needs more core. */
225 233
348 MEM_TYPE_PROCESS, 356 MEM_TYPE_PROCESS,
349 MEM_TYPE_HASH_TABLE, 357 MEM_TYPE_HASH_TABLE,
350 MEM_TYPE_FRAME, 358 MEM_TYPE_FRAME,
351 MEM_TYPE_WINDOW 359 MEM_TYPE_WINDOW
352 }; 360 };
361
362 static POINTER_TYPE *lisp_align_malloc P_ ((size_t, enum mem_type));
363 static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
364 void refill_memory_reserve ();
365
353 366
354 #if GC_MARK_STACK || defined GC_MALLOC_CHECK 367 #if GC_MARK_STACK || defined GC_MALLOC_CHECK
355 368
356 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES 369 #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
357 #include <stdio.h> /* For fprintf. */ 370 #include <stdio.h> /* For fprintf. */
449 static void mem_rotate_right P_ ((struct mem_node *)); 462 static void mem_rotate_right P_ ((struct mem_node *));
450 static void mem_delete P_ ((struct mem_node *)); 463 static void mem_delete P_ ((struct mem_node *));
451 static void mem_delete_fixup P_ ((struct mem_node *)); 464 static void mem_delete_fixup P_ ((struct mem_node *));
452 static INLINE struct mem_node *mem_find P_ ((void *)); 465 static INLINE struct mem_node *mem_find P_ ((void *));
453 466
467
454 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS 468 #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
455 static void check_gcpros P_ ((void)); 469 static void check_gcpros P_ ((void));
456 #endif 470 #endif
457 471
458 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */ 472 #endif /* GC_MARK_STACK || GC_MALLOC_CHECK */
509 pending_malloc_warning = 0; 523 pending_malloc_warning = 0;
510 } 524 }
511 525
512 526
513 #ifdef DOUG_LEA_MALLOC 527 #ifdef DOUG_LEA_MALLOC
514 # define BYTES_USED (mallinfo ().arena) 528 # define BYTES_USED (mallinfo ().uordblks)
515 #else 529 #else
516 # define BYTES_USED _bytes_used 530 # define BYTES_USED _bytes_used
517 #endif 531 #endif
518
519
520 /* Called if malloc returns zero. */
521
522 void
523 memory_full ()
524 {
525 Vmemory_full = Qt;
526
527 #ifndef SYSTEM_MALLOC
528 bytes_used_when_full = BYTES_USED;
529 #endif
530
531 /* The first time we get here, free the spare memory. */
532 if (spare_memory)
533 {
534 free (spare_memory);
535 spare_memory = 0;
536 }
537
538 /* This used to call error, but if we've run out of memory, we could
539 get infinite recursion trying to build the string. */
540 while (1)
541 Fsignal (Qnil, Vmemory_signal_data);
542 }
543
544 DEFUN ("memory-full-p", Fmemory_full_p, Smemory_full_p, 0, 0, 0,
545 doc: /* t if memory is nearly full, nil otherwise. */)
546 ()
547 {
548 return (spare_memory ? Qnil : Qt);
549 }
550
551 /* If we released our reserve (due to running out of memory),
552 and we have a fair amount free once again,
553 try to set aside another reserve in case we run out once more.
554
555 This is called when a relocatable block is freed in ralloc.c. */
556
557 void
558 refill_memory_reserve ()
559 {
560 #ifndef SYSTEM_MALLOC
561 if (spare_memory == 0)
562 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY);
563 #endif
564 }
565 532
566 /* Called if we can't allocate relocatable space for a buffer. */ 533 /* Called if we can't allocate relocatable space for a buffer. */
567 534
568 void 535 void
569 buffer_memory_full () 536 buffer_memory_full ()
576 malloc. */ 543 malloc. */
577 544
578 #ifndef REL_ALLOC 545 #ifndef REL_ALLOC
579 memory_full (); 546 memory_full ();
580 #endif 547 #endif
581
582 Vmemory_full = Qt;
583 548
584 /* This used to call error, but if we've run out of memory, we could 549 /* This used to call error, but if we've run out of memory, we could
585 get infinite recursion trying to build the string. */ 550 get infinite recursion trying to build the string. */
586 while (1) 551 while (1)
587 Fsignal (Qnil, Vmemory_signal_data); 552 Fsignal (Qnil, Vmemory_signal_data);
804 POINTER_TYPE *block; 769 POINTER_TYPE *block;
805 { 770 {
806 BLOCK_INPUT; 771 BLOCK_INPUT;
807 free (block); 772 free (block);
808 UNBLOCK_INPUT; 773 UNBLOCK_INPUT;
774 /* We don't call refill_memory_reserve here
775 because that duplicates doing so in emacs_blocked_free
776 and the criterion should go there. */
809 } 777 }
810 778
811 779
812 /* Like strdup, but uses xmalloc. */ 780 /* Like strdup, but uses xmalloc. */
813 781
1177 static void 1145 static void
1178 emacs_blocked_free (ptr, ptr2) 1146 emacs_blocked_free (ptr, ptr2)
1179 void *ptr; 1147 void *ptr;
1180 const void *ptr2; 1148 const void *ptr2;
1181 { 1149 {
1150 EMACS_INT bytes_used_now;
1151
1182 BLOCK_INPUT_ALLOC; 1152 BLOCK_INPUT_ALLOC;
1183 1153
1184 #ifdef GC_MALLOC_CHECK 1154 #ifdef GC_MALLOC_CHECK
1185 if (ptr) 1155 if (ptr)
1186 { 1156 {
1205 free (ptr); 1175 free (ptr);
1206 1176
1207 /* If we released our reserve (due to running out of memory), 1177 /* If we released our reserve (due to running out of memory),
1208 and we have a fair amount free once again, 1178 and we have a fair amount free once again,
1209 try to set aside another reserve in case we run out once more. */ 1179 try to set aside another reserve in case we run out once more. */
1210 if (spare_memory == 0 1180 if (! NILP (Vmemory_full)
1211 /* Verify there is enough space that even with the malloc 1181 /* Verify there is enough space that even with the malloc
1212 hysteresis this call won't run out again. 1182 hysteresis this call won't run out again.
1213 The code here is correct as long as SPARE_MEMORY 1183 The code here is correct as long as SPARE_MEMORY
1214 is substantially larger than the block size malloc uses. */ 1184 is substantially larger than the block size malloc uses. */
1215 && (bytes_used_when_full 1185 && (bytes_used_when_full
1216 > BYTES_USED + max (malloc_hysteresis, 4) * SPARE_MEMORY)) 1186 > ((bytes_used_when_reconsidered = BYTES_USED)
1217 spare_memory = (char *) malloc ((size_t) SPARE_MEMORY); 1187 + max (malloc_hysteresis, 4) * SPARE_MEMORY)))
1188 refill_memory_reserve ();
1218 1189
1219 __free_hook = emacs_blocked_free; 1190 __free_hook = emacs_blocked_free;
1220 UNBLOCK_INPUT_ALLOC; 1191 UNBLOCK_INPUT_ALLOC;
1221 } 1192 }
1222 1193
3377 return result; 3348 return result;
3378 } 3349 }
3379 } 3350 }
3380 3351
3381 3352
3353
3354 /************************************************************************
3355 Memory Full Handling
3356 ************************************************************************/
3357
3358
3359 /* Called if malloc returns zero. */
3360
3361 void
3362 memory_full ()
3363 {
3364 int i;
3365
3366 Vmemory_full = Qt;
3367
3368 memory_full_cons_threshold = sizeof (struct cons_block);
3369
3370 /* The first time we get here, free the spare memory. */
3371 for (i = 0; i < sizeof (spare_memory) / sizeof (char *); i++)
3372 if (spare_memory[i])
3373 {
3374 if (i == 0)
3375 free (spare_memory[i]);
3376 else if (i >= 1 && i <= 4)
3377 lisp_align_free (spare_memory[i]);
3378 else
3379 lisp_free (spare_memory[i]);
3380 spare_memory[i] = 0;
3381 }
3382
3383 /* Record the space now used. When it decreases substantially,
3384 we can refill the memory reserve. */
3385 #ifndef SYSTEM_MALLOC
3386 bytes_used_when_full = BYTES_USED;
3387 #endif
3388
3389 /* This used to call error, but if we've run out of memory, we could
3390 get infinite recursion trying to build the string. */
3391 while (1)
3392 Fsignal (Qnil, Vmemory_signal_data);
3393 }
3394
3395 /* If we released our reserve (due to running out of memory),
3396 and we have a fair amount free once again,
3397 try to set aside another reserve in case we run out once more.
3398
3399 This is called when a relocatable block is freed in ralloc.c,
3400 and also directly from this file, in case we're not using ralloc.c. */
3401
3402 void
3403 refill_memory_reserve ()
3404 {
3405 #ifndef SYSTEM_MALLOC
3406 if (spare_memory[0] == 0)
3407 spare_memory[0] = (char *) malloc ((size_t) SPARE_MEMORY);
3408 if (spare_memory[1] == 0)
3409 spare_memory[1] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3410 MEM_TYPE_CONS);
3411 if (spare_memory[2] == 0)
3412 spare_memory[2] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3413 MEM_TYPE_CONS);
3414 if (spare_memory[3] == 0)
3415 spare_memory[3] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3416 MEM_TYPE_CONS);
3417 if (spare_memory[4] == 0)
3418 spare_memory[4] = (char *) lisp_align_malloc (sizeof (struct cons_block),
3419 MEM_TYPE_CONS);
3420 if (spare_memory[5] == 0)
3421 spare_memory[5] = (char *) lisp_malloc (sizeof (struct string_block),
3422 MEM_TYPE_STRING);
3423 if (spare_memory[6] == 0)
3424 spare_memory[6] = (char *) lisp_malloc (sizeof (struct string_block),
3425 MEM_TYPE_STRING);
3426 if (spare_memory[0] && spare_memory[1] && spare_memory[5])
3427 Vmemory_full = Qnil;
3428 #endif
3429 }
3382 3430
3383 /************************************************************************ 3431 /************************************************************************
3384 C Stack Marking 3432 C Stack Marking
3385 ************************************************************************/ 3433 ************************************************************************/
3386 3434
6006 malloc_hysteresis = 32; 6054 malloc_hysteresis = 32;
6007 #else 6055 #else
6008 malloc_hysteresis = 0; 6056 malloc_hysteresis = 0;
6009 #endif 6057 #endif
6010 6058
6011 spare_memory = (char *) malloc (SPARE_MEMORY); 6059 refill_memory_reserve ();
6012 6060
6013 ignore_warnings = 0; 6061 ignore_warnings = 0;
6014 gcprolist = 0; 6062 gcprolist = 0;
6015 byte_stack_list = 0; 6063 byte_stack_list = 0;
6016 staticidx = 0; 6064 staticidx = 0;
6107 Vmemory_signal_data 6155 Vmemory_signal_data
6108 = list2 (Qerror, 6156 = list2 (Qerror,
6109 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs")); 6157 build_string ("Memory exhausted--use M-x save-some-buffers then exit and restart Emacs"));
6110 6158
6111 DEFVAR_LISP ("memory-full", &Vmemory_full, 6159 DEFVAR_LISP ("memory-full", &Vmemory_full,
6112 doc: /* Non-nil means we are handling a memory-full error. */); 6160 doc: /* Non-nil means Emacs cannot get much more Lisp memory. */);
6113 Vmemory_full = Qnil; 6161 Vmemory_full = Qnil;
6114 6162
6115 staticpro (&Qgc_cons_threshold); 6163 staticpro (&Qgc_cons_threshold);
6116 Qgc_cons_threshold = intern ("gc-cons-threshold"); 6164 Qgc_cons_threshold = intern ("gc-cons-threshold");
6117 6165
6122 doc: /* Accumulated time elapsed in garbage collections. 6170 doc: /* Accumulated time elapsed in garbage collections.
6123 The time is in seconds as a floating point value. */); 6171 The time is in seconds as a floating point value. */);
6124 DEFVAR_INT ("gcs-done", &gcs_done, 6172 DEFVAR_INT ("gcs-done", &gcs_done,
6125 doc: /* Accumulated number of garbage collections done. */); 6173 doc: /* Accumulated number of garbage collections done. */);
6126 6174
6127 defsubr (&Smemory_full_p);
6128 defsubr (&Scons); 6175 defsubr (&Scons);
6129 defsubr (&Slist); 6176 defsubr (&Slist);
6130 defsubr (&Svector); 6177 defsubr (&Svector);
6131 defsubr (&Smake_byte_code); 6178 defsubr (&Smake_byte_code);
6132 defsubr (&Smake_list); 6179 defsubr (&Smake_list);