Skip to content

Commit

Permalink
Server: Backport new bitvector utility functions
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Lindqvist <[email protected]>
  • Loading branch information
cazfi committed Oct 2, 2023
1 parent 17c4cdf commit 04057e7
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
12 changes: 12 additions & 0 deletions freeciv/apply_patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
# 0047-Meson-Turn-audio-option-to-a-combo.patch
# Rework disabling audio
# osdn #48757
# 0008-Add-new-bitvector-utility-functions.patch
# New bitvector utility functions
# osdn #48731
# 0047-Add-bv_match_dbv-utility-function.patch
# New bitvector utility function
# osdn #48771
# 0057-Fix-bitvector-copy-functions.patch
# Fix to bitvector utility functions
# osdn #48772

# Not in the upstream Freeciv server
# ----------------------------------
Expand Down Expand Up @@ -55,6 +64,9 @@ declare -a PATCHLIST=(
"backports/0042-Meson-Make-manual-generator-build-optional"
"backports/0036-tile_move_cost_ptrs-Make-cardinal_move-signed"
"backports/0047-Meson-Turn-audio-option-to-a-combo"
"backports/0008-Add-new-bitvector-utility-functions"
"backports/0047-Add-bv_match_dbv-utility-function"
"backports/0057-Fix-bitvector-copy-functions"
"meson_webperimental"
"metachange"
"text_fixes"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
From ef337f93a4c8d4147da296703ef466252a449137 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <[email protected]>
Date: Tue, 26 Sep 2023 12:32:50 +0300
Subject: [PATCH 08/47] Add new bitvector utility functions

- dbv_copy()
- dbv_to_bv()
- bv_to_dbv()

See osdn #48731

Signed-off-by: Marko Lindqvist <[email protected]>
---
utility/bitvector.c | 30 ++++++++++++++++++++++++++++++
utility/bitvector.h | 4 ++++
2 files changed, 34 insertions(+)

diff --git a/utility/bitvector.c b/utility/bitvector.c
index 2d1851b68b..65047bfda6 100644
--- a/utility/bitvector.c
+++ b/utility/bitvector.c
@@ -198,6 +198,36 @@ bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2)
_BV_BYTES(pdbv2->bits));
}

+/***********************************************************************//**
+ Copy dynamic bit vector content from another.
+***************************************************************************/
+void dbv_copy(struct dbv *dest, const struct dbv *src)
+{
+ if (dest->bits != src->bits) {
+ dbv_resize(dest, src->bits);
+ }
+
+ memcpy(&dest->vec, &src->vec, _BV_BYTES(src->bits));
+}
+
+/***********************************************************************//**
+ Copy dynamic bit vector content to static bitvector. Static vector
+ is assumed to be at least same size as the dynamic one.
+***************************************************************************/
+void dbv_to_bv(unsigned char *dest, const struct dbv *src)
+{
+ memcpy(dest, &(src->vec), _BV_BYTES(src->bits));
+}
+
+/***********************************************************************//**
+ Copy static bit vector content to dynamic bitvector. Static vector
+ is assumed to be at least same size as the dynamic one.
+***************************************************************************/
+void bv_to_dbv(struct dbv *dest, const unsigned char *src)
+{
+ memcpy(&(dest->vec), dest, _BV_BYTES(dest->bits));
+}
+
/***********************************************************************//**
Debug a dynamic bitvector.
***************************************************************************/
diff --git a/utility/bitvector.h b/utility/bitvector.h
index cccde12a83..9ce4230853 100644
--- a/utility/bitvector.h
+++ b/utility/bitvector.h
@@ -50,6 +50,10 @@ void dbv_clr(struct dbv *pdbv, int bit);
void dbv_clr_all(struct dbv *pdbv);

bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2);
+void dbv_copy(struct dbv *dest, const struct dbv *src);
+
+void dbv_to_bv(unsigned char *dest, const struct dbv *src);
+void bv_to_dbv(struct dbv *dest, const unsigned char *src);

