libc: add find_next_zero_bit
The pci ep framework use bitmap manage free bar Signed-off-by: lipengfei28 <lipengfei28@xiaomi.com>
This commit is contained in:
parent
b18262d78f
commit
4fd78583f7
@ -93,6 +93,9 @@
|
||||
(*(((FAR unsigned long *)(addr)) + BIT_WORD(nr)) & \
|
||||
BIT_WORD_MASK(nr))
|
||||
|
||||
#define find_first_zero_bit(addr, size) \
|
||||
find_next_zero_bit((addr), (size), 0)
|
||||
|
||||
/****************************************************************************
|
||||
* Type Definitions
|
||||
****************************************************************************/
|
||||
@ -112,6 +115,11 @@ extern "C"
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
#ifndef __ASSEMBLER__
|
||||
unsigned long find_next_zero_bit(FAR const unsigned long *addr,
|
||||
unsigned long size,
|
||||
unsigned long offset);
|
||||
#endif
|
||||
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
|
@ -24,6 +24,7 @@
|
||||
list(
|
||||
APPEND
|
||||
SRCS
|
||||
lib_bitmap.c
|
||||
lib_mknod.c
|
||||
lib_umask.c
|
||||
lib_utsname.c
|
||||
|
@ -20,7 +20,7 @@
|
||||
|
||||
# Add the internal C files to the build
|
||||
|
||||
CSRCS += lib_mknod.c lib_umask.c lib_utsname.c lib_getrandom.c
|
||||
CSRCS += lib_bitmap.c lib_mknod.c lib_umask.c lib_utsname.c lib_getrandom.c
|
||||
CSRCS += lib_xorshift128.c lib_tea_encrypt.c lib_tea_decrypt.c
|
||||
CSRCS += lib_cxx_initialize.c lib_impure.c lib_memfd.c lib_mutex.c
|
||||
CSRCS += lib_fchmodat.c lib_fstatat.c lib_getfullpath.c lib_openat.c
|
||||
|
116
libs/libc/misc/lib_bitmap.c
Normal file
116
libs/libc/misc/lib_bitmap.c
Normal file
@ -0,0 +1,116 @@
|
||||
/****************************************************************************
|
||||
* libs/libc/misc/lib_bitmap.c
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <nuttx/bits.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: find_next_zero_bit
|
||||
*
|
||||
* Description:
|
||||
* This function is used to find the first cleared bit in a memory region.
|
||||
*
|
||||
* Invoke to get the bit set in map, implement find_{first,next}_zero_bit.
|
||||
*
|
||||
* Input Parameters:
|
||||
* addr - Map start memory address
|
||||
* size - Max size to find bit
|
||||
* offset - Offset of start to finding
|
||||
*
|
||||
* Returned Value:
|
||||
* Return index position(0 ~ size-1)if Finded, otherwise return size
|
||||
****************************************************************************/
|
||||
|
||||
unsigned long find_next_zero_bit(FAR const unsigned long *addr,
|
||||
unsigned long size,
|
||||
unsigned long offset)
|
||||
{
|
||||
FAR const unsigned long *p = addr + BIT_WORD(offset);
|
||||
unsigned long result = offset & ~(BITS_PER_LONG - 1);
|
||||
unsigned long tmp;
|
||||
|
||||
if (offset >= size)
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
size -= result;
|
||||
offset %= BITS_PER_LONG;
|
||||
if (offset)
|
||||
{
|
||||
tmp = *(p++);
|
||||
tmp |= ~0ul >> (BITS_PER_LONG - offset);
|
||||
if (size < BITS_PER_LONG)
|
||||
{
|
||||
goto found_first;
|
||||
}
|
||||
|
||||
if (~tmp)
|
||||
{
|
||||
goto found_middle;
|
||||
}
|
||||
|
||||
size -= BITS_PER_LONG;
|
||||
result += BITS_PER_LONG;
|
||||
}
|
||||
|
||||
while (size & ~(BITS_PER_LONG - 1))
|
||||
{
|
||||
if (~(tmp = *(p++)))
|
||||
{
|
||||
goto found_middle;
|
||||
}
|
||||
|
||||
result += BITS_PER_LONG;
|
||||
size -= BITS_PER_LONG;
|
||||
}
|
||||
|
||||
if (!size)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
tmp = *p;
|
||||
|
||||
found_first:
|
||||
tmp |= ~0ul << size;
|
||||
|
||||
/* Are any bits zero? */
|
||||
|
||||
if (tmp == ~0ul)
|
||||
{
|
||||
return result + size;
|
||||
}
|
||||
|
||||
found_middle:
|
||||
return result + ffsl(~tmp) - 1;
|
||||
}
|
Loading…
Reference in New Issue
Block a user