Skip to content

Commit

Permalink
vga: refactor vram_size clamping and rounding
Browse files Browse the repository at this point in the history
Make the code a bit more obvious.

We don't have min/max, so a general helper for clamp probably isn't
acceptable either.

Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
  • Loading branch information
rkrcmar authored and kraxel committed Mar 3, 2015
1 parent bb7443f commit 619616c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions hw/display/vga.c
Expand Up @@ -2094,6 +2094,17 @@ static const GraphicHwOps vga_ops = {
.text_update = vga_update_text,
};

static inline uint32_t uint_clamp(uint32_t val, uint32_t vmin, uint32_t vmax)
{
if (val < vmin) {
return vmin;
}
if (val > vmax) {
return vmax;
}
return val;
}

void vga_common_init(VGACommonState *s, Object *obj, bool global_vmstate)
{
int i, j, v, b;
Expand Down Expand Up @@ -2121,13 +2132,10 @@ void vga_common_init(VGACommonState *s, Object *obj, bool global_vmstate)
expand4to8[i] = v;
}

/* valid range: 1 MB -> 512 MB */
s->vram_size = 1024 * 1024;
while (s->vram_size < (s->vram_size_mb << 20) &&
s->vram_size < (512 << 20)) {
s->vram_size <<= 1;
}
s->vram_size_mb = s->vram_size >> 20;
s->vram_size_mb = uint_clamp(s->vram_size_mb, 1, 512);
s->vram_size_mb = pow2ceil(s->vram_size_mb);
s->vram_size = s->vram_size_mb << 20;

if (!s->vbe_size) {
s->vbe_size = s->vram_size;
}
Expand Down

0 comments on commit 619616c

Please sign in to comment.