[9] | 1 | /* VALLOC - Aligned memory allocator
|
---|
| 2 | * Emulation of the 4.2BSD library routine of the same name.
|
---|
| 3 | * Copyright 1985 by Ken Harrenstien, SRI International
|
---|
| 4 | * This software is quasi-public; it may be used freely with
|
---|
| 5 | * like software, but may NOT be sold or made part of licensed
|
---|
| 6 | * products without permission of the author. In all cases
|
---|
| 7 | * the source code and any modifications thereto must remain
|
---|
| 8 | * available to any user.
|
---|
| 9 | *
|
---|
| 10 | * This is part of the SB library package.
|
---|
| 11 | * Any software using the SB library must likewise be made
|
---|
| 12 | * quasi-public, with freely available sources.
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | #include "sb.h"
|
---|
| 16 |
|
---|
| 17 | char *
|
---|
| 18 | valloc(size)
|
---|
| 19 | unsigned size;
|
---|
| 20 | { register int pagmsk;
|
---|
| 21 | register SBMO i;
|
---|
| 22 | register struct smblk *sm, *smr;
|
---|
| 23 | struct smblk *sbm_mget(), *sbm_split();
|
---|
| 24 |
|
---|
| 25 | pagmsk = getpagesize() - 1; /* Get page size in bytes, less 1 */
|
---|
| 26 | if(!(sm = sbm_mget(size+pagmsk, size+pagmsk))) /* Get area big enuf */
|
---|
| 27 | return(0);
|
---|
| 28 | /* Now find # bytes prior to 1st page boundary.
|
---|
| 29 | * This expression gives 0 if already at boundary, else #-1.
|
---|
| 30 | */
|
---|
| 31 | i = pagmsk - ((int)(sm->smaddr) & pagmsk);
|
---|
| 32 | if(i) /* If need to split off preceding stuff, */
|
---|
| 33 | { smr = sbm_split(sm, i+1); /* do so (note i adjusted) */
|
---|
| 34 | sbm_mfree(sm); /* Release preceding mem */
|
---|
| 35 | if(!(sm = smr)) return(0); /* If couldn't split, fail */
|
---|
| 36 | }
|
---|
| 37 | if(i = (sm->smlen - size)) /* See if any trailing stuff */
|
---|
| 38 | { smr = sbm_split(sm, size); /* Yeah, split it off too */
|
---|
| 39 | if(smr) sbm_mfree(smr); /* If couldn't split, excess OK. */
|
---|
| 40 | }
|
---|
| 41 | return((char *)(sm->smaddr));
|
---|
| 42 | }
|
---|