bitmap: Replace bitmap with that of the libsyscommon 86/295986/2
authorYoungjae Cho <y0.cho@samsung.com>
Mon, 17 Jul 2023 01:19:26 +0000 (10:19 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Tue, 18 Jul 2023 07:02:46 +0000 (07:02 +0000)
Change-Id: I701b7102f466f6f16dae6ce499fc04a0218b5c68
Signed-off-by: Youngjae Cho <y0.cho@samsung.com>
src/extcon/hdmi.c
src/power/power-event-lock.c
src/shared/apps.c
src/shared/bitmap.c [deleted file]
src/shared/bitmap.h [deleted file]
tests/deviced-common-private-test/CMakeLists.txt
tests/deviced-common-private-test/test-bitmap.c [deleted file]

index 34b11ef..a082344 100644 (file)
@@ -20,8 +20,8 @@
 #include <vconf.h>
 #include <libsyscommon/libgdbus.h>
 #include <libsyscommon/list.h>
+#include <libsyscommon/bitmap.h>
 #include "core/log.h"
-#include "shared/bitmap.h"
 #include "shared/device-notifier.h"
 #include "core.h"
 #include "display-ops.h"
@@ -33,7 +33,7 @@
 
 #define HDMI_NUMBER_MAX        (64)                                    // max number of supported hdmi
 
-static struct dd_bitmap *bm_hdmi;
+static struct syscommon_bitmap *bm_hdmi;
 static struct extcon_ops hdmi_extcon_ops;
 
 static void hdmi_send_broadcast(int status)
@@ -64,7 +64,7 @@ static int get_hdmi_status(void)
                return HDMI_DISCONNECTED;
        }
 
-       if (count_set_bit(bm_hdmi) > 0)
+       if (syscommon_bitmap_count_set_bit(bm_hdmi) > 0)
                return HDMI_CONNECTED;
 
        return HDMI_DISCONNECTED;
@@ -79,9 +79,9 @@ static void update_hdmi_bitmap(int index, int status)
        }
 
        if (status == HDMI_CONNECTED)
-               set_bit(bm_hdmi, index);
+               syscommon_bitmap_set_bit(bm_hdmi, index);
        else
-               clear_bit(bm_hdmi, index);
+               syscommon_bitmap_clear_bit(bm_hdmi, index);
 }
 
 static void update_all_hdmi_bitmap(int status)
@@ -92,9 +92,9 @@ static void update_all_hdmi_bitmap(int status)
        }
 
        if (status == HDMI_CONNECTED)
-               set_all_bits(bm_hdmi);
+               syscommon_bitmap_set_all_bits(bm_hdmi);
        else
-               clear_all_bits(bm_hdmi);
+               syscommon_bitmap_clear_all_bits(bm_hdmi);
 }
 
 static int hdmi_update(const char *index, int status)
@@ -159,12 +159,13 @@ static void hdmi_init(void *data)
        if (ret < 0)
                _E("Failed to init dbus method: %d", ret);
 
