2020: update lib source files (trying to compile on 32 bits arch)
This commit is contained in:
@@ -10,8 +10,8 @@
|
|||||||
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#ifndef BITS_H
|
#ifndef _BITS_H
|
||||||
#define BITS_H
|
#define _BITS_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -49,6 +49,30 @@ typedef unsigned int uint;
|
|||||||
typedef unsigned short ushort;
|
typedef unsigned short ushort;
|
||||||
typedef unsigned char uchar;
|
typedef unsigned char uchar;
|
||||||
|
|
||||||
|
/* count set bits: 10101000 -> 3
|
||||||
|
* ^ ^ ^
|
||||||
|
*/
|
||||||
|
static __always_inline int popcount64(u64 n)
|
||||||
|
{
|
||||||
|
# if __has_builtin(__builtin_popcountl)
|
||||||
|
# ifdef DEBUG_BITS
|
||||||
|
log_f(1, "builtin.\n");
|
||||||
|
# endif
|
||||||
|
return __builtin_popcountl(n);
|
||||||
|
|
||||||
|
# else
|
||||||
|
# ifdef DEBUG_BITS
|
||||||
|
log_f(1, "emulated.\n");
|
||||||
|
# endif
|
||||||
|
int count = 0;
|
||||||
|
while (n) {
|
||||||
|
count++;
|
||||||
|
n &= (n - 1);
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
/* char is a special case, as it can be signed or unsigned
|
/* char is a special case, as it can be signed or unsigned
|
||||||
*/
|
*/
|
||||||
typedef signed char schar;
|
typedef signed char schar;
|
||||||
@@ -74,7 +98,7 @@ static __always_inline int ctz64(u64 n)
|
|||||||
# ifdef DEBUG_BITS
|
# ifdef DEBUG_BITS
|
||||||
log_f(1, "emulated.\n");
|
log_f(1, "emulated.\n");
|
||||||
# endif
|
# endif
|
||||||
return popcount64((n & −n) − 1);
|
return popcount64((n & -n) - 1);
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +120,7 @@ static __always_inline int ctz32(u32 n)
|
|||||||
# ifdef DEBUG_BITS
|
# ifdef DEBUG_BITS
|
||||||
log_f(1, "emulated.\n");
|
log_f(1, "emulated.\n");
|
||||||
# endif
|
# endif
|
||||||
return popcount32((n & −n) − 1);
|
return popcount32((n & -n) - 1);
|
||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,30 +242,6 @@ static __always_inline uint ffs32(u32 n)
|
|||||||
# endif
|
# endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* count set bits: 10101000 -> 3
|
|
||||||
* ^ ^ ^
|
|
||||||
*/
|
|
||||||
static __always_inline int popcount64(u64 n)
|
|
||||||
{
|
|
||||||
# if __has_builtin(__builtin_popcountl)
|
|
||||||
# ifdef DEBUG_BITS
|
|
||||||
log_f(1, "builtin.\n");
|
|
||||||
# endif
|
|
||||||
return __builtin_popcountl(n);
|
|
||||||
|
|
||||||
# else
|
|
||||||
# ifdef DEBUG_BITS
|
|
||||||
log_f(1, "emulated.\n");
|
|
||||||
# endif
|
|
||||||
int count = 0;
|
|
||||||
while (n) {
|
|
||||||
count++;
|
|
||||||
n &= (n - 1);
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
# endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static __always_inline int popcount32(u32 n)
|
static __always_inline int popcount32(u32 n)
|
||||||
{
|
{
|
||||||
# if __has_builtin(__builtin_popcount)
|
# if __has_builtin(__builtin_popcount)
|
||||||
@@ -518,4 +518,4 @@ int __bits_per(unsigned long n)
|
|||||||
#define bit_for_each32_2(pos, tmp, ul) \
|
#define bit_for_each32_2(pos, tmp, ul) \
|
||||||
for (tmp = ul, pos = ctz32(tmp); tmp; tmp ^= 1U << pos, pos = ctz32(tmp))
|
for (tmp = ul, pos = ctz32(tmp); tmp; tmp ^= 1U << pos, pos = ctz32(tmp))
|
||||||
|
|
||||||
#endif /* BITS_H */
|
#endif /* _BITS_H */
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
/* bits.h - bits functions.
|
/* br.h - misc macros.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||||
* Licensed under the GNU General Public License v3.0 or later.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
@@ -18,12 +18,18 @@
|
|||||||
#ifndef _BR_H
|
#ifndef _BR_H
|
||||||
#define _BR_H
|
#define _BR_H
|
||||||
|
|
||||||
|
/* Indirect stringification. Doing two levels allows the parameter to be a
|
||||||
|
* macro itself. For example, compile with -DFOO=bar, __stringify(FOO)
|
||||||
|
* converts to "bar".
|
||||||
|
*/
|
||||||
|
#define __stringify_1(x...) #x
|
||||||
|
#define __stringify(x...) __stringify_1(x)
|
||||||
|
|
||||||
/* generate a (maybe) unique id.
|
/* generate a (maybe) unique id.
|
||||||
*/
|
*/
|
||||||
#define ___PASTE(x, y) x##y
|
#define ___PASTE(x, y) x##y
|
||||||
#define __PASTE(x, y) ___PASTE(x, y)
|
#define __PASTE(x, y) ___PASTE(x, y)
|
||||||
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
|
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
|
||||||
//__##prefix##__COUNTER__
|
|
||||||
|
|
||||||
/* see https://lkml.org/lkml/2018/3/20/845 for explanation of this monster
|
/* see https://lkml.org/lkml/2018/3/20/845 for explanation of this monster
|
||||||
*/
|
*/
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
/* pool.c - A simple pool manager.
|
/* pool.c - A simple pool manager.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2021 Bruno Raoult ("br")
|
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||||
* Licensed under the GNU General Public License v3.0 or later.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
* Some rights reserved. See COPYING.
|
* Some rights reserved. See COPYING.
|
||||||
*
|
*
|
||||||
@@ -25,18 +25,16 @@
|
|||||||
void pool_stats(pool_t *pool)
|
void pool_stats(pool_t *pool)
|
||||||
{
|
{
|
||||||
if (pool) {
|
if (pool) {
|
||||||
# ifdef DEBUG_POOL
|
|
||||||
block_t *block;
|
block_t *block;
|
||||||
|
|
||||||
log_f(1, "[%s] pool [%p]: blocks:%u avail:%u alloc:%u grow:%u eltsize:%lu\n",
|
log_f(1, "[%s] pool [%p]: blocks:%u avail:%u alloc:%u grow:%u eltsize:%zu\n",
|
||||||
pool->name, (void *)pool, pool->nblocks, pool->available, pool->allocated,
|
pool->name, (void *)pool, pool->nblocks, pool->available,
|
||||||
pool->growsize, pool->eltsize);
|
pool->allocated, pool->growsize, pool->eltsize);
|
||||||
log(5, "\tblocks: ");
|
log(5, "\tblocks: ");
|
||||||
list_for_each_entry(block, &pool->list_blocks, list_blocks) {
|
list_for_each_entry(block, &poo7l->list_blocks, list_blocks) {
|
||||||
log(5, "%p ", block);
|
log(5, "%p ", block);
|
||||||
}
|
}
|
||||||
log(5, "\n");
|
log(5, "\n");
|
||||||
# endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,14 +43,13 @@ pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
|
|||||||
pool_t *pool;
|
pool_t *pool;
|
||||||
|
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log_f(1, "name=[%s] growsize=%u eltsize=%lu\n",
|
log_f(1, "name=[%s] growsize=%u eltsize=%zu\n", name, growsize, eltsize);
|
||||||
name, growsize, eltsize);
|
|
||||||
# endif
|
# endif
|
||||||
/* we need at least sizeof(struct list_head) space in pool elements
|
/* we need at least sizeof(struct list_head) space in pool elements
|
||||||
*/
|
*/
|
||||||
if (eltsize < sizeof (struct list_head)) {
|
if (eltsize < sizeof (struct list_head)) {
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log_f(1, "[%s]: structure size too small (%lu < %lu), adjusting to %lu.\n",
|
log_f(1, "[%s]: structure size too small (%zu < %zu), adjusting to %zu.\n",
|
||||||
name, eltsize, sizeof(struct list_head), sizeof(struct list_head));
|
name, eltsize, sizeof(struct list_head), sizeof(struct list_head));
|
||||||
# endif
|
# endif
|
||||||
eltsize = sizeof(struct list_head);
|
eltsize = sizeof(struct list_head);
|
||||||
@@ -67,6 +64,8 @@ pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
|
|||||||
pool->nblocks = 0;
|
pool->nblocks = 0;
|
||||||
INIT_LIST_HEAD(&pool->list_available);
|
INIT_LIST_HEAD(&pool->list_available);
|
||||||
INIT_LIST_HEAD(&pool->list_blocks);
|
INIT_LIST_HEAD(&pool->list_blocks);
|
||||||
|
} else {
|
||||||
|
errno = ENOMEM;
|
||||||
}
|
}
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
@@ -74,11 +73,9 @@ pool_t *pool_create(const char *name, u32 growsize, size_t eltsize)
|
|||||||
static u32 _pool_add(pool_t *pool, struct list_head *elt)
|
static u32 _pool_add(pool_t *pool, struct list_head *elt)
|
||||||
{
|
{
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log_f(6, "pool=%p &head=%p elt=%p off1=%lu off2=%lu\n",
|
log_f(6, "pool=%p &head=%p elt=%p off1=%zu off2=%zu\n",
|
||||||
(void *)pool,
|
(void *)pool, (void *)&pool->list_available, (void *)elt,
|
||||||
(void *)&pool->list_available,
|
(void *)&pool->list_available - (void *)pool,
|
||||||
(void *)elt,
|
|
||||||
(void *)&pool->list_available-(void *)pool,
|
|
||||||
offsetof(pool_t, list_available));
|
offsetof(pool_t, list_available));
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
@@ -105,9 +102,6 @@ void *pool_get(pool_t *pool)
|
|||||||
return NULL;
|
return NULL;
|
||||||
if (!pool->available) {
|
if (!pool->available) {
|
||||||
block_t *block = malloc(sizeof(block_t) + pool->eltsize * pool->growsize);
|
block_t *block = malloc(sizeof(block_t) + pool->eltsize * pool->growsize);
|
||||||
void *cur;
|
|
||||||
u32 i;
|
|
||||||
|
|
||||||
if (!block) {
|
if (!block) {
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log_f(1, "[%s]: failed block allocation\n", pool->name);
|
log_f(1, "[%s]: failed block allocation\n", pool->name);
|
||||||
@@ -131,16 +125,15 @@ void *pool_get(pool_t *pool)
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
pool->allocated += pool->growsize;
|
pool->allocated += pool->growsize;
|
||||||
for (i = 0; i < pool->growsize; ++i) {
|
for (u32 i = 0; i < pool->growsize; ++i) {
|
||||||
cur = block->data + i * pool->eltsize;
|
void *cur = block->data + i * pool->eltsize;
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log_f(7, "alloc=%p cur=%p\n", block, cur);
|
log_f(7, "alloc=%p cur=%p\n", block, cur);
|
||||||
# endif
|
# endif
|
||||||
_pool_add(pool, (struct list_head *)cur);
|
_pool_add(pool, (struct list_head *)cur);
|
||||||
}
|
}
|
||||||
//pool_stats(pool);
|
|
||||||
}
|
}
|
||||||
/* this is the effective address if the object (and also the
|
/* this is the effective address of the object (and also the
|
||||||
* pool list_head address)
|
* pool list_head address)
|
||||||
*/
|
*/
|
||||||
return _pool_get(pool);
|
return _pool_get(pool);
|
||||||
@@ -157,11 +150,11 @@ void pool_destroy(pool_t *pool)
|
|||||||
log(5, "blocks:");
|
log(5, "blocks:");
|
||||||
# endif
|
# endif
|
||||||
list_for_each_entry_safe(block, tmp, &pool->list_blocks, list_blocks) {
|
list_for_each_entry_safe(block, tmp, &pool->list_blocks, list_blocks) {
|
||||||
list_del(&block->list_blocks);
|
|
||||||
free(block);
|
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log(5, " %p", block);
|
log(5, " %p", block);
|
||||||
# endif
|
# endif
|
||||||
|
list_del(&block->list_blocks);
|
||||||
|
free(block);
|
||||||
}
|
}
|
||||||
# ifdef DEBUG_POOL
|
# ifdef DEBUG_POOL
|
||||||
log(5, "\n");
|
log(5, "\n");
|
||||||
@@ -221,5 +214,6 @@ int main(int ac, char**av)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pool_stats(pool);
|
pool_stats(pool);
|
||||||
|
pool_destroy(pool);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
/* bits.h - bits functions.
|
/* br.h - misc macros.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
* Copyright (C) 2021-2022 Bruno Raoult ("br")
|
||||||
* Licensed under the GNU General Public License v3.0 or later.
|
* Licensed under the GNU General Public License v3.0 or later.
|
||||||
|
@@ -8,7 +8,6 @@
|
|||||||
/* Fast hashing routine for ints, longs and pointers.
|
/* Fast hashing routine for ints, longs and pointers.
|
||||||
(C) 2002 Nadia Yvette Chambers, IBM */
|
(C) 2002 Nadia Yvette Chambers, IBM */
|
||||||
|
|
||||||
#include <asm/types.h>
|
|
||||||
#include <asm/bitsperlong.h>
|
#include <asm/bitsperlong.h>
|
||||||
#include "bits.h"
|
#include "bits.h"
|
||||||
#include "br.h"
|
#include "br.h"
|
||||||
|
@@ -12,8 +12,6 @@
|
|||||||
#define _LINUX_HASHTABLE_H
|
#define _LINUX_HASHTABLE_H
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include <linux/types.h>
|
|
||||||
#include <linux/kernel.h>
|
|
||||||
#include "hash.h"
|
#include "hash.h"
|
||||||
//#include <linux/rculist.h>
|
//#include <linux/rculist.h>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user