Article View: comp.sys.amiga.tech
Article #8376ALERT, bug in DOSAlloc code
From: Chuck McManis
Date: Thu, 24 Mar 1988 23:57
Date: Thu, 24 Mar 1988 23:57
47 lines
1914 bytes
1914 bytes
Bob Page pointed out to me that the formula I was using to calculate
allocation lengths in my DOSAlloc() example were broken. I checked on
my original source and found out where I screwed up. As mentioned
previously, DOS allocate things in integral numbers of long words and
stores the length in the long word at the -1 offset. The generic DOS alloc
looks something like :
+------------+
| length |
Returned ptr (long *) --->+------------+
| long 0 |
+------------+
...
+------------+
| long n |
+------------+
So if you are allocating a BPTR that DOS may want to unallocate sometime
you can calculate the memory you need with the formula
((((size+3)/4)+1)*sizeof(long))
Which figures out how many longwords you need (size+3)/4 [ size is in BYTES ]
adds one to it for the length longword, and then multiplies by the size
of a long word to get a value back into BYTES. This is passed to AllocMem().
This same number is stored in the length, so the new DOSAlloc() looks like:
long *
DOSAlloc(size)
int size;
{
ULONG *T;
T = (long *)AllocMem((((size+3)/4)+1)*sizeof(long));
if (!T) return(NULL);
*T = ((((size+3)/4)+1)*sizeof(long));
return(T+1);
}
DOSFree can still be defined as
#define DOSFree(p) FreeMem((long *)p -1,*((long *)p - 1))
Sorry for the junk earlier.
--Chuck
--Chuck McManis
uucp: {anywhere}!sun!cmcmanis BIX: cmcmanis ARPAnet: cmcmanis@sun.com
These opinions are my own and no one elses, but you knew that didn't you.
Technical Details
Message-ID:
<46937@sun.uucp>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!rutgers!bellcore!decvax!decwrl!pyramid!oliveb!sun!pepper!cmcmanisShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!rutgers!bellcore!decvax!decwrl!pyramid!oliveb!sun!pepper!cmcmanis From: cmcmanis%pepper@Sun.COM (Chuck McManis) Newsgroups: comp.sys.amiga.tech Subject: ALERT, bug in DOSAlloc code Keywords: BUG, example code, ECO Message-ID: <46937@sun.uucp> Date: 24 Mar 88 23:57:30 GMT Sender: news@sun.uucp Lines: 47