1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22: #include <config.h>
23: #include <stdio.h>
24: #include <limits.h>
25:
26: #ifdef STDC_HEADERS
27: #include <stddef.h>
28: #endif
29:
30: #ifdef ALLOC_DEBUG
31: #undef INLINE
32: #endif
33:
34:
35:
36: #include <signal.h>
37:
38: #ifdef HAVE_GTK_AND_PTHREAD
39: #include <pthread.h>
40: #endif
41:
42:
43:
44:
45:
46: #undef HIDE_LISP_IMPLEMENTATION
47: #include "lisp.h"
48: #include "process.h"
49: #include "intervals.h"
50: #include "puresize.h"
51: #include "buffer.h"
52: #include "window.h"
53: #include "keyboard.h"
54: #include "frame.h"
55: #include "blockinput.h"
56: #include "charset.h"
57: #include "syssignal.h"
58: #include <setjmp.h>
59:
60:
61:
62:
63: #if defined SYSTEM_MALLOC || defined DOUG_LEA_MALLOC
64: #undef GC_MALLOC_CHECK
65: #endif
66:
67: #ifdef HAVE_UNISTD_H
68: #include <unistd.h>
69: #else
70: extern POINTER_TYPE *sbrk ();
71: #endif
72:
73: #ifdef HAVE_FCNTL_H
74: #define INCLUDED_FCNTL
75: #include <fcntl.h>
76: #endif
77: #ifndef O_WRONLY
78: #define O_WRONLY 1
79: #endif
80:
81: #ifdef WINDOWSNT
82: #include <fcntl.h>
83: #include "w32.h"
84: #endif
85:
86: #ifdef DOUG_LEA_MALLOC
87:
88: #include <malloc.h>
89:
90: #ifndef __malloc_size_t
91: #define __malloc_size_t int
92: #endif
93:
94:
95:
96:
97: #define MMAP_MAX_AREAS 100000000
98:
99: #else
100:
101:
102:
103: #define __malloc_size_t size_t
104: extern __malloc_size_t _bytes_used;
105: extern __malloc_size_t __malloc_extra_blocks;
106:
107: #endif
108:
109: #if ! defined (SYSTEM_MALLOC) && defined (HAVE_GTK_AND_PTHREAD)
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128: static pthread_mutex_t alloc_mutex;
129:
130: #define BLOCK_INPUT_ALLOC \
131: do \
132: { \
133: if (pthread_equal (pthread_self (), main_thread)) \
134: BLOCK_INPUT; \
135: pthread_mutex_lock (&alloc_mutex); \
136: } \
137: while (0)
138: #define UNBLOCK_INPUT_ALLOC \
139: do \
140: { \
141: pthread_mutex_unlock (&alloc_mutex); \
142: if (pthread_equal (pthread_self (), main_thread)) \
143: UNBLOCK_INPUT; \
144: } \
145: while (0)
146:
147: #else
148:
149: #define BLOCK_INPUT_ALLOC BLOCK_INPUT
150: #define UNBLOCK_INPUT_ALLOC UNBLOCK_INPUT
151:
152: #endif
153:
154:
155:
156: static __malloc_size_t bytes_used_when_full;
157:
158: static __malloc_size_t bytes_used_when_reconsidered;
159:
160:
161:
162:
163: #define MARK_STRING(S) ((S)->size |= ARRAY_MARK_FLAG)
164: #define UNMARK_STRING(S) ((S)->size &= ~ARRAY_MARK_FLAG)
165: #define STRING_MARKED_P(S) (((S)->size & ARRAY_MARK_FLAG) != 0)
166:
167: #define VECTOR_MARK(V) ((V)->size |= ARRAY_MARK_FLAG)
168: #define VECTOR_UNMARK(V) ((V)->size &= ~ARRAY_MARK_FLAG)
169: #define VECTOR_MARKED_P(V) (((V)->size & ARRAY_MARK_FLAG) != 0)
170:
171:
172:
173:
174:
175:
176: #define GC_STRING_BYTES(S) (STRING_BYTES (S))
177: #define GC_STRING_CHARS(S) ((S)->size & ~ARRAY_MARK_FLAG)
178:
179:
180:
181: int consing_since_gc;
182:
183:
184:
185: EMACS_INT cons_cells_consed;
186: EMACS_INT floats_consed;
187: EMACS_INT vector_cells_consed;
188: EMACS_INT symbols_consed;
189: EMACS_INT string_chars_consed;
190: EMACS_INT misc_objects_consed;
191: EMACS_INT intervals_consed;
192: EMACS_INT strings_consed;
193:
194:
195:
196: EMACS_INT gc_cons_threshold;
197:
198:
199:
200: EMACS_INT gc_relative_threshold;
201:
202: static Lisp_Object Vgc_cons_percentage;
203:
204:
205:
206:
207: EMACS_INT memory_full_cons_threshold;
208:
209:
210:
211: int gc_in_progress;
212:
213:
214:
215:
216:
217: int abort_on_gc;
218:
219:
220:
221: int garbage_collection_messages;
222:
223: #ifndef VIRT_ADDR_VARIES
224: extern
225: #endif
226: int malloc_sbrk_used;
227:
228: #ifndef VIRT_ADDR_VARIES
229: extern
230: #endif
231: int malloc_sbrk_unused;
232:
233:
234:
235: static int total_conses, total_markers, total_symbols, total_vector_size;
236: static int total_free_conses, total_free_markers, total_free_symbols;
237: static int total_free_floats, total_floats;
238:
239:
240:
241:
242:
243: char *spare_memory[7];
244:
245:
246:
247: #define SPARE_MEMORY (1 << 14)
248:
249:
250:
251: static int malloc_hysteresis;
252:
253:
254:
255: Lisp_Object Vpurify_flag;
256:
257:
258:
259: Lisp_Object Vmemory_full;
260:
261: #ifndef HAVE_SHM
262:
263:
264:
265:
266:
267:
268:
269: EMACS_INT pure[PURESIZE / sizeof (EMACS_INT)] = {1,};
270: #define PUREBEG (char *) pure
271:
272: #else
273:
274: #define pure PURE_SEG_BITS
275: #define PUREBEG (char *)PURE_SEG_BITS
276:
277: #endif
278:
279:
280:
281: static char *purebeg;
282: static size_t pure_size;
283:
284:
285:
286:
287: static size_t pure_bytes_used_before_overflow;
288:
289:
290:
291: #define PURE_POINTER_P(P) \
292: (((PNTR_COMPARISON_TYPE) (P) \
293: < (PNTR_COMPARISON_TYPE) ((char *) purebeg + pure_size)) \
294: && ((PNTR_COMPARISON_TYPE) (P) \
295: >= (PNTR_COMPARISON_TYPE) purebeg))
296:
297:
298:
299: EMACS_INT pure_bytes_used;
300:
301:
302:
303: static EMACS_INT pure_bytes_used_lisp;
304:
305:
306:
307: static EMACS_INT pure_bytes_used_non_lisp;
308:
309:
310:
311:
312: char *pending_malloc_warning;
313:
314:
315:
316: Lisp_Object Vmemory_signal_data;
317:
318:
319:
320: #ifndef MAX_SAVE_STACK
321: #define MAX_SAVE_STACK 16000
322: #endif
323:
324:
325:
326: char *stack_copy;
327: int stack_copy_size;
328:
329:
330:
331:
332: int ignore_warnings;
333:
334: Lisp_Object Qgc_cons_threshold, Qchar_table_extra_slots;
335:
336:
337:
338: Lisp_Object Vpost_gc_hook, Qpost_gc_hook;
339:
340: Lisp_Object Vgc_elapsed;
341: EMACS_INT gcs_done;
342:
343: static void mark_buffer P_ ((Lisp_Object));
344: extern void mark_kboards P_ ((void));
345: extern void mark_backtrace P_ ((void));
346: static void gc_sweep P_ ((void));
347: static void mark_glyph_matrix P_ ((struct glyph_matrix *));
348: static void mark_face_cache P_ ((struct face_cache *));
349:
350: #ifdef HAVE_WINDOW_SYSTEM
351: extern void mark_fringe_data P_ ((void));
352: static void mark_image P_ ((struct image *));
353: static void mark_image_cache P_ ((struct frame *));
354: #endif
355:
356: static struct Lisp_String *allocate_string P_ ((void));
357: static void compact_small_strings P_ ((void));
358: static void free_large_strings P_ ((void));
359: static void sweep_strings P_ ((void));
360:
361: extern int message_enable_multibyte;
362:
363:
364:
365:
366:
367: enum mem_type
368: {
369: MEM_TYPE_NON_LISP,
370: MEM_TYPE_BUFFER,
371: MEM_TYPE_CONS,
372: MEM_TYPE_STRING,
373: MEM_TYPE_MISC,
374: MEM_TYPE_SYMBOL,
375: MEM_TYPE_FLOAT,
376:
377:
378:
379: MEM_TYPE_VECTOR,
380: MEM_TYPE_PROCESS,
381: MEM_TYPE_HASH_TABLE,
382: MEM_TYPE_FRAME,
383: MEM_TYPE_WINDOW
384: };
385:
386: static POINTER_TYPE *lisp_align_malloc P_ ((size_t, enum mem_type));
387: static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
388: void refill_memory_reserve ();
389:
390:
391: #if GC_MARK_STACK || defined GC_MALLOC_CHECK
392:
393: #if GC_MARK_STACK == GC_USE_GCPROS_CHECK_ZOMBIES
394: #include <stdio.h>
395: #endif
396:
397:
398:
399:
400: Lisp_Object Vdead;
401:
402: #ifdef GC_MALLOC_CHECK
403:
404: enum mem_type allocated_mem_type;
405: int dont_register_blocks;
406:
407: #endif
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433: struct mem_node
434: {
435:
436:
437: struct mem_node *left, *right;
438:
439:
440: struct mem_node *parent;
441:
442:
443: void *start, *end;
444:
445:
446: enum {MEM_BLACK, MEM_RED} color;
447:
448:
449: enum mem_type type;
450: };
451:
452:
453:
454: Lisp_Object *stack_base;
455:
456:
457:
458: static struct mem_node *mem_root;
459:
460:
461:
462: static void *min_heap_address, *max_heap_address;
463:
464:
465:
466: static struct mem_node mem_z;
467: #define MEM_NIL &mem_z
468:
469: static POINTER_TYPE *lisp_malloc P_ ((size_t, enum mem_type));
470: static struct Lisp_Vector *allocate_vectorlike P_ ((EMACS_INT, enum mem_type));
471: static void lisp_free P_ ((POINTER_TYPE *));
472: static void mark_stack P_ ((void));
473: static int live_vector_p P_ ((struct mem_node *, void *));
474: static int live_buffer_p P_ ((struct mem_node *, void *));
475: static int live_string_p P_ ((struct mem_node *, void *));
476: static int live_cons_p P_ ((struct mem_node *, void *));
477: static int live_symbol_p P_ ((struct mem_node *, void *));
478: static int live_float_p P_ ((struct mem_node *, void *));
479: static int live_misc_p P_ ((struct mem_node *, void *));
480: static void mark_maybe_object P_ ((Lisp_Object));
481: static void mark_memory P_ ((void *, void *, int));
482: static void mem_init P_ ((void));
483: static struct mem_node *mem_insert P_ ((void *, void *, enum mem_type));
484: static void mem_insert_fixup P_ ((struct mem_node *));
485: static void mem_rotate_left P_ ((struct mem_node *));
486: static void mem_rotate_right P_ ((struct mem_node *));
487: static void mem_delete P_ ((struct mem_node *));
488: static void mem_delete_fixup P_ ((struct mem_node *));
489: static INLINE struct mem_node *mem_find P_ ((void *));
490:
491:
492: #if GC_MARK_STACK == GC_MARK_STACK_CHECK_GCPROS
493: static void check_gcpros P_ ((void));
494: #endif
495:
496: #endif
497:
498:
499:
500: struct gcpro *gcprolist;
501:
502:
503:
504:
505: #define NSTATICS 1280
506: Lisp_Object *staticvec[NSTATICS] = {&Vpurify_flag};
507:
508:
509:
510: int staticidx = 0;
511:
512: static POINTER_TYPE *pure_alloc P_ ((size_t, int));
513:
514:
515:
516:
517:
518: #define ALIGN(ptr, ALIGNMENT) \
519: ((POINTER_TYPE *) ((((EMACS_UINT)(ptr)) + (ALIGNMENT) - 1) \
520: & ~((ALIGNMENT) - 1)))
521:
522:
523: ^L
524:
525:
526:
527:
528:
529:
530: void
531: malloc_warning (str)
532: char *str;
533: {
534: pending_malloc_warning = str;
535: }
536:
537:
538:
539:
540: void
541: display_malloc_warning ()
542: {
543: call3 (intern ("display-warning"),
544: intern ("alloc"),
545: build_string (pending_malloc_warning),
546: intern ("emergency"));
547: pending_malloc_warning = 0;
548: }
549:
550:
551: #ifdef DOUG_LEA_MALLOC
552: # define BYTES_USED (mallinfo ().uordblks)
553: #else
554: # define BYTES_USED _bytes_used
555: #endif
556: ^L
557:
558:
559: void
560: buffer_memory_full ()
561: {
562:
563:
564:
565:
566:
567:
568:
569: #ifndef REL_ALLOC
570: memory_full ();
571: #endif
572:
573:
574:
575: xsignal (Qnil, Vmemory_signal_data);
576: }
577:
578:
579: #ifdef XMALLOC_OVERRUN_CHECK
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593: #define XMALLOC_OVERRUN_CHECK_SIZE 16
594:
595: static char xmalloc_overrun_check_header[XMALLOC_OVERRUN_CHECK_SIZE-4] =
596: { 0x9a, 0x9b, 0xae, 0xaf,
597: 0xbf, 0xbe, 0xce, 0xcf,
598: 0xea, 0xeb, 0xec, 0xed };
599:
600: static char xmalloc_overrun_check_trailer[XMALLOC_OVERRUN_CHECK_SIZE] =
601: { 0xaa, 0xab, 0xac, 0xad,
602: 0xba, 0xbb, 0xbc, 0xbd,
603: 0xca, 0xcb, 0xcc, 0xcd,
604: 0xda, 0xdb, 0xdc, 0xdd };
605:
606: