subr_unit.c (143283) | subr_unit.c (143550) |
---|---|
1/*- 2 * Copyright (c) 2004 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * | 1/*- 2 * Copyright (c) 2004 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 9 unchanged lines hidden (view full) --- 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * |
26 * $FreeBSD: head/sys/kern/subr_unit.c 143283 2005-03-08 10:40:48Z phk $ | 26 * $FreeBSD: head/sys/kern/subr_unit.c 143550 2005-03-14 06:51:29Z phk $ |
27 * 28 * 29 * Unit number allocation functions. 30 * 31 * These functions implement a mixed run-length/bitmap management of unit 32 * number spaces in a very memory efficient manner. 33 * 34 * Allocation policy is always lowest free number first. --- 159 unchanged lines hidden (view full) --- 194 u_int high; /* Highest item */ 195 u_int busy; /* Count of allocated items */ 196 u_int alloc; /* Count of memory allocations */ 197 u_int first; /* items in allocated from start */ 198 u_int last; /* items free at end */ 199 struct mtx *mtx; 200}; 201 | 27 * 28 * 29 * Unit number allocation functions. 30 * 31 * These functions implement a mixed run-length/bitmap management of unit 32 * number spaces in a very memory efficient manner. 33 * 34 * Allocation policy is always lowest free number first. --- 159 unchanged lines hidden (view full) --- 194 u_int high; /* Highest item */ 195 u_int busy; /* Count of allocated items */ 196 u_int alloc; /* Count of memory allocations */ 197 u_int first; /* items in allocated from start */ 198 u_int last; /* items free at end */ 199 struct mtx *mtx; 200}; 201 |
202static void print_unrhdr(struct unrhdr *uh); | |
203 204#if defined(DIAGNOSTIC) || !defined(_KERNEL) 205/* 206 * Consistency check function. 207 * 208 * Checks the internal consistency as well as we can. 209 * 210 * Called at all boundaries of this API. --- 95 unchanged lines hidden (view full) --- 306 else 307 uh->mtx = &unitmtx; 308 TAILQ_INIT(&uh->head); 309 uh->low = low; 310 uh->high = high; 311 uh->first = 0; 312 uh->last = 1 + (high - low); 313 check_unrhdr(uh, __LINE__); | 202 203#if defined(DIAGNOSTIC) || !defined(_KERNEL) 204/* 205 * Consistency check function. 206 * 207 * Checks the internal consistency as well as we can. 208 * 209 * Called at all boundaries of this API. --- 95 unchanged lines hidden (view full) --- 305 else 306 uh->mtx = &unitmtx; 307 TAILQ_INIT(&uh->head); 308 uh->low = low; 309 uh->high = high; 310 uh->first = 0; 311 uh->last = 1 + (high - low); 312 check_unrhdr(uh, __LINE__); |
314printf("NEW_UNRHDR %x-%x -> %p\n", low, high, uh); | |
315 return (uh); 316} 317 318void 319delete_unrhdr(struct unrhdr *uh) 320{ 321 322 check_unrhdr(uh, __LINE__); --- 236 unchanged lines hidden (view full) --- 559 uh->busy++; 560 return (x); 561 } 562 563 /* 564 * We can always allocate from the first list element, so if we have 565 * nothing on the list, we must have run out of unit numbers. 566 */ | 313 return (uh); 314} 315 316void 317delete_unrhdr(struct unrhdr *uh) 318{ 319 320 check_unrhdr(uh, __LINE__); --- 236 unchanged lines hidden (view full) --- 557 uh->busy++; 558 return (x); 559 } 560 561 /* 562 * We can always allocate from the first list element, so if we have 563 * nothing on the list, we must have run out of unit numbers. 564 */ |
567 if (up == NULL) { 568printf("Out of units %p\n", uh); 569print_unrhdr(uh); | 565 if (up == NULL) |
570 return (-1); | 566 return (-1); |
571 } | |
572 573 KASSERT(up->ptr != uh, ("UNR first element is allocated")); 574 575 if (up->ptr == NULL) { /* free run */ 576 uh->first++; 577 up->len--; 578 } else { /* bitmap */ 579 ub = up->ptr; --- 145 unchanged lines hidden (view full) --- 725 free_unrl(uh, item, &p1, &p2); 726 mtx_unlock(uh->mtx); 727 if (p1 != NULL) 728 Free(p1); 729 if (p2 != NULL) 730 Free(p2); 731} 732 | 567 568 KASSERT(up->ptr != uh, ("UNR first element is allocated")); 569 570 if (up->ptr == NULL) { /* free run */ 571 uh->first++; 572 up->len--; 573 } else { /* bitmap */ 574 ub = up->ptr; --- 145 unchanged lines hidden (view full) --- 720 free_unrl(uh, item, &p1, &p2); 721 mtx_unlock(uh->mtx); 722 if (p1 != NULL) 723 Free(p1); 724 if (p2 != NULL) 725 Free(p2); 726} 727 |
728#ifndef _KERNEL /* USERLAND test driver */ 729 |
|
733/* 734 * Simple stochastic test driver for the above functions 735 */ 736 737static void 738print_unr(struct unrhdr *uh, struct unr *up) 739{ 740 u_int x; --- 32 unchanged lines hidden (view full) --- 773 print_unr(uh, up); 774 if (up->ptr == NULL || up->ptr == uh) 775 x += up->len; 776 else 777 x += NBITS; 778 } 779} 780 | 730/* 731 * Simple stochastic test driver for the above functions 732 */ 733 734static void 735print_unr(struct unrhdr *uh, struct unr *up) 736{ 737 u_int x; --- 32 unchanged lines hidden (view full) --- 770 print_unr(uh, up); 771 if (up->ptr == NULL || up->ptr == uh) 772 x += up->len; 773 else 774 x += NBITS; 775 } 776} 777 |
781#ifndef _KERNEL /* USERLAND test driver */ 782 | |
783/* Number of unrs to test */ 784#define NN 10000 785 786int 787main(int argc __unused, const char **argv __unused) 788{ 789 struct unrhdr *uh; 790 u_int i, x, m, j; --- 49 unchanged lines hidden --- | 778/* Number of unrs to test */ 779#define NN 10000 780 781int 782main(int argc __unused, const char **argv __unused) 783{ 784 struct unrhdr *uh; 785 u_int i, x, m, j; --- 49 unchanged lines hidden --- |