app/base/base-config.[ch] use a gulong for the tile_cache_size.

2002-05-28  Sven Neumann  <sven@gimp.org>

	* app/base/base-config.[ch]
	* app/gui/user-install-dialog.c: use a gulong for the tile_cache_size.

	* app/base/tile-cache.c: cleanup, no changes.

	* app/config/gimpconfig-types.c (string_to_memsize): avoid overflows.
This commit is contained in:
Sven Neumann 2002-05-28 20:29:27 +00:00 committed by Sven Neumann
parent 6707e317d1
commit d453d02362
7 changed files with 76 additions and 33 deletions

View File

@ -1,3 +1,12 @@
2002-05-28 Sven Neumann <sven@gimp.org>
* app/base/base-config.[ch]
* app/gui/user-install-dialog.c: use a gulong for the tile_cache_size.
* app/base/tile-cache.c: cleanup, no changes.
* app/config/gimpconfig-types.c (string_to_memsize): avoid overflows.
2002-05-28 Michael Natterer <mitch@gimp.org>
Yes, this is a bit hackish...

View File

@ -36,7 +36,7 @@ static GimpBaseConfig static_base_config =
INIT_MEMBER(temp_path , NULL),
INIT_MEMBER(swap_path , NULL),
INIT_MEMBER(tile_cache_size , 33554432),
INIT_MEMBER(tile_cache_size , 1 << 25),
INIT_MEMBER(stingy_memory_use, FALSE),
INIT_MEMBER(num_processors , 1)
};

View File

@ -26,7 +26,7 @@ struct _GimpBaseConfig
{
gchar *temp_path;
gchar *swap_path;
guint tile_cache_size;
gulong tile_cache_size;
gboolean stingy_memory_use;
gint num_processors;
};

View File

