libc: rewrite brk() and sbrk()
Also fix Coverity 1 scan defects, CID 155773 and CID 155768, removing less-than-zero comparisons of unsigned values that were never true.
This commit is contained in:
parent
6e816b293d
commit
b05c21397e
@ -1,56 +1,60 @@
|
|||||||
/*
|
/*
|
||||||
* This file is part of the UCB release of Plan 9. It is subject to the license
|
* This file is part of Jehanne.
|
||||||
* terms in the LICENSE file found in the top-level directory of this
|
*
|
||||||
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
|
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
|
||||||
* part of the UCB release of Plan 9, including this file, may be copied,
|
*
|
||||||
* modified, propagated, or distributed except according to the terms contained
|
* Jehanne is free software: you can redistribute it and/or modify
|
||||||
* in the LICENSE file.
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, version 2 of the License.
|
||||||
|
*
|
||||||
|
* Jehanne is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
#define PORTABLE_SYSCALLS
|
||||||
#include <u.h>
|
#include <u.h>
|
||||||
#include <libc.h>
|
#include <libc.h>
|
||||||
|
|
||||||
extern char end[];
|
extern char end[];
|
||||||
static char *bloc = { end };
|
static char *last_brk = { end };
|
||||||
|
|
||||||
enum
|
#define ROUND_BRK(b) (((uintptr_t)b + 7) & ~7)
|
||||||
{
|
|
||||||
Round = 7
|
|
||||||
};
|
|
||||||
|
|
||||||
uintptr_t
|
static uintptr_t
|
||||||
brk_(uintptr_t p)
|
setbrk(uintptr_t p)
|
||||||
{
|
{
|
||||||
long f;
|
long b;
|
||||||
f = create("#0/brk/set", -1, p);
|
/* assert: devself is still working */
|
||||||
if(f >= 0){
|
assert((b = create("#0/brk/set", -1, p)) < 0);
|
||||||
/* this should never happen */
|
if(b == -1)
|
||||||
close(f);
|
return ~0; // an error occurred
|
||||||
return -1;
|
return (uintptr_t)~b;
|
||||||
}
|
|
||||||
return (uintptr_t)~f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
brk(void *p)
|
brk(void *p)
|
||||||
{
|
{
|
||||||
uintptr_t bl;
|
uintptr_t new_brk;
|
||||||
|
|
||||||
bl = ((uintptr_t)p + Round) & ~Round;
|
new_brk = ROUND_BRK(p);
|
||||||
if(brk_(bl) < 0)
|
if(setbrk(new_brk) == ~0)
|
||||||
return -1;
|
return -1;
|
||||||
bloc = (char*)bl;
|
last_brk = (char*)new_brk;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void*
|
void*
|
||||||
sbrk(uint32_t n)
|
sbrk(uint32_t increment)
|
||||||
{
|
{
|
||||||
uintptr_t bl;
|
uintptr_t new_brk;
|
||||||
|
|
||||||
bl = ((uintptr_t)bloc + Round) & ~Round;
|
new_brk = ROUND_BRK(last_brk);
|
||||||
if(brk_(bl+n) < 0)
|
if(setbrk(new_brk+increment) == ~0)
|
||||||
return (void*)-1;
|
return (void*)-1;
|
||||||
bloc = (char*)bl + n;
|
last_brk = (char*)new_brk + increment;
|
||||||
return (void*)bl;
|
return (void*)new_brk;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user