forked from mit-pdos/xv6-public
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nit in sbrk indirect block fix dup to share fd struct
- Loading branch information
kaashoek
committed
Aug 24, 2006
1 parent
8b58e81
commit ea2909b
Showing
10 changed files
with
210 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include "types.h" | ||
#include "stat.h" | ||
#include "user.h" | ||
#include "param.h" | ||
|
||
// Memory allocator by Kernighan and Ritchie, The C programming Language, | ||
// 2nd ed. Section 8.7. | ||
|
||
typedef long Align; | ||
|
||
union header { | ||
struct { | ||
union header *ptr; | ||
uint size; | ||
} s; | ||
Align x; | ||
}; | ||
|
||
typedef union header Header; | ||
|
||
static Header base; | ||
static Header *freep = 0; | ||
|
||
void | ||
free(void *ap) | ||
{ | ||
Header *bp, *p; | ||
|
||
bp = (Header *) ap - 1; | ||
for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr) | ||
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr)) | ||
break; | ||
if (bp + bp->s.size == p->s.ptr) { | ||
bp->s.size += p->s.ptr->s.size; | ||
bp->s.ptr = p->s.ptr->s.ptr; | ||
} else | ||
bp->s.ptr = p->s.ptr; | ||
if (p + p->s.size == bp) { | ||
p->s.size += bp->s.size; | ||
p->s.ptr = bp->s.ptr; | ||
} else | ||
p->s.ptr = bp; | ||
freep = p; | ||
} | ||
|
||
static Header * | ||
morecore(uint nu) | ||
{ | ||
char *cp; | ||
Header *up; | ||
|
||
if (nu < PAGE) | ||
nu = PAGE; | ||
cp = sbrk(nu * sizeof(Header)); | ||
if (cp == (char *) -1) | ||
return 0; | ||
up = (Header *) cp; | ||
up->s.size = nu; | ||
free((void *)(up + 1)); | ||
return freep; | ||
} | ||
|
||
void * | ||
malloc(uint nbytes) | ||
{ | ||
Header *p, *prevp; | ||
uint nunits; | ||
|
||
nunits = (nbytes + sizeof(Header) - 1)/sizeof(Header) + 1; | ||
if ((prevp = freep) == 0) { | ||
base.s.ptr = freep = prevp = &base; | ||
base.s.size = 0; | ||
} | ||
for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) { | ||
if (p->s.size >= nunits) { | ||
if (p->s.size == nunits) | ||
prevp->s.ptr = p->s.ptr; | ||
else { | ||
p->s.size -= nunits; | ||
p += p->s.size; | ||
p->s.size = nunits; | ||
} | ||
freep = prevp; | ||
return (void *) (p + 1); | ||
} | ||
if (p == freep) | ||
if ((p = morecore(nunits)) == 0) | ||
return 0; | ||
} | ||
} |
Oops, something went wrong.