Subject:
[patch] alloca()
From:
Brent Thompson ####@####.####
Date:
13 Oct 2000 16:17:19 -0000
Message-Id: <39E73482.4050703@nlc.com>
Brent Thompson wrote:
> Greg Haerr wrote:
> > Send me the changes. I suppose we should include malloc/free
> > instead of alloca, since too few systems implement it.
> > However, alloca is way faster than malloc, so I'll look at
> > how it's used and perhaps save this for the day Microwindows
> > has a ./configure setup.
>
> Agreed. What would you think about macro-izing this?
> Say:
>
> #ifdef USE_ALLOCA
> #define ALLOCA(size) alloca(size)
> #define FREEA(pmem)
> #else /* no alloca(), so use malloc()/free() instead */
> #define ALLOCA(size) malloc(size)
> #define FREEA(pmem) free(pmem)
> #endif
>
> Then always use ALLOCA() & FREEA() properly so that
> malloc()/free() works. But if there is an alloca()
> available, define USE_ALLOCA and all is faster! [:-)]
Here is a patch to <include/device.h> that implements
this macro-ization of alloca(). Also included are patches
to the 2 files which currently use alloca(),
<engine/devfont.c> and <mwin/winlib/edit.c>, to use these
macros.
BAT
--
Brent A. Thompson, Next Level Communications <www.nlc.com>
1776 22nd Street #100, West Des Moines, IA 50266-1444
EMail: ####@####.#### Phone: (515)991-3853
*** device.h_0.88 Thu Aug 3 13:42:48 2000
--- device.h Fri Oct 13 10:45:44 2000
***************
*** 18,23 ****
--- 18,24 ----
#define FONTMAPPER 0 /* =1 for Morten's font mapper*/
#define EPRINTF GdError /* error output*/
#define DPRINTF GdError /* debug output*/
+ /*#define USE_ALLOCA 1 /* alloca() is available */
/* max char height/width must be >= 16 and a multiple of sizeof(MWIMAGEBITS)*/
#define MAX_CHAR_HEIGHT 128 /* maximum text bitmap height*/
***************
*** 520,525 ****
--- 521,536 ----
/* error.c*/
int GdError(const char *format, ...);
+
+ #ifdef USE_ALLOCA
+ /* alloca() is available, so use it for better performance */
+ #define ALLOCA(size) alloca(size)
+ #define FREEA(pmem)
+ #else
+ /* no alloca(), so use malloc()/free() instead */
+ #define ALLOCA(size) malloc(size)
+ #define FREEA(pmem) free(pmem)
+ #endif
/* no assert() in MSDOS or ELKS...*/
#if MSDOS | ELKS
*** devfont.c_0.88 Mon Oct 9 10:38:30 2000
--- devfont.c Fri Oct 13 11:08:00 2000
***************
*** 2203,2209 ****
}
sbegin=s;
! bitmap = (MWPIXELVAL *)alloca(pf->cfont_width * pf->font_height *
sizeof(MWPIXELVAL));
while( getnextchar(s, c) )
--- 2203,2209 ----
}
sbegin=s;
! bitmap = (MWPIXELVAL *)ALLOCA(pf->cfont_width * pf->font_height *
sizeof(MWPIXELVAL));
while( getnextchar(s, c) )
***************
*** 2245,2250 ****
--- 2245,2252 ----
if(s>=sbegin+cc)break;
}
+
+ FREEA(bitmap);
}
/*
*** edit.c_0.88 Mon Oct 9 14:55:56 2000
--- edit.c Fri Oct 13 11:12:00 2000
***************
*** 480,486 ****
fprintf (stderr, "ASSERT failure: %s.\n", "Edit Paint");
#endif
! dispBuffer = alloca (dispLen + 1);
if (dwStyle & ES_PASSWORD)
memset (dispBuffer, '*', dispLen);
--- 480,486 ----
fprintf (stderr, "ASSERT failure: %s.\n", "Edit Paint");
#endif
! dispBuffer = ALLOCA(dispLen + 1);
if (dwStyle & ES_PASSWORD)
memset (dispBuffer, '*', dispLen);
***************
*** 509,514 ****
--- 509,516 ----
dispBuffer,-1);
EndPaint (hWnd, &ps);
+
+ FREEA(dispBuffer);
}
break;
#if 0//fix: no WM_KEYDOWN