From 46d33884f2f1061ae1a77c26928f97d573e57c6c Mon Sep 17 00:00:00 2001 From: rcyeske Date: Fri, 2 Mar 2001 06:06:23 +0000 Subject: * ratpoison.h (xmalloc, xrealloc, fatal): Prototype. * main.c (xmalloc): Move here from sbuf.c. (xrealloc): Likewise. (fatal): Likewise. * number.c (find_empty_cell): Use xrealloc, remove error check. (init_numbers): Likewise. * manage.c (get_window_name): Likewise. * main.c (main): Likewise. * list.c (add_to_window_list): Likewise. (add_to_window_list): Likewise. * events.c (handle_key): Likewise. * input.c (keysym_to_string): Likewise. (get_more_input): Use xrealloc, remove error check. * main.c (read_rc_file): Take a file pointer rather than a filename. (read_startup_files): If ~/.ratpoisonrc is not readable try /etc/ratpoisonrc. --- src/ChangeLog | 21 +++++++++++++++++++++ src/events.c | 8 ++------ src/input.c | 21 +++------------------ src/list.c | 26 ++++++-------------------- src/main.c | 40 +++++++++++++++++++++++++++++++--------- src/manage.c | 7 ++----- src/number.c | 15 ++------------- src/ratpoison.h | 4 ++++ src/sbuf.c | 31 ------------------------------- 9 files changed, 71 insertions(+), 102 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 8de2f21..21151e8 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,5 +1,26 @@ 2001-03-01 Ryan Yeske + * ratpoison.h (xmalloc, xrealloc, fatal): Prototype. + + * main.c (xmalloc): Move here from sbuf.c. + (xrealloc): Likewise. + (fatal): Likewise. + + * number.c (find_empty_cell): Use xrealloc, remove error check. + (init_numbers): Likewise. + + * manage.c (get_window_name): Likewise. + + * main.c (main): Likewise. + + * list.c (add_to_window_list): Likewise. + (add_to_window_list): Likewise. + + * events.c (handle_key): Likewise. + + * input.c (keysym_to_string): Likewise. + (get_more_input): Use xrealloc, remove error check. + * actions.c (cmd_source): Open the file. Error report as appropriate. (cmd_windows): Call current_screen() to find the current screen. diff --git a/src/events.c b/src/events.c index 9bc29e6..7e68c14 100644 --- a/src/events.c +++ b/src/events.c @@ -318,12 +318,8 @@ handle_key (screen_info *s) else { keysym_name = keysym_to_string (keysym, mod); - msg = (char *) malloc ( strlen ( keysym_name ) + 20 ); - if ( msg == NULL ) - { - PRINT_ERROR ("Out of memory\n"); - exit (EXIT_FAILURE); - } + msg = (char *) xmalloc ( strlen ( keysym_name ) + 20 ); + snprintf (msg, strlen (keysym_name) + 13, "%s unbound key!", keysym_name); free (keysym_name); diff --git a/src/input.c b/src/input.c index 41b4e2e..64725d9 100644 --- a/src/input.c +++ b/src/input.c @@ -43,12 +43,7 @@ keysym_to_string (KeySym keysym, unsigned int modifier) unsigned char *name; int pos, i; - name = malloc (15); - if (name == NULL) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + name = xmalloc (15); for (pos = 0, i = 0; i < 6; i++) { @@ -177,12 +172,7 @@ get_more_input (char *prompt, char *preinput) char *str; /* Allocate some memory to start with */ - str = (char *) malloc ( allocated_len ); - if ( str == NULL ) - { - PRINT_ERROR ("Out of memory\n"); - exit (EXIT_FAILURE); - } + str = (char *) xmalloc ( allocated_len ); /* load in the preinput */ strcpy (str, preinput); @@ -216,12 +206,7 @@ get_more_input (char *prompt, char *preinput) if (cur_len > allocated_len - 1) { allocated_len += 100; - str = realloc ( str, allocated_len ); - if (str == NULL) - { - PRINT_ERROR ("Out of memory\n"); - exit (EXIT_FAILURE); - } + str = xrealloc ( str, allocated_len ); } str[cur_len] = ch; cur_len++; diff --git a/src/list.c b/src/list.c index 12b5bbb..2192b1e 100644 --- a/src/list.c +++ b/src/list.c @@ -47,12 +47,8 @@ add_to_window_list (screen_info *s, Window w) { rp_window *new_window; - new_window = malloc (sizeof (rp_window)); - if (new_window == NULL) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + new_window = xmalloc (sizeof (rp_window)); + new_window->w = w; new_window->scr = s; new_window->last_access = 0; @@ -69,11 +65,8 @@ add_to_window_list (screen_info *s, Window w) XSelectInput (dpy, new_window->w, PropertyChangeMask | ColormapChangeMask); - if ((new_window->name = malloc (strlen ("Unnamed") + 1)) == NULL) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + new_window->name = xmalloc (strlen ("Unnamed") + 1); + strcpy (new_window->name, "Unnamed"); /* Add the window to the end of the unmapped list. */ @@ -123,15 +116,8 @@ set_current_window (rp_window *win) void init_window_list () { - rp_mapped_window_sentinel = malloc (sizeof (rp_window)); - rp_unmapped_window_sentinel = malloc (sizeof (rp_window)); - - if (!rp_mapped_window_sentinel - || !rp_unmapped_window_sentinel) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + rp_mapped_window_sentinel = xmalloc (sizeof (rp_window)); + rp_unmapped_window_sentinel = xmalloc (sizeof (rp_window)); rp_mapped_window_sentinel->next = rp_mapped_window_sentinel; rp_mapped_window_sentinel->prev = rp_mapped_window_sentinel; diff --git a/src/main.c b/src/main.c index 802d37f..1ccebe8 100644 --- a/src/main.c +++ b/src/main.c @@ -73,6 +73,32 @@ static struct option ratpoison_longopts[] = { {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; static char ratpoison_opts[] = "hvrk"; +void +fatal (const char *msg) +{ + fprintf (stderr, "ratpoison: %s", msg); + abort (); +} + +void * +xmalloc (size_t size) +{ + register void *value = malloc (size); + if (value == 0) + fatal ("Virtual memory exhausted"); + return value; +} + +void * +xrealloc (void *ptr, size_t size) +{ + register void *value = realloc (ptr, size); + if (value == 0) + fatal ("Virtual memory exhausted"); + PRINT_DEBUG("realloc: %d\n", size); + return value; +} + void sighandler (int signum) { @@ -192,8 +218,8 @@ read_rc_file (FILE *file) char *line; int linesize = n; - partial = (char*)malloc(n); - line = (char*)malloc(linesize); + partial = (char*)xmalloc(n); + line = (char*)xmalloc(linesize); *line = '\0'; while (fgets (partial, n, file) != NULL) @@ -201,7 +227,7 @@ read_rc_file (FILE *file) if ((strlen (line) + strlen (partial)) <= linesize) { linesize *= 2; - line = (char*) realloc (line, linesize); + line = (char*) xrealloc (line, linesize); } strcat (line, partial); @@ -245,7 +271,7 @@ read_startup_files () fprintf (stderr, "ratpoison: $HOME not set!?\n"); else { - char *filename = (char*)malloc (strlen (homedir) + strlen ("/.ratpoisonrc") + 1); + char *filename = (char*)xmalloc (strlen (homedir) + strlen ("/.ratpoisonrc") + 1); sprintf (filename, "%s/.ratpoisonrc", homedir); if ((fileptr = fopen (filename, "r")) == NULL) @@ -359,11 +385,7 @@ main (int argc, char *argv[]) } num_screens = ScreenCount (dpy); - if ((screens = (screen_info *)malloc (sizeof (screen_info) * num_screens)) == NULL) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + screens = (screen_info *)xmalloc (sizeof (screen_info) * num_screens); PRINT_DEBUG ("%d screens.\n", num_screens); diff --git a/src/manage.c b/src/manage.c index 0eb0e28..38d9c93 100644 --- a/src/manage.c +++ b/src/manage.c @@ -107,11 +107,8 @@ get_window_name (Window w) if (list_len > 0) { - if ((name = malloc (strlen (name_list[0]) + 1)) == NULL) - { - PRINT_ERROR ("Out of memory!\n"); - exit (EXIT_FAILURE); - } + name = xmalloc (strlen (name_list[0]) + 1); + strcpy (name, name_list[0]); /* Its our responsibility to free this. */ diff --git a/src/number.c b/src/number.c index 880245c..0e33e21 100644 --- a/src/number.c +++ b/src/number.c @@ -62,12 +62,7 @@ find_empty_cell () if (num_taken >= max_taken) { max_taken *= 2; - numbers_taken = realloc (numbers_taken, sizeof (int) * max_taken); - if (numbers_taken == NULL) - { - PRINT_ERROR ("Out of memory\n"); - exit (EXIT_FAILURE); - } + numbers_taken = xrealloc (numbers_taken, sizeof (int) * max_taken); } num_taken++; @@ -122,11 +117,5 @@ init_numbers () max_taken = 10; num_taken = 0; - numbers_taken = malloc (max_taken * sizeof (int)); - if (numbers_taken == NULL) - { - PRINT_ERROR ("Cannot allocate memory for numbers_taken.\n"); - exit (EXIT_FAILURE); - } - + numbers_taken = xmalloc (max_taken * sizeof (int)); } diff --git a/src/ratpoison.h b/src/ratpoison.h index 3a06d5e..bb8ef41 100644 --- a/src/ratpoison.h +++ b/src/ratpoison.h @@ -63,4 +63,8 @@ screen_info *find_screen (Window w); void read_rc_file (FILE *file); +void fatal (const char *msg); +void *xmalloc (size_t size); +void *xrealloc (void *ptr, size_t size); + #endif /* ! _RATPOISON_H */ diff --git a/src/sbuf.c b/src/sbuf.c index 2bc2d03..bd77d7c 100644 --- a/src/sbuf.c +++ b/src/sbuf.c @@ -22,37 +22,6 @@ #include "ratpoison.h" #include "sbuf.h" -/* ------------------------------------ move to separate file */ -#include -#include - -void -fatal (const char *msg) -{ - fprintf (stderr, "%s", msg); - abort (); -} - -void * -xmalloc (size_t size) -{ - register void *value = malloc (size); - if (value == 0) - fatal ("virtual memory exhausted"); - return value; -} - -void * -xrealloc (void *ptr, size_t size) -{ - register void *value = realloc (ptr, size); - if (value == 0) - fatal ("Virtual memory exhausted"); - PRINT_DEBUG("realloc: %d\n", size); - return value; -} -/*------------------------------------------------------------*/ - struct sbuf * sbuf_new (size_t initsz) { -- cgit v1.2.3