- fix integer overflows.

- bump VMEM_MAXORDER.

should fix "idx < VMEM_MAXORDER" assertion failure reported by
Martijn van Buul on current-users@.
This commit is contained in:
yamt 2006-08-16 13:19:03 +00:00
parent b70c967724
commit c777d5404f
1 changed files with 12 additions and 8 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr_vmem.c,v 1.3 2006/07/21 10:08:41 yamt Exp $ */ /* $NetBSD: subr_vmem.c,v 1.4 2006/08/16 13:19:03 yamt Exp $ */
/*- /*-
* Copyright (c)2006 YAMAMOTO Takashi, * Copyright (c)2006 YAMAMOTO Takashi,
@ -38,7 +38,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_vmem.c,v 1.3 2006/07/21 10:08:41 yamt Exp $"); __KERNEL_RCSID(0, "$NetBSD: subr_vmem.c,v 1.4 2006/08/16 13:19:03 yamt Exp $");
#define VMEM_DEBUG #define VMEM_DEBUG
@ -81,7 +81,7 @@ struct vmem_btag;
void vmem_dump(const vmem_t *); void vmem_dump(const vmem_t *);
#endif /* defined(VMEM_DEBUG) */ #endif /* defined(VMEM_DEBUG) */
#define VMEM_MAXORDER 20 #define VMEM_MAXORDER (sizeof(vmem_size_t) * CHAR_BIT)
#define VMEM_HASHSIZE_INIT 4096 /* XXX */ #define VMEM_HASHSIZE_INIT 4096 /* XXX */
#define VM_FITMASK (VM_BESTFIT | VM_INSTANTFIT) #define VM_FITMASK (VM_BESTFIT | VM_INSTANTFIT)
@ -143,20 +143,24 @@ typedef struct vmem_btag bt_t;
/* ---- misc */ /* ---- misc */
#define ORDER2SIZE(order) ((vmem_size_t)1 << (order))
static int static int
calc_order(vmem_size_t size) calc_order(vmem_size_t size)
{ {
vmem_size_t target;
int i; int i;
KASSERT(size != 0); KASSERT(size != 0);
i = 0; i = 0;
while (1 << (i + 1) <= size) { target = size >> 1;
while (ORDER2SIZE(i) <= target) {
i++; i++;
} }
KASSERT(1 << i <= size); KASSERT(ORDER2SIZE(i) <= size);
KASSERT(size < 1 << (i + 1)); KASSERT(size < ORDER2SIZE(i + 1) || ORDER2SIZE(i + 1) < ORDER2SIZE(i));
return i; return i;
} }
@ -259,7 +263,7 @@ bt_freehead_toalloc(vmem_t *vm, vmem_size_t size, vm_flag_t strat)
KASSERT(size != 0); KASSERT(size != 0);
idx = calc_order(qsize); idx = calc_order(qsize);
if (strat == VM_INSTANTFIT && 1 << idx != qsize) { if (strat == VM_INSTANTFIT && ORDER2SIZE(idx) != qsize) {
idx++; idx++;
/* check too large request? */ /* check too large request? */
} }
@ -516,7 +520,7 @@ vmem_create(const char *name, vmem_addr_t base, vmem_size_t size,
vm->vm_name = name; vm->vm_name = name;
vm->vm_quantum_mask = quantum - 1; vm->vm_quantum_mask = quantum - 1;
vm->vm_quantum_shift = calc_order(quantum); vm->vm_quantum_shift = calc_order(quantum);
KASSERT((1 << vm->vm_quantum_shift) == quantum); KASSERT(ORDER2SIZE(vm->vm_quantum_shift) == quantum);
vm->vm_allocfn = allocfn; vm->vm_allocfn = allocfn;
vm->vm_freefn = freefn; vm->vm_freefn = freefn;
vm->vm_source = source; vm->vm_source = source;