Skip to content

Commit

Permalink
hw/i2c/pmbus_device: Fix modifying QOM class internals from instance
Browse files Browse the repository at this point in the history
QOM object instance should not modify its class state (because
all other objects instanciated from this class get affected).

Instead of modifying the PMBusDeviceClass 'device_num_pages' field
the first time a instance is initialized (in pmbus_pages_alloc),
introduce a new pmbus_pages_num() helper which returns the page
number from the class without modifying the class state.

The code logic become slighly simplified.

Inspired-by: Bernhard Beschow <shentey@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Message-Id: <20230523064408.57941-4-philmd@linaro.org>
  • Loading branch information
philmd committed Aug 31, 2023
1 parent 0773885 commit f0e4588
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions hw/i2c/pmbus_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,18 @@ static void pmbus_quick_cmd(SMBusDevice *smd, uint8_t read)
}
}

static void pmbus_pages_alloc(PMBusDevice *pmdev)
static uint8_t pmbus_pages_num(PMBusDevice *pmdev)
{
const PMBusDeviceClass *k = PMBUS_DEVICE_GET_CLASS(pmdev);

/* some PMBus devices don't use the PAGE command, so they get 1 page */
PMBusDeviceClass *k = PMBUS_DEVICE_GET_CLASS(pmdev);
if (k->device_num_pages == 0) {
k->device_num_pages = 1;
}
pmdev->num_pages = k->device_num_pages;
pmdev->pages = g_new0(PMBusPage, k->device_num_pages);
return k->device_num_pages ? : 1;
}

static void pmbus_pages_alloc(PMBusDevice *pmdev)
{
pmdev->num_pages = pmbus_pages_num(pmdev);
pmdev->pages = g_new0(PMBusPage, pmdev->num_pages);
}

void pmbus_check_limits(PMBusDevice *pmdev)
Expand Down

0 comments on commit f0e4588

Please sign in to comment.