2004-09-30 07:04:03 +00:00
|
|
|
/*-
|
2017-11-27 15:20:12 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2004-09-30 07:04:03 +00:00
|
|
|
* Copyright (c) 2004 Poul-Henning Kamp
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
2005-03-08 10:40:48 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Unit number allocation functions.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
|
|
|
* These functions implement a mixed run-length/bitmap management of unit
|
2005-03-08 10:40:48 +00:00
|
|
|
* number spaces in a very memory efficient manner.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
2005-03-08 10:40:48 +00:00
|
|
|
* Allocation policy is always lowest free number first.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
2005-03-08 10:40:48 +00:00
|
|
|
* A return value of -1 signals that no more unit numbers are available.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
2005-03-08 10:40:48 +00:00
|
|
|
* There is no cost associated with the range of unitnumbers, so unless
|
|
|
|
* the resource really is finite, specify INT_MAX to new_unrhdr() and
|
|
|
|
* forget about checking the return value.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
2005-03-08 10:40:48 +00:00
|
|
|
* If a mutex is not provided when the unit number space is created, a
|
|
|
|
* default global mutex is used. The advantage to passing a mutex in, is
|
2011-02-21 09:01:34 +00:00
|
|
|
* that the alloc_unrl() function can be called with the mutex already
|
2005-03-08 10:40:48 +00:00
|
|
|
* held (it will not be released by alloc_unrl()).
|
|
|
|
*
|
|
|
|
* The allocation function alloc_unr{l}() never sleeps (but it may block on
|
|
|
|
* the mutex of course).
|
|
|
|
*
|
|
|
|
* Freeing a unit number may require allocating memory, and can therefore
|
|
|
|
* sleep so the free_unr() function does not come in a pre-locked variant.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
|
|
|
* A userland test program is included.
|
|
|
|
*
|
2011-02-21 09:01:34 +00:00
|
|
|
* Memory usage is a very complex function of the exact allocation
|
2005-03-08 10:40:48 +00:00
|
|
|
* pattern, but always very compact:
|
|
|
|
* * For the very typical case where a single unbroken run of unit
|
|
|
|
* numbers are allocated 44 bytes are used on i386.
|
|
|
|
* * For a unit number space of 1000 units and the random pattern
|
|
|
|
* in the usermode test program included, the worst case usage
|
|
|
|
* was 252 bytes on i386 for 500 allocated and 500 free units.
|
|
|
|
* * For a unit number space of 10000 units and the random pattern
|
|
|
|
* in the usermode test program included, the worst case usage
|
|
|
|
* was 798 bytes on i386 for 5000 allocated and 5000 free units.
|
|
|
|
* * The worst case is where every other unit number is allocated and
|
2012-09-14 21:28:56 +00:00
|
|
|
* the rest are free. In that case 44 + N/4 bytes are used where
|
2005-03-08 10:40:48 +00:00
|
|
|
* N is the number of the highest unit allocated.
|
2004-09-30 07:04:03 +00:00
|
|
|
*/
|
|
|
|
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
#include <sys/param.h>
|
2004-09-30 07:04:03 +00:00
|
|
|
#include <sys/types.h>
|
2013-08-30 07:37:45 +00:00
|
|
|
#include <sys/_unrhdr.h>
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
#ifdef _KERNEL
|
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
#include <sys/bitstring.h>
|
2004-09-30 07:04:03 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
2005-03-08 10:40:48 +00:00
|
|
|
#include <sys/limits.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In theory it would be smarter to allocate the individual blocks
|
|
|
|
* with the zone allocator, but at this time the expectation is that
|
|
|
|
* there will typically not even be enough allocations to fill a single
|
|
|
|
* page, so we stick with malloc for now.
|
|
|
|
*/
|
|
|
|
static MALLOC_DEFINE(M_UNIT, "Unitno", "Unit number allocation");
|
|
|
|
|
|
|
|
#define Malloc(foo) malloc(foo, M_UNIT, M_WAITOK | M_ZERO)
|
|
|
|
#define Free(foo) free(foo, M_UNIT)
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
static struct mtx unitmtx;
|
|
|
|
|
|
|
|
MTX_SYSINIT(unit, &unitmtx, "unit# allocation", MTX_DEF);
|
|
|
|
|
2018-11-20 14:58:41 +00:00
|
|
|
#ifdef UNR64_LOCKED
|
|
|
|
uint64_t
|
|
|
|
alloc_unr64(struct unrhdr64 *unr64)
|
|
|
|
{
|
|
|
|
uint64_t item;
|
|
|
|
|
|
|
|
mtx_lock(&unitmtx);
|
|
|
|
item = unr64->counter++;
|
|
|
|
mtx_unlock(&unitmtx);
|
|
|
|
return (item);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
#else /* ...USERLAND */
|
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
#include <bitstring.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <stdbool.h>
|
2004-09-30 07:04:03 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2005-03-08 10:40:48 +00:00
|
|
|
#include <string.h>
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
#define KASSERT(cond, arg) \
|
|
|
|
do { \
|
|
|
|
if (!(cond)) { \
|
|
|
|
printf arg; \
|
2005-03-08 10:40:48 +00:00
|
|
|
abort(); \
|
2004-09-30 07:04:03 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
static int no_alloc;
|
|
|
|
#define Malloc(foo) _Malloc(foo, __LINE__)
|
|
|
|
static void *
|
|
|
|
_Malloc(size_t foo, int line)
|
|
|
|
{
|
|
|
|
|
|
|
|
KASSERT(no_alloc == 0, ("malloc in wrong place() line %d", line));
|
|
|
|
return (calloc(foo, 1));
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
#define Free(foo) free(foo)
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unrhdr;
|
|
|
|
|
|
|
|
struct mtx {
|
|
|
|
int state;
|
|
|
|
} unitmtx;
|
|
|
|
|
|
|
|
static void
|
|
|
|
mtx_lock(struct mtx *mp)
|
|
|
|
{
|
|
|
|
KASSERT(mp->state == 0, ("mutex already locked"));
|
|
|
|
mp->state = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mtx_unlock(struct mtx *mp)
|
|
|
|
{
|
|
|
|
KASSERT(mp->state == 1, ("mutex not locked"));
|
|
|
|
mp->state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MA_OWNED 9
|
|
|
|
|
|
|
|
static void
|
|
|
|
mtx_assert(struct mtx *mp, int flag)
|
|
|
|
{
|
|
|
|
if (flag == MA_OWNED) {
|
|
|
|
KASSERT(mp->state == 1, ("mtx_assert(MA_OWNED) not true"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CTASSERT(foo)
|
2010-06-17 16:12:06 +00:00
|
|
|
#define WITNESS_WARN(flags, lock, fmt, ...) (void)0
|
2005-03-08 10:40:48 +00:00
|
|
|
|
|
|
|
#endif /* USERLAND */
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is our basic building block.
|
|
|
|
*
|
|
|
|
* It can be used in three different ways depending on the value of the ptr
|
|
|
|
* element:
|
|
|
|
* If ptr is NULL, it represents a run of free items.
|
|
|
|
* If ptr points to the unrhdr it represents a run of allocated items.
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
* Otherwise it points to a bitstring of allocated items.
|
2004-09-30 07:04:03 +00:00
|
|
|
*
|
|
|
|
* For runs the len field is the length of the run.
|
|
|
|
* For bitmaps the len field represents the number of allocated items.
|
|
|
|
*
|
|
|
|
* The bitmap is the same size as struct unr to optimize memory management.
|
|
|
|
*/
|
|
|
|
struct unr {
|
|
|
|
TAILQ_ENTRY(unr) list;
|
|
|
|
u_int len;
|
|
|
|
void *ptr;
|
|
|
|
};
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unrb {
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
bitstr_t map[sizeof(struct unr) / sizeof(bitstr_t)];
|
2005-03-08 10:40:48 +00:00
|
|
|
};
|
|
|
|
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
CTASSERT((sizeof(struct unr) % sizeof(bitstr_t)) == 0);
|
|
|
|
|
|
|
|
/* Number of bits we can store in the bitmap */
|
|
|
|
#define NBITS (8 * sizeof(((struct unrb*)NULL)->map))
|
|
|
|
|
|
|
|
/* Is the unrb empty in at least the first len bits? */
|
|
|
|
static inline bool
|
|
|
|
ub_empty(struct unrb *ub, int len) {
|
|
|
|
int first_set;
|
|
|
|
|
|
|
|
bit_ffs(ub->map, len, &first_set);
|
|
|
|
return (first_set == -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the unrb full? That is, is the number of set elements equal to len? */
|
|
|
|
static inline bool
|
|
|
|
ub_full(struct unrb *ub, int len)
|
|
|
|
{
|
|
|
|
int first_clear;
|
|
|
|
|
|
|
|
bit_ffc(ub->map, len, &first_clear);
|
|
|
|
return (first_clear == -1);
|
|
|
|
}
|
2005-03-08 10:40:48 +00:00
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
#if defined(DIAGNOSTIC) || !defined(_KERNEL)
|
|
|
|
/*
|
|
|
|
* Consistency check function.
|
|
|
|
*
|
|
|
|
* Checks the internal consistency as well as we can.
|
2017-01-14 04:16:13 +00:00
|
|
|
*
|
2004-09-30 07:04:03 +00:00
|
|
|
* Called at all boundaries of this API.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
check_unrhdr(struct unrhdr *uh, int line)
|
|
|
|
{
|
|
|
|
struct unr *up;
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unrb *ub;
|
2016-05-23 20:29:18 +00:00
|
|
|
int w;
|
|
|
|
u_int y, z;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
y = uh->first;
|
2004-09-30 07:04:03 +00:00
|
|
|
z = 0;
|
|
|
|
TAILQ_FOREACH(up, &uh->head, list) {
|
|
|
|
z++;
|
|
|
|
if (up->ptr != uh && up->ptr != NULL) {
|
2005-03-08 10:40:48 +00:00
|
|
|
ub = up->ptr;
|
|
|
|
KASSERT (up->len <= NBITS,
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
("UNR inconsistency: len %u max %zd (line %d)\n",
|
2005-03-08 10:40:48 +00:00
|
|
|
up->len, NBITS, line));
|
2004-09-30 07:04:03 +00:00
|
|
|
z++;
|
|
|
|
w = 0;
|
2016-05-23 20:29:18 +00:00
|
|
|
bit_count(ub->map, 0, up->len, &w);
|
2005-03-08 10:40:48 +00:00
|
|
|
y += w;
|
2017-01-14 04:16:13 +00:00
|
|
|
} else if (up->ptr != NULL)
|
2004-09-30 07:04:03 +00:00
|
|
|
y += up->len;
|
|
|
|
}
|
|
|
|
KASSERT (y == uh->busy,
|
|
|
|
("UNR inconsistency: items %u found %u (line %d)\n",
|
|
|
|
uh->busy, y, line));
|
|
|
|
KASSERT (z == uh->alloc,
|
|
|
|
("UNR inconsistency: chunks %u found %u (line %d)\n",
|
|
|
|
uh->alloc, z, line));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static __inline void
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
check_unrhdr(struct unrhdr *uh __unused, int line __unused)
|
2004-09-30 07:04:03 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Userland memory management. Just use calloc and keep track of how
|
|
|
|
* many elements we have allocated for check_unrhdr().
|
|
|
|
*/
|
|
|
|
|
|
|
|
static __inline void *
|
2005-03-08 10:40:48 +00:00
|
|
|
new_unr(struct unrhdr *uh, void **p1, void **p2)
|
2004-09-30 07:04:03 +00:00
|
|
|
{
|
2005-03-08 10:40:48 +00:00
|
|
|
void *p;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
uh->alloc++;
|
|
|
|
KASSERT(*p1 != NULL || *p2 != NULL, ("Out of cached memory"));
|
|
|
|
if (*p1 != NULL) {
|
|
|
|
p = *p1;
|
|
|
|
*p1 = NULL;
|
|
|
|
return (p);
|
|
|
|
} else {
|
|
|
|
p = *p2;
|
|
|
|
*p2 = NULL;
|
|
|
|
return (p);
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
delete_unr(struct unrhdr *uh, void *ptr)
|
|
|
|
{
|
2007-07-04 06:56:58 +00:00
|
|
|
struct unr *up;
|
2005-03-08 10:40:48 +00:00
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
uh->alloc--;
|
2007-07-04 06:56:58 +00:00
|
|
|
up = ptr;
|
|
|
|
TAILQ_INSERT_TAIL(&uh->ppfree, up, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clean_unrhdrl(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
struct unr *up;
|
|
|
|
|
|
|
|
mtx_assert(uh->mtx, MA_OWNED);
|
|
|
|
while ((up = TAILQ_FIRST(&uh->ppfree)) != NULL) {
|
|
|
|
TAILQ_REMOVE(&uh->ppfree, up, list);
|
|
|
|
mtx_unlock(uh->mtx);
|
|
|
|
Free(up);
|
|
|
|
mtx_lock(uh->mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clean_unrhdr(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
|
|
|
|
mtx_lock(uh->mtx);
|
|
|
|
clean_unrhdrl(uh);
|
|
|
|
mtx_unlock(uh->mtx);
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 07:37:45 +00:00
|
|
|
void
|
|
|
|
init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex)
|
2004-09-30 07:04:03 +00:00
|
|
|
{
|
|
|
|
|
2010-07-09 10:57:55 +00:00
|
|
|
KASSERT(low >= 0 && low <= high,
|
2010-07-08 16:53:19 +00:00
|
|
|
("UNR: use error: new_unrhdr(%d, %d)", low, high));
|
2005-03-08 10:40:48 +00:00
|
|
|
if (mutex != NULL)
|
|
|
|
uh->mtx = mutex;
|
|
|
|
else
|
|
|
|
uh->mtx = &unitmtx;
|
2004-09-30 07:04:03 +00:00
|
|
|
TAILQ_INIT(&uh->head);
|
2007-07-04 06:56:58 +00:00
|
|
|
TAILQ_INIT(&uh->ppfree);
|
2004-09-30 07:04:03 +00:00
|
|
|
uh->low = low;
|
|
|
|
uh->high = high;
|
2005-03-08 10:40:48 +00:00
|
|
|
uh->first = 0;
|
|
|
|
uh->last = 1 + (high - low);
|
2022-04-20 21:58:22 +00:00
|
|
|
uh->busy = 0;
|
|
|
|
uh->alloc = 0;
|
2004-09-30 07:04:03 +00:00
|
|
|
check_unrhdr(uh, __LINE__);
|
2013-08-30 07:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a new unrheader set.
|
|
|
|
*
|
|
|
|
* Highest and lowest valid values given as parameters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct unrhdr *
|
|
|
|
new_unrhdr(int low, int high, struct mtx *mutex)
|
|
|
|
{
|
|
|
|
struct unrhdr *uh;
|
|
|
|
|
|
|
|
uh = Malloc(sizeof *uh);
|
|
|
|
init_unrhdr(uh, low, high, mutex);
|
2004-09-30 07:04:03 +00:00
|
|
|
return (uh);
|
|
|
|
}
|
|
|
|
|
2004-10-25 12:27:03 +00:00
|
|
|
void
|
|
|
|
delete_unrhdr(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
check_unrhdr(uh, __LINE__);
|
2004-10-25 12:27:03 +00:00
|
|
|
KASSERT(uh->busy == 0, ("unrhdr has %u allocations", uh->busy));
|
|
|
|
KASSERT(uh->alloc == 0, ("UNR memory leak in delete_unrhdr"));
|
2007-07-04 06:56:58 +00:00
|
|
|
KASSERT(TAILQ_FIRST(&uh->ppfree) == NULL,
|
|
|
|
("unrhdr has postponed item for free"));
|
2004-10-25 12:27:03 +00:00
|
|
|
Free(uh);
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:53:50 +00:00
|
|
|
void
|
|
|
|
clear_unrhdr(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
struct unr *up, *uq;
|
|
|
|
|
|
|
|
KASSERT(TAILQ_EMPTY(&uh->ppfree),
|
|
|
|
("unrhdr has postponed item for free"));
|
2017-10-16 16:14:50 +00:00
|
|
|
TAILQ_FOREACH_SAFE(up, &uh->head, list, uq) {
|
2017-10-11 21:53:50 +00:00
|
|
|
if (up->ptr != uh) {
|
|
|
|
Free(up->ptr);
|
|
|
|
}
|
|
|
|
Free(up);
|
|
|
|
}
|
|
|
|
uh->busy = 0;
|
|
|
|
uh->alloc = 0;
|
2017-10-16 16:14:50 +00:00
|
|
|
init_unrhdr(uh, uh->low, uh->high, uh->mtx);
|
|
|
|
|
|
|
|
check_unrhdr(uh, __LINE__);
|
2017-10-11 21:53:50 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
static __inline int
|
|
|
|
is_bitmap(struct unrhdr *uh, struct unr *up)
|
|
|
|
{
|
|
|
|
return (up->ptr != uh && up->ptr != NULL);
|
|
|
|
}
|
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
/*
|
2005-03-08 10:40:48 +00:00
|
|
|
* Look for sequence of items which can be combined into a bitmap, if
|
|
|
|
* multiple are present, take the one which saves most memory.
|
2017-01-14 04:16:13 +00:00
|
|
|
*
|
2005-03-08 10:40:48 +00:00
|
|
|
* Return (1) if a sequence was found to indicate that another call
|
|
|
|
* might be able to do more. Return (0) if we found no suitable sequence.
|
|
|
|
*
|
|
|
|
* NB: called from alloc_unr(), no new memory allocation allowed.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
optimize_unr(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
struct unr *up, *uf, *us;
|
|
|
|
struct unrb *ub, *ubf;
|
|
|
|
u_int a, l, ba;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for the run of items (if any) which when collapsed into
|
|
|
|
* a bitmap would save most memory.
|
|
|
|
*/
|
|
|
|
us = NULL;
|
|
|
|
ba = 0;
|
|
|
|
TAILQ_FOREACH(uf, &uh->head, list) {
|
|
|
|
if (uf->len >= NBITS)
|
|
|
|
continue;
|
|
|
|
a = 1;
|
|
|
|
if (is_bitmap(uh, uf))
|
|
|
|
a++;
|
|
|
|
l = uf->len;
|
|
|
|
up = uf;
|
|
|
|
while (1) {
|
|
|
|
up = TAILQ_NEXT(up, list);
|
|
|
|
if (up == NULL)
|
|
|
|
break;
|
|
|
|
if ((up->len + l) > NBITS)
|
|
|
|
break;
|
|
|
|
a++;
|
|
|
|
if (is_bitmap(uh, up))
|
|
|
|
a++;
|
|
|
|
l += up->len;
|
|
|
|
}
|
|
|
|
if (a > ba) {
|
|
|
|
ba = a;
|
|
|
|
us = uf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ba < 3)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the first element is not a bitmap, make it one.
|
|
|
|
* Trying to do so without allocating more memory complicates things
|
|
|
|
* a bit
|
|
|
|
*/
|
|
|
|
if (!is_bitmap(uh, us)) {
|
|
|
|
uf = TAILQ_NEXT(us, list);
|
|
|
|
TAILQ_REMOVE(&uh->head, us, list);
|
|
|
|
a = us->len;
|
|
|
|
l = us->ptr == uh ? 1 : 0;
|
|
|
|
ub = (void *)us;
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
bit_nclear(ub->map, 0, NBITS - 1);
|
|
|
|
if (l)
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_nset(ub->map, 0, a);
|
|
|
|
if (!is_bitmap(uh, uf)) {
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
if (uf->ptr == NULL)
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_nclear(ub->map, a, a + uf->len - 1);
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
else
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_nset(ub->map, a, a + uf->len - 1);
|
|
|
|
uf->ptr = ub;
|
|
|
|
uf->len += a;
|
|
|
|
us = uf;
|
|
|
|
} else {
|
|
|
|
ubf = uf->ptr;
|
|
|
|
for (l = 0; l < uf->len; l++, a++) {
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
if (bit_test(ubf->map, l))
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_set(ub->map, a);
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
else
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_clear(ub->map, a);
|
|
|
|
}
|
|
|
|
uf->len = a;
|
|
|
|
delete_unr(uh, uf->ptr);
|
|
|
|
uf->ptr = ub;
|
|
|
|
us = uf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ub = us->ptr;
|
|
|
|
while (1) {
|
|
|
|
uf = TAILQ_NEXT(us, list);
|
|
|
|
if (uf == NULL)
|
|
|
|
return (1);
|
|
|
|
if (uf->len + us->len > NBITS)
|
|
|
|
return (1);
|
|
|
|
if (uf->ptr == NULL) {
|
|
|
|
bit_nclear(ub->map, us->len, us->len + uf->len - 1);
|
|
|
|
us->len += uf->len;
|
|
|
|
TAILQ_REMOVE(&uh->head, uf, list);
|
|
|
|
delete_unr(uh, uf);
|
|
|
|
} else if (uf->ptr == uh) {
|
|
|
|
bit_nset(ub->map, us->len, us->len + uf->len - 1);
|
|
|
|
us->len += uf->len;
|
|
|
|
TAILQ_REMOVE(&uh->head, uf, list);
|
|
|
|
delete_unr(uh, uf);
|
|
|
|
} else {
|
|
|
|
ubf = uf->ptr;
|
|
|
|
for (l = 0; l < uf->len; l++, us->len++) {
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
if (bit_test(ubf->map, l))
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_set(ub->map, us->len);
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
else
|
2005-03-08 10:40:48 +00:00
|
|
|
bit_clear(ub->map, us->len);
|
|
|
|
}
|
|
|
|
TAILQ_REMOVE(&uh->head, uf, list);
|
|
|
|
delete_unr(uh, ubf);
|
|
|
|
delete_unr(uh, uf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if a given unr should be collapsed with a neighbor.
|
|
|
|
*
|
|
|
|
* NB: called from alloc_unr(), no new memory allocation allowed.
|
2004-09-30 07:04:03 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
collapse_unr(struct unrhdr *uh, struct unr *up)
|
|
|
|
{
|
|
|
|
struct unr *upp;
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unrb *ub;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* If bitmap is all set or clear, change it to runlength */
|
|
|
|
if (is_bitmap(uh, up)) {
|
|
|
|
ub = up->ptr;
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
if (ub_full(ub, up->len)) {
|
2005-03-08 10:40:48 +00:00
|
|
|
delete_unr(uh, up->ptr);
|
|
|
|
up->ptr = uh;
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
} else if (ub_empty(ub, up->len)) {
|
2005-03-08 10:40:48 +00:00
|
|
|
delete_unr(uh, up->ptr);
|
|
|
|
up->ptr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If nothing left in runlength, delete it */
|
|
|
|
if (up->len == 0) {
|
|
|
|
upp = TAILQ_PREV(up, unrhd, list);
|
|
|
|
if (upp == NULL)
|
|
|
|
upp = TAILQ_NEXT(up, list);
|
|
|
|
TAILQ_REMOVE(&uh->head, up, list);
|
|
|
|
delete_unr(uh, up);
|
|
|
|
up = upp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have "hot-spot" still, merge with neighbor if possible */
|
|
|
|
if (up != NULL) {
|
|
|
|
upp = TAILQ_PREV(up, unrhd, list);
|
|
|
|
if (upp != NULL && up->ptr == upp->ptr) {
|
|
|
|
up->len += upp->len;
|
|
|
|
TAILQ_REMOVE(&uh->head, upp, list);
|
|
|
|
delete_unr(uh, upp);
|
|
|
|
}
|
|
|
|
upp = TAILQ_NEXT(up, list);
|
|
|
|
if (upp != NULL && up->ptr == upp->ptr) {
|
|
|
|
up->len += upp->len;
|
|
|
|
TAILQ_REMOVE(&uh->head, upp, list);
|
|
|
|
delete_unr(uh, upp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Merge into ->first if possible */
|
|
|
|
upp = TAILQ_FIRST(&uh->head);
|
|
|
|
if (upp != NULL && upp->ptr == uh) {
|
|
|
|
uh->first += upp->len;
|
2004-09-30 07:04:03 +00:00
|
|
|
TAILQ_REMOVE(&uh->head, upp, list);
|
|
|
|
delete_unr(uh, upp);
|
2005-03-08 10:40:48 +00:00
|
|
|
if (up == upp)
|
|
|
|
up = NULL;
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
2005-03-08 10:40:48 +00:00
|
|
|
|
|
|
|
/* Merge into ->last if possible */
|
|
|
|
upp = TAILQ_LAST(&uh->head, unrhd);
|
|
|
|
if (upp != NULL && upp->ptr == NULL) {
|
|
|
|
uh->last += upp->len;
|
2004-09-30 07:04:03 +00:00
|
|
|
TAILQ_REMOVE(&uh->head, upp, list);
|
|
|
|
delete_unr(uh, upp);
|
2005-03-08 10:40:48 +00:00
|
|
|
if (up == upp)
|
|
|
|
up = NULL;
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
2005-03-08 10:40:48 +00:00
|
|
|
|
|
|
|
/* Try to make bitmaps */
|
|
|
|
while (optimize_unr(uh))
|
|
|
|
continue;
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a free unr.
|
|
|
|
*/
|
2005-03-08 10:40:48 +00:00
|
|
|
int
|
|
|
|
alloc_unrl(struct unrhdr *uh)
|
2004-09-30 07:04:03 +00:00
|
|
|
{
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unr *up;
|
|
|
|
struct unrb *ub;
|
2004-09-30 07:04:03 +00:00
|
|
|
u_int x;
|
|
|
|
int y;
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
mtx_assert(uh->mtx, MA_OWNED);
|
2004-09-30 07:04:03 +00:00
|
|
|
check_unrhdr(uh, __LINE__);
|
2005-03-08 10:40:48 +00:00
|
|
|
x = uh->low + uh->first;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
up = TAILQ_FIRST(&uh->head);
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/*
|
|
|
|
* If we have an ideal split, just adjust the first+last
|
|
|
|
*/
|
|
|
|
if (up == NULL && uh->last > 0) {
|
|
|
|
uh->first++;
|
|
|
|
uh->last--;
|
2004-09-30 07:04:03 +00:00
|
|
|
uh->busy++;
|
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-01-14 04:16:13 +00:00
|
|
|
* We can always allocate from the first list element, so if we have
|
2005-03-08 10:40:48 +00:00
|
|
|
* nothing on the list, we must have run out of unit numbers.
|
2004-09-30 07:04:03 +00:00
|
|
|
*/
|
2005-03-14 06:51:29 +00:00
|
|
|
if (up == NULL)
|
2005-03-08 10:40:48 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
KASSERT(up->ptr != uh, ("UNR first element is allocated"));
|
|
|
|
|
|
|
|
if (up->ptr == NULL) { /* free run */
|
|
|
|
uh->first++;
|
|
|
|
up->len--;
|
|
|
|
} else { /* bitmap */
|
|
|
|
ub = up->ptr;
|
|
|
|
bit_ffc(ub->map, up->len, &y);
|
|
|
|
KASSERT(y != -1, ("UNR corruption: No clear bit in bitmap."));
|
|
|
|
bit_set(ub->map, y);
|
|
|
|
x += y;
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
|
|
|
uh->busy++;
|
2005-03-08 10:40:48 +00:00
|
|
|
collapse_unr(uh, up);
|
2004-09-30 07:04:03 +00:00
|
|
|
return (x);
|
|
|
|
}
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
int
|
|
|
|
alloc_unr(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mtx_lock(uh->mtx);
|
|
|
|
i = alloc_unrl(uh);
|
2007-07-04 06:56:58 +00:00
|
|
|
clean_unrhdrl(uh);
|
2005-03-08 10:40:48 +00:00
|
|
|
mtx_unlock(uh->mtx);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:23:55 +00:00
|
|
|
static int
|
|
|
|
alloc_unr_specificl(struct unrhdr *uh, u_int item, void **p1, void **p2)
|
|
|
|
{
|
|
|
|
struct unr *up, *upn;
|
|
|
|
struct unrb *ub;
|
|
|
|
u_int i, last, tl;
|
|
|
|
|
|
|
|
mtx_assert(uh->mtx, MA_OWNED);
|
|
|
|
|
|
|
|
if (item < uh->low + uh->first || item > uh->high)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
up = TAILQ_FIRST(&uh->head);
|
|
|
|
/* Ideal split. */
|
|
|
|
if (up == NULL && item - uh->low == uh->first) {
|
|
|
|
uh->first++;
|
|
|
|
uh->last--;
|
|
|
|
uh->busy++;
|
|
|
|
check_unrhdr(uh, __LINE__);
|
|
|
|
return (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = item - uh->low - uh->first;
|
|
|
|
|
|
|
|
if (up == NULL) {
|
|
|
|
up = new_unr(uh, p1, p2);
|
|
|
|
up->ptr = NULL;
|
|
|
|
up->len = i;
|
|
|
|
TAILQ_INSERT_TAIL(&uh->head, up, list);
|
|
|
|
up = new_unr(uh, p1, p2);
|
|
|
|
up->ptr = uh;
|
|
|
|
up->len = 1;
|
|
|
|
TAILQ_INSERT_TAIL(&uh->head, up, list);
|
|
|
|
uh->last = uh->high - uh->low - i;
|
|
|
|
uh->busy++;
|
|
|
|
check_unrhdr(uh, __LINE__);
|
|
|
|
return (item);
|
|
|
|
} else {
|
|
|
|
/* Find the item which contains the unit we want to allocate. */
|
|
|
|
TAILQ_FOREACH(up, &uh->head, list) {
|
|
|
|
if (up->len > i)
|
|
|
|
break;
|
|
|
|
i -= up->len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (up == NULL) {
|
|
|
|
if (i > 0) {
|
|
|
|
up = new_unr(uh, p1, p2);
|
|
|
|
up->ptr = NULL;
|
|
|
|
up->len = i;
|
|
|
|
TAILQ_INSERT_TAIL(&uh->head, up, list);
|
|
|
|
}
|
|
|
|
up = new_unr(uh, p1, p2);
|
|
|
|
up->ptr = uh;
|
|
|
|
up->len = 1;
|
|
|
|
TAILQ_INSERT_TAIL(&uh->head, up, list);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_bitmap(uh, up)) {
|
|
|
|
ub = up->ptr;
|
|
|
|
if (bit_test(ub->map, i) == 0) {
|
|
|
|
bit_set(ub->map, i);
|
|
|
|
goto done;
|
|
|
|
} else
|
|
|
|
return (-1);
|
|
|
|
} else if (up->ptr == uh)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
KASSERT(up->ptr == NULL,
|
|
|
|
("alloc_unr_specificl: up->ptr != NULL (up=%p)", up));
|
|
|
|
|
|
|
|
/* Split off the tail end, if any. */
|
|
|
|
tl = up->len - (1 + i);
|
|
|
|
if (tl > 0) {
|
|
|
|
upn = new_unr(uh, p1, p2);
|
|
|
|
upn->ptr = NULL;
|
|
|
|
upn->len = tl;
|
|
|
|
TAILQ_INSERT_AFTER(&uh->head, up, upn, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split off head end, if any */
|
|
|
|
if (i > 0) {
|
|
|
|
upn = new_unr(uh, p1, p2);
|
|
|
|
upn->len = i;
|
|
|
|
upn->ptr = NULL;
|
|
|
|
TAILQ_INSERT_BEFORE(up, upn, list);
|
|
|
|
}
|
|
|
|
up->len = 1;
|
|
|
|
up->ptr = uh;
|
|
|
|
|
|
|
|
done:
|
|
|
|
last = uh->high - uh->low - (item - uh->low);
|
|
|
|
if (uh->last > last)
|
|
|
|
uh->last = last;
|
|
|
|
uh->busy++;
|
|
|
|
collapse_unr(uh, up);
|
|
|
|
check_unrhdr(uh, __LINE__);
|
|
|
|
return (item);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
alloc_unr_specific(struct unrhdr *uh, u_int item)
|
|
|
|
{
|
|
|
|
void *p1, *p2;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "alloc_unr_specific");
|
|
|
|
|
|
|
|
p1 = Malloc(sizeof(struct unr));
|
|
|
|
p2 = Malloc(sizeof(struct unr));
|
|
|
|
|
|
|
|
mtx_lock(uh->mtx);
|
|
|
|
i = alloc_unr_specificl(uh, item, &p1, &p2);
|
|
|
|
mtx_unlock(uh->mtx);
|
|
|
|
|
|
|
|
if (p1 != NULL)
|
|
|
|
Free(p1);
|
|
|
|
if (p2 != NULL)
|
|
|
|
Free(p2);
|
|
|
|
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
/*
|
|
|
|
* Free a unr.
|
|
|
|
*
|
|
|
|
* If we can save unrs by using a bitmap, do so.
|
|
|
|
*/
|
2005-03-08 10:40:48 +00:00
|
|
|
static void
|
|
|
|
free_unrl(struct unrhdr *uh, u_int item, void **p1, void **p2)
|
2004-09-30 07:04:03 +00:00
|
|
|
{
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unr *up, *upp, *upn;
|
|
|
|
struct unrb *ub;
|
|
|
|
u_int pl;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
KASSERT(item >= uh->low && item <= uh->high,
|
|
|
|
("UNR: free_unr(%u) out of range [%u...%u]",
|
|
|
|
item, uh->low, uh->high));
|
|
|
|
check_unrhdr(uh, __LINE__);
|
|
|
|
item -= uh->low;
|
2005-03-08 10:40:48 +00:00
|
|
|
upp = TAILQ_FIRST(&uh->head);
|
|
|
|
/*
|
|
|
|
* Freeing in the ideal split case
|
|
|
|
*/
|
|
|
|
if (item + 1 == uh->first && upp == NULL) {
|
|
|
|
uh->last++;
|
|
|
|
uh->first--;
|
2004-09-30 07:04:03 +00:00
|
|
|
uh->busy--;
|
|
|
|
check_unrhdr(uh, __LINE__);
|
2005-03-08 10:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Freeing in the ->first section. Create a run starting at the
|
|
|
|
* freed item. The code below will subdivide it.
|
|
|
|
*/
|
|
|
|
if (item < uh->first) {
|
|
|
|
up = new_unr(uh, p1, p2);
|
|
|
|
up->ptr = uh;
|
|
|
|
up->len = uh->first - item;
|
|
|
|
TAILQ_INSERT_HEAD(&uh->head, up, list);
|
|
|
|
uh->first -= up->len;
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
item -= uh->first;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Find the item which contains the unit we want to free */
|
|
|
|
TAILQ_FOREACH(up, &uh->head, list) {
|
|
|
|
if (up->len > item)
|
|
|
|
break;
|
|
|
|
item -= up->len;
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Handle bitmap items */
|
|
|
|
if (is_bitmap(uh, up)) {
|
|
|
|
ub = up->ptr;
|
2017-01-14 04:16:13 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
KASSERT(bit_test(ub->map, item) != 0,
|
|
|
|
("UNR: Freeing free item %d (bitmap)\n", item));
|
|
|
|
bit_clear(ub->map, item);
|
|
|
|
uh->busy--;
|
|
|
|
collapse_unr(uh, up);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
KASSERT(up->ptr == uh, ("UNR Freeing free item %d (run))\n", item));
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Just this one left, reap it */
|
|
|
|
if (up->len == 1) {
|
|
|
|
up->ptr = NULL;
|
|
|
|
uh->busy--;
|
|
|
|
collapse_unr(uh, up);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Check if we can shift the item into the previous 'free' run */
|
|
|
|
upp = TAILQ_PREV(up, unrhd, list);
|
|
|
|
if (item == 0 && upp != NULL && upp->ptr == NULL) {
|
|
|
|
upp->len++;
|
|
|
|
up->len--;
|
|
|
|
uh->busy--;
|
|
|
|
collapse_unr(uh, up);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Check if we can shift the item to the next 'free' run */
|
|
|
|
upn = TAILQ_NEXT(up, list);
|
|
|
|
if (item == up->len - 1 && upn != NULL && upn->ptr == NULL) {
|
|
|
|
upn->len++;
|
|
|
|
up->len--;
|
|
|
|
uh->busy--;
|
|
|
|
collapse_unr(uh, up);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Split off the tail end, if any. */
|
|
|
|
pl = up->len - (1 + item);
|
|
|
|
if (pl > 0) {
|
|
|
|
upp = new_unr(uh, p1, p2);
|
|
|
|
upp->ptr = uh;
|
|
|
|
upp->len = pl;
|
|
|
|
TAILQ_INSERT_AFTER(&uh->head, up, upp, list);
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
/* Split off head end, if any */
|
|
|
|
if (item > 0) {
|
|
|
|
upp = new_unr(uh, p1, p2);
|
|
|
|
upp->len = item;
|
|
|
|
upp->ptr = uh;
|
|
|
|
TAILQ_INSERT_BEFORE(up, upp, list);
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
2005-03-08 10:40:48 +00:00
|
|
|
up->len = 1;
|
|
|
|
up->ptr = NULL;
|
|
|
|
uh->busy--;
|
|
|
|
collapse_unr(uh, up);
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
void
|
|
|
|
free_unr(struct unrhdr *uh, u_int item)
|
|
|
|
{
|
|
|
|
void *p1, *p2;
|
|
|
|
|
2007-06-19 13:13:17 +00:00
|
|
|
WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "free_unr");
|
2005-03-08 10:40:48 +00:00
|
|
|
p1 = Malloc(sizeof(struct unr));
|
|
|
|
p2 = Malloc(sizeof(struct unr));
|
|
|
|
mtx_lock(uh->mtx);
|
|
|
|
free_unrl(uh, item, &p1, &p2);
|
2007-07-04 06:56:58 +00:00
|
|
|
clean_unrhdrl(uh);
|
2005-03-08 10:40:48 +00:00
|
|
|
mtx_unlock(uh->mtx);
|
|
|
|
if (p1 != NULL)
|
|
|
|
Free(p1);
|
|
|
|
if (p2 != NULL)
|
|
|
|
Free(p2);
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-14 06:51:29 +00:00
|
|
|
#ifndef _KERNEL /* USERLAND test driver */
|
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
/*
|
2016-04-29 21:11:31 +00:00
|
|
|
* Simple stochastic test driver for the above functions. The code resides
|
|
|
|
* here so that it can access static functions and structures.
|
2004-09-30 07:04:03 +00:00
|
|
|
*/
|
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
static bool verbose;
|
|
|
|
#define VPRINTF(...) {if (verbose) printf(__VA_ARGS__);}
|
|
|
|
|
2004-09-30 07:04:03 +00:00
|
|
|
static void
|
|
|
|
print_unr(struct unrhdr *uh, struct unr *up)
|
|
|
|
{
|
|
|
|
u_int x;
|
2005-03-08 10:40:48 +00:00
|
|
|
struct unrb *ub;
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
printf(" %p len = %5u ", up, up->len);
|
|
|
|
if (up->ptr == NULL)
|
|
|
|
printf("free\n");
|
|
|
|
else if (up->ptr == uh)
|
|
|
|
printf("alloc\n");
|
|
|
|
else {
|
2005-03-08 10:40:48 +00:00
|
|
|
ub = up->ptr;
|
Improve performance and functionality of the bitstring(3) api
Two new functions are provided, bit_ffs_at() and bit_ffc_at(), which allow
for efficient searching of set or cleared bits starting from any bit offset
within the bit string.
Performance is improved by operating on longs instead of bytes and using
ffsl() for searches within a long. ffsl() is a compiler builtin in both
clang and gcc for most architectures, converting what was a brute force
while loop search into a couple of instructions.
All of the bitstring(3) API continues to be contained in the header file.
Some of the functions are large enough that perhaps they should be uninlined
and moved to a library, but that is beyond the scope of this commit.
sys/sys/bitstring.h:
Convert the majority of the existing bit string implementation from
macros to inline functions.
Properly protect the implementation from inadvertant macro expansion
when included in a user's program by prefixing all private
macros/functions and local variables with '_'.
Add bit_ffs_at() and bit_ffc_at(). Implement bit_ffs() and
bit_ffc() in terms of their "at" counterparts.
Provide a kernel implementation of bit_alloc(), making the full API
usable in the kernel.
Improve code documenation.
share/man/man3/bitstring.3:
Add pre-exisiting API bit_ffc() to the synopsis.
Document new APIs.
Document the initialization state of the bit strings
allocated/declared by bit_alloc() and bit_decl().
Correct documentation for bitstr_size(). The original code comments
indicate the size is in bytes, not "elements of bitstr_t". The new
implementation follows this lead. Only hastd assumed "elements"
rather than bytes and it has been corrected.
etc/mtree/BSD.tests.dist:
tests/sys/Makefile:
tests/sys/sys/Makefile:
tests/sys/sys/bitstring.c:
Add tests for all existing and new functionality.
include/bitstring.h
Include all headers needed by sys/bitstring.h
lib/libbluetooth/bluetooth.h:
usr.sbin/bluetooth/hccontrol/le.c:
Include bitstring.h instead of sys/bitstring.h.
sbin/hastd/activemap.c:
Correct usage of bitstr_size().
sys/dev/xen/blkback/blkback.c
Use new bit_alloc.
sys/kern/subr_unit.c:
Remove hard-coded assumption that sizeof(bitstr_t) is 1. Get rid of
unrb.busy, which caches the number of bits set in unrb.map. When
INVARIANTS are disabled, nothing needs to know that information.
callapse_unr can be adapted to use bit_ffs and bit_ffc instead.
Eliminating unrb.busy saves memory, simplifies the code, and
provides a slight speedup when INVARIANTS are disabled.
sys/net/flowtable.c:
Use the new kernel implementation of bit-alloc, instead of hacking
the old libc-dependent macro.
sys/sys/param.h
Update __FreeBSD_version to indicate availability of new API
Submitted by: gibbs, asomers
Reviewed by: gibbs, ngie
MFC after: 4 weeks
Sponsored by: Spectra Logic Corp
Differential Revision: https://reviews.freebsd.org/D6004
2016-05-04 22:34:11 +00:00
|
|
|
printf("bitmap [");
|
2005-03-08 10:40:48 +00:00
|
|
|
for (x = 0; x < up->len; x++) {
|
|
|
|
if (bit_test(ub->map, x))
|
|
|
|
printf("#");
|
2017-01-14 04:16:13 +00:00
|
|
|
else
|
2005-03-08 10:40:48 +00:00
|
|
|
printf(" ");
|
2004-09-30 07:04:03 +00:00
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_unrhdr(struct unrhdr *uh)
|
|
|
|
{
|
|
|
|
struct unr *up;
|
|
|
|
u_int x;
|
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
printf(
|
|
|
|
"%p low = %u high = %u first = %u last = %u busy %u chunks = %u\n",
|
|
|
|
uh, uh->low, uh->high, uh->first, uh->last, uh->busy, uh->alloc);
|
|
|
|
x = uh->low + uh->first;
|
2004-09-30 07:04:03 +00:00
|
|
|
TAILQ_FOREACH(up, &uh->head, list) {
|
|
|
|
printf(" from = %5u", x);
|
|
|
|
print_unr(uh, up);
|
|
|
|
if (up->ptr == NULL || up->ptr == uh)
|
|
|
|
x += up->len;
|
|
|
|
else
|
|
|
|
x += NBITS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:23:55 +00:00
|
|
|
static void
|
|
|
|
test_alloc_unr(struct unrhdr *uh, u_int i, char a[])
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (a[i]) {
|
2016-04-29 21:11:31 +00:00
|
|
|
VPRINTF("F %u\n", i);
|
2010-07-05 16:23:55 +00:00
|
|
|
free_unr(uh, i);
|
|
|
|
a[i] = 0;
|
|
|
|
} else {
|
|
|
|
no_alloc = 1;
|
|
|
|
j = alloc_unr(uh);
|
|
|
|
if (j != -1) {
|
|
|
|
a[j] = 1;
|
2016-04-29 21:11:31 +00:00
|
|
|
VPRINTF("A %d\n", j);
|
2010-07-05 16:23:55 +00:00
|
|
|
}
|
|
|
|
no_alloc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_alloc_unr_specific(struct unrhdr *uh, u_int i, char a[])
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
j = alloc_unr_specific(uh, i);
|
|
|
|
if (j == -1) {
|
2016-04-29 21:11:31 +00:00
|
|
|
VPRINTF("F %u\n", i);
|
2010-07-05 16:23:55 +00:00
|
|
|
a[i] = 0;
|
|
|
|
free_unr(uh, i);
|
|
|
|
} else {
|
|
|
|
a[i] = 1;
|
2016-04-29 21:11:31 +00:00
|
|
|
VPRINTF("A %d\n", j);
|
2010-07-05 16:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
static void
|
|
|
|
usage(char** argv)
|
|
|
|
{
|
|
|
|
printf("%s [-h] [-r REPETITIONS] [-v]\n", argv[0]);
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
|
|
|
int
|
2016-04-29 21:11:31 +00:00
|
|
|
main(int argc, char **argv)
|
2004-09-30 07:04:03 +00:00
|
|
|
{
|
|
|
|
struct unrhdr *uh;
|
2016-04-29 21:11:31 +00:00
|
|
|
char *a;
|
|
|
|
long count = 10000; /* Number of unrs to test */
|
2016-05-24 00:14:58 +00:00
|
|
|
long reps = 1, m;
|
2016-04-29 21:11:31 +00:00
|
|
|
int ch;
|
2019-12-13 04:48:20 +00:00
|
|
|
u_int i;
|
2016-04-29 21:11:31 +00:00
|
|
|
|
|
|
|
verbose = false;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hr:v")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'r':
|
|
|
|
errno = 0;
|
|
|
|
reps = strtol(optarg, NULL, 0);
|
|
|
|
if (errno == ERANGE || errno == EINVAL) {
|
|
|
|
usage(argv);
|
|
|
|
exit(2);
|
|
|
|
}
|
2017-01-14 04:13:28 +00:00
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage(argv);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
}
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2005-03-08 10:40:48 +00:00
|
|
|
setbuf(stdout, NULL);
|
2016-04-29 21:11:31 +00:00
|
|
|
uh = new_unrhdr(0, count - 1, NULL);
|
2005-03-08 10:40:48 +00:00
|
|
|
print_unrhdr(uh);
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
a = calloc(count, sizeof(char));
|
|
|
|
if (a == NULL)
|
|
|
|
err(1, "calloc failed");
|
2004-09-30 07:04:03 +00:00
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
printf("sizeof(struct unr) %zu\n", sizeof(struct unr));
|
|
|
|
printf("sizeof(struct unrb) %zu\n", sizeof(struct unrb));
|
|
|
|
printf("sizeof(struct unrhdr) %zu\n", sizeof(struct unrhdr));
|
2016-05-05 15:21:33 +00:00
|
|
|
printf("NBITS %lu\n", (unsigned long)NBITS);
|
2016-04-29 21:11:31 +00:00
|
|
|
for (m = 0; m < count * reps; m++) {
|
2019-12-13 04:48:20 +00:00
|
|
|
i = arc4random_uniform(count);
|
2005-03-08 10:40:48 +00:00
|
|
|
#if 0
|
|
|
|
if (a[i] && (j & 1))
|
|
|
|
continue;
|
|
|
|
#endif
|
2019-12-13 04:48:20 +00:00
|
|
|
if ((arc4random() & 1) != 0)
|
2010-07-05 16:23:55 +00:00
|
|
|
test_alloc_unr(uh, i, a);
|
|
|
|
else
|
|
|
|
test_alloc_unr_specific(uh, i, a);
|
|
|
|
|
2016-04-29 21:11:31 +00:00
|
|
|
if (verbose)
|
2004-09-30 07:04:03 +00:00
|
|
|
print_unrhdr(uh);
|
|
|
|
check_unrhdr(uh, __LINE__);
|
|
|
|
}
|
2016-05-24 00:14:58 +00:00
|
|
|
for (i = 0; i < (u_int)count; i++) {
|
2005-03-08 10:40:48 +00:00
|
|
|
if (a[i]) {
|
2016-04-29 21:11:31 +00:00
|
|
|
if (verbose) {
|
|
|
|
printf("C %u\n", i);
|
|
|
|
print_unrhdr(uh);
|
|
|
|
}
|
2004-10-25 12:27:03 +00:00
|
|
|
free_unr(uh, i);
|
2005-03-08 10:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-25 12:27:03 +00:00
|
|
|
print_unrhdr(uh);
|
|
|
|
delete_unrhdr(uh);
|
2016-04-29 21:11:31 +00:00
|
|
|
free(a);
|
2004-09-30 07:04:03 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|