-       bm_hdmi = init_bitmap(HDMI_NUMBER_MAX);
+       bm_hdmi = syscommon_bitmap_init_bitmap(HDMI_NUMBER_MAX);
 }
 
 static void hdmi_exit(void *data)
 {
-       deinit_bitmap(bm_hdmi);
+       syscommon_bitmap_deinit_bitmap(bm_hdmi);
+       bm_hdmi = NULL;
 }
 
 static struct extcon_ops hdmi_extcon_ops = {
index cca7921..15beb06 100644 (file)
@@ -20,8 +20,9 @@
 #include <stdint.h>
 #include <linux/input.h>
 #include <libsyscommon/ini-parser.h>
+#include <libsyscommon/bitmap.h>
+#include <libsyscommon/file.h>
 
-#include "shared/bitmap.h"
 #include "shared/device-notifier.h"
 #include "shared/log.h"
 #include "power-event-lock.h"
@@ -35,7 +36,7 @@
  * of an event from going to sleep regardless of the power state. */
 #define EVENT_LOCK           "eventlock"
 
-static struct dd_bitmap *eventlock;
+static struct syscommon_bitmap *eventlock;
 static int notifier_id[EL_MAX][2];
 
 void event_wake_lock(enum eventlock_type type)
@@ -45,8 +46,8 @@ void event_wake_lock(enum eventlock_type type)
        if (type <= EL_MIN || type >= EL_MAX)
                return;
 
-       set_bit(eventlock, type);
-       setcount = count_set_bit(eventlock);
+       syscommon_bitmap_set_bit(eventlock, type);
+       setcount = syscommon_bitmap_count_set_bit(eventlock);
 
        _I("Set eventlock of type=%d, current number of eventlock=%d", type, setcount);
 
@@ -63,8 +64,8 @@ void event_wake_unlock(enum eventlock_type type)
        if (type <= EL_MIN || type >= EL_MAX)
                return;
 
-       clear_bit(eventlock, type);
-       setcount = count_set_bit(eventlock);
+       syscommon_bitmap_clear_bit(eventlock, type);
+       setcount = syscommon_bitmap_count_set_bit(eventlock);
 
        _I("Unset eventlock of type=%d, current number of eventlock=%d", type, setcount);
 
@@ -143,7 +144,7 @@ void power_event_lock_init(void)
 {
        int charger_wakelock = 0;
 
-       eventlock = init_bitmap(EL_MAX);
+       eventlock = syscommon_bitmap_init_bitmap(EL_MAX);
        if (!eventlock) {
                _E("Failed to init event lock bitmap");
                return;
index bb3a9c9..2a97507 100644 (file)
 
 #include <stdarg.h>
 #include <libsyscommon/libgdbus.h>
+#include <libsyscommon/bitmap.h>
 
 #include "core/log.h"
 #include "shared/common.h"
 #include "apps.h"
 #include "display-plugin.h"
 #include "display-state-transition.h"
-#include "shared/bitmap.h"
 
 #define POPUP_METHOD "PopupLaunch"
 #define BUFF_MAX        255
 
-static struct dd_bitmap *bm_background;
+static struct syscommon_bitmap *bm_background;
 static int pid_max;
 
 static const struct app_dbus_match {
@@ -161,7 +161,7 @@ int remove_notification(char *type, int id)
 
 bool is_app_background(pid_t pid)
 {
-       return test_bit(bm_background, pid);
+       return syscommon_bitmap_test_bit(bm_background, pid);
 }
 
 void init_bm_background(void)
@@ -173,7 +173,7 @@ void init_bm_background(void)
                pid_max = 32768;
 
        /* need (pid_max + 1) bits to represent pid 0 ~ pid_max */
-       bm_background = init_bitmap(pid_max + 1);
+       bm_background = syscommon_bitmap_init_bitmap(pid_max + 1);
        if (!bm_background)
                _E("Failed to allock bm_background.");
 }
@@ -187,9 +187,9 @@ void set_app_state(pid_t pid, enum application_state as)
                return;
 
        if (as == APPLICATION_BACKGROUND)
-               set_bit(bm_background, pid);
+               syscommon_bitmap_set_bit(bm_background, pid);
        else if (as == APPLICATION_FOREGROUND || as == APPLICATION_TERMINATED)
-               clear_bit(bm_background, pid);
+               syscommon_bitmap_clear_bit(bm_background, pid);
        else
                _E("Invalid as=%d", as);
 }
@@ -201,5 +201,5 @@ static void __CONSTRUCTOR__ initialize(void)
 
 static void __DESTRUCTOR__ finalize(void)
 {
-       deinit_bitmap(bm_background);
+       syscommon_bitmap_deinit_bitmap(bm_background);
 }
diff --git a/src/shared/bitmap.c b/src/shared/bitmap.c
deleted file mode 100644 (file)
index d528188..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * deviced
- *
- * Copyright (c) 2020 - 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed 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.
- */
-
-#include <stdlib.h>
-#include <errno.h>
-
-#include "bitmap.h"
-#include "log.h"
-
-static void bitmap_alloc_size(struct dd_bitmap *bm, unsigned int nbits)
-{
-       unsigned int bytes = ROUNDUP_BITS_TO_LONGS(nbits) * BYTES_PER_LONG;
-
-       bytes += BITMAP_MARGIN;
-
-       bm->b = (unsigned long *) malloc(bytes);
-       if (!bm->b)
-               return;
-
-       bm->size = nbits;
-}
-
-void set_all_bits(struct dd_bitmap *bm)
-{
-       unsigned long complete_longs = bm->size / BITS_PER_LONG;
-       unsigned long complete_bytes = complete_longs * BYTES_PER_LONG;
-       unsigned long residue_bits = bm->size % BITS_PER_LONG;
-
-       /* set all bits of the long element except the last long element */
-       memset(bm->b, ~0, complete_bytes);
-
-       /* set residue bits in the last long element */
-       if (residue_bits)
-               bm->b[complete_longs] = (1UL << residue_bits) - 1;
-}
-
-void clear_all_bits(struct dd_bitmap *bm)
-{
-       unsigned int bytes = ROUNDUP_BITS_TO_LONGS(bm->size) * BYTES_PER_LONG;
-       memset(bm->b, 0, bytes);
-}
-
-void set_bit(struct dd_bitmap *bm, unsigned long nr)
-{
-       if (!bm)
-               return;
-
-       if (nr >= bm->size) {
-               _E("Requested nr=%lu exceeds max bit=%lu", nr, bm->size - 1);
-               return;
-       }
-
-       bm->b[BIT_WORD(nr)] |= BIT_MASK(nr);
-}
-
-void clear_bit(struct dd_bitmap *bm, unsigned long nr)
-{
-       if (!bm)
-               return;
-
-       if (nr >= bm->size) {
-               _E("Requested nr=%lu exceeds max bit=%lu", nr, bm->size - 1);
-               return;
-       }
-
-       bm->b[BIT_WORD(nr)] &= ~BIT_MASK(nr);
-}
-
-bool test_bit(struct dd_bitmap *bm, unsigned long nr)
-{
-       if (!bm)
-               return false;
-
-       if (nr >= bm->size) {
-               _E("Requested nr=%lu exceeds max bit=%lu", nr, bm->size - 1);
-               return false;
-       }
-
-       return bm->b[BIT_WORD(nr)] & BIT_MASK(nr);
-}
-
-int count_set_bit(struct dd_bitmap *bm)
-{
-       int i;
-       int count = 0;
-
-       if (!bm)
-               return -1;
-
-       for (i = 0; i < ROUNDUP_BITS_TO_LONGS(bm->size); ++i)
-               count += __builtin_popcountl(bm->b[i]);
-
-       return count;
-}
-
-int count_unset_bit(struct dd_bitmap *bm)
-{
-       if (!bm)
-               return -1;
-
-       return bm->size - count_set_bit(bm);
-}
-
-struct dd_bitmap* init_bitmap(unsigned int nbits)
-{
-       struct dd_bitmap *bm = NULL;
-
-       if (nbits == 0)
-               return NULL;
-
-       bm = (struct dd_bitmap *) malloc(sizeof(struct dd_bitmap));
-       if (!bm)
-               return NULL;
-
-       bm->b = NULL;
-       bm->size = 0;
-
-       bitmap_alloc_size(bm, nbits);
-       if (!bm->b) {
-               free(bm);
-               return NULL;
-       }
-
-       clear_all_bits(bm);
-       bm->size = nbits;
-
-       return bm;
-}
-
-void deinit_bitmap(struct dd_bitmap *bm)
-{
-       if (!bm)
-               return;
-
-       free(bm->b);
-       free(bm);
-}
diff --git a/src/shared/bitmap.h b/src/shared/bitmap.h
deleted file mode 100644 (file)
index 902a44d..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * deviced
- *
- * Copyright (c) 2020 - 2021 Samsung Electronics Co., Ltd.
- *
- * Licensed 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.
- */
-
-#ifndef __DD_BITMAP_H__
-#define __DD_BITMAP_H__
-
-#include <string.h>
-
-#include "shared/common.h"
-
-/* Align bits by size of long, and convert it to long,
- * for example, if long is 64bit then,
- *   ROUNDUP_BITS_TO_LONGS(0) -> 0
- *   ROUNDUP_BITS_TO_LONGS(1) -> 1
- *   ROUNDUP_BITS_TO_LONGS(64) -> 1
- *   ROUNDUP_BITS_TO_LONGS(65) -> 2
- *   ROUNDUP_BITS_TO_LONGS(128) -> 2
- *   ROUNDUP_BITS_TO_LONGS(129) -> 3
- *   ... */
-#define ROUNDUP_BITS_TO_LONGS(nbits) (roundup(nbits, BITS_PER_LONG) / BITS_PER_LONG)
-
-#define BIT_WORD(nr)      ((nr) / BITS_PER_LONG)
-#define BIT_MASK(nr)      (1UL << ((nr) % BITS_PER_LONG))
-#define BITMAP_MARGIN     (BYTES_PER_LONG * 2) /* trailing margin for safety, bytes */
-
-struct dd_bitmap {
-       /* bitmap */
-       unsigned long *b;
-
-       /* number of bits, not the maximum bit.
-        * maximum bit is size - 1 */
-       unsigned long size;
-};
-
-void set_bit(struct dd_bitmap *bm, unsigned long nr);
-void clear_bit(struct dd_bitmap *bm, unsigned long nr);
-bool test_bit(struct dd_bitmap *bm, unsigned long nr);
-void set_all_bits(struct dd_bitmap *bm);
-void clear_all_bits(struct dd_bitmap *bm);
-int count_set_bit(struct dd_bitmap *bm);
-int count_unset_bit(struct dd_bitmap *bm);
-struct dd_bitmap* init_bitmap(unsigned int nbits);
-void deinit_bitmap(struct dd_bitmap *bm);
-
-#endif
index c4262d7..6e69ca8 100644 (file)
@@ -23,7 +23,6 @@ TARGET_LINK_LIBRARIES(test-shared ${ORIG_REQUIRED_PKGS_LDFLAGS})
 
 PKG_CHECK_MODULES(TEST_REQUIRED_PKGS REQUIRED cmocka)
 
-LIST(APPEND TEST_DRIVERS test-bitmap.c)
 LIST(APPEND TEST_DRIVERS test-device-notifier.c)
 LIST(APPEND TEST_DRIVERS test-plugin.c)
 
diff --git a/tests/deviced-common-private-test/test-bitmap.c b/tests/deviced-common-private-test/test-bitmap.c
deleted file mode 100644 (file)
index 23e5644..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-#include <shared/bitmap.h>
-
-#include "test-main.h"
-
-static void test_init_bitmap_p(void **state)
-{
-       struct dd_bitmap *bm;
-
-       bm = init_bitmap(10);
-       assert_non_null(bm);
-
-       deinit_bitmap(bm);
-}
-
-static void test_init_bitmap_n(void **state)
-{
-       struct dd_bitmap *bm = NULL;
-
-       bm = init_bitmap(0);
-       assert_null(bm);
-}
-
-static void test_test_bit(void **state)
-{
-       struct dd_bitmap *bm;
-       const int bmsize = 37;
-       bool bit;
-       int i, nbit;
-
-       bm = init_bitmap(bmsize);
-       assert_non_null(bm);
-
-       for (i = 0; i < bmsize; ++i) {
-               bit = test_bit(bm, i);
-               assert_false(bit);
-       }
-
-       /* count bit by setting one by one */
-       for (i = 0; i < bmsize; ++i) {
-               set_bit(bm, i);
-               nbit = count_set_bit(bm);
-               assert_int_equal(nbit, i + 1);
-       }
-
-       /* test the marginal bit */
-       bit = test_bit(bm, 0);
-       assert_true(bit);
-       bit = test_bit(bm, bmsize - 1);
-       assert_true(bit);
-       bit = test_bit(bm, bmsize);
-       assert_false(bit);
-
-
-       /* count bit by clearing one by one */
-       for (i = 0; i < bmsize; ++i) {
-               clear_bit(bm, i);
-               nbit = count_set_bit(bm);
-               assert_int_equal(nbit, bmsize - i - 1);
-       }
-
-       deinit_bitmap(bm);
-}
-
-static void test_all_bit(void **state)
-{
-       struct dd_bitmap *bm;
-       const int bmsize = 37;
-       int nbit;
-
-       bm = init_bitmap(bmsize);
-       assert_non_null(bm);
-
-       set_all_bits(bm);
-       nbit = count_set_bit(bm);
-       assert_int_equal(nbit, bmsize);
-
-       clear_all_bits(bm);
-       nbit = count_set_bit(bm);
-       assert_int_equal(nbit, 0);
-
-       deinit_bitmap(bm);
-}
-
-static int run_bitmap_test(void)
-{
-       static const struct CMUnitTest testsuite[] = {
-               cmocka_unit_test(test_init_bitmap_p),
-               cmocka_unit_test(test_init_bitmap_n),
-               cmocka_unit_test(test_test_bit),
-               cmocka_unit_test(test_all_bit),
-       };
-
-       return cmocka_run_group_tests(testsuite, NULL, NULL);
-}
-ADD_TEST_FUNCTION(run_bitmap_test)