From 45f32f3c0ef2c2c02fb4c9c16b4d3ffd4acb1f0f Mon Sep 17 00:00:00 2001 From: deuce <> Date: Mon, 8 May 2006 18:48:19 +0000 Subject: [PATCH] USe alloca() instead of malloc()/free() when possible... fixes at least one memory leak in the block copy function. --- src/syncdraw/block.c | 5 ++--- src/syncdraw/crt.c | 5 ++--- src/syncdraw/effekt.c | 3 +-- src/syncdraw/miscfunctions.c | 15 ++++----------- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/syncdraw/block.c b/src/syncdraw/block.c index 9103c8be68..b1a0d72eba 100644 --- a/src/syncdraw/block.c +++ b/src/syncdraw/block.c @@ -81,7 +81,7 @@ CopyBlock(char Mode) Dragging=TRUE; do { - buf=(char *)malloc(80*ti.screenheight*2); + buf=(char *)alloca(80*ti.screenheight*2); memcpy(buf,Screen[ActivePage][FirstLine],80*ti.screenheight*2); for (y=Y1;y<=Y2;y++) { @@ -262,7 +262,7 @@ blockmode(void) X1 = CursorX; } - buf=(char *)malloc(80*ti.screenheight*2); + buf=(char *)alloca(80*ti.screenheight*2); memcpy(buf,Screen[ActivePage][FirstLine],80*ti.screenheight*2); for(y=Y1-FirstLine;y<=Y2-FirstLine;y++) { for(x=X1;x<=X2;x++) { @@ -273,7 +273,6 @@ blockmode(void) puttext(1,1,80,ti.screenheight-1,buf); else puttext(1,2,80,ti.screenheight-1,buf); - free(buf); Statusline(); Colors(Attribute); diff --git a/src/syncdraw/crt.c b/src/syncdraw/crt.c index aef43a51da..66f9cff802 100644 --- a/src/syncdraw/crt.c +++ b/src/syncdraw/crt.c @@ -15,9 +15,9 @@ DrawBox(int x1, int y1, int x2, int y2) width=x2-x1+3; height=y2-y1+1; if(width>height) - buf=(char *)malloc(width*2); + buf=(char *)alloca(width*2); else - buf=(char *)malloc(height*2); + buf=(char *)alloca(height*2); i=0; buf[i++]=218; @@ -47,5 +47,4 @@ DrawBox(int x1, int y1, int x2, int y2) } puttext(x1,y1+1,x1,y2-1,buf); puttext(x2,y1+1,x2,y2-1,buf); - free(buf); } diff --git a/src/syncdraw/effekt.c b/src/syncdraw/effekt.c index 0a7ec57359..6ffafb5ef1 100644 --- a/src/syncdraw/effekt.c +++ b/src/syncdraw/effekt.c @@ -22,7 +22,7 @@ draweffekt(int xpos, int ypos, int effekt, char *blabla, int highlite) col = effect.Colortable[1][1]; l=strlen(blabla); - buf=(char *)malloc(l*2); + buf=(char *)alloca(l*2); p=0; for (x = 0; x < l; x++) { @@ -76,7 +76,6 @@ draweffekt(int xpos, int ypos, int effekt, char *blabla, int highlite) buf[p++]=col+(highlite?16:0); } puttext(xpos,ypos,xpos+l-1,ypos,buf); - free(buf); } void diff --git a/src/syncdraw/miscfunctions.c b/src/syncdraw/miscfunctions.c index 7fa6bd1537..6032b18641 100644 --- a/src/syncdraw/miscfunctions.c +++ b/src/syncdraw/miscfunctions.c @@ -102,7 +102,7 @@ CoolWrite(int x, int y, char *a) int b = 0, i = 0, j = 0; char *buf; - buf=(char *)malloc(strlen(a)*2); + buf=(char *)alloca(strlen(a)*2); while (a[b] != 0) { i++; if (a[b] == 32) @@ -122,7 +122,6 @@ CoolWrite(int x, int y, char *a) } } puttext(x, y, x+strlen(a)-1,y,buf); - free(buf); } void CodeWrite(int x, int y, char *a) @@ -130,7 +129,7 @@ CodeWrite(int x, int y, char *a) int b = 0, i = 0, attr=15; char *buf; - buf=(char *)malloc(strlen(a)*2); + buf=(char *)alloca(strlen(a)*2); while (a[b] != 0) { if (a[b] == ']') attr=15; @@ -141,7 +140,6 @@ CodeWrite(int x, int y, char *a) b++; } puttext(x,y,x+strlen(a)-1,y,buf); - free(buf); } unsigned char @@ -453,7 +451,7 @@ inputfield(char *Str, int length, int x1, int y) char *buf; int oldx,oldy; - buf=(char *)malloc(length*2); + buf=(char *)alloca(length*2); strcpy(nul,Str); oldx=wherex(); oldy=wherey(); @@ -507,8 +505,6 @@ inputfield(char *Str, int length, int x1, int y) } } while ((ch != 13) & (ch != 27)); - free(buf); - gotoxy(oldx,oldy); if (ch == 27) return Str; @@ -550,7 +546,7 @@ int bufprintf(char *buf, int attr, char *fmat, ...) ret=_vsnprintf(str,sizeof(str)-1,fmat,argptr); #else ret=vsnprintf(NULL,0,fmat,argptr); - str=(char *)malloc(ret+1); + str=(char *)alloca(ret+1); if(str==NULL) return(EOF); ret=vsprintf(str,fmat,argptr); @@ -564,8 +560,5 @@ int bufprintf(char *buf, int attr, char *fmat, ...) } else i=EOF; -#ifndef _WIN32 - free(str); -#endif return(i); } -- GitLab