Mercurial > emacs
view src/ralloc.c @ 1787:5e245540d06f
Make scrollbar structures into lisp objects, so that they can be
GC'd; this allows windows and scrollbars can refer to each other
without worrying about dangling pointers.
* xterm.h (struct x_display): vertical_scrollbars and
judge_timestamp members deleted.
(struct scrollbar): Redesigned to be a template for a Lisp_Vector.
(SCROLLBAR_VEC_SIZE, XSCROLLBAR, SCROLLBAR_PACK, SCROLLBAR_UNPACK,
SCROLLBAR_X_WINDOW, SET_SCROLLBAR_X_WINDOW,
VERTICAL_SCROLLBAR_INSIDE_WIDTH, VERTICAL_SCROLLBAR_TOP_RANGE,
VERTICAL_SCROLLBAR_INSIDE_HEIGHT, VERTICAL_SCROLLBAR_MIN_HANDLE):
New macros, to help deal with the lispy structures, and deal with
the graphics.
* frame.h (WINDOW_VERTICAL_SCROLLBAR): Macro deleted.
(struct frame): New fields `scrollbars' and
`condemned_scrollbars', for use by the scrollbar implementation.
[MULTI_FRAME and not MULTI_FRAME] (FRAME_SCROLLBARS,
FRAME_CONDEMNED_SCROLLBARS): Accessors for the new field.
* window.h (struct window): Doc fix for vertical_scrollbar field.
* frame.c (make_frame): Initialize the `scrollbars' and
`condemned_scrollbars' fields of the new frame.
* alloc.c (mark_object): Mark the `scrollbars' and
`condemned_scrollbars' slots of frames.
* xterm.c (x_window_to_scrollbar): Scrollbars are chained on
frames' scrollbar field, not their x.display->vertical_scrollbars
field.
(x_scrollbar_create, x_scrollbar_set_handle, x_scrollbar_move,
x_scrollbar_remove, XTset_vertical_scrollbar,
XTcondemn_scrollbars, XTredeem_scrollbar, XTjudge_scrollbars,
x_scrollbar_expose, x_scrollbar_handle_click,
x_scrollbar_handle_motion): Substantially rewritten to correct
typos and brainos, and to accomodate the lispy structures.
* xterm.c (x_scrollbar_background_expose): Function deleted; we
don't want anything in the background there after all.
(XTread_socket): Don't call x_scrollbar_background_expose. We
don't care.
* xterm.h (CHAR_TO_PIXEL_WIDTH, CHAR_TO_PIXEL_HEIGHT,
PIXEL_TO_CHAR_WIDTH, PIXEL_TO_CHAR_HEIGHT): Rewritten, using:
(CHAR_TO_PIXEL_ROW, CHAR_TO_PIXEL_COL, PIXEL_TO_CHAR_ROW,
PIXEL_TO_CHAR_COL): New macros.
* xfns.c [not HAVE_X11] (Fx_create_frame): Use the
PIXEL_TO_CHAR_{HEIGHT,WIDTH} macros to figure the frame's
character size, and the CHAR_TO_PIXEL* macros for vice versa.
* xterm.c (XTwrite_glyphs, XTclear_end_of_line, stufflines,
scraplines, dumprectangle, pixel_to_glyph_coords, x_draw_box,
clear_cursor, x_display_bar_cursor, x_draw_single_glyph,
x_set_mouse_position): Use the CHAR_TO_PIXEL_* macros.
* xterm.c (x_wm_set_size_hint): The max_width and max_height
members of the size_hints are expressed in pixels, not columns.
* xterm.c (x_set_window_size): Remove ibw var; it's not used.
Set FRAME_WIDTH (f) to cols instead of rows. Duh.
* xterm.c (pixel_to_glyph_coords): Properly set *bounds to the
character cell bounding the position, even when the position is
off the frame.
* termhooks.h (mouse_position_hook): Doc fix.
(set_vertical_scrollbar_hook): This doesn't return anything any
more, and doesn't take a struct scrollbar * argument any more.
(condemn_scrollbars_hook, redeem_scrollbar_hook,
judge_scrollbars_hook): Doc fixes.
* term.c (mouse_position_hook): Doc fix.
(set_vertical_scrollbar_hook): This doesn't return
anything any more. Doc fixes.
* keyboard.c (kbd_buffer_get_event): Receive the scrollbar's
window from *mouse_position_hook and pass it to
make_lispy_movement, instead of working with a pointer to a struct
scrollbar.
(make_lispy_event): We don't need a window_from_scrollbar function
anymore; we are given the window directly in *EVENT.
Unify the code which generates
text-area mouse clicks and scrollbar clicks; use the same code to
distinguish clicks from drags on the scrollbar as in the text area.
Distinguish clicks from drags by storing a copy of the lispy
position list returned as part of the event.
(button_down_location): Make this a lisp vector, rather than an
array of random structures.
(struct mouse_position): Remove this; it's been replaced by a lisp
list.
(make_lispy_movement): Accept the scrollbar's window as a
parameter, rather than the scrollbar itself.
If FRAME is zero, assume that the other arguments are garbage.
(syms_of_keyboard): No need to staticpro each window of
button_down_location now; just initialize and staticpro it.
* window.c (window_from_scrollbar): Function deleted; no longer
needed.
* xdisp.c (redisplay_window): Just pass the window to
set_vertical_scrollbar hook; don't pass the scrollbar object too.
* xterm.c (XTmouse_position): Don't return a pointer to the
scrollbar for scrollbar motion; instead, return the scrollbar's
window.
* xterm.c (XTmouse_position): Entirely rewritten, using
XTranslateCoordinates. Call x_scrollbar_report_motion to handle
scrollbar movement events.
(x_scrollbar_report_motion): New function, to help out
XTmouse_position.
* termhooks.h (struct input_event): Replace the frame member with
a Lisp_Object member by the name of frame_or_window. Doc fixes.
Remove the scrollbar member; instead, use frame_or_window to hold the
window whose scrollbar was clicked.
* keyboard.c (kbd_buffer_store_event, kbd_buffer_get_event,
make_lispy_event): Adjust references to frame member of struct
input_event to use frame_or_window now.
* xterm.c (construct_mouse_click, XTread_socket): Same.
* xterm.c (last_mouse_bar, last_mouse_bar_frame, last_mouse_part,
last_mouse_scroll_range_start, last_mouse_scroll_range_end):
Replaced with...
(last_mouse_scrollbar): New variable.
(note_mouse_movement): Clear last_mouse_scrollbar when we have
receieved a new motion.
(syms_of_xterm): Staticpro last_mouse_scrollbar.
* xterm.c (note_mouse_position): Renamed to note_mouse_movement,
because that's what it really does.
(x_scrollbar_handle_motion): Renamed to x_scrollbar_note_movement,
for consistency.
(XTread_socket): Adjusted.
* xterm.c (XTset_scrollbar): Renamed to XTset_vertical_scrollbar.
(x_term_init): Adjusted.
* emacs.c (shut_down_emacs): New function.
(fatal_error_signal, Fkill_emacs): Call it, instead of writing it out.
* xterm.c (x_connection_closed): Call shut_down_emacs instead of
Fkill_emacs; the latter will try to perform operations on the X
server and die a horrible death.
* lisp.h (shut_down_emacs): Add extern declaration for it.
* xterm.c (x_error_quitter): Move the abort call to after we print
the error message. No harm in that.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Thu, 14 Jan 1993 15:34:14 +0000 |
parents | 5c3898c5aa2d |
children | e94a593c3952 |
line wrap: on
line source
/* Block-relocating memory allocator. Copyright (C) 1992 Free Software Foundation, Inc. This file is part of GNU Emacs. GNU Emacs 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 1, or (at your option) any later version. GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ /* NOTES: Only relocate the blocs neccessary for SIZE in r_alloc_sbrk, rather than all of them. This means allowing for a possible hole between the first bloc and the end of malloc storage. */ #ifdef emacs #include "config.h" #include "lisp.h" /* Needed for VALBITS. */ #undef NULL /* The important properties of this type are that 1) it's a pointer, and 2) arithmetic on it should work as if the size of the object pointed to has a size of 1. */ #ifdef __STDC__ typedef void *POINTER; #else #ifdef HAVE_CONFIG_H #include "config.h" #endif typedef char *POINTER; #endif typedef unsigned long SIZE; /* Declared in dispnew.c, this version doesn't screw up if regions overlap. */ extern void safe_bcopy (); #include "getpagesize.h" #else /* Not emacs. */ #include <stddef.h> typedef size_t SIZE; typedef void *POINTER; #include <unistd.h> #include <malloc.h> #include <string.h> #define safe_bcopy(x, y, z) memmove (y, x, z) #endif /* emacs. */ #define NIL ((POINTER) 0) /* A flag to indicate whether we have initialized ralloc yet. For Emacs's sake, please do not make this local to malloc_init; on some machines, the dumping procedure makes all static variables read-only. On these machines, the word static is #defined to be the empty string, meaning that r_alloc_initialized becomes an automatic variable, and loses its value each time Emacs is started up. */ static int r_alloc_initialized = 0; static void r_alloc_init (); /* Declarations for working with the malloc, ralloc, and system breaks. */ /* Function to set the real break value. */ static POINTER (*real_morecore) (); /* The break value, as seen by malloc (). */ static POINTER virtual_break_value; /* The break value, viewed by the relocatable blocs. */ static POINTER break_value; /* The REAL (i.e., page aligned) break value of the process. */ static POINTER page_break_value; /* This is the size of a page. We round memory requests to this boundary. */ static int page_size; /* Whenever we get memory from the system, get this many extra bytes. This must be a multiple of page_size. */ static int extra_bytes; /* Macros for rounding. Note that rounding to any value is possible by changing the definition of PAGE. */ #define PAGE (getpagesize ()) #define ALIGNED(addr) (((unsigned int) (addr) & (page_size - 1)) == 0) #define ROUNDUP(size) (((unsigned int) (size) + page_size - 1) & ~(page_size - 1)) #define ROUND_TO_PAGE(addr) (addr & (~(page_size - 1))) /* Functions to get and return memory from the system. */ /* Obtain SIZE bytes of space. If enough space is not presently available in our process reserve, (i.e., (page_break_value - break_value)), this means getting more page-aligned space from the system. Return non-zero if all went well, or zero if we couldn't allocate the memory. */ static int obtain (size) SIZE size; { SIZE already_available = page_break_value - break_value; if (already_available < size) { SIZE get = ROUNDUP (size - already_available); /* Get some extra, so we can come here less often. */ get += extra_bytes; if ((*real_morecore) (get) == 0) return 0; page_break_value += get; } break_value += size; return 1; } /* Obtain SIZE bytes of space and return a pointer to the new area. If we could not allocate the space, return zero. */ static POINTER get_more_space (size) SIZE size; { POINTER ptr = break_value; if (obtain (size)) return ptr; else return 0; } /* Note that SIZE bytes of space have been relinquished by the process. If SIZE is more than a page, return the space to the system. */ static void relinquish (size) SIZE size; { POINTER new_page_break; int excess; break_value -= size; new_page_break = (POINTER) ROUNDUP (break_value); excess = (char *) page_break_value - (char *) new_page_break; if (excess > extra_bytes * 2) { /* Keep extra_bytes worth of empty space. And don't free anything unless we can free at least extra_bytes. */ if ((*real_morecore) (extra_bytes - excess) == 0) abort (); page_break_value += extra_bytes - excess; } /* Zero the space from the end of the "official" break to the actual break, so that bugs show up faster. */ bzero (break_value, ((char *) page_break_value - (char *) break_value)); } /* The meat - allocating, freeing, and relocating blocs. */ /* These structures are allocated in the malloc arena. The linked list is kept in order of increasing '.data' members. The data blocks abut each other; if b->next is non-nil, then b->data + b->size == b->next->data. */ typedef struct bp { struct bp *next; struct bp *prev; POINTER *variable; POINTER data; SIZE size; } *bloc_ptr; #define NIL_BLOC ((bloc_ptr) 0) #define BLOC_PTR_SIZE (sizeof (struct bp)) /* Head and tail of the list of relocatable blocs. */ static bloc_ptr first_bloc, last_bloc; /* Find the bloc referenced by the address in PTR. Returns a pointer to that block. */ static bloc_ptr find_bloc (ptr) POINTER *ptr; { register bloc_ptr p = first_bloc; while (p != NIL_BLOC) { if (p->variable == ptr && p->data == *ptr) return p; p = p->next; } return p; } /* Allocate a bloc of SIZE bytes and append it to the chain of blocs. Returns a pointer to the new bloc, or zero if we couldn't allocate memory for the new block. */ static bloc_ptr get_bloc (size) SIZE size; { register bloc_ptr new_bloc; if (! (new_bloc = (bloc_ptr) malloc (BLOC_PTR_SIZE)) || ! (new_bloc->data = get_more_space (size))) { if (new_bloc) free (new_bloc); return 0; } new_bloc->size = size; new_bloc->next = NIL_BLOC; new_bloc->variable = (POINTER *) NIL; if (first_bloc) { new_bloc->prev = last_bloc; last_bloc->next = new_bloc; last_bloc = new_bloc; } else { first_bloc = last_bloc = new_bloc; new_bloc->prev = NIL_BLOC; } return new_bloc; } /* Relocate all blocs from BLOC on upward in the list to the zone indicated by ADDRESS. Direction of relocation is determined by the position of ADDRESS relative to BLOC->data. If BLOC is NIL_BLOC, nothing is done. Note that ordering of blocs is not affected by this function. */ static void relocate_some_blocs (bloc, address) bloc_ptr bloc; POINTER address; { if (bloc != NIL_BLOC) { register SIZE offset = address - bloc->data; register SIZE data_size = 0; register bloc_ptr b; for (b = bloc; b != NIL_BLOC; b = b->next) { data_size += b->size; b->data += offset; *b->variable = b->data; } safe_bcopy (address - offset, address, data_size); } } /* Free BLOC from the chain of blocs, relocating any blocs above it and returning BLOC->size bytes to the free area. */ static void free_bloc (bloc) bloc_ptr bloc; { if (bloc == first_bloc && bloc == last_bloc) { first_bloc = last_bloc = NIL_BLOC; } else if (bloc == last_bloc) { last_bloc = bloc->prev; last_bloc->next = NIL_BLOC; } else if (bloc == first_bloc) { first_bloc = bloc->next; first_bloc->prev = NIL_BLOC; } else { bloc->next->prev = bloc->prev; bloc->prev->next = bloc->next; } relocate_some_blocs (bloc->next, bloc->data); relinquish (bloc->size); free (bloc); } /* Interface routines. */ static int use_relocatable_buffers; /* Obtain SIZE bytes of storage from the free pool, or the system, as necessary. If relocatable blocs are in use, this means relocating them. This function gets plugged into the GNU malloc's __morecore hook. We provide hysteresis, never relocating by less than extra_bytes. If we're out of memory, we should return zero, to imitate the other __morecore hook values - in particular, __default_morecore in the GNU malloc package. */ POINTER r_alloc_sbrk (size) long size; { /* This is the first address not currently available for the heap. */ POINTER top; /* Amount of empty space below that. */ SIZE already_available; POINTER ptr; if (! use_relocatable_buffers) return (*real_morecore) (size); top = first_bloc ? first_bloc->data : page_break_value; already_available = (char *) top - (char *) virtual_break_value; /* Do we not have enough gap already? */ if (size > 0 && already_available < size) { /* Get what we need, plus some extra so we can come here less often. */ SIZE get = size - already_available + extra_bytes; if (! obtain (get)) return 0; if (first_bloc) relocate_some_blocs (first_bloc, first_bloc->data + get); /* Zero out the space we just allocated, to help catch bugs quickly. */ bzero (virtual_break_value, get); } /* Can we keep extra_bytes of gap while freeing at least extra_bytes? */ else if (size < 0 && already_available - size > 2 * extra_bytes) { /* Ok, do so. This is how many to free. */ SIZE give_back = already_available - size - extra_bytes; if (first_bloc) relocate_some_blocs (first_bloc, first_bloc->data - give_back); relinquish (give_back); } ptr = virtual_break_value; virtual_break_value += size; return ptr; } /* Allocate a relocatable bloc of storage of size SIZE. A pointer to the data is returned in *PTR. PTR is thus the address of some variable which will use the data area. If we can't allocate the necessary memory, set *PTR to zero, and return zero. */ POINTER r_alloc (ptr, size) POINTER *ptr; SIZE size; { register bloc_ptr new_bloc; if (! r_alloc_initialized) r_alloc_init (); new_bloc = get_bloc (size); if (new_bloc) { new_bloc->variable = ptr; *ptr = new_bloc->data; } else *ptr = 0; return *ptr; } /* Free a bloc of relocatable storage whose data is pointed to by PTR. Store 0 in *PTR to show there's no block allocated. */ void r_alloc_free (ptr) register POINTER *ptr; { register bloc_ptr dead_bloc; dead_bloc = find_bloc (ptr); if (dead_bloc == NIL_BLOC) abort (); free_bloc (dead_bloc); *ptr = 0; } /* Given a pointer at address PTR to relocatable data, resize it to SIZE. Do this by shifting all blocks above this one up in memory, unless SIZE is less than or equal to the current bloc size, in which case do nothing. Change *PTR to reflect the new bloc, and return this value. If more memory cannot be allocated, then leave *PTR unchanged, and return zero. */ POINTER r_re_alloc (ptr, size) POINTER *ptr; SIZE size; { register bloc_ptr bloc; bloc = find_bloc (ptr); if (bloc == NIL_BLOC) abort (); if (size <= bloc->size) /* Wouldn't it be useful to actually resize the bloc here? */ return *ptr; if (! obtain (size - bloc->size)) return 0; relocate_some_blocs (bloc->next, bloc->data + size); /* Zero out the new space in the bloc, to help catch bugs faster. */ bzero (bloc->data + bloc->size, size - bloc->size); /* Indicate that this block has a new size. */ bloc->size = size; return *ptr; } /* The hook `malloc' uses for the function which gets more space from the system. */ extern POINTER (*__morecore) (); /* Intialize various things for memory allocation. */ static void r_alloc_init () { if (r_alloc_initialized) return; r_alloc_initialized = 1; real_morecore = __morecore; __morecore = r_alloc_sbrk; virtual_break_value = break_value = (*real_morecore) (0); if (break_value == NIL) abort (); page_size = PAGE; extra_bytes = ROUNDUP (50000); page_break_value = (POINTER) ROUNDUP (break_value); /* Clear the rest of the last page; this memory is in our address space even though it is after the sbrk value. */ bzero (break_value, (page_break_value - break_value)); use_relocatable_buffers = 1; }