Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
d6bee616
Commit
d6bee616
authored
Sep 13, 2013
by
Rusty Russell
Browse files
Options
Browse Files
Download
Plain Diff
Merge
https://github.com/dgibson/ccan
parents
60465a3c
6a9c567b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
336 additions
and
0 deletions
+336
-0
ccan/bitmap/LICENSE
ccan/bitmap/LICENSE
+1
-0
ccan/bitmap/_info
ccan/bitmap/_info
+31
-0
ccan/bitmap/bitmap.h
ccan/bitmap/bitmap.h
+188
-0
ccan/bitmap/test/run.c
ccan/bitmap/test/run.c
+116
-0
No files found.
ccan/bitmap/LICENSE
0 → 120000
View file @
d6bee616
../../licenses/LGPL-2.1
\ No newline at end of file
ccan/bitmap/_info
0 → 100644
View file @
d6bee616
#include <string.h>
#include "config.h"
/**
* bitmap - bitmap handling
*
* This code handles manipulation of bitmaps, arbitrary length arrays
* of bits.
*
* License: LGPL (v2.1 or any later version)
* Author: David Gibson <david@gibson.dropbear.id.au>
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/endian\n");
return 0;
}
if (strcmp(argv[1], "testdepends") == 0) {
printf("ccan/array_size\n");
printf("ccan/foreach\n");
return 0;
}
return 1;
}
ccan/bitmap/bitmap.h
0 → 100644
View file @
d6bee616
/* Licensed under LGPLv2+ - see LICENSE file for details */
#ifndef CCAN_BITMAP_H_
#define CCAN_BITMAP_H_
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ccan/endian/endian.h>
typedef
unsigned
long
bitmap_word
;
#define BITMAP_WORD_BITS (sizeof(bitmap_word) * CHAR_BIT)
#define BITMAP_NWORDS(_n) \
(((_n) + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
/*
* We wrap each word in a structure for type checking.
*/
typedef
struct
{
bitmap_word
w
;
}
bitmap
;
#define BITMAP_DECLARE(_name, _nbits) \
bitmap (_name)[BITMAP_NWORDS(_nbits)]
static
inline
size_t
bitmap_sizeof
(
int
nbits
)
{
return
BITMAP_NWORDS
(
nbits
)
*
sizeof
(
bitmap_word
);
}
static
inline
bitmap
*
bitmap_alloc
(
int
nbits
)
{
return
malloc
(
bitmap_sizeof
(
nbits
));
}
static
inline
bitmap_word
bitmap_bswap
(
bitmap_word
w
)
{
if
(
BITMAP_WORD_BITS
==
32
)
return
cpu_to_be32
(
w
);
else
if
(
BITMAP_WORD_BITS
==
64
)
return
cpu_to_be64
(
w
);
}
#define BITMAP_WORD(_bm, _n) ((_bm)[(_n) / BITMAP_WORD_BITS].w)
#define BITMAP_WORDBIT(_n) \
(bitmap_bswap(1UL << (BITMAP_WORD_BITS - ((_n) % BITMAP_WORD_BITS) - 1)))
#define BITMAP_HEADWORDS(_nbits) \
((_nbits) / BITMAP_WORD_BITS)
#define BITMAP_HEADBYTES(_nbits) \
(BITMAP_HEADWORDS(_nbits) * sizeof(bitmap_word))
#define BITMAP_TAILWORD(_bm, _nbits) \
((_bm)[BITMAP_HEADWORDS(_nbits)].w)
#define BITMAP_HASTAIL(_nbits) (((_nbits) % BITMAP_WORD_BITS) != 0)
#define BITMAP_TAILBITS(_nbits) \
(bitmap_bswap(~(-1UL >> ((_nbits) % BITMAP_WORD_BITS))))
#define BITMAP_TAIL(_bm, _nbits) \
(BITMAP_TAILWORD(_bm, _nbits) & BITMAP_TAILBITS(_nbits))
static
inline
void
bitmap_set_bit
(
bitmap
*
bitmap
,
int
n
)
{
BITMAP_WORD
(
bitmap
,
n
)
|=
BITMAP_WORDBIT
(
n
);
}
static
inline
void
bitmap_clear_bit
(
bitmap
*
bitmap
,
int
n
)
{
BITMAP_WORD
(
bitmap
,
n
)
&=
~
BITMAP_WORDBIT
(
n
);
}
static
inline
void
bitmap_change_bit
(
bitmap
*
bitmap
,
int
n
)
{
BITMAP_WORD
(
bitmap
,
n
)
^=
BITMAP_WORDBIT
(
n
);
}
static
inline
bool
bitmap_test_bit
(
bitmap
*
bitmap
,
int
n
)
{
return
!!
(
BITMAP_WORD
(
bitmap
,
n
)
&
BITMAP_WORDBIT
(
n
));
}
static
inline
void
bitmap_zero
(
bitmap
*
bitmap
,
int
nbits
)
{
memset
(
bitmap
,
0
,
bitmap_sizeof
(
nbits
));
}
static
inline
void
bitmap_fill
(
bitmap
*
bitmap
,
int
nbits
)
{
memset
(
bitmap
,
0xff
,
bitmap_sizeof
(
nbits
));
}
static
inline
void
bitmap_copy
(
bitmap
*
dst
,
bitmap
*
src
,
int
nbits
)
{
memcpy
(
dst
,
src
,
bitmap_sizeof
(
nbits
));
}
#define BITMAP_DEF_BINOP(_name, _op) \
static inline void bitmap_##_name(bitmap *dst, bitmap *src1, bitmap *src2, \
int nbits) \
{ \
int i = 0; \
for (i = 0; i < BITMAP_NWORDS(nbits); i++) { \
dst[i].w = src1[i].w _op src2[i].w; \
} \
}
BITMAP_DEF_BINOP
(
and
,
&
)
BITMAP_DEF_BINOP
(
or
,
|
)
BITMAP_DEF_BINOP
(
xor
,
^
)
BITMAP_DEF_BINOP
(
andnot
,
&
~
)
#undef BITMAP_DEF_BINOP
static
inline
void
bitmap_complement
(
bitmap
*
dst
,
bitmap
*
src
,
int
nbits
)
{
int
i
;
for
(
i
=
0
;
i
<
BITMAP_NWORDS
(
nbits
);
i
++
)
dst
[
i
].
w
=
~
src
[
i
].
w
;
}
static
inline
bool
bitmap_equal
(
bitmap
*
src1
,
bitmap
*
src2
,
int
nbits
)
{
return
(
memcmp
(
src1
,
src2
,
BITMAP_HEADBYTES
(
nbits
))
==
0
)
&&
(
!
BITMAP_HASTAIL
(
nbits
)
||
(
BITMAP_TAIL
(
src1
,
nbits
)
==
BITMAP_TAIL
(
src2
,
nbits
)));
}
static
inline
bool
bitmap_intersects
(
bitmap
*
src1
,
bitmap
*
src2
,
int
nbits
)
{
int
i
;
for
(
i
=
0
;
i
<
BITMAP_HEADWORDS
(
nbits
);
i
++
)
{
if
(
src1
[
i
].
w
&
src2
[
i
].
w
)
return
true
;
}
if
(
BITMAP_HASTAIL
(
nbits
)
&&
(
BITMAP_TAIL
(
src1
,
nbits
)
&
BITMAP_TAIL
(
src2
,
nbits
)))
return
true
;
return
false
;
}
static
inline
bool
bitmap_subset
(
bitmap
*
src1
,
bitmap
*
src2
,
int
nbits
)
{
int
i
;
for
(
i
=
0
;
i
<
BITMAP_HEADWORDS
(
nbits
);
i
++
)
{
if
(
src1
[
i
].
w
&
~
src2
[
i
].
w
)
return
false
;
}
if
(
BITMAP_HASTAIL
(
nbits
)
&&
(
BITMAP_TAIL
(
src1
,
nbits
)
&
~
BITMAP_TAIL
(
src2
,
nbits
)))
return
false
;
return
true
;
}
static
inline
bool
bitmap_full
(
bitmap
*
bitmap
,
int
nbits
)
{
int
i
;
for
(
i
=
0
;
i
<
BITMAP_HEADWORDS
(
nbits
);
i
++
)
{
if
(
bitmap
[
i
].
w
!=
-
1UL
)
return
false
;
}
if
(
BITMAP_HASTAIL
(
nbits
)
&&
(
BITMAP_TAIL
(
bitmap
,
nbits
)
!=
BITMAP_TAILBITS
(
nbits
)))
return
false
;
return
true
;
}
static
inline
bool
bitmap_empty
(
bitmap
*
bitmap
,
int
nbits
)
{
int
i
;
for
(
i
=
0
;
i
<
BITMAP_HEADWORDS
(
nbits
);
i
++
)
{
if
(
bitmap
[
i
].
w
!=
0
)
return
false
;
}
if
(
BITMAP_HASTAIL
(
nbits
)
&&
(
BITMAP_TAIL
(
bitmap
,
nbits
)
!=
0
))
return
false
;
return
true
;
}
#endif
/* CCAN_BITMAP_H_ */
ccan/bitmap/test/run.c
0 → 100644
View file @
d6bee616
#include <ccan/bitmap/bitmap.h>
#include <ccan/tap/tap.h>
#include <ccan/array_size/array_size.h>
#include <ccan/foreach/foreach.h>
int
bitmap_sizes
[]
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
16
,
17
,
24
,
32
,
33
,
64
,
65
,
127
,
128
,
129
,
1023
,
1024
,
1025
,
};
#define NSIZES ARRAY_SIZE(bitmap_sizes)
#define NTESTS 9
static
void
test_sizes
(
int
nbits
,
bool
dynalloc
)
{
BITMAP_DECLARE
(
sbitmap
,
nbits
);
uint32_t
marker
;
bitmap
*
bitmap
;
int
i
,
j
;
bool
wrong
;
if
(
dynalloc
)
{
bitmap
=
bitmap_alloc
(
nbits
);
ok1
(
bitmap
!=
NULL
);
}
else
{
bitmap
=
sbitmap
;
marker
=
0xdeadbeef
;
}
bitmap_zero
(
bitmap
,
nbits
);
wrong
=
false
;
for
(
i
=
0
;
i
<
nbits
;
i
++
)
{
wrong
=
wrong
||
bitmap_test_bit
(
bitmap
,
i
);
}
ok1
(
!
wrong
);
bitmap_fill
(
bitmap
,
nbits
);
wrong
=
false
;
for
(
i
=
0
;
i
<
nbits
;
i
++
)
{
wrong
=
wrong
||
!
bitmap_test_bit
(
bitmap
,
i
);
}
ok1
(
!
wrong
);
wrong
=
false
;
for
(
i
=
0
;
i
<
nbits
;
i
++
)
{
bitmap_zero
(
bitmap
,
nbits
);
bitmap_set_bit
(
bitmap
,
i
);
for
(
j
=
0
;
j
<
nbits
;
j
++
)
{
bool
val
=
(
i
==
j
);
wrong
=
wrong
||
(
bitmap_test_bit
(
bitmap
,
j
)
!=
val
);
}
}
ok1
(
!
wrong
);
wrong
=
false
;
for
(
i
=
0
;
i
<
nbits
;
i
++
)
{
bitmap_fill
(
bitmap
,
nbits
);
bitmap_clear_bit
(
bitmap
,
i
);
for
(
j
=
0
;
j
<
nbits
;
j
++
)
{
bool
val
=
!
(
i
==
j
);
wrong
=
wrong
||
(
bitmap_test_bit
(
bitmap
,
j
)
!=
val
);
}
}
ok1
(
!
wrong
);
bitmap_zero
(
bitmap
,
nbits
);
ok1
(
bitmap_empty
(
bitmap
,
nbits
));
wrong
=
false
;
for
(
i
=
0
;
i
<
nbits
;
i
++
)
{
bitmap_zero
(
bitmap
,
nbits
);
bitmap_set_bit
(
bitmap
,
i
);
wrong
=
wrong
||
bitmap_empty
(
bitmap
,
nbits
);
}
ok1
(
!
wrong
);
bitmap_fill
(
bitmap
,
nbits
);
ok1
(
bitmap_full
(
bitmap
,
nbits
));
wrong
=
false
;
for
(
i
=
0
;
i
<
nbits
;
i
++
)
{
bitmap_fill
(
bitmap
,
nbits
);
bitmap_clear_bit
(
bitmap
,
i
);
wrong
=
wrong
||
bitmap_full
(
bitmap
,
nbits
);
}
ok1
(
!
wrong
);
if
(
dynalloc
)
{
free
(
bitmap
);
}
else
{
ok1
(
marker
==
0xdeadbeef
);
}
}
int
main
(
void
)
{
int
i
;
bool
dynalloc
;
/* This is how many tests you plan to run */
plan_tests
(
NSIZES
*
NTESTS
*
2
);
for
(
i
=
0
;
i
<
NSIZES
;
i
++
)
{
foreach_int
(
dynalloc
,
false
,
true
)
{
diag
(
"Testing %d-bit bitmap (%s allocation)"
,
bitmap_sizes
[
i
],
dynalloc
?
"dynamic"
:
"static"
);
test_sizes
(
bitmap_sizes
[
i
],
dynalloc
);
}
}
/* This exits depending on whether all tests passed */
return
exit_status
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment