[project @ 2006-01-24 23:05:22 by rjw]

Don't allocate memory until it's actually needed.

svn path=/import/netsurf/; revision=2029
This commit is contained in:
Richard Wilson 2006-01-24 23:05:22 +00:00
parent 0631647241
commit ef1f971a9a
2 changed files with 36 additions and 10 deletions

View File

@ -13,7 +13,7 @@
* sprites. * sprites.
*/ */
//#define NDEBUG #define NDEBUG
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
@ -172,15 +172,12 @@ struct bitmap *bitmap_create(int width, int height, bool clear)
bitmap->width = width; bitmap->width = width;
bitmap->height = height; bitmap->height = height;
bitmap->opaque = false; bitmap->opaque = false;
if (!bitmap_initialise(bitmap, clear)) { if (clear)
free(bitmap); bitmap->init = BITMAP_INITIALISE_FULL;
return NULL; else
} bitmap->init = BITMAP_INITIALISE_QUICK;
/* link into our list of bitmaps at the head */ /* link into our list of bitmaps at the head */
bitmap_maintenance = true;
bitmap_maintenance_priority |=
(bitmap_direct_used > bitmap_direct_size * 1.1);
if (bitmap_head) { if (bitmap_head) {
bitmap->next = bitmap_head; bitmap->next = bitmap_head;
bitmap_head->previous = bitmap; bitmap_head->previous = bitmap;
@ -210,6 +207,7 @@ struct bitmap *bitmap_create_file(char *file)
return NULL; return NULL;
bitmap->opaque = true; bitmap->opaque = true;
bitmap->persistent = true; bitmap->persistent = true;
bitmap->init = BITMAP_INITIALISE_DONE;
strcpy(bitmap->filename, file); strcpy(bitmap->filename, file);
/* link in at the head */ /* link in at the head */
@ -243,6 +241,7 @@ bool bitmap_initialise(struct bitmap *bitmap, bool clear)
if (!bitmap->sprite_area) { if (!bitmap->sprite_area) {
return false; return false;
} }
bitmap->init = BITMAP_INITIALISE_DONE;
bitmap_direct_used += area_size; bitmap_direct_used += area_size;
/* area control block */ /* area control block */
@ -264,6 +263,10 @@ bool bitmap_initialise(struct bitmap *bitmap, bool clear)
sprite->right_bit = 31; sprite->right_bit = 31;
sprite->image = sprite->mask = 44; sprite->image = sprite->mask = 44;
sprite->mode = tinct_SPRITE_MODE; sprite->mode = tinct_SPRITE_MODE;
bitmap_maintenance = true;
bitmap_maintenance_priority |=
(bitmap_direct_used > bitmap_direct_size * 0.9);
return true; return true;
} }
@ -343,6 +346,8 @@ bool bitmap_get_opaque(struct bitmap *bitmap)
char *bitmap_get_buffer(struct bitmap *bitmap) char *bitmap_get_buffer(struct bitmap *bitmap)
{ {
bool clear;
assert(bitmap); assert(bitmap);
/* move to the head of the list */ /* move to the head of the list */
@ -357,6 +362,13 @@ char *bitmap_get_buffer(struct bitmap *bitmap)
bitmap_head = bitmap; bitmap_head = bitmap;
} }
/* dynamically create the buffer */
if (bitmap->init != BITMAP_INITIALISE_DONE) {
clear = (bitmap->init == BITMAP_INITIALISE_FULL);
if (!bitmap_initialise(bitmap, clear))
return NULL;
}
/* image is already decompressed, no change to image states */ /* image is already decompressed, no change to image states */
if (bitmap->sprite_area) if (bitmap->sprite_area)
return ((char *) (bitmap->sprite_area)) + 16 + 44; return ((char *) (bitmap->sprite_area)) + 16 + 44;
@ -398,6 +410,7 @@ size_t bitmap_get_rowstride(struct bitmap *bitmap)
void bitmap_destroy(struct bitmap *bitmap) void bitmap_destroy(struct bitmap *bitmap)
{ {
struct bitmap_compressed_header *header;
unsigned int area_size; unsigned int area_size;
assert(bitmap); assert(bitmap);
@ -417,8 +430,13 @@ void bitmap_destroy(struct bitmap *bitmap)
bitmap_direct_used -= area_size; bitmap_direct_used -= area_size;
free(bitmap->sprite_area); free(bitmap->sprite_area);
} }
if (bitmap->compressed) if (bitmap->compressed) {
header = (struct bitmap_compressed_header *)
bitmap->compressed;
bitmap_compressed_used -= header->input_size +
sizeof(struct bitmap_compressed_header);
free(bitmap->compressed); free(bitmap->compressed);
}
if (bitmap->filename[0]) if (bitmap->filename[0])
bitmap_delete_file(bitmap); bitmap_delete_file(bitmap);
free(bitmap); free(bitmap);
@ -707,9 +725,10 @@ void bitmap_load_file(struct bitmap *bitmap)
} else if ((int)((*(((int *)bitmap->compressed) + 6)) + } else if ((int)((*(((int *)bitmap->compressed) + 6)) +
sizeof(struct bitmap_compressed_header)) == len && sizeof(struct bitmap_compressed_header)) == len &&
strncmp(bitmap->compressed + 8, "bitmap", 6) == 0) { strncmp(bitmap->compressed + 8, "bitmap", 6) == 0) {
bitmap_compressed_used -= len;
bitmap_compressed = (struct bitmap_compressed_header *) bitmap_compressed = (struct bitmap_compressed_header *)
bitmap->compressed; bitmap->compressed;
bitmap_compressed_used -= bitmap_compressed->input_size +
sizeof(struct bitmap_compressed_header);
bitmap->width = bitmap_compressed->width; bitmap->width = bitmap_compressed->width;
bitmap->height = bitmap_compressed->height; bitmap->height = bitmap_compressed->height;
} else { } else {

View File

@ -12,12 +12,19 @@
struct osspriteop_area; struct osspriteop_area;
typedef enum {
BITMAP_INITIALISE_DONE, /** Initialisation has been done */
BITMAP_INITIALISE_QUICK, /** Just allocate memory */
BITMAP_INITIALISE_FULL /** Clear the sprite buffer */
} bitmap_initialisation;
struct bitmap { struct bitmap {
int width; int width;
int height; int height;
bool opaque; bool opaque;
bool modified; bool modified;
bool persistent; bool persistent;
bitmap_initialisation init;
osspriteop_area *sprite_area; /** Uncompressed data, or NULL */ osspriteop_area *sprite_area; /** Uncompressed data, or NULL */
char *compressed; /** Compressed data, or NULL */ char *compressed; /** Compressed data, or NULL */