Mercurial > hg > xemacs-beta
view src/free-hook.c @ 5127:a9c41067dd88 ben-lisp-object
more cleanups, terminology clarification, lots of doc work
-------------------- ChangeLog entries follow: --------------------
man/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* internals/internals.texi (Introduction to Allocation):
* internals/internals.texi (Integers and Characters):
* internals/internals.texi (Allocation from Frob Blocks):
* internals/internals.texi (lrecords):
* internals/internals.texi (Low-level allocation):
Rewrite section on allocation of Lisp objects to reflect the new
reality. Remove references to nonexistent XSETINT and XSETCHAR.
modules/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* postgresql/postgresql.c (allocate_pgconn):
* postgresql/postgresql.c (allocate_pgresult):
* postgresql/postgresql.h (struct Lisp_PGconn):
* postgresql/postgresql.h (struct Lisp_PGresult):
* ldap/eldap.c (allocate_ldap):
* ldap/eldap.h (struct Lisp_LDAP):
Same changes as in src/ dir. See large log there in ChangeLog,
but basically:
ALLOC_LISP_OBJECT -> ALLOC_NORMAL_LISP_OBJECT
LISP_OBJECT_HEADER -> NORMAL_LISP_OBJECT_HEADER
../hlo/src/ChangeLog addition:
2010-03-05 Ben Wing <ben@xemacs.org>
* alloc.c:
* alloc.c (old_alloc_sized_lcrecord):
* alloc.c (very_old_free_lcrecord):
* alloc.c (copy_lisp_object):
* alloc.c (zero_sized_lisp_object):
* alloc.c (zero_nonsized_lisp_object):
* alloc.c (lisp_object_storage_size):
* alloc.c (free_normal_lisp_object):
* alloc.c (FREE_FIXED_TYPE_WHEN_NOT_IN_GC):
* alloc.c (ALLOC_FROB_BLOCK_LISP_OBJECT):
* alloc.c (Fcons):
* alloc.c (noseeum_cons):
* alloc.c (make_float):
* alloc.c (make_bignum):
* alloc.c (make_bignum_bg):
* alloc.c (make_ratio):
* alloc.c (make_ratio_bg):
* alloc.c (make_ratio_rt):
* alloc.c (make_bigfloat):
* alloc.c (make_bigfloat_bf):
* alloc.c (size_vector):
* alloc.c (make_compiled_function):
* alloc.c (Fmake_symbol):
* alloc.c (allocate_extent):
* alloc.c (allocate_event):
* alloc.c (make_key_data):
* alloc.c (make_button_data):
* alloc.c (make_motion_data):
* alloc.c (make_process_data):
* alloc.c (make_timeout_data):
* alloc.c (make_magic_data):
* alloc.c (make_magic_eval_data):
* alloc.c (make_eval_data):
* alloc.c (make_misc_user_data):
* alloc.c (Fmake_marker):
* alloc.c (noseeum_make_marker):
* alloc.c (size_string_direct_data):
* alloc.c (make_uninit_string):
* alloc.c (make_string_nocopy):
* alloc.c (mark_lcrecord_list):
* alloc.c (alloc_managed_lcrecord):
* alloc.c (free_managed_lcrecord):
* alloc.c (sweep_lcrecords_1):
* alloc.c (malloced_storage_size):
* buffer.c (allocate_buffer):
* buffer.c (compute_buffer_usage):
* buffer.c (DEFVAR_BUFFER_LOCAL_1):
* buffer.c (nuke_all_buffer_slots):
* buffer.c (common_init_complex_vars_of_buffer):
* buffer.h (struct buffer_text):
* buffer.h (struct buffer):
* bytecode.c:
* bytecode.c (make_compiled_function_args):
* bytecode.c (size_compiled_function_args):
* bytecode.h (struct compiled_function_args):
* casetab.c (allocate_case_table):
* casetab.h (struct Lisp_Case_Table):
* charset.h (struct Lisp_Charset):
* chartab.c (fill_char_table):
* chartab.c (Fmake_char_table):
* chartab.c (make_char_table_entry):
* chartab.c (copy_char_table_entry):
* chartab.c (Fcopy_char_table):
* chartab.c (put_char_table):
* chartab.h (struct Lisp_Char_Table_Entry):
* chartab.h (struct Lisp_Char_Table):
* console-gtk-impl.h (struct gtk_device):
* console-gtk-impl.h (struct gtk_frame):
* console-impl.h (struct console):
* console-msw-impl.h (struct Lisp_Devmode):
* console-msw-impl.h (struct mswindows_device):
* console-msw-impl.h (struct msprinter_device):
* console-msw-impl.h (struct mswindows_frame):
* console-msw-impl.h (struct mswindows_dialog_id):
* console-stream-impl.h (struct stream_console):
* console-stream.c (stream_init_console):
* console-tty-impl.h (struct tty_console):
* console-tty-impl.h (struct tty_device):
* console-tty.c (allocate_tty_console_struct):
* console-x-impl.h (struct x_device):
* console-x-impl.h (struct x_frame):
* console.c (allocate_console):
* console.c (nuke_all_console_slots):
* console.c (DEFVAR_CONSOLE_LOCAL_1):
* console.c (common_init_complex_vars_of_console):
* data.c (make_weak_list):
* data.c (make_weak_box):
* data.c (make_ephemeron):
* database.c:
* database.c (struct Lisp_Database):
* database.c (allocate_database):
* database.c (finalize_database):
* device-gtk.c (allocate_gtk_device_struct):
* device-impl.h (struct device):
* device-msw.c:
* device-msw.c (mswindows_init_device):
* device-msw.c (msprinter_init_device):
* device-msw.c (finalize_devmode):
* device-msw.c (allocate_devmode):
* device-tty.c (allocate_tty_device_struct):
* device-x.c (allocate_x_device_struct):
* device.c:
* device.c (nuke_all_device_slots):
* device.c (allocate_device):
* dialog-msw.c (handle_question_dialog_box):
* elhash.c:
* elhash.c (struct Lisp_Hash_Table):
* elhash.c (finalize_hash_table):
* elhash.c (make_general_lisp_hash_table):
* elhash.c (Fcopy_hash_table):
* elhash.h (htentry):
* emacs.c (main_1):
* eval.c:
* eval.c (size_multiple_value):
* event-stream.c (finalize_command_builder):
* event-stream.c (allocate_command_builder):
* event-stream.c (free_command_builder):
* event-stream.c (event_stream_generate_wakeup):
* event-stream.c (event_stream_resignal_wakeup):
* event-stream.c (event_stream_disable_wakeup):
* event-stream.c (event_stream_wakeup_pending_p):
* events.h (struct Lisp_Timeout):
* events.h (struct command_builder):
* extents-impl.h:
* extents-impl.h (struct extent_auxiliary):
* extents-impl.h (struct extent_info):
* extents-impl.h (set_extent_no_chase_aux_field):
* extents-impl.h (set_extent_no_chase_normal_field):
* extents.c:
* extents.c (gap_array_marker):
* extents.c (gap_array):
* extents.c (extent_list_marker):
* extents.c (extent_list):
* extents.c (stack_of_extents):
* extents.c (gap_array_make_marker):
* extents.c (extent_list_make_marker):
* extents.c (allocate_extent_list):
* extents.c (SLOT):
* extents.c (mark_extent_auxiliary):
* extents.c (allocate_extent_auxiliary):
* extents.c (attach_extent_auxiliary):
* extents.c (size_gap_array):
* extents.c (finalize_extent_info):
* extents.c (allocate_extent_info):
* extents.c (uninit_buffer_extents):
* extents.c (allocate_soe):
* extents.c (copy_extent):
* extents.c (vars_of_extents):
* extents.h:
* faces.c (allocate_face):
* faces.h (struct Lisp_Face):
* faces.h (struct face_cachel):
* file-coding.c:
* file-coding.c (finalize_coding_system):
* file-coding.c (sizeof_coding_system):
* file-coding.c (Fcopy_coding_system):
* file-coding.h (struct Lisp_Coding_System):
* file-coding.h (MARKED_SLOT):
* fns.c (size_bit_vector):
* font-mgr.c:
* font-mgr.c (finalize_fc_pattern):
* font-mgr.c (print_fc_pattern):
* font-mgr.c (Ffc_pattern_p):
* font-mgr.c (Ffc_pattern_create):
* font-mgr.c (Ffc_name_parse):
* font-mgr.c (Ffc_name_unparse):
* font-mgr.c (Ffc_pattern_duplicate):
* font-mgr.c (Ffc_pattern_add):
* font-mgr.c (Ffc_pattern_del):
* font-mgr.c (Ffc_pattern_get):
* font-mgr.c (fc_config_create_using):
* font-mgr.c (fc_strlist_to_lisp_using):
* font-mgr.c (fontset_to_list):
* font-mgr.c (Ffc_config_p):
* font-mgr.c (Ffc_config_up_to_date):
* font-mgr.c (Ffc_config_build_fonts):
* font-mgr.c (Ffc_config_get_cache):
* font-mgr.c (Ffc_config_get_fonts):
* font-mgr.c (Ffc_config_set_current):
* font-mgr.c (Ffc_config_get_blanks):
* font-mgr.c (Ffc_config_get_rescan_interval):
* font-mgr.c (Ffc_config_set_rescan_interval):
* font-mgr.c (Ffc_config_app_font_add_file):
* font-mgr.c (Ffc_config_app_font_add_dir):
* font-mgr.c (Ffc_config_app_font_clear):
* font-mgr.c (size):
* font-mgr.c (Ffc_config_substitute):
* font-mgr.c (Ffc_font_render_prepare):
* font-mgr.c (Ffc_font_match):
* font-mgr.c (Ffc_font_sort):
* font-mgr.c (finalize_fc_config):
* font-mgr.c (print_fc_config):
* font-mgr.h:
* font-mgr.h (struct fc_pattern):
* font-mgr.h (XFC_PATTERN):
* font-mgr.h (struct fc_config):
* font-mgr.h (XFC_CONFIG):
* frame-gtk.c (allocate_gtk_frame_struct):
* frame-impl.h (struct frame):
* frame-msw.c (mswindows_init_frame_1):
* frame-x.c (allocate_x_frame_struct):
* frame.c (nuke_all_frame_slots):
* frame.c (allocate_frame_core):
* gc.c:
* gc.c (GC_CHECK_NOT_FREE):
* glyphs.c (finalize_image_instance):
* glyphs.c (allocate_image_instance):
* glyphs.c (Fcolorize_image_instance):
* glyphs.c (allocate_glyph):
* glyphs.c (unmap_subwindow_instance_cache_mapper):
* glyphs.c (register_ignored_expose):
* glyphs.h (struct Lisp_Image_Instance):
* glyphs.h (struct Lisp_Glyph):
* glyphs.h (struct glyph_cachel):
* glyphs.h (struct expose_ignore):
* gui.c (allocate_gui_item):
* gui.h (struct Lisp_Gui_Item):
* keymap.c (struct Lisp_Keymap):
* keymap.c (make_keymap):
* lisp.h:
* lisp.h (struct Lisp_String_Direct_Data):
* lisp.h (struct Lisp_String_Indirect_Data):
* lisp.h (struct Lisp_Vector):
* lisp.h (struct Lisp_Bit_Vector):
* lisp.h (DECLARE_INLINE_LISP_BIT_VECTOR):
* lisp.h (struct weak_box):
* lisp.h (struct ephemeron):
* lisp.h (struct weak_list):
* lrecord.h:
* lrecord.h (struct lrecord_implementation):
* lrecord.h (MC_ALLOC_CALL_FINALIZER):
* lrecord.h (struct lcrecord_list):
* lstream.c (finalize_lstream):
* lstream.c (sizeof_lstream):
* lstream.c (Lstream_new):
* lstream.c (Lstream_delete):
* lstream.h (struct lstream):
* marker.c:
* marker.c (finalize_marker):
* marker.c (compute_buffer_marker_usage):
* mule-charset.c:
* mule-charset.c (make_charset):
* mule-charset.c (compute_charset_usage):
* objects-impl.h (struct Lisp_Color_Instance):
* objects-impl.h (struct Lisp_Font_Instance):
* objects-tty-impl.h (struct tty_color_instance_data):
* objects-tty-impl.h (struct tty_font_instance_data):
* objects-tty.c (tty_initialize_color_instance):
* objects-tty.c (tty_initialize_font_instance):
* objects.c (finalize_color_instance):
* objects.c (Fmake_color_instance):
* objects.c (finalize_font_instance):
* objects.c (Fmake_font_instance):
* objects.c (reinit_vars_of_objects):
* opaque.c:
* opaque.c (sizeof_opaque):
* opaque.c (make_opaque_ptr):
* opaque.c (free_opaque_ptr):
* opaque.h:
* opaque.h (Lisp_Opaque):
* opaque.h (Lisp_Opaque_Ptr):
* print.c (printing_unreadable_lcrecord):
* print.c (external_object_printer):
* print.c (debug_p4):
* process.c (finalize_process):
* process.c (make_process_internal):
* procimpl.h (struct Lisp_Process):
* rangetab.c (Fmake_range_table):
* rangetab.c (Fcopy_range_table):
* rangetab.h (struct Lisp_Range_Table):
* scrollbar.c:
* scrollbar.c (create_scrollbar_instance):
* scrollbar.c (compute_scrollbar_instance_usage):
* scrollbar.h (struct scrollbar_instance):
* specifier.c (finalize_specifier):
* specifier.c (sizeof_specifier):
* specifier.c (set_specifier_caching):
* specifier.h (struct Lisp_Specifier):
* specifier.h (struct specifier_caching):
* symeval.h:
* symeval.h (SYMBOL_VALUE_MAGIC_P):
* symeval.h (DEFVAR_SYMVAL_FWD):
* symsinit.h:
* syntax.c (init_buffer_syntax_cache):
* syntax.h (struct syntax_cache):
* toolbar.c:
* toolbar.c (allocate_toolbar_button):
* toolbar.c (update_toolbar_button):
* toolbar.h (struct toolbar_button):
* tooltalk.c (struct Lisp_Tooltalk_Message):
* tooltalk.c (make_tooltalk_message):
* tooltalk.c (struct Lisp_Tooltalk_Pattern):
* tooltalk.c (make_tooltalk_pattern):
* ui-gtk.c:
* ui-gtk.c (allocate_ffi_data):
* ui-gtk.c (emacs_gtk_object_finalizer):
* ui-gtk.c (allocate_emacs_gtk_object_data):
* ui-gtk.c (allocate_emacs_gtk_boxed_data):
* ui-gtk.h:
* window-impl.h (struct window):
* window-impl.h (struct window_mirror):
* window.c (finalize_window):
* window.c (allocate_window):
* window.c (new_window_mirror):
* window.c (mark_window_as_deleted):
* window.c (make_dummy_parent):
* window.c (compute_window_mirror_usage):
* window.c (compute_window_usage):
Overall point of this change and previous ones in this repository:
(1) Introduce new, clearer terminology: everything other than int
or char is a "record" object, which comes in two types: "normal
objects" and "frob-block objects". Fix up all places that
referred to frob-block objects as "simple", "basic", etc.
(2) Provide an advertised interface for doing operations on Lisp
objects, including creating new types, that is clean and
consistent in its naming, uses the above-referenced terms and
avoids referencing "lrecords", "old lcrecords", etc., which should
hide under the surface.
(3) Make the size_in_bytes and finalizer methods take a
Lisp_Object rather than a void * for consistency with other methods.
(4) Separate finalizer method into finalizer and disksaver, so
that normal finalize methods don't have to worry about disksaving.
Other specifics:
(1) Renaming:
LISP_OBJECT_HEADER -> NORMAL_LISP_OBJECT_HEADER
ALLOC_LISP_OBJECT -> ALLOC_NORMAL_LISP_OBJECT
implementation->basic_p -> implementation->frob_block_p
ALLOCATE_FIXED_TYPE_AND_SET_IMPL -> ALLOC_FROB_BLOCK_LISP_OBJECT
*FCCONFIG*, wrap_fcconfig -> *FC_CONFIG*, wrap_fc_config
*FCPATTERN*, wrap_fcpattern -> *FC_PATTERN*, wrap_fc_pattern
(the last two changes make the naming of these macros consistent
with the naming of all other macros, since the objects are named
fc-config and fc-pattern with a hyphen)
(2) Lots of documentation fixes in lrecord.h.
(3) Eliminate macros for copying, freeing, zeroing objects, getting
their storage size. Instead, new functions:
zero_sized_lisp_object()
zero_nonsized_lisp_object()
lisp_object_storage_size()
free_normal_lisp_object()
(copy_lisp_object() already exists)
LISP_OBJECT_FROB_BLOCK_P() (actually a macro)
Eliminated:
free_lrecord()
zero_lrecord()
copy_lrecord()
copy_sized_lrecord()
old_copy_lcrecord()
old_copy_sized_lcrecord()
old_zero_lcrecord()
old_zero_sized_lcrecord()
LISP_OBJECT_STORAGE_SIZE()
COPY_SIZED_LISP_OBJECT()
COPY_SIZED_LCRECORD()
COPY_LISP_OBJECT()
ZERO_LISP_OBJECT()
FREE_LISP_OBJECT()
(4) Catch the remaining places where lrecord stuff was used directly
and use the advertised interface, e.g. alloc_sized_lrecord() ->
ALLOC_SIZED_LISP_OBJECT().
(5) Make certain statically-declared pseudo-objects
(buffer_local_flags, console_local_flags) have their lheader
initialized correctly, so things like copy_lisp_object() can work
on them. Make extent_auxiliary_defaults a proper heap object
Vextent_auxiliary_defaults, and make extent auxiliaries dumpable
so that this object can be dumped. allocate_extent_auxiliary()
now just creates the object, and attach_extent_auxiliary()
creates an extent auxiliary and attaches to an extent, like the
old allocate_extent_auxiliary().
(6) Create EXTENT_AUXILIARY_SLOTS macro, similar to the foo-slots.h
files but in a macro instead of a file. The purpose is to avoid
duplication when iterating over all the slots in an extent auxiliary.
Use it.
(7) In lstream.c, don't zero out object after allocation because
allocation routines take care of this.
(8) In marker.c, fix a mistake in computing marker overhead.
(9) In print.c, clean up printing_unreadable_lcrecord(),
external_object_printer() to avoid lots of ifdef NEW_GC's.
(10) Separate toolbar-button allocation into a separate
allocate_toolbar_button() function for use in the example code
in lrecord.h.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Fri, 05 Mar 2010 04:08:17 -0600 |
parents | 6f2158fa75ed |
children | 88bd4f3ef8e4 |
line wrap: on
line source
/* This file is part of XEmacs. XEmacs is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. XEmacs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with XEmacs; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Synched up with: Not in FSF. */ /* Debugging hooks for malloc. */ /* These hooks work with gmalloc to catch allocation errors. In particular, the following is trapped: * Freeing the same pointer twice. * Trying to free a pointer not returned by malloc. * Trying to realloc a pointer not returned by malloc. In addition, every word of every block freed is set to 0xdeadbeef (-559038737). This causes many uses of freed storage to be trapped or recognized. When you use this, the storage used by the last FREE_QUEUE_LIMIT calls to free() is not recycled. When you call free for the Nth time, the (N - FREE_QUEUE_LIMIT)'th block is actually recycled. For these last FREE_QUEUE_LIMIT calls to free() a backtrace is saved showing where it was called from. The function find_backtrace() is provided here to be called from GDB with a pointer (such as would be passed to free()) as argument, e.g. (gdb) p/a *find_backtrace (0x234000). If SAVE_ARGS is defined, the first three arguments to each function are saved as well as the return addresses. If UNMAPPED_FREE is defined, instead of setting every word of freed storage to 0xdeadbeef, every call to malloc goes on its own page(s). When free() is called, the block is read and write protected. This is very useful when debugging, since it usually generates a bus error when the deadbeef hack might only cause some garbage to be printed. However, this is too slow for everyday use, since it takes an enormous number of pages. Some other features that would be useful are: * Checking for storage leaks. This could be done by a GC-like facility that would scan the data segment looking for pointers to allocated storage and tell you about those that are no longer referenced. This could be invoked at any time. Another possibility is to report on what allocated storage is still in use when the process is exited. Typically there will be a large amount, so this might not be very useful. */ #ifdef emacs #include <config.h> #include "lisp.h" #else void *malloc (size_t); #endif #if !defined(HAVE_LIBMCHECK) #include <stdio.h> #include "hash.h" #ifdef UNMAPPED_FREE #include <sys/mman.h> #include <sys/param.h> #define ROUND_UP_TO_PAGE(i) (((i) + PAGEOFFSET) & PAGEMASK) #endif #include <sys/types.h> /* System function prototypes don't belong in C source files */ /* extern void free (void *); */ static struct hash_table *pointer_table; extern void (*__free_hook) (void *); extern void *(*__malloc_hook) (size_t); static void *check_malloc (size_t); typedef void (*fun_ptr) (void); /* free_queue is not too useful without backtrace logging */ #define FREE_QUEUE_LIMIT 1 #define TRACE_LIMIT 20 typedef struct { fun_ptr return_pc; #ifdef SAVE_ARGS void *arg[3]; #endif } fun_entry; typedef struct { void *address; unsigned long length; } free_queue_entry; static free_queue_entry free_queue[FREE_QUEUE_LIMIT]; static int current_free; static int strict_free_check; static void check_free (void *ptr) { __free_hook = 0; __malloc_hook = 0; if (!pointer_table) pointer_table = make_hash_table (max (100, FREE_QUEUE_LIMIT * 2)); if (ptr != 0) { long size; #ifdef UNMAPPED_FREE unsigned long rounded_up_size; #endif EMACS_INT present = (EMACS_INT) gethash (ptr, pointer_table, (const void **) (void *) &size); if (!present) { /* This can only happen if you try to free something that didn't come from malloc */ #if !defined(__linux__) /* I originally wrote: "There's really no need to drop core." I have seen the error of my ways. -slb */ assert (!strict_free_check); #endif printf("Freeing unmalloc'ed memory at %p\n", ptr); __free_hook = check_free; __malloc_hook = check_malloc; goto end; } if (size < 0) { /* This happens when you free twice */ #if !defined(__linux__) /* See above comment. */ assert (!strict_free_check); #endif printf("Freeing %p twice\n", ptr); __free_hook = check_free; __malloc_hook = check_malloc; goto end; } puthash (ptr, (void *)-size, pointer_table); #ifdef UNMAPPED_FREE /* Round up size to an even number of pages. */ rounded_up_size = ROUND_UP_TO_PAGE (size); /* Protect the pages freed from all access */ if (strict_free_check) mprotect (ptr, rounded_up_size, PROT_NONE); #else /* Set every word in the block to 0xdeadbeef */ if (strict_free_check) { unsigned long long_length = (size + (sizeof (long) - 1)) / sizeof (long); unsigned long i; /* Not using the DEADBEEF_CONSTANT #define, since we don't know * that allocation sizes will be multiples of eight. */ for (i = 0; i < long_length; i++) ((unsigned long *) ptr)[i] = 0xdeadbeef; } #endif free_queue[current_free].address = ptr; free_queue[current_free].length = size; current_free++; if (current_free >= FREE_QUEUE_LIMIT) current_free = 0; /* Really free this if there's something there */ { void *old = free_queue[current_free].address; if (old) { #ifdef UNMAPPED_FREE unsigned long old_len = free_queue[current_free].length; mprotect (old, old_len, PROT_READ | PROT_WRITE | PROT_EXEC); #endif free (old); remhash (old, pointer_table); } } } __free_hook = check_free; __malloc_hook = check_malloc; end: return; } static void * check_malloc (size_t size) { size_t rounded_up_size; void *result; __free_hook = 0; __malloc_hook = 0; if (size == 0) { result = 0; goto end; } #ifdef UNMAPPED_FREE /* Round up to an even number of pages. */ rounded_up_size = ROUND_UP_TO_PAGE (size); #else rounded_up_size = size; #endif result = malloc (rounded_up_size); if (!pointer_table) pointer_table = make_hash_table (FREE_QUEUE_LIMIT * 2); puthash (result, (void *)size, pointer_table); __free_hook = check_free; __malloc_hook = check_malloc; end: return result; } extern void *(*__realloc_hook) (void *, size_t); #ifdef MIN #undef MIN #endif #define MIN(A, B) ((A) < (B) ? (A) : (B)) /* Don't optimize realloc */ static void * check_realloc (void * ptr, size_t size) { EMACS_INT present; size_t old_size; void *result = malloc (size); if (!ptr) return result; present = (EMACS_INT) gethash (ptr, pointer_table, (const void **) &old_size); if (!present) { /* This can only happen by reallocing a pointer that didn't come from malloc. */ #if !defined(__linux__) /* see comment in check_free(). */ ABORT (); #endif printf("Realloc'ing unmalloc'ed pointer at %p\n", ptr); } if (result == 0) goto end; memcpy (result, ptr, MIN (size, old_size)); free (ptr); end: return result; } void enable_strict_free_check (void); void enable_strict_free_check (void) { strict_free_check = 1; } void disable_strict_free_check (void); void disable_strict_free_check (void) { strict_free_check = 0; } /* Note: All BLOCK_INPUT stuff removed from this file because it's completely gone in XEmacs */ static void * block_input_malloc (size_t size); static void block_input_free (void* ptr) { __free_hook = 0; __malloc_hook = 0; free (ptr); __free_hook = block_input_free; __malloc_hook = block_input_malloc; } static void * block_input_malloc (size_t size) { void* result; __free_hook = 0; __malloc_hook = 0; result = malloc (size); __free_hook = block_input_free; __malloc_hook = block_input_malloc; return result; } static void * block_input_realloc (void* ptr, size_t size) { void* result; __free_hook = 0; __malloc_hook = 0; __realloc_hook = 0; result = realloc (ptr, size); __free_hook = block_input_free; __malloc_hook = block_input_malloc; __realloc_hook = block_input_realloc; return result; } #ifdef emacs void disable_free_hook (void); void disable_free_hook (void) { __free_hook = block_input_free; __malloc_hook = block_input_malloc; __realloc_hook = block_input_realloc; } void init_free_hook (void) { __free_hook = check_free; __malloc_hook = check_malloc; __realloc_hook = check_realloc; current_free = 0; strict_free_check = 1; } void really_free_one_entry (void *, int, int *); DEFUN ("really-free", Freally_free, 0, 1, "P", /* Actually free the storage held by the free() debug hook. A no-op if the free hook is disabled. */ (UNUSED (arg))) { int count[2]; Lisp_Object lisp_count[2]; if ((__free_hook != 0) && pointer_table) { count[0] = 0; count[1] = 0; __free_hook = 0; maphash ((maphash_function)really_free_one_entry, pointer_table, (void *)&count); memset (free_queue, 0, sizeof (free_queue_entry) * FREE_QUEUE_LIMIT); current_free = 0; __free_hook = check_free; lisp_count[0] = make_int (count[0]); lisp_count[1] = make_int (count[1]); return Fcons (lisp_count[0], lisp_count[1]); } else return Fcons (make_int (0), make_int (0)); } void really_free_one_entry (void *key, int contents, int *countp) { if (contents < 0) { free (key); #ifdef UNMAPPED_FREE mprotect (key, -contents, PROT_READ | PROT_WRITE | PROT_EXEC); #endif remhash (key, pointer_table); countp[0]++; countp[1] += -contents; } } void syms_of_free_hook (void) { DEFSUBR (Freally_free); } #else void (*__free_hook)(void *) = check_free; void *(*__malloc_hook)(size_t) = check_malloc; void *(*__realloc_hook)(void *, size_t) = check_realloc; #endif #endif /* !defined(HAVE_LIBMCHECK) */ #if defined(DEBUG_INPUT_BLOCKING) || defined (DEBUG_GCPRO) /* Note: There is no more input blocking in XEmacs */ typedef enum { block_type, unblock_type, totally_type, gcpro1_type, gcpro2_type, gcpro3_type, gcpro4_type, gcpro5_type, ungcpro_type } blocktype; struct block_input_history_struct { char *file; int line; blocktype type; int value; }; typedef struct block_input_history_struct block_input_history; #endif /* DEBUG_INPUT_BLOCKING || DEBUG_GCPRO */ #ifdef DEBUG_INPUT_BLOCKING int blhistptr; #define BLHISTLIMIT 1000 block_input_history blhist[BLHISTLIMIT]; note_block_input (char *file, int line) { note_block (file, line, block_type); assert (interrupt_input_blocked <= 2); } note_unblock_input (char* file, int line) { note_block (file, line, unblock_type); } note_totally_unblocked (char* file, int line) { note_block (file, line, totally_type); } note_block (char *file, int line, blocktype type) { blhist[blhistptr].file = file; blhist[blhistptr].line = line; blhist[blhistptr].type = type; blhist[blhistptr].value = interrupt_input_blocked; blhistptr++; if (blhistptr >= BLHISTLIMIT) blhistptr = 0; } #endif /* DEBUG_INPUT_BLOCKING */ #ifdef DEBUG_GCPRO int gcprohistptr; #define GCPROHISTLIMIT 1000 block_input_history gcprohist[GCPROHISTLIMIT]; static void log_gcpro (char *file, int line, struct gcpro *value, blocktype type) { if (type == ungcpro_type) { if (value == gcprolist) goto OK; assert (gcprolist); if (value == gcprolist->next) goto OK; assert (gcprolist->next); if (value == gcprolist->next->next) goto OK; assert (gcprolist->next->next); if (value == gcprolist->next->next->next) goto OK; assert (gcprolist->next->next->next); if (value == gcprolist->next->next->next->next) goto OK; ABORT (); OK:; } gcprohist[gcprohistptr].file = file; gcprohist[gcprohistptr].line = line; gcprohist[gcprohistptr].type = type; gcprohist[gcprohistptr].value = (int) value; gcprohistptr++; if (gcprohistptr >= GCPROHISTLIMIT) gcprohistptr = 0; } void debug_gcpro1 (char *file, int line, struct gcpro *gcpro1, Lisp_Object *var) { gcpro1->next = gcprolist; gcpro1->var = var; gcpro1->nvars = 1; gcprolist = gcpro1; log_gcpro (file, line, gcpro1, gcpro1_type); } void debug_gcpro2 (char *file, int line, struct gcpro *gcpro1, struct gcpro *gcpro2, Lisp_Object *var1, Lisp_Object *var2) { gcpro1->next = gcprolist; gcpro1->var = var1; gcpro1->nvars = 1; gcpro2->next = gcpro1; gcpro2->var = var2; gcpro2->nvars = 1; gcprolist = gcpro2; log_gcpro (file, line, gcpro2, gcpro2_type); } void debug_gcpro3 (char *file, int line, struct gcpro *gcpro1, struct gcpro *gcpro2, struct gcpro *gcpro3, Lisp_Object *var1, Lisp_Object *var2, Lisp_Object *var3) { gcpro1->next = gcprolist; gcpro1->var = var1; gcpro1->nvars = 1; gcpro2->next = gcpro1; gcpro2->var = var2; gcpro2->nvars = 1; gcpro3->next = gcpro2; gcpro3->var = var3; gcpro3->nvars = 1; gcprolist = gcpro3; log_gcpro (file, line, gcpro3, gcpro3_type); } void debug_gcpro4 (char *file, int line, struct gcpro *gcpro1, struct gcpro *gcpro2, struct gcpro *gcpro3, struct gcpro *gcpro4, Lisp_Object *var1, Lisp_Object *var2, Lisp_Object *var3, Lisp_Object *var4) { log_gcpro (file, line, gcpro4, gcpro4_type); gcpro1->next = gcprolist; gcpro1->var = var1; gcpro1->nvars = 1; gcpro2->next = gcpro1; gcpro2->var = var2; gcpro2->nvars = 1; gcpro3->next = gcpro2; gcpro3->var = var3; gcpro3->nvars = 1; gcpro4->next = gcpro3; gcpro4->var = var4; gcpro4->nvars = 1; gcprolist = gcpro4; } void debug_gcpro5 (char *file, int line, struct gcpro *gcpro1, struct gcpro *gcpro2, struct gcpro *gcpro3, struct gcpro *gcpro4, struct gcpro *gcpro5, Lisp_Object *var1, Lisp_Object *var2, Lisp_Object *var3, Lisp_Object *var4, Lisp_Object *var5) { log_gcpro (file, line, gcpro5, gcpro5_type); gcpro1->next = gcprolist; gcpro1->var = var1; gcpro1->nvars = 1; gcpro2->next = gcpro1; gcpro2->var = var2; gcpro2->nvars = 1; gcpro3->next = gcpro2; gcpro3->var = var3; gcpro3->nvars = 1; gcpro4->next = gcpro3; gcpro4->var = var4; gcpro4->nvars = 1; gcpro5->next = gcpro4; gcpro5->var = var5; gcpro5->nvars = 1; gcprolist = gcpro5; } void debug_ungcpro (char *file, int line, struct gcpro *gcpro1) { log_gcpro (file, line, gcpro1, ungcpro_type); gcprolist = gcpro1->next; } /* To be called from the debugger */ void show_gcprohist (void); void show_gcprohist (void) { int i, j; for (i = 0, j = gcprohistptr; i < GCPROHISTLIMIT; i++, j++) { if (j >= GCPROHISTLIMIT) j = 0; printf ("%3d %s %d %s 0x%x\n", j, gcprohist[j].file, gcprohist[j].line, (gcprohist[j].type == gcpro1_type ? "GCPRO1" : gcprohist[j].type == gcpro2_type ? "GCPRO2" : gcprohist[j].type == gcpro3_type ? "GCPRO3" : gcprohist[j].type == gcpro4_type ? "GCPRO4" : gcprohist[j].type == gcpro5_type ? "GCPRO5" : gcprohist[j].type == ungcpro_type ? "UNGCPRO" : "???"), gcprohist[j].value); } fflush (stdout); } #endif /* DEBUG_GCPRO */