From e2f7cc05665666f0a9c9bcc31e416f7130984e98 Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Tue, 24 Aug 2021 12:17:40 +0800 Subject: [PATCH] Sanity check with customization CLEAN, RAMSIZE, RAMSHOW. --- defines.h | 10 +++--- display.c | 26 ++++++++++++--- eval.c | 2 +- eval.h | 2 +- exec.c | 1 + line.c | 10 +++--- main.c | 95 ++++++++++++++++++++++++------------------------------- names.c | 1 + window.c | 8 +++-- wrapper.c | 9 ------ wrapper.h | 4 --- 11 files changed, 82 insertions(+), 86 deletions(-) diff --git a/defines.h b/defines.h index 3eeb282..1b328a4 100644 --- a/defines.h +++ b/defines.h @@ -40,17 +40,17 @@ /* Dynamic RAM tracking and reporting redefinitions */ #define RAMSIZE 0 /* dynamic RAM memory usage tracking */ #if RAMSIZE -# define RAMSHOW 1 /* auto dynamic RAM reporting */ -# include +# define RAMSHOW 1 /* auto dynamic RAM reporting */ +# include /* size_t */ void *allocate( size_t size) ; void release( void *ptr) ; -# define malloc allocate -# define free release +# define malloc( sz) allocate(sz) +# define free( ptr) release( ptr) #endif /* De-allocate memory always on exit (if the operating system or main - program can not + program can not) */ #define CLEAN 0 /* de-alloc memory on exit */ #if CLEAN diff --git a/display.c b/display.c index faf3367..4f2455b 100644 --- a/display.c +++ b/display.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include @@ -26,7 +28,7 @@ #include "termio.h" #include "terminal.h" #include "version.h" -#include "wrapper.h" +//#include "wrapper.h" #include "utf8.h" #include "window.h" @@ -101,6 +103,19 @@ static int newscreensize( int h, int w) ; #endif +/* xmalloc is used at early initialization before memory usage tracking is + enabled so it bypass the memory tracking macroes. +*/ +static void *xmalloc( size_t size) { + void *ret = (malloc)( size) ; + if( !ret) { + fprintf( stderr, "fatal: Out of memory\n") ; + exit( EXIT_FAILURE) ; + } + + return ret ; +} + /* Initialize the data structures used by the display code. The edge vectors used to access the screens are set up. The operating system's terminal I/O channel is set up. All the other things get initialized at @@ -140,16 +155,17 @@ void vtinit( void) { #if CLEAN /* free up all the dynamically allocated video structures */ void vtfree( void) { +/* as xmalloc bypass the malloc macro, we need bypass the free macro too */ for( int i = 0 ; i < term.t_maxrow ; ++i ) { - free( vscreen[ i]) ; + (free)( vscreen[ i]) ; #if MEMMAP == 0 || SCROLLCODE - free( pscreen[ i]) ; + (free)( pscreen[ i]) ; #endif } - free( vscreen) ; + (free)( vscreen) ; #if MEMMAP == 0 || SCROLLCODE - free( pscreen) ; + (free)( pscreen) ; #endif } #endif diff --git a/eval.c b/eval.c index ba2b0d1..8c3dbe8 100644 --- a/eval.c +++ b/eval.c @@ -65,7 +65,7 @@ int rval = 0 ; /* return value of a subprocess */ static int saveflag = 0 ; /* Flags, saved with the $target var */ -long envram = 0l ; /* # of bytes current in use by malloc */ +unsigned envram = 0 ; /* # of bytes current in use by malloc */ /* User variables ******************************************/ diff --git a/eval.h b/eval.h index af9e838..41d71d6 100644 --- a/eval.h +++ b/eval.h @@ -12,7 +12,7 @@ extern int macbug ; /* macro debuging flag */ extern int cmdstatus ; /* last command status */ extern int rval ; /* return value of a subprocess */ -extern long envram ; /* # of bytes current in use by malloc */ +extern unsigned envram ; /* # of bytes current in use by malloc */ int readfirst_f( void) ; int is_it_cmd( char *token) ; diff --git a/exec.c b/exec.c index e85134d..f61a4af 100644 --- a/exec.c +++ b/exec.c @@ -13,6 +13,7 @@ #include "buffer.h" #include "bind.h" +#include "defines.h" /* malloc/allocate, free/release */ #include "eval.h" #include "file.h" #include "flook.h" diff --git a/line.c b/line.c index abd7acc..1e94d16 100644 --- a/line.c +++ b/line.c @@ -37,18 +37,18 @@ static int ldelnewline( void) ; * was taken up by the keycode structure). */ -#define KBLOCK 250 /* sizeof kill buffer chunks */ +#define KBLOCK 248 /* sizeof kill buffer chunks */ typedef struct kill { - struct kill *d_next; /* Link to next chunk, NULL if last. */ - char d_chunk[KBLOCK]; /* Deleted text. */ + struct kill *d_next ; /* Link to next chunk, NULL if last. */ + char d_chunk[ KBLOCK] ; /* Deleted text. */ } *kill_p ; static kill_p kbufp = NULL ; /* current kill buffer chunk pointer */ static kill_p kbufh = NULL ; /* kill buffer header pointer */ static int kused = KBLOCK ; /* # of bytes used in kill buffer */ -static int klen ; /* length of kill buffer content */ -static char *value = NULL ; /* temp buffer for value */ +static int klen ; /* length of kill buffer content */ +static char *value = NULL ; /* temp buffer for value */ /* * return some of the contents of the kill buffer diff --git a/main.c b/main.c index 268c18b..413dad1 100644 --- a/main.c +++ b/main.c @@ -347,80 +347,67 @@ static void edinit( char *bname) { wp->w_flag = WFMODE | WFHARD; /* Full. */ } + /***** Compiler specific Library functions ****/ -#if RAMSIZE -/* These routines will allow me to track memory usage by placing - a layer on top of the standard system malloc() and free() calls. - with this code defined, the environment variable, $RAM, will - report on the number of bytes allocated via malloc. +#if RAMSIZE - with SHOWRAM defined, the number is also posted on the - end of the bottom mode line and is updated whenever it is changed. +/* These routines will allow me to track memory usage by placing a layer on + top of the standard system malloc() and free() calls. with this code + defined, the environment variable, $RAM, will report on the number of + bytes allocated via malloc. + + with SHOWRAM defined, the number is also posted on the end of the bottom + mode line and is updated whenever it is changed. */ -static void dspram( void) ; - -#undef malloc -#undef free -#if 0 -char *allocate(nbytes) - /* allocate nbytes and track */ -unsigned nbytes; /* # of bytes to allocate */ -#endif -void *allocate( size_t nbytes) -{ - char *mp; /* ptr returned from malloc */ -/* char *malloc(); */ - - mp = malloc(nbytes); - if (mp) { - envram += nbytes; #if RAMSHOW - dspram(); + static void dspram( void) ; +#endif + +void *allocate( size_t nbytes) { + nbytes += sizeof nbytes ; /* add overhead to track allocation */ + size_t *mp = (malloc)( nbytes) ; /* call the function not the macro */ + if( mp) { + *mp++ = nbytes ; + envram += nbytes ; +#if RAMSHOW + dspram() ; #endif } - return mp; + return mp ; } -#if 0 -release(mp) - /* release malloced memory and track */ -char *mp; /* chunk of RAM to release */ -#endif -void release( void *mp) -{ - unsigned *lp; /* ptr to the long containing the block size */ - - if (mp) { +void release( void *mp) { + if( mp) { + size_t *sp = mp ; + sp-- ; /* update amount of ram currently malloced */ - lp = ((unsigned *) mp) - 1; - envram -= (long) *lp - 2; - free(mp); + envram -= *sp ; + (free)( sp) ; /* call the function not the macro */ #if RAMSHOW - dspram(); + dspram() ; #endif } } #if RAMSHOW -static void dspram( void) -{ /* display the amount of RAM currently malloced */ - char mbuf[20]; - char *sp; +static void dspram( void) { /* display the amount of RAM currently malloced */ + char mbuf[ 20] ; - TTmove(term.t_nrow - 1, 70); + TTmove( term.t_nrow, term.t_ncol - 12) ; #if COLOR - TTforg(7); - TTbacg(0); + TTforg( 7) ; + TTbacg(0) ; #endif - sprintf(mbuf, "[%lu]", envram); - sp = &mbuf[0]; - while (*sp) - TTputc(*sp++); - TTmove(term.t_nrow, 0); - movecursor(term.t_nrow, 0); + sprintf( mbuf, "[%10u]", envram) ; + char *sp = mbuf ; + while( *sp) + TTputc( *sp++) ; + + TTmove( term.t_nrow, 0) ; + movecursor( term.t_nrow, 0) ; } #endif #endif @@ -461,7 +448,7 @@ void cexit( int status) { /* and the video buffers */ vtfree() ; - (exit)( status) ; + (exit)( status) ; /* call the function, not the macro */ } #endif diff --git a/names.c b/names.c index 378d260..2f351a7 100644 --- a/names.c +++ b/names.c @@ -16,6 +16,7 @@ #include "bind.h" #include "bindable.h" #include "buffer.h" +#include "defines.h" /* malloc/allocate */ #include "display.h" #include "eval.h" #include "exec.h" diff --git a/window.c b/window.c index e9f31c9..d51bc0b 100644 --- a/window.c +++ b/window.c @@ -6,6 +6,7 @@ */ #include +#include /* malloc(), free() */ #include "basic.h" #include "buffer.h" @@ -52,7 +53,7 @@ TBINDABLE( redraw) { /* The command make the next window (next => down the screen) the current window. There are no real errors, although the command does nothing if - there is only 1 window on the screen. Bound to "C-X C-N". + there is only 1 window on the screen. Bound to "C-X O". with an argument this command finds the th window from the top @@ -318,7 +319,10 @@ BINDABLE( splitwind) { return FALSE ; } - wp = xmalloc( sizeof *wp) ; + wp = malloc( sizeof *wp) ; + if( wp == NULL) + return mloutfail( "Out of memory") ; + ++curbp->b_nwnd; /* Displayed twice. */ wp->w_bufp = curbp; wp->w_dotp = curwp->w_dotp; diff --git a/wrapper.c b/wrapper.c index 21dda69..ebb4d3a 100644 --- a/wrapper.c +++ b/wrapper.c @@ -1,5 +1,4 @@ /* wrapper.c -- implements wrapper.h */ - #include "wrapper.h" #include @@ -26,12 +25,4 @@ void xmkstemp( char *template) { close( fd) ; } -void *xmalloc( size_t size) { - void *ret = malloc( size) ; - if( !ret) - die( "Out of memory") ; - - return ret ; -} - /* end of wrapper.c */ diff --git a/wrapper.h b/wrapper.h index 0f556b2..7be2c0e 100644 --- a/wrapper.h +++ b/wrapper.h @@ -2,11 +2,7 @@ #ifndef WRAPPER_H_ #define WRAPPER_H_ -#include /* size_t */ - void xmkstemp( char *fname_template) ; -void *xmalloc( size_t size) ; - #endif /* end of wrapper.h */