home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!unlinfo.unl.edu!news
- From: mgleason@cse.unl.edu (Mike Gleason)
- Subject: BlockZero.c snippet
- Message-ID: <1992Sep15.185019.22483@unlinfo.unl.edu>
- Sender: news@unlinfo.unl.edu
- Nntp-Posting-Host: cse.unl.edu
- Organization: NCEMRSoft
- Date: Tue, 15 Sep 1992 18:50:19 GMT
- Lines: 75
-
- I think J. McCarthy & M. Mora started a good idea by posting code snippets here,
- so here's a freebie function that clears an area of memory (which can start
- on an odd boundary). It's much faster than using memset(ptr,0,n) because
- this function uses move.l's for the majority of the block. It compiles
- under Think C, but the asm {} blocks may break other compilers.
-
- I use this function all the time, especially when dealing with parameter
- blocks. e.g.:
-
- #define ZERO(a) BlockZero(&(a), sizeof (a))
-
- {
- HParamBlockRec pb;
-
- ZERO(pb);
- }
-
-
- Code follows. Further optimizations, your cool code snippets, bug fixes
- <gulp> welcome....
-
- ----------------
-
- void BlockZero(void *area, register unsigned long n);
-
- void BlockZero(void *area, register unsigned long n)
- {
- register char *p = (char *)area;
- register unsigned long fours;
- register unsigned long remainder, zero;
-
- if (n) {
- if ((long) p & 01) {
- /* Ptr on an odd boundary... */
- asm {
- clr.b (p)+
- }
- --n;
- /*
- * Ptr is now on an even boundary,
- * and it's first byte is zeroed.
- */
- }
-
- fours = n / 4L; /* Can be zero. */
-
- /* Do the meat of the block using longwords for speed. */
-
- asm {
- clr.l zero
- bra @2
- @1: move.l zero, (p)+
- @2: subq.l #1, fours
- tst.l fours
- bge @1
- }
-
- /*
- * If the size of the block isn't divisible by 4, we'll
- * have to do the remaining bytes (up to 3) by hand.
- */
-
- remainder = n & 03; /* same as remainder = n % 4. */
-
- asm {
- bra @4
- @3: clr.b (p)+
- @4: dbra remainder, @3
- }
- }
- } /* BlockZero */
-
- /* eof BlockZero.c */
- --
- --mg mgleason@cse.unl.edu
-