void dbv_debug(struct dbv *pdbv);

--
2.40.1

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
From e0f53590a33f02bc3c082382c02dabdfb0861f60 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <[email protected]>
Date: Sun, 1 Oct 2023 03:53:26 +0300
Subject: [PATCH 47/47] Add bv_match_dbv() utility function

See osdn #48771

Signed-off-by: Marko Lindqvist <[email protected]>
---
utility/bitvector.c | 18 ++++++++++++++++++
utility/bitvector.h | 1 +
2 files changed, 19 insertions(+)

diff --git a/utility/bitvector.c b/utility/bitvector.c
index 65047bfda6..7debd38ede 100644
--- a/utility/bitvector.c
+++ b/utility/bitvector.c
@@ -198,6 +198,24 @@ bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2)
_BV_BYTES(pdbv2->bits));
}

+/***********************************************************************//**
+ Is content of static bitvector same as that of dynamic one.
+ Comparison size is taken from the dynamic one.
+***************************************************************************/
+bool bv_match_dbv(const struct dbv *match, const unsigned char *src)
+{
+ size_t bytes = _BV_BYTES(match->bits);
+ int i;
+
+ for (i = 0; i < bytes; i++) {
+ if (match->vec[i] != src[i]) {
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
/***********************************************************************//**
Copy dynamic bit vector content from another.
***************************************************************************/
diff --git a/utility/bitvector.h b/utility/bitvector.h
index 9ce4230853..7c0879a4cf 100644
--- a/utility/bitvector.h
+++ b/utility/bitvector.h
@@ -50,6 +50,7 @@ void dbv_clr(struct dbv *pdbv, int bit);
void dbv_clr_all(struct dbv *pdbv);

bool dbv_are_equal(const struct dbv *pdbv1, const struct dbv *pdbv2);
+bool bv_match_dbv(const struct dbv *match, const unsigned char *src);
void dbv_copy(struct dbv *dest, const struct dbv *src);

void dbv_to_bv(unsigned char *dest, const struct dbv *src);
--
2.40.1

48 changes: 48 additions & 0 deletions freeciv/patches/backports/0057-Fix-bitvector-copy-functions.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
From 7184a785f82ecd5aac8fe0830355e71d4614501b Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <[email protected]>
Date: Sun, 1 Oct 2023 23:17:57 +0300
Subject: [PATCH 57/57] Fix bitvector copy functions

So far unused functions were copying either from or to wrong address

See osdn #48772

Signed-off-by: Marko Lindqvist <[email protected]>
---
utility/bitvector.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/utility/bitvector.c b/utility/bitvector.c
index 7debd38ede..17a65411e6 100644
--- a/utility/bitvector.c
+++ b/utility/bitvector.c
@@ -225,7 +225,7 @@ void dbv_copy(struct dbv *dest, const struct dbv *src)
dbv_resize(dest, src->bits);
}

- memcpy(&dest->vec, &src->vec, _BV_BYTES(src->bits));
+ memcpy(&dest->vec, src->vec, _BV_BYTES(src->bits));
}

/***********************************************************************//**
@@ -234,7 +234,7 @@ void dbv_copy(struct dbv *dest, const struct dbv *src)
***************************************************************************/
void dbv_to_bv(unsigned char *dest, const struct dbv *src)
{
- memcpy(dest, &(src->vec), _BV_BYTES(src->bits));
+ memcpy(dest, src->vec, _BV_BYTES(src->bits));
}

/***********************************************************************//**
@@ -243,7 +243,7 @@ void dbv_to_bv(unsigned char *dest, const struct dbv *src)
***************************************************************************/
void bv_to_dbv(struct dbv *dest, const unsigned char *src)
{
- memcpy(&(dest->vec), dest, _BV_BYTES(dest->bits));
+ memcpy(dest->vec, dest, _BV_BYTES(dest->bits));
}

/***********************************************************************//**
--
2.40.1

0 comments on commit 04057e7

Please sign in to comment.