clang-format

This commit is contained in:
2024-09-10 13:03:02 -04:00
parent 53c617d779
commit d66450e427
381 changed files with 28864 additions and 34170 deletions

View File

@@ -34,102 +34,88 @@
#include <lib.h>
#include <array.h>
struct array *
array_create(void)
{
struct array *a;
struct array *array_create(void) {
struct array *a;
a = kmalloc(sizeof(*a));
if (a != NULL) {
array_init(a);
}
return a;
a = kmalloc(sizeof(*a));
if (a != NULL) {
array_init(a);
}
return a;
}
void
array_destroy(struct array *a)
{
array_cleanup(a);
kfree(a);
void array_destroy(struct array *a) {
array_cleanup(a);
kfree(a);
}
void
array_init(struct array *a)
{
a->num = a->max = 0;
a->v = NULL;
void array_init(struct array *a) {
a->num = a->max = 0;
a->v = NULL;
}
void
array_cleanup(struct array *a)
{
/*
* Require array to be empty - helps avoid memory leaks since
* we don't/can't free anything any contents may be pointing
* to.
*/
ARRAYASSERT(a->num == 0);
kfree(a->v);
void array_cleanup(struct array *a) {
/*
* Require array to be empty - helps avoid memory leaks since
* we don't/can't free anything any contents may be pointing
* to.
*/
ARRAYASSERT(a->num == 0);
kfree(a->v);
#ifdef ARRAYS_CHECKED
a->v = NULL;
a->v = NULL;
#endif
}
int
array_preallocate(struct array *a, unsigned num)
{
void **newptr;
unsigned newmax;
int array_preallocate(struct array *a, unsigned num) {
void **newptr;
unsigned newmax;
if (num > a->max) {
/* Don't touch A until the allocation succeeds. */
newmax = a->max;
while (num > newmax) {
newmax = newmax ? newmax*2 : 4;
}
if (num > a->max) {
/* Don't touch A until the allocation succeeds. */
newmax = a->max;
while (num > newmax) {
newmax = newmax ? newmax * 2 : 4;
}
/*
* We don't have krealloc, and it wouldn't be
* worthwhile to implement just for this. So just
* allocate a new block and copy. (Exercise: what
* about this and/or kmalloc makes it not worthwhile?)
*/
/*
* We don't have krealloc, and it wouldn't be
* worthwhile to implement just for this. So just
* allocate a new block and copy. (Exercise: what
* about this and/or kmalloc makes it not worthwhile?)
*/
newptr = kmalloc(newmax*sizeof(*a->v));
if (newptr == NULL) {
return ENOMEM;
}
memcpy(newptr, a->v, a->num*sizeof(*a->v));
kfree(a->v);
a->v = newptr;
a->max = newmax;
}
return 0;
newptr = kmalloc(newmax * sizeof(*a->v));
if (newptr == NULL) {
return ENOMEM;
}
memcpy(newptr, a->v, a->num * sizeof(*a->v));
kfree(a->v);
a->v = newptr;
a->max = newmax;
}
return 0;
}
int
array_setsize(struct array *a, unsigned num)
{
int result;
int array_setsize(struct array *a, unsigned num) {
int result;
result = array_preallocate(a, num);
if (result) {
return result;
}
a->num = num;
result = array_preallocate(a, num);
if (result) {
return result;
}
a->num = num;
return 0;
return 0;
}
void
array_remove(struct array *a, unsigned index)
{
unsigned num_to_move;
void array_remove(struct array *a, unsigned index) {
unsigned num_to_move;
ARRAYASSERT(a->num <= a->max);
ARRAYASSERT(index < a->num);
ARRAYASSERT(a->num <= a->max);
ARRAYASSERT(index < a->num);
num_to_move = a->num - (index + 1);
memmove(a->v + index, a->v + index+1, num_to_move*sizeof(void *));
a->num--;
num_to_move = a->num - (index + 1);
memmove(a->v + index, a->v + index + 1, num_to_move * sizeof(void *));
a->num--;
}

View File

@@ -43,134 +43,113 @@
* bitmap data saved on disk becomes endian-dependent, which is a
* severe nuisance.
*/
#define BITS_PER_WORD (CHAR_BIT)
#define WORD_TYPE unsigned char
#define WORD_ALLBITS (0xff)
#define BITS_PER_WORD (CHAR_BIT)
#define WORD_TYPE unsigned char
#define WORD_ALLBITS (0xff)
struct bitmap {
unsigned nbits;
WORD_TYPE *v;
unsigned nbits;
WORD_TYPE *v;
};
struct bitmap *bitmap_create(unsigned nbits) {
struct bitmap *b;
unsigned words;
struct bitmap *
bitmap_create(unsigned nbits)
{
struct bitmap *b;
unsigned words;
words = DIVROUNDUP(nbits, BITS_PER_WORD);
b = kmalloc(sizeof(struct bitmap));
if (b == NULL) {
return NULL;
}
b->v = kmalloc(words * sizeof(WORD_TYPE));
if (b->v == NULL) {
kfree(b);
return NULL;
}
words = DIVROUNDUP(nbits, BITS_PER_WORD);
b = kmalloc(sizeof(struct bitmap));
if (b == NULL) {
return NULL;
bzero(b->v, words * sizeof(WORD_TYPE));
b->nbits = nbits;
/* Mark any leftover bits at the end in use */
if (words > nbits / BITS_PER_WORD) {
unsigned j, ix = words - 1;
unsigned overbits = nbits - ix * BITS_PER_WORD;
KASSERT(nbits / BITS_PER_WORD == words - 1);
KASSERT(overbits > 0 && overbits < BITS_PER_WORD);
for (j = overbits; j < BITS_PER_WORD; j++) {
b->v[ix] |= ((WORD_TYPE)1 << j);
}
}
return b;
}
void *bitmap_getdata(struct bitmap *b) { return b->v; }
int bitmap_alloc(struct bitmap *b, unsigned *index) {
unsigned ix;
unsigned maxix = DIVROUNDUP(b->nbits, BITS_PER_WORD);
unsigned offset;
for (ix = 0; ix < maxix; ix++) {
if (b->v[ix] != WORD_ALLBITS) {
for (offset = 0; offset < BITS_PER_WORD; offset++) {
WORD_TYPE mask = ((WORD_TYPE)1) << offset;
if ((b->v[ix] & mask) == 0) {
b->v[ix] |= mask;
*index = (ix * BITS_PER_WORD) + offset;
KASSERT(*index < b->nbits);
return 0;
}
b->v = kmalloc(words*sizeof(WORD_TYPE));
if (b->v == NULL) {
kfree(b);
return NULL;
}
bzero(b->v, words*sizeof(WORD_TYPE));
b->nbits = nbits;
/* Mark any leftover bits at the end in use */
if (words > nbits / BITS_PER_WORD) {
unsigned j, ix = words-1;
unsigned overbits = nbits - ix*BITS_PER_WORD;
KASSERT(nbits / BITS_PER_WORD == words-1);
KASSERT(overbits > 0 && overbits < BITS_PER_WORD);
for (j=overbits; j<BITS_PER_WORD; j++) {
b->v[ix] |= ((WORD_TYPE)1 << j);
}
}
return b;
}
KASSERT(0);
}
}
return ENOSPC;
}
void *
bitmap_getdata(struct bitmap *b)
{
return b->v;
static inline void bitmap_translate(unsigned bitno, unsigned *ix,
WORD_TYPE *mask) {
unsigned offset;
*ix = bitno / BITS_PER_WORD;
offset = bitno % BITS_PER_WORD;
*mask = ((WORD_TYPE)1) << offset;
}
int
bitmap_alloc(struct bitmap *b, unsigned *index)
{
unsigned ix;
unsigned maxix = DIVROUNDUP(b->nbits, BITS_PER_WORD);
unsigned offset;
void bitmap_mark(struct bitmap *b, unsigned index) {
unsigned ix;
WORD_TYPE mask;
for (ix=0; ix<maxix; ix++) {
if (b->v[ix]!=WORD_ALLBITS) {
for (offset = 0; offset < BITS_PER_WORD; offset++) {
WORD_TYPE mask = ((WORD_TYPE)1) << offset;
KASSERT(index < b->nbits);
bitmap_translate(index, &ix, &mask);
if ((b->v[ix] & mask)==0) {
b->v[ix] |= mask;
*index = (ix*BITS_PER_WORD)+offset;
KASSERT(*index < b->nbits);
return 0;
}
}
KASSERT(0);
}
}
return ENOSPC;
KASSERT((b->v[ix] & mask) == 0);
b->v[ix] |= mask;
}
static
inline
void
bitmap_translate(unsigned bitno, unsigned *ix, WORD_TYPE *mask)
{
unsigned offset;
*ix = bitno / BITS_PER_WORD;
offset = bitno % BITS_PER_WORD;
*mask = ((WORD_TYPE)1) << offset;
void bitmap_unmark(struct bitmap *b, unsigned index) {
unsigned ix;
WORD_TYPE mask;
KASSERT(index < b->nbits);
bitmap_translate(index, &ix, &mask);
KASSERT((b->v[ix] & mask) != 0);
b->v[ix] &= ~mask;
}
void
bitmap_mark(struct bitmap *b, unsigned index)
{
unsigned ix;
WORD_TYPE mask;
int bitmap_isset(struct bitmap *b, unsigned index) {
unsigned ix;
WORD_TYPE mask;
KASSERT(index < b->nbits);
bitmap_translate(index, &ix, &mask);
KASSERT((b->v[ix] & mask)==0);
b->v[ix] |= mask;
bitmap_translate(index, &ix, &mask);
return (b->v[ix] & mask);
}
void
bitmap_unmark(struct bitmap *b, unsigned index)
{
unsigned ix;
WORD_TYPE mask;
KASSERT(index < b->nbits);
bitmap_translate(index, &ix, &mask);
KASSERT((b->v[ix] & mask)!=0);
b->v[ix] &= ~mask;
}
int
bitmap_isset(struct bitmap *b, unsigned index)
{
unsigned ix;
WORD_TYPE mask;
bitmap_translate(index, &ix, &mask);
return (b->v[ix] & mask);
}
void
bitmap_destroy(struct bitmap *b)
{
kfree(b->v);
kfree(b);
void bitmap_destroy(struct bitmap *b) {
kfree(b->v);
kfree(b);
}

View File

@@ -43,33 +43,23 @@
* loop-based.
*/
uint16_t
bswap16(uint16_t val)
{
return ((val & 0x00ff) << 8)
| ((val & 0xff00) >> 8);
uint16_t bswap16(uint16_t val) {
return ((val & 0x00ff) << 8) | ((val & 0xff00) >> 8);
}
uint32_t
bswap32(uint32_t val)
{
return ((val & 0x000000ff) << 24)
| ((val & 0x0000ff00) << 8)
| ((val & 0x00ff0000) >> 8)
| ((val & 0xff000000) >> 24);
uint32_t bswap32(uint32_t val) {
return ((val & 0x000000ff) << 24) | ((val & 0x0000ff00) << 8) |
((val & 0x00ff0000) >> 8) | ((val & 0xff000000) >> 24);
}
uint64_t
bswap64(uint64_t val)
{
return ((val & 0x00000000000000ff) << 56)
| ((val & 0x000000000000ff00) << 40)
| ((val & 0x0000000000ff0000) << 24)
| ((val & 0x00000000ff000000) << 8)
| ((val & 0x000000ff00000000) << 8)
| ((val & 0x0000ff0000000000) << 24)
| ((val & 0x00ff000000000000) >> 40)
| ((val & 0xff00000000000000) >> 56);
uint64_t bswap64(uint64_t val) {
return ((val & 0x00000000000000ff) << 56) |
((val & 0x000000000000ff00) << 40) |
((val & 0x0000000000ff0000) << 24) |
((val & 0x00000000ff000000) << 8) | ((val & 0x000000ff00000000) << 8) |
((val & 0x0000ff0000000000) << 24) |
((val & 0x00ff000000000000) >> 40) |
((val & 0xff00000000000000) >> 56);
}
/*
@@ -92,9 +82,9 @@ bswap64(uint64_t val)
*/
#if _BYTE_ORDER == _LITTLE_ENDIAN
#define TO(tag, bits, type) \
type ntoh##tag(type val) { return bswap##bits(val); } \
type hton##tag(type val) { return bswap##bits(val); }
#define TO(tag, bits, type) \
type ntoh##tag(type val) { return bswap##bits(val); } \
type hton##tag(type val) { return bswap##bits(val); }
#endif
/*
@@ -103,9 +93,9 @@ bswap64(uint64_t val)
* the wrong option.
*/
#if _BYTE_ORDER == _BIG_ENDIAN
#define TO(tag, bits, type) \
type ntoh##tag(type val) { return val; } \
type hton##tag(type val) { return val; }
#define TO(tag, bits, type) \
type ntoh##tag(type val) { return val; } \
type hton##tag(type val) { return val; }
#endif
#if _BYTE_ORDER == _PDP_ENDIAN
@@ -116,11 +106,10 @@ bswap64(uint64_t val)
#error "_BYTE_ORDER not set"
#endif
TO(s, 16, uint16_t)
TO(l, 32, uint32_t)
TO(s, 16, uint16_t)
TO(l, 32, uint32_t)
TO(ll, 64, uint64_t)
/*
* Some utility functions for handling 64-bit values.
*
@@ -134,27 +123,23 @@ TO(ll, 64, uint64_t)
* word.
*/
void
join32to64(uint32_t x1, uint32_t x2, uint64_t *y2)
{
void join32to64(uint32_t x1, uint32_t x2, uint64_t *y2) {
#if _BYTE_ORDER == _BIG_ENDIAN
*y2 = ((uint64_t)x1 << 32) | (uint64_t)x2;
*y2 = ((uint64_t)x1 << 32) | (uint64_t)x2;
#elif _BYTE_ORDER == _LITTLE_ENDIAN
*y2 = (uint64_t)x1 | ((uint64_t)x2 << 32);
*y2 = (uint64_t)x1 | ((uint64_t)x2 << 32);
#else
#error "Eh?"
#endif
}
void
split64to32(uint64_t x, uint32_t *y1, uint32_t *y2)
{
void split64to32(uint64_t x, uint32_t *y1, uint32_t *y2) {
#if _BYTE_ORDER == _BIG_ENDIAN
*y1 = x >> 32;
*y2 = x & 0xffffffff;
*y1 = x >> 32;
*y2 = x & 0xffffffff;
#elif _BYTE_ORDER == _LITTLE_ENDIAN
*y1 = x & 0xffffffff;
*y2 = x >> 32;
*y1 = x & 0xffffffff;
*y2 = x >> 32;
#else
#error "Eh?"
#endif

View File

@@ -27,7 +27,6 @@
* SUCH DAMAGE.
*/
#include <types.h>
#include <lib.h>
@@ -36,13 +35,10 @@
* We overwrite the current character with a space in case we're on
* a terminal where backspace is nondestructive.
*/
static
void
backsp(void)
{
putch('\b');
putch(' ');
putch('\b');
static void backsp(void) {
putch('\b');
putch(' ');
putch('\b');
}
/*
@@ -50,64 +46,56 @@ backsp(void)
* common control characters. Do not include the terminating newline
* in the buffer passed back.
*/
void
kgets(char *buf, size_t maxlen)
{
size_t pos = 0;
int ch;
void kgets(char *buf, size_t maxlen) {
size_t pos = 0;
int ch;
while (1) {
ch = getch();
if (ch=='\n' || ch=='\r') {
putch('\n');
break;
}
while (1) {
ch = getch();
if (ch == '\n' || ch == '\r') {
putch('\n');
break;
}
/* Only allow the normal 7-bit ascii */
if (ch>=32 && ch<127 && pos < maxlen-1) {
putch(ch);
buf[pos++] = ch;
}
else if ((ch=='\b' || ch==127) && pos>0) {
/* backspace */
backsp();
pos--;
}
else if (ch==3) {
/* ^C - return empty string */
putch('^');
putch('C');
putch('\n');
pos = 0;
break;
}
else if (ch==18) {
/* ^R - reprint input */
buf[pos] = 0;
kprintf("^R\n%s", buf);
}
else if (ch==21) {
/* ^U - erase line */
while (pos > 0) {
backsp();
pos--;
}
}
else if (ch==23) {
/* ^W - erase word */
while (pos > 0 && buf[pos-1]==' ') {
backsp();
pos--;
}
while (pos > 0 && buf[pos-1]!=' ') {
backsp();
pos--;
}
}
else {
beep();
}
}
/* Only allow the normal 7-bit ascii */
if (ch >= 32 && ch < 127 && pos < maxlen - 1) {
putch(ch);
buf[pos++] = ch;
} else if ((ch == '\b' || ch == 127) && pos > 0) {
/* backspace */
backsp();
pos--;
} else if (ch == 3) {
/* ^C - return empty string */
putch('^');
putch('C');
putch('\n');
pos = 0;
break;
} else if (ch == 18) {
/* ^R - reprint input */
buf[pos] = 0;
kprintf("^R\n%s", buf);
} else if (ch == 21) {
/* ^U - erase line */
while (pos > 0) {
backsp();
pos--;
}
} else if (ch == 23) {
/* ^W - erase word */
while (pos > 0 && buf[pos - 1] == ' ') {
backsp();
pos--;
}
while (pos > 0 && buf[pos - 1] != ' ') {
backsp();
pos--;
}
} else {
beep();
}
}
buf[pos] = 0;
buf[pos] = 0;
}

View File

@@ -37,10 +37,9 @@
#include <current.h>
#include <synch.h>
#include <mainbus.h>
#include <vfs.h> // for vfs_sync()
#include <vfs.h> // for vfs_sync()
#include <lamebus/ltrace.h> // for ltrace_stop()
/* Flags word for DEBUG() macro. */
uint32_t dbflags = 0;
@@ -50,79 +49,66 @@ static struct lock *kprintf_lock;
/* Lock for polled kprintfs */
static struct spinlock kprintf_spinlock;
/*
* Warning: all this has to work from interrupt handlers and when
* interrupts are disabled.
*/
/*
* Create the kprintf lock. Must be called before creating a second
* thread or enabling a second CPU.
*/
void
kprintf_bootstrap(void)
{
KASSERT(kprintf_lock == NULL);
void kprintf_bootstrap(void) {
KASSERT(kprintf_lock == NULL);
kprintf_lock = lock_create("kprintf_lock");
if (kprintf_lock == NULL) {
panic("Could not create kprintf_lock\n");
}
spinlock_init(&kprintf_spinlock);
kprintf_lock = lock_create("kprintf_lock");
if (kprintf_lock == NULL) {
panic("Could not create kprintf_lock\n");
}
spinlock_init(&kprintf_spinlock);
}
/*
* Send characters to the console. Backend for __printf.
*/
static
void
console_send(void *junk, const char *data, size_t len)
{
size_t i;
static void console_send(void *junk, const char *data, size_t len) {
size_t i;
(void)junk;
(void)junk;
for (i=0; i<len; i++) {
putch(data[i]);
}
for (i = 0; i < len; i++) {
putch(data[i]);
}
}
/*
* Printf to the console.
*/
int
kprintf(const char *fmt, ...)
{
int chars;
va_list ap;
bool dolock;
int kprintf(const char *fmt, ...) {
int chars;
va_list ap;
bool dolock;
dolock = kprintf_lock != NULL
&& curthread->t_in_interrupt == false
&& curthread->t_curspl == 0
&& curcpu->c_spinlocks == 0;
dolock = kprintf_lock != NULL && curthread->t_in_interrupt == false &&
curthread->t_curspl == 0 && curcpu->c_spinlocks == 0;
if (dolock) {
lock_acquire(kprintf_lock);
}
else {
spinlock_acquire(&kprintf_spinlock);
}
if (dolock) {
lock_acquire(kprintf_lock);
} else {
spinlock_acquire(&kprintf_spinlock);
}
va_start(ap, fmt);
chars = __vprintf(console_send, NULL, fmt, ap);
va_end(ap);
va_start(ap, fmt);
chars = __vprintf(console_send, NULL, fmt, ap);
va_end(ap);
if (dolock) {
lock_release(kprintf_lock);
}
else {
spinlock_release(&kprintf_spinlock);
}
if (dolock) {
lock_release(kprintf_lock);
} else {
spinlock_release(&kprintf_spinlock);
}
return chars;
return chars;
}
/*
@@ -130,87 +116,83 @@ kprintf(const char *fmt, ...)
* passed and then halts the system.
*/
void
panic(const char *fmt, ...)
{
va_list ap;
void panic(const char *fmt, ...) {
va_list ap;
/*
* When we reach panic, the system is usually fairly screwed up.
* It's not entirely uncommon for anything else we try to do
* here to trigger more panics.
*
* This variable makes sure that if we try to do something here,
* and it causes another panic, *that* panic doesn't try again;
* trying again almost inevitably causes infinite recursion.
*
* This is not excessively paranoid - these things DO happen!
*/
static volatile int evil;
/*
* When we reach panic, the system is usually fairly screwed up.
* It's not entirely uncommon for anything else we try to do
* here to trigger more panics.
*
* This variable makes sure that if we try to do something here,
* and it causes another panic, *that* panic doesn't try again;
* trying again almost inevitably causes infinite recursion.
*
* This is not excessively paranoid - these things DO happen!
*/
static volatile int evil;
if (evil == 0) {
evil = 1;
if (evil == 0) {
evil = 1;
/*
* Not only do we not want to be interrupted while
* panicking, but we also want the console to be
* printing in polling mode so as not to do context
* switches. So turn interrupts off on this CPU.
*/
splhigh();
}
/*
* Not only do we not want to be interrupted while
* panicking, but we also want the console to be
* printing in polling mode so as not to do context
* switches. So turn interrupts off on this CPU.
*/
splhigh();
}
if (evil == 1) {
evil = 2;
if (evil == 1) {
evil = 2;
/* Kill off other threads and halt other CPUs. */
thread_panic();
}
/* Kill off other threads and halt other CPUs. */
thread_panic();
}
if (evil == 2) {
evil = 3;
if (evil == 2) {
evil = 3;
/* Print the message. */
kprintf("panic: ");
va_start(ap, fmt);
__vprintf(console_send, NULL, fmt, ap);
va_end(ap);
}
/* Print the message. */
kprintf("panic: ");
va_start(ap, fmt);
__vprintf(console_send, NULL, fmt, ap);
va_end(ap);
}
if (evil == 3) {
evil = 4;
if (evil == 3) {
evil = 4;
/* Drop to the debugger. */
ltrace_stop(0);
}
/* Drop to the debugger. */
ltrace_stop(0);
}
if (evil == 4) {
evil = 5;
if (evil == 4) {
evil = 5;
/* Try to sync the disks. */
vfs_sync();
}
/* Try to sync the disks. */
vfs_sync();
}
if (evil == 5) {
evil = 6;
if (evil == 5) {
evil = 6;
/* Shut down or reboot the system. */
mainbus_panic();
}
/* Shut down or reboot the system. */
mainbus_panic();
}
/*
* Last resort, just in case.
*/
/*
* Last resort, just in case.
*/
for (;;);
for (;;)
;
}
/*
* Assertion failures go through this.
*/
void
badassert(const char *expr, const char *file, int line, const char *func)
{
panic("Assertion failed: %s, at %s:%d (%s)\n",
expr, file, line, func);
void badassert(const char *expr, const char *file, int line, const char *func) {
panic("Assertion failed: %s, at %s:%d (%s)\n", expr, file, line, func);
}

View File

@@ -34,29 +34,25 @@
/*
* Like strdup, but calls kmalloc.
*/
char *
kstrdup(const char *s)
{
char *z;
char *kstrdup(const char *s) {
char *z;
z = kmalloc(strlen(s)+1);
if (z == NULL) {
return NULL;
}
strcpy(z, s);
return z;
z = kmalloc(strlen(s) + 1);
if (z == NULL) {
return NULL;
}
strcpy(z, s);
return z;
}
/*
* Standard C function to return a string for a given errno.
* Kernel version; panics if it hits an unknown error.
*/
const char *
strerror(int errcode)
{
if (errcode>=0 && errcode < sys_nerr) {
return sys_errlist[errcode];
}
panic("Invalid error code %d\n", errcode);
return NULL;
const char *strerror(int errcode) {
if (errcode >= 0 && errcode < sys_nerr) {
return sys_errlist[errcode];
}
panic("Invalid error code %d\n", errcode);
return NULL;
}

View File

@@ -33,37 +33,31 @@
/*
* ts1 + ts2
*/
void
timespec_add(const struct timespec *ts1,
const struct timespec *ts2,
struct timespec *ret)
{
ret->tv_nsec = ts1->tv_nsec + ts2->tv_nsec;
ret->tv_sec = ts1->tv_sec + ts2->tv_sec;
if (ret->tv_nsec >= 1000000000) {
ret->tv_nsec -= 1000000000;
ret->tv_sec += 1;
}
void timespec_add(const struct timespec *ts1, const struct timespec *ts2,
struct timespec *ret) {
ret->tv_nsec = ts1->tv_nsec + ts2->tv_nsec;
ret->tv_sec = ts1->tv_sec + ts2->tv_sec;
if (ret->tv_nsec >= 1000000000) {
ret->tv_nsec -= 1000000000;
ret->tv_sec += 1;
}
}
/*
* ts1 - ts2
*/
void
timespec_sub(const struct timespec *ts1,
const struct timespec *ts2,
struct timespec *ret)
{
/* in case ret and ts1 or ts2 are the same */
struct timespec r;
void timespec_sub(const struct timespec *ts1, const struct timespec *ts2,
struct timespec *ret) {
/* in case ret and ts1 or ts2 are the same */
struct timespec r;
r = *ts1;
if (r.tv_nsec < ts2->tv_nsec) {
r.tv_nsec += 1000000000;
r.tv_sec--;
}
r = *ts1;
if (r.tv_nsec < ts2->tv_nsec) {
r.tv_nsec += 1000000000;
r.tv_sec--;
}
r.tv_nsec -= ts2->tv_nsec;
r.tv_sec -= ts2->tv_sec;
*ret = r;
r.tv_nsec -= ts2->tv_nsec;
r.tv_sec -= ts2->tv_sec;
*ret = r;
}

View File

@@ -38,127 +38,117 @@
* See uio.h for a description.
*/
int
uiomove(void *ptr, size_t n, struct uio *uio)
{
struct iovec *iov;
size_t size;
int result;
int uiomove(void *ptr, size_t n, struct uio *uio) {
struct iovec *iov;
size_t size;
int result;
if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) {
panic("uiomove: Invalid uio_rw %d\n", (int) uio->uio_rw);
}
if (uio->uio_segflg==UIO_SYSSPACE) {
KASSERT(uio->uio_space == NULL);
}
else {
KASSERT(uio->uio_space == proc_getas());
}
if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE) {
panic("uiomove: Invalid uio_rw %d\n", (int)uio->uio_rw);
}
if (uio->uio_segflg == UIO_SYSSPACE) {
KASSERT(uio->uio_space == NULL);
} else {
KASSERT(uio->uio_space == proc_getas());
}
while (n > 0 && uio->uio_resid > 0) {
/* get the first iovec */
iov = uio->uio_iov;
size = iov->iov_len;
while (n > 0 && uio->uio_resid > 0) {
/* get the first iovec */
iov = uio->uio_iov;
size = iov->iov_len;
if (size > n) {
size = n;
}
if (size > n) {
size = n;
}
if (size == 0) {
/* move to the next iovec and try again */
uio->uio_iov++;
uio->uio_iovcnt--;
if (uio->uio_iovcnt == 0) {
/*
* This should only happen if you set
* uio_resid incorrectly (to more than
* the total length of buffers the uio
* points to).
*/
panic("uiomove: ran out of buffers\n");
}
continue;
}
if (size == 0) {
/* move to the next iovec and try again */
uio->uio_iov++;
uio->uio_iovcnt--;
if (uio->uio_iovcnt == 0) {
/*
* This should only happen if you set
* uio_resid incorrectly (to more than
* the total length of buffers the uio
* points to).
*/
panic("uiomove: ran out of buffers\n");
}
continue;
}
switch (uio->uio_segflg) {
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ) {
memmove(iov->iov_kbase, ptr, size);
}
else {
memmove(ptr, iov->iov_kbase, size);
}
iov->iov_kbase = ((char *)iov->iov_kbase+size);
break;
case UIO_USERSPACE:
case UIO_USERISPACE:
if (uio->uio_rw == UIO_READ) {
result = copyout(ptr, iov->iov_ubase,size);
}
else {
result = copyin(iov->iov_ubase, ptr, size);
}
if (result) {
return result;
}
iov->iov_ubase += size;
break;
default:
panic("uiomove: Invalid uio_segflg %d\n",
(int)uio->uio_segflg);
}
switch (uio->uio_segflg) {
case UIO_SYSSPACE:
if (uio->uio_rw == UIO_READ) {
memmove(iov->iov_kbase, ptr, size);
} else {
memmove(ptr, iov->iov_kbase, size);
}
iov->iov_kbase = ((char *)iov->iov_kbase + size);
break;
case UIO_USERSPACE:
case UIO_USERISPACE:
if (uio->uio_rw == UIO_READ) {
result = copyout(ptr, iov->iov_ubase, size);
} else {
result = copyin(iov->iov_ubase, ptr, size);
}
if (result) {
return result;
}
iov->iov_ubase += size;
break;
default:
panic("uiomove: Invalid uio_segflg %d\n", (int)uio->uio_segflg);
}
iov->iov_len -= size;
uio->uio_resid -= size;
uio->uio_offset += size;
ptr = ((char *)ptr + size);
n -= size;
}
iov->iov_len -= size;
uio->uio_resid -= size;
uio->uio_offset += size;
ptr = ((char *)ptr + size);
n -= size;
}
return 0;
return 0;
}
int
uiomovezeros(size_t n, struct uio *uio)
{
/* static, so initialized as zero */
static char zeros[16];
size_t amt;
int result;
int uiomovezeros(size_t n, struct uio *uio) {
/* static, so initialized as zero */
static char zeros[16];
size_t amt;
int result;
/* This only makes sense when reading */
KASSERT(uio->uio_rw == UIO_READ);
/* This only makes sense when reading */
KASSERT(uio->uio_rw == UIO_READ);
while (n > 0) {
amt = sizeof(zeros);
if (amt > n) {
amt = n;
}
result = uiomove(zeros, amt, uio);
if (result) {
return result;
}
n -= amt;
}
while (n > 0) {
amt = sizeof(zeros);
if (amt > n) {
amt = n;
}
result = uiomove(zeros, amt, uio);
if (result) {
return result;
}
n -= amt;
}
return 0;
return 0;
}
/*
* Convenience function to initialize an iovec and uio for kernel I/O.
*/
void
uio_kinit(struct iovec *iov, struct uio *u,
void *kbuf, size_t len, off_t pos, enum uio_rw rw)
{
iov->iov_kbase = kbuf;
iov->iov_len = len;
u->uio_iov = iov;
u->uio_iovcnt = 1;
u->uio_offset = pos;
u->uio_resid = len;
u->uio_segflg = UIO_SYSSPACE;
u->uio_rw = rw;
u->uio_space = NULL;
void uio_kinit(struct iovec *iov, struct uio *u, void *kbuf, size_t len,
off_t pos, enum uio_rw rw) {
iov->iov_kbase = kbuf;
iov->iov_len = len;
u->uio_iov = iov;
u->uio_iovcnt = 1;
u->uio_offset = pos;
u->uio_resid = len;
u->uio_segflg = UIO_SYSSPACE;
u->uio_rw = rw;
u->uio_space = NULL;
}