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