c
// Add a reference count to the gpio_chardev_data struct
struct gpio_chardev_data {
  // ...
  int watched_lines_refcount;
};

// Initialize the reference count
cdev->watched_lines_refcount = 1;

// Increment the reference count when memory is allocated
cdev->watched_lines = bitmap_zalloc(...);
cdev->watched_lines_refcount++;

// Decrement the reference count before freeing memory
6  if (cdev->watched_lines_refcount > 0) {
7    bitmap_free(cdev->watched_lines);
8    cdev->watched_lines_refcount--;
9  }
