vnc: palette: use a pool to reduce memory allocations
We now that the palette will never have more than 256 elements. Let's use a pool to reduce malloc calls. Signed-off-by: Corentin Chary <corentincj@iksaif.net> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
		
							parent
							
								
									ce702e93b0
								
							
						
					
					
						commit
						e31e3694af
					
				@ -63,23 +63,9 @@ VncPalette *palette_new(size_t max, int bpp)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void palette_destroy(VncPalette *palette)
 | 
					void palette_destroy(VncPalette *palette)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    int i;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (palette == NULL) {
 | 
					    if (palette == NULL) {
 | 
				
			||||||
        return ;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (i = 0; i < VNC_PALETTE_HASH_SIZE; i++) {
 | 
					 | 
				
			||||||
        VncPaletteEntry *entry = QLIST_FIRST(&palette->table[i]);
 | 
					 | 
				
			||||||
        while (entry) {
 | 
					 | 
				
			||||||
            VncPaletteEntry *tmp = QLIST_NEXT(entry, next);
 | 
					 | 
				
			||||||
            QLIST_REMOVE(entry, next);
 | 
					 | 
				
			||||||
            qemu_free(entry);
 | 
					 | 
				
			||||||
            entry = tmp;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        qemu_free(palette);
 | 
					        qemu_free(palette);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int palette_put(VncPalette *palette, uint32_t color)
 | 
					int palette_put(VncPalette *palette, uint32_t color)
 | 
				
			||||||
@ -97,7 +83,7 @@ int palette_put(VncPalette *palette, uint32_t color)
 | 
				
			|||||||
    if (!entry) {
 | 
					    if (!entry) {
 | 
				
			||||||
        VncPaletteEntry *entry;
 | 
					        VncPaletteEntry *entry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        entry = qemu_mallocz(sizeof(*entry));
 | 
					        entry = &palette->pool[palette->size];
 | 
				
			||||||
        entry->color = color;
 | 
					        entry->color = color;
 | 
				
			||||||
        entry->idx = idx;
 | 
					        entry->idx = idx;
 | 
				
			||||||
        QLIST_INSERT_HEAD(&palette->table[hash], entry, next);
 | 
					        QLIST_INSERT_HEAD(&palette->table[hash], entry, next);
 | 
				
			||||||
 | 
				
			|||||||
@ -34,6 +34,7 @@
 | 
				
			|||||||
#include <stdint.h>
 | 
					#include <stdint.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define VNC_PALETTE_HASH_SIZE 256
 | 
					#define VNC_PALETTE_HASH_SIZE 256
 | 
				
			||||||
 | 
					#define VNC_PALETTE_MAX_SIZE  256
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct VncPaletteEntry {
 | 
					typedef struct VncPaletteEntry {
 | 
				
			||||||
    int idx;
 | 
					    int idx;
 | 
				
			||||||
@ -42,7 +43,7 @@ typedef struct VncPaletteEntry {
 | 
				
			|||||||
} VncPaletteEntry;
 | 
					} VncPaletteEntry;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct VncPalette {
 | 
					typedef struct VncPalette {
 | 
				
			||||||
    QObject_HEAD;
 | 
					    VncPaletteEntry pool[VNC_PALETTE_MAX_SIZE];
 | 
				
			||||||
    size_t size;
 | 
					    size_t size;
 | 
				
			||||||
    size_t max;
 | 
					    size_t max;
 | 
				
			||||||
    int bpp;
 | 
					    int bpp;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user