Sanity check with customization CLEAN, RAMSIZE, RAMSHOW.

This commit is contained in:
Renaud 2021-08-24 12:17:40 +08:00
parent a370d748c4
commit e2f7cc0566
11 changed files with 82 additions and 86 deletions

View File

@ -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 <stdlib.h>
# define RAMSHOW 1 /* auto dynamic RAM reporting */
# include <stdlib.h> /* 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

View File

@ -16,6 +16,8 @@
#include <errno.h>
#include <locale.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -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

2
eval.c
View File

@ -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 ******************************************/

2
eval.h
View File

@ -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) ;

1
exec.c
View File

@ -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"

10
line.c
View File

@ -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

95
main.c
View File

@ -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

View File

@ -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"

View File

@ -6,6 +6,7 @@
*/
#include <assert.h>
#include <stdlib.h> /* 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 <n>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;

View File

@ -1,5 +1,4 @@
/* wrapper.c -- implements wrapper.h */
#include "wrapper.h"
#include <stdio.h>
@ -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 */

View File

@ -2,11 +2,7 @@
#ifndef WRAPPER_H_
#define WRAPPER_H_
#include <stdlib.h> /* size_t */
void xmkstemp( char *fname_template) ;
void *xmalloc( size_t size) ;
#endif
/* end of wrapper.h */