Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!tandem!zorch!xanthian From: xanthian@zorch.SF-Bay.ORG (Kent Paul Dolan) Newsgroups: comp.sys.atari.st.tech Subject: Need brief help from Atari Lattice C user with "Malloc" Message-ID: <1991Apr10.045720.28081@zorch.SF-Bay.ORG> Date: 10 Apr 91 04:57:20 GMT References: <1991Apr5.094747.14526@csug.rdg.ac.uk> Organization: SF-Bay Public-Access Unix Lines: 93 Hi, sorry to butt in on the wrong side of the Amiga/Atari free fire zone, but I need a little help from someone familiar with the Atari version of Lattice C. I'm trying to bring up a version of the Boyer/Moore sublinear time string search algorithm on a BSD 4.3 Unix box. Unfortunately, from my point of view, the sources I have are an Atari ST port that was done without #ifdef conditionalizing, and I'm having trouble understanding a small piece of Lattice C code, specifically, what some calls to Malloc are doing. My (Amiga) version of Lattice C doesn't include the "Malloc" call, so my own docs are useless. The code looks like this: |=| #include |=| #include |=| #include "bm.h" |=| #include "extern.h" |=| |=| static void loinit(); |=| static int loread(); |=| |=| Execute(DescVec, NPats, TextFile, Buffer, inbufsiz) |=| { |=| ... |=| /* initialize */ |=| loinit(); |=| ... |=| } /* Execute */ |=| |=| static char *lobuf, *lopos; |=| static int lobread, loeof, lbsiz; |=| |=| static void loinit() |=| { |=| int newsiz; |=| |=| if (lobuf == (char *)0) { (1) |=| lbsiz = Malloc(-1); (2) |=| while (newsiz = lbsiz & (lbsiz-1)) { |=| lbsiz = newsiz; |=| } (3) |=| lobuf = (char *)Malloc(lbsiz); (4) |=| if (lobuf == (char *)-1) { |=| fprintf(stderr,"bm: memory allocation failed\n"); |=| exit(1); |=| } |=| } |=| lopos = lobuf; |=| lobread = 0; |=| loeof = 0; |=| } Elsewhere, _mneed is set to 30000 bytes, which, I understand, establishes a minimum heap space available by setting a global for the compiler. Pretty obviously, after you stare at the (rather clever) loop at (2) until your eyes cross a bit, what's going on is an allocation of a block sized a power of 2 the largest contained in the value returned to lbsize by "Malloc" at (1). Questions: a) Is the "Malloc" call at (1) getting the largest available block in the operating-system-maintained free list? b) If not, what's going on with the "-1" parameter (and is it being promoted to a long integer behind the scenes, while I'm asking)? c) Could the call at (1) return a value _larger_ than "_mneed" is set? d) I'm used to seeing (lower case) "malloc" return a zero (== null pointer) when allocation of the requested size isn't possible. Why is the test of the value returned by "Malloc" at (3) done against a pointer cast of "-1" at (4) instead? e) What would a zero return have implied that a "-1" return doesn't from "Malloc"? f) What exactly is the "-1" return telling me about an allocation failure? Email responses only, please; I really try to keep out of newsgroups where I have no business, since I tend to answer 10% of what I read otherwise. ;-) If this is something everyone but me reading this doesn't already understand, email a request for a summary of responses, and I'll gladly post one back here. Kent, the man from xanth.