@ -60,18 +60,18 @@ typedef struct _TileList
Tile *last;
} TileList;
static gulong max_tile_size = TILE_WIDTH * TILE_HEIGHT * 4;
static gulong cur_cache_size = 0;
static gulong max_cache_size = 0;
static gulong max_tile_size = TILE_WIDTH * TILE_HEIGHT * 4;
static gulong cur_cache_size = 0;
static gulong max_cache_size = 0;
static gulong cur_cache_dirty = 0;
static TileList clean_list = { NULL, NULL };
static TileList dirty_list = { NULL, NULL };
static TileList clean_list = { NULL, NULL };
static TileList dirty_list = { NULL, NULL };
#ifdef USE_PTHREADS
static pthread_t preswap_thread;
static pthread_mutex_t dirty_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t dirty_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t dirty_signal = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t tile_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t tile_mutex = PTHREAD_MUTEX_INITIALIZER;
#define CACHE_LOCK pthread_mutex_lock(&tile_mutex)
#define CACHE_UNLOCK pthread_mutex_unlock(&tile_mutex)
#else
@ -99,8 +99,10 @@ tile_cache_insert (Tile *tile)
* it was the most recently accessed tile.
*/
list = (TileList*)(tile->listhead);
newlist = (tile->dirty || tile->swap_offset == -1) ? &dirty_list : &clean_list;
list = (TileList *) tile->listhead;
newlist = ((tile->dirty || tile->swap_offset == -1) ?
&dirty_list : &clean_list);
/* if list is NULL, the tile is not in the cache */
@ -120,7 +122,9 @@ tile_cache_insert (Tile *tile)
list->first = tile->next;
tile->listhead = NULL;
if (list == &dirty_list) cur_cache_dirty -= tile_size (tile);
if (list == &dirty_list)
cur_cache_dirty -= tile_size (tile);
}
else
{
@ -154,8 +158,11 @@ tile_cache_insert (Tile *tile)
tile->prev = newlist->last;
tile->listhead = newlist;
if (newlist->last) newlist->last->next = tile;
else newlist->first = tile;
if (newlist->last)
newlist->last->next = tile;
else
newlist->first = tile;
newlist->last = tile;
/* gosgood@idt.net 1999-12-04 */
@ -163,9 +170,10 @@ tile_cache_insert (Tile *tile)
/* Invariant: test for selecting dirty list should be the same */
/* as counting files dirty. */
if ((tile->dirty) || ( tile->swap_offset == -1))
if (tile->dirty || (tile->swap_offset == -1))
{
cur_cache_dirty += tile_size (tile);
if (1)
{
#ifdef USE_PTHREADS
@ -199,7 +207,7 @@ tile_cache_flush_internal (Tile *tile)
/* Find where the tile is in the cache.
*/
list = (TileList*)(tile->listhead);
list = (TileList *) tile->listhead;
if (list)
{
@ -207,7 +215,9 @@ tile_cache_flush_internal (Tile *tile)
* is referencing.
*/
cur_cache_size -= tile_size (tile);
if (list == &dirty_list) cur_cache_dirty -= tile_size (tile);
if (list == &dirty_list)
cur_cache_dirty -= tile_size (tile);
if (tile->next)
tile->next->prev = tile->prev;
@ -334,7 +344,8 @@ tile_idle_thread (gpointer data)
{
list = tile->listhead;
if (list == &dirty_list) cur_cache_dirty -= tile_size (tile);
if (list == &dirty_list)
cur_cache_dirty -= tile_size (tile);
if (tile->next)
tile->next->prev = tile->prev;
@ -350,9 +361,12 @@ tile_idle_thread (gpointer data)
tile->prev = clean_list.last;
tile->listhead = &clean_list;
if (clean_list.last) clean_list.last->next = tile;
else clean_list.first = tile;
clean_list.last = tile;
if (clean_list.last)
clean_list.last->next = tile;
else
clean_list.first = tile;
clean_list.last = tile;
CACHE_UNLOCK;
@ -373,15 +387,19 @@ tile_idle_thread (gpointer data)
}
}
#else
#else /* !USE_PTHREADS */
static gboolean
tile_idle_preswap (gpointer data)
{
Tile *tile;
if (cur_cache_dirty*2 < max_cache_size) return TRUE;
if ((tile = dirty_list.first))
if (cur_cache_dirty*2 < max_cache_size)
return TRUE;
if ((tile = dirty_list.first))
{
tile_swap_out(tile);
tile_swap_out (tile);
dirty_list.first = tile->next;
if (tile->next)
@ -392,8 +410,12 @@ tile_idle_preswap (gpointer data)
tile->next = NULL;
tile->prev = clean_list.last;
tile->listhead = &clean_list;
if (clean_list.last) clean_list.last->next = tile;
else clean_list.first = tile;
if (clean_list.last)
clean_list.last->next = tile;
else
clean_list.first = tile;
clean_list.last = tile;
cur_cache_dirty -= tile_size (tile);
}

View File

@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib-object.h>
@ -166,7 +167,8 @@ string_to_memsize (const GValue *src_value,
goto error;
size = strtoul (str, &end, 0);
if (size == ULONG_MAX)
if (size == ULONG_MAX && errno == ERANGE)
goto error;
if (end && *end)
@ -190,8 +192,17 @@ string_to_memsize (const GValue *src_value,
default:
goto error;
}
/* protect against overflow */
if (shift)
{
gulong limit = G_MAXULONG >> (shift);
size <<= shift;
if (size != (size & limit))
goto error;
size <<= shift;
}
}
g_value_set_ulong (dest_value, size);
@ -199,6 +210,7 @@ string_to_memsize (const GValue *src_value,
return;
error:
g_value_set_ulong (dest_value, 0);
g_warning ("Can't convert string to GimpMemsize.");
};

View File

@ -1128,7 +1128,7 @@ user_install_tuning (void)
gtk_widget_show (hbox);
tile_cache_adj = gtk_adjustment_new (base_config->tile_cache_size,
0, (4069.0 * 1024 * 1024), 1.0, 1.0, 0.0);
0, G_MAXULONG, 1.0, 1.0, 0.0);
memsize = gimp_mem_size_entry_new (GTK_ADJUSTMENT (tile_cache_adj));
gtk_box_pack_end (GTK_BOX (hbox), memsize, FALSE, FALSE, 0);
gtk_widget_show (memsize);
@ -1310,7 +1310,7 @@ user_install_resolution_done (void)
GList *update = NULL;
GList *remove = NULL;
gint new_tile_cache_size;
gulong new_tile_cache_size;
gchar *new_swap_path;
gboolean new_using_xserver_resolution;
gdouble new_monitor_xres;

View File

@ -1128,7 +1128,7 @@ user_install_tuning (void)
gtk_widget_show (hbox);
tile_cache_adj = gtk_adjustment_new (base_config->tile_cache_size,
0, (4069.0 * 1024 * 1024), 1.0, 1.0, 0.0);
0, G_MAXULONG, 1.0, 1.0, 0.0);
memsize = gimp_mem_size_entry_new (GTK_ADJUSTMENT (tile_cache_adj));
gtk_box_pack_end (GTK_BOX (hbox), memsize, FALSE, FALSE, 0);
gtk_widget_show (memsize);
@ -1310,7 +1310,7 @@ user_install_resolution_done (void)
GList *update = NULL;
GList *remove = NULL;
gint new_tile_cache_size;
gulong new_tile_cache_size;
gchar *new_swap_path;
gboolean new_using_xserver_resolution;
gdouble new_monitor_xres;