Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mimsy!oddjob!gargoyle!ihnp4!cuae2!ltuxa!ttrdc!levy From: levy@ttrdc.UUCP (Daniel R. Levy) Newsgroups: comp.lang.c Subject: Re: Indirection and MALLOC Message-ID: <1690@ttrdc.UUCP> Date: Wed, 13-May-87 14:26:22 EDT Article-I.D.: ttrdc.1690 Posted: Wed May 13 14:26:22 1987 Date-Received: Sat, 16-May-87 07:51:48 EDT References: <7331@brl-adm.ARPA> Organization: AT&T, Skokie, IL Lines: 69 Summary: bad idea to use malloc() unprotected if also used in interrupt service routine In article <7331@brl-adm.ARPA>, SXSAW%ALASKA.BITNET@wiscvm.wisc.EDU writes: < Re AST re-entrency - Since the alloc/dealloc routines save the size and < address of FREE'd blocks across calls - to manipulate later, and since the < code can be interrupted during this manipulation via an AST routine which < calls MALLOC - well....... It only takes about 10 lines of code to create < a test case for this that WILL FAIL sometimes - and sometimes not, depends < on the timing. (with this type of coding going on in the CRTL I begin to < wonder about other routines) I have generally found that with VMS4.x and < the additional features added to LIB$GET_VM, etc. (i.e. the memory ZONE < concepts) - that the best way to go is use the RTL procedures. naturally < you code will be less portable, but.... It is not a good idea to use malloc() re-entrantly in an interrupt service routine even on a UNIX system. The code below illustrates the problem on a System V release 2 3B20 (will core dump with a memory fault) when run and given rapidly repeated keyboard interrupts, unless PROTECT_MALLOC is #define'd. This ought to work just fine on VMS, too. -------- #include main() { int astrtn(); int i; char *malloc(); char *c; void exit(); (void) signal(SIGINT,astrtn); for (;;) { #ifdef PROTECT_MALLOC (void) signal(SIGINT,SIG_IGN); #endif if (!(c=malloc((unsigned)100))) { (void) write(1,"malloc() failure\n",(unsigned)17); exit(1); } #ifdef PROTECT_MALLOC (void) signal(SIGINT,astrtn); #endif for (i=0; i<100; i++) *c++ = 'y'; } } astrtn() { char *d; char *malloc(); void exit(); int i; (void) signal(SIGINT,SIG_IGN); (void) write(1,"INTERRUPT\n",(unsigned)10); if (!(d=malloc((unsigned)100))) { (void) write(1,"astrtn malloc failure\n",(unsigned)22); exit(2); } for (i=0; i<100; i++) *d++ = 'x'; (void) signal(SIGINT,astrtn); return 0; } -------- -- |------------dan levy------------| Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa, | an engihacker @ | vax135}!ttrdc!ttrda!levy | at&t computer systems division | Disclaimer: try datclaimer. |--------skokie, illinois--------|