__CRAZY cleanliness: pointer arithmetic
This commit is contained in:
parent
7cbc0b6fa2
commit
4b456b1457
35
aalloc.c
35
aalloc.c
@ -1,6 +1,6 @@
|
|||||||
#include "sh.h"
|
#include "sh.h"
|
||||||
|
|
||||||
__RCSID("$MirOS: src/bin/mksh/aalloc.c,v 1.3 2008/11/12 05:05:17 tg Exp $");
|
__RCSID("$MirOS: src/bin/mksh/aalloc.c,v 1.4 2008/11/12 05:11:05 tg Exp $");
|
||||||
|
|
||||||
/* mksh integration of aalloc */
|
/* mksh integration of aalloc */
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ typedef /* unsigned */ ptrdiff_t TCookie;
|
|||||||
|
|
||||||
typedef union {
|
typedef union {
|
||||||
TCookie iv;
|
TCookie iv;
|
||||||
void *pv;
|
char *pv;
|
||||||
} TPtr;
|
} TPtr;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -54,8 +54,8 @@ typedef union {
|
|||||||
|
|
||||||
struct TBlock {
|
struct TBlock {
|
||||||
TCookie cookie;
|
TCookie cookie;
|
||||||
void *endp;
|
char *endp;
|
||||||
void *last;
|
char *last;
|
||||||
void *storage;
|
void *storage;
|
||||||
};
|
};
|
||||||
typedef struct TBlock *PBlock;
|
typedef struct TBlock *PBlock;
|
||||||
@ -172,11 +172,11 @@ anew(void)
|
|||||||
} while (!(bp->cookie & PVMASK));
|
} while (!(bp->cookie & PVMASK));
|
||||||
|
|
||||||
/* first byte after block */
|
/* first byte after block */
|
||||||
bp->endp = bp + AALLOC_INITSZ; /* bp + size of the block */
|
bp->endp = (char *)bp + AALLOC_INITSZ; /* bp + size of the block */
|
||||||
/* next entry (forward pointer) available for new allocation */
|
/* next entry (forward pointer) available for new allocation */
|
||||||
bp->last = &bp->storage; /* first entry */
|
bp->last = (char *)&bp->storage; /* first entry */
|
||||||
|
|
||||||
ap->bp.pv = bp;
|
ap->bp.pv = (char *)bp;
|
||||||
ap->bp.iv ^= gcookie;
|
ap->bp.iv ^= gcookie;
|
||||||
#ifdef AALLOC_TRACK
|
#ifdef AALLOC_TRACK
|
||||||
ap->prev.pv = track;
|
ap->prev.pv = track;
|
||||||
@ -203,7 +203,7 @@ check_bp(PArea ap, const char *funcname, TCookie ocookie)
|
|||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
p.iv = ap->bp.iv ^ gcookie;
|
p.iv = ap->bp.iv ^ gcookie;
|
||||||
if ((ptrdiff_t)(bp = p.pv) & PVMASK) {
|
if ((ptrdiff_t)(bp = (PBlock)p.pv) & PVMASK) {
|
||||||
AALLOC_WARN("%s: area %p block pointer destroyed: %08tX",
|
AALLOC_WARN("%s: area %p block pointer destroyed: %08tX",
|
||||||
funcname, ap, p.iv);
|
funcname, ap, p.iv);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
@ -219,12 +219,12 @@ check_bp(PArea ap, const char *funcname, TCookie ocookie)
|
|||||||
funcname, bp, bp->endp, bp->last);
|
funcname, bp, bp->endp, bp->last);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
if (bp->endp < (void *)bp) {
|
if (bp->endp < (char *)bp) {
|
||||||
AALLOC_WARN("%s: block %p end pointer out of bounds: %p",
|
AALLOC_WARN("%s: block %p end pointer out of bounds: %p",
|
||||||
funcname, bp, bp->endp);
|
funcname, bp, bp->endp);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
if ((bp->last < (void *)&bp->storage) || (bp->last >= bp->endp)) {
|
if ((bp->last < (char *)&bp->storage) || (bp->last >= bp->endp)) {
|
||||||
AALLOC_WARN("%s: block %p last pointer out of bounds: "
|
AALLOC_WARN("%s: block %p last pointer out of bounds: "
|
||||||
"%p < %p < %p", funcname, bp, &bp->storage, bp->last,
|
"%p < %p < %p", funcname, bp, &bp->storage, bp->last,
|
||||||
bp->endp);
|
bp->endp);
|
||||||
@ -275,13 +275,13 @@ adelete_leak(PArea ap, PBlock bp)
|
|||||||
{
|
{
|
||||||
TPtr *cp;
|
TPtr *cp;
|
||||||
|
|
||||||
while (bp->last > (void *)&bp->storage) {
|
while (bp->last > (char *)&bp->storage) {
|
||||||
bp->last -= PVALIGN;
|
bp->last -= PVALIGN;
|
||||||
cp = *((void **)bp->last);
|
cp = *((void **)bp->last);
|
||||||
cp->iv ^= bp->cookie;
|
cp->iv ^= bp->cookie;
|
||||||
AALLOC_WARN("leaking %s pointer %p in area %p (%p %tu)",
|
AALLOC_WARN("leaking %s pointer %p in area %p (%p %tu)",
|
||||||
cp->pv == bp->last ? "valid" : "underflown",
|
cp->pv == bp->last ? "valid" : "underflown",
|
||||||
(char *)cp + PVALIGN, ap, bp, bp->endp - (void *)bp);
|
(char *)cp + PVALIGN, ap, bp, bp->endp - (char *)bp);
|
||||||
free(cp);
|
free(cp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -296,7 +296,7 @@ adelete(PArea *pap)
|
|||||||
|
|
||||||
/* ignore invalid areas */
|
/* ignore invalid areas */
|
||||||
if ((bp = check_bp(*pap, "adelete", 0)) == NULL) {
|
if ((bp = check_bp(*pap, "adelete", 0)) == NULL) {
|
||||||
if (bp->last != &bp->storage)
|
if (bp->last != (char *)&bp->storage)
|
||||||
adelete_leak(*pap, bp);
|
adelete_leak(*pap, bp);
|
||||||
free(bp);
|
free(bp);
|
||||||
}
|
}
|
||||||
@ -351,12 +351,12 @@ alloc(size_t nmemb, size_t size, PArea ap)
|
|||||||
size_t bsz;
|
size_t bsz;
|
||||||
|
|
||||||
/* make room for more forward ptrs in the block allocation */
|
/* make room for more forward ptrs in the block allocation */
|
||||||
bsz = bp->endp - (void *)bp;
|
bsz = bp->endp - (char *)bp;
|
||||||
safe_muladd((size_t)2, bsz, 0);
|
safe_muladd((size_t)2, bsz, 0);
|
||||||
safe_realloc(bp, bsz);
|
safe_realloc(bp, bsz);
|
||||||
|
|
||||||
/* “bp” has possibly changed, enter its new value into ap */
|
/* “bp” has possibly changed, enter its new value into ap */
|
||||||
ap->bp.pv = bp;
|
ap->bp.pv = (char *)bp;
|
||||||
ap->bp.iv ^= gcookie;
|
ap->bp.iv ^= gcookie;
|
||||||
}
|
}
|
||||||
*((void **)bp->last) = ptr; /* next available forward ptr */
|
*((void **)bp->last) = ptr; /* next available forward ptr */
|
||||||
@ -424,7 +424,7 @@ check_ptr(void *vp, PArea ap, PBlock *bpp, const char *what, const char *extra)
|
|||||||
if ((bp = check_bp(ap, what, 0)) == NULL)
|
if ((bp = check_bp(ap, what, 0)) == NULL)
|
||||||
AALLOC_ABORT("cannot continue");
|
AALLOC_ABORT("cannot continue");
|
||||||
ptr->iv ^= bp->cookie;
|
ptr->iv ^= bp->cookie;
|
||||||
if (ptr->pv < (void *)&bp->storage || ptr->pv >= bp->last) {
|
if (ptr->pv < (char *)&bp->storage || ptr->pv >= bp->last) {
|
||||||
AALLOC_WARN("trying to %s rogue pointer %p from area %p "
|
AALLOC_WARN("trying to %s rogue pointer %p from area %p "
|
||||||
"(block %p..%p), backpointer %p out of bounds%s",
|
"(block %p..%p), backpointer %p out of bounds%s",
|
||||||
what + 1, vp, ap, bp, bp->last, ptr->pv, extra);
|
what + 1, vp, ap, bp, bp->last, ptr->pv, extra);
|
||||||
@ -460,8 +460,9 @@ afree(void *vp, PArea ap)
|
|||||||
bp->last -= PVALIGN; /* mark the last forward pointer as free */
|
bp->last -= PVALIGN; /* mark the last forward pointer as free */
|
||||||
/* if our forward pointer was not the last one, relocate the latter */
|
/* if our forward pointer was not the last one, relocate the latter */
|
||||||
if (ptr->pv < bp->last) {
|
if (ptr->pv < bp->last) {
|
||||||
TPtr *tmp = bp->last; /* former last forward pointer */
|
TPtr *tmp;
|
||||||
|
|
||||||
|
tmp = (TPtr *)bp->last; /* former last forward pointer */
|
||||||
tmp->pv = ptr->pv; /* its backpointer to former our … */
|
tmp->pv = ptr->pv; /* its backpointer to former our … */
|
||||||
tmp->iv ^= bp->cookie; /* … forward pointer, and cookie it */
|
tmp->iv ^= bp->cookie; /* … forward pointer, and cookie it */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user