cmcmanis%pepper@Sun.COM (Chuck McManis) (03/25/88)
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.