2000-12-29 23:22:01 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
1999-02-21 07:20:54 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
#include <glib.h>
|
1999-06-15 06:18:02 +08:00
|
|
|
|
2001-02-07 09:16:18 +08:00
|
|
|
#include <stdio.h> /* SEEK_SET */
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <unistd.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
#endif
|
1998-06-21 01:09:32 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-10-05 03:26:07 +08:00
|
|
|
#ifdef G_OS_WIN32
|
1999-02-21 07:20:54 +08:00
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
1999-09-01 12:27:30 +08:00
|
|
|
#ifndef _O_BINARY
|
|
|
|
#define _O_BINARY 0
|
|
|
|
#endif
|
|
|
|
#ifndef _O_TEMPORARY
|
|
|
|
#define _O_TEMPORARY 0
|
|
|
|
#endif
|
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
#include "apptypes.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-12-14 03:48:24 +08:00
|
|
|
#include "tile.h"
|
2001-01-23 22:21:37 +08:00
|
|
|
#include "tile_pvt.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
#include "tile_swap.h"
|
2001-01-23 22:21:37 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
#define MAX_OPEN_SWAP_FILES 16
|
|
|
|
|
1998-07-13 07:01:33 +08:00
|
|
|
typedef struct _SwapFile SwapFile;
|
|
|
|
typedef struct _DefSwapFile DefSwapFile;
|
|
|
|
typedef struct _Gap Gap;
|
|
|
|
typedef struct _AsyncSwapArgs AsyncSwapArgs;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
struct _SwapFile
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
gchar *filename;
|
|
|
|
gint swap_num;
|
|
|
|
SwapFunc swap_func;
|
|
|
|
gpointer user_data;
|
|
|
|
gint fd;
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _DefSwapFile
|
|
|
|
{
|
|
|
|
GList *gaps;
|
2001-01-23 22:21:37 +08:00
|
|
|
glong swap_file_end;
|
|
|
|
off_t cur_position;
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _Gap
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
glong start;
|
|
|
|
glong end;
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
1998-08-28 19:24:23 +08:00
|
|
|
struct _AsyncSwapArgs
|
1998-07-13 07:01:33 +08:00
|
|
|
{
|
|
|
|
DefSwapFile *def_swap_file;
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd;
|
1998-07-13 07:01:33 +08:00
|
|
|
Tile *tile;
|
|
|
|
};
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
static void tile_swap_init (void);
|
|
|
|
static guint tile_swap_hash (gint *key);
|
|
|
|
static gint tile_swap_compare (gint *a,
|
|
|
|
gint *b);
|
|
|
|
static void tile_swap_command (Tile *tile,
|
|
|
|
gint command);
|
|
|
|
static void tile_swap_open (SwapFile *swap_file);
|
|
|
|
|
|
|
|
static gint tile_swap_default (gint fd,
|
|
|
|
Tile *tile,
|
|
|
|
gint cmd,
|
|
|
|
gpointer user_data);
|
|
|
|
static void tile_swap_default_in (DefSwapFile *def_swap_file,
|
|
|
|
gint fd,
|
|
|
|
Tile *tile);
|
|
|
|
static void tile_swap_default_in_async (DefSwapFile *def_swap_file,
|
|
|
|
gint fd,
|
|
|
|
Tile *tile);
|
|
|
|
static void tile_swap_default_out (DefSwapFile *def_swap_file,
|
|
|
|
gint fd,
|
|
|
|
Tile *tile);
|
|
|
|
static void tile_swap_default_delete (DefSwapFile *def_swap_file,
|
|
|
|
gint fd,
|
|
|
|
Tile *tile);
|
|
|
|
static glong tile_swap_find_offset (DefSwapFile *def_swap_file,
|
|
|
|
gint fd,
|
|
|
|
gint bytes);
|
|
|
|
static void tile_swap_resize (DefSwapFile *def_swap_file,
|
|
|
|
gint fd,
|
|
|
|
glong new_size);
|
|
|
|
static Gap * tile_swap_gap_new (glong start,
|
|
|
|
glong end);
|
|
|
|
static void tile_swap_gap_destroy (Gap *gap);
|
1998-07-13 07:01:33 +08:00
|
|
|
#ifdef USE_PTHREADS
|
2001-01-23 22:21:37 +08:00
|
|
|
static gpointer tile_swap_in_thread (gpointer);
|
1998-07-13 07:01:33 +08:00
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
static gboolean initialize = TRUE;
|
|
|
|
static GHashTable * swap_files = NULL;
|
|
|
|
static GList * open_swap_files = NULL;
|
|
|
|
static gint nopen_swap_files = 0;
|
|
|
|
static gint next_swap_num = 1;
|
|
|
|
static glong swap_file_grow = 16 * TILE_WIDTH * TILE_HEIGHT * 4;
|
1998-06-21 01:09:32 +08:00
|
|
|
#ifdef USE_PTHREADS
|
2001-01-23 22:21:37 +08:00
|
|
|
static pthread_mutex_t swapfile_mutex = PTHREAD_MUTEX_INITIALIZER;
|
1998-07-13 07:01:33 +08:00
|
|
|
|
|
|
|
/* async_swapin_mutex protects only the list, not the tiles therein */
|
2001-01-23 22:21:37 +08:00
|
|
|
static pthread_t swapin_thread;
|
|
|
|
static pthread_mutex_t async_swapin_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static pthread_cond_t async_swapin_signal = PTHREAD_COND_INITIALIZER;
|
|
|
|
static GSList * async_swapin_tiles = NULL;
|
|
|
|
static GSList * async_swapin_tiles_end = NULL;
|
1998-06-21 01:09:32 +08:00
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-07-05 08:03:06 +08:00
|
|
|
static gboolean seek_err_msg = TRUE, read_err_msg = TRUE, write_err_msg = TRUE;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
static void
|
|
|
|
tile_swap_print_gaps (DefSwapFile *def_swap_file)
|
|
|
|
{
|
|
|
|
GList *gaps;
|
2001-01-23 22:21:37 +08:00
|
|
|
Gap *gap;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
gaps = def_swap_file->gaps;
|
|
|
|
while (gaps)
|
|
|
|
{
|
|
|
|
gap = gaps->data;
|
|
|
|
gaps = gaps->next;
|
|
|
|
|
|
|
|
g_print (" %6ld - %6ld\n", gap->start, gap->end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_exit1 (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
extern gint tile_ref_count;
|
|
|
|
SwapFile *swap_file;
|
1997-11-25 06:05:25 +08:00
|
|
|
DefSwapFile *def_swap_file;
|
|
|
|
|
|
|
|
if (tile_ref_count != 0)
|
1999-06-06 07:41:45 +08:00
|
|
|
g_warning ("tile ref count balance: %d\n", tile_ref_count);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
swap_file = value;
|
|
|
|
if (swap_file->swap_func == tile_swap_default)
|
|
|
|
{
|
|
|
|
def_swap_file = swap_file->user_data;
|
|
|
|
if (def_swap_file->swap_file_end != 0)
|
|
|
|
{
|
1999-06-06 07:41:45 +08:00
|
|
|
g_warning ("swap file not empty: \"%s\"\n", swap_file->filename);
|
1997-11-25 06:05:25 +08:00
|
|
|
tile_swap_print_gaps (def_swap_file);
|
|
|
|
}
|
|
|
|
|
1999-10-26 03:20:41 +08:00
|
|
|
#if defined (__EMX__) || defined (G_OS_WIN32)
|
1999-04-28 02:59:53 +08:00
|
|
|
/* should close before unlink */
|
|
|
|
if (swap_file->fd > 0)
|
|
|
|
{
|
|
|
|
close (swap_file->fd);
|
|
|
|
swap_file->fd = -1;
|
|
|
|
}
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
unlink (swap_file->filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_exit (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1999-12-14 03:48:24 +08:00
|
|
|
#ifdef HINTS_SANITY
|
|
|
|
extern int tile_exist_peak;
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
g_printerr ("Tile exist peak was %d Tile structs (%d bytes)",
|
|
|
|
tile_exist_peak, tile_exist_peak * sizeof(Tile));
|
1999-12-14 03:48:24 +08:00
|
|
|
#endif
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (swap_files)
|
|
|
|
g_hash_table_foreach (swap_files, tile_swap_exit1, NULL);
|
|
|
|
}
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
gint
|
|
|
|
tile_swap_add (gchar *filename,
|
1997-11-25 06:05:25 +08:00
|
|
|
SwapFunc swap_func,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
SwapFile *swap_file;
|
1997-11-25 06:05:25 +08:00
|
|
|
DefSwapFile *def_swap_file;
|
|
|
|
|
1998-06-29 08:33:16 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_mutex_lock(&swapfile_mutex);
|
|
|
|
#endif
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (initialize)
|
|
|
|
tile_swap_init ();
|
|
|
|
|
|
|
|
swap_file = g_new (SwapFile, 1);
|
|
|
|
swap_file->filename = g_strdup (filename);
|
|
|
|
swap_file->swap_num = next_swap_num++;
|
|
|
|
|
|
|
|
if (!swap_func)
|
|
|
|
{
|
|
|
|
swap_func = tile_swap_default;
|
|
|
|
|
|
|
|
def_swap_file = g_new (DefSwapFile, 1);
|
|
|
|
def_swap_file->gaps = NULL;
|
|
|
|
def_swap_file->swap_file_end = 0;
|
|
|
|
def_swap_file->cur_position = 0;
|
|
|
|
|
|
|
|
user_data = def_swap_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
swap_file->swap_func = swap_func;
|
|
|
|
swap_file->user_data = user_data;
|
|
|
|
swap_file->fd = -1;
|
|
|
|
|
|
|
|
g_hash_table_insert (swap_files, &swap_file->swap_num, swap_file);
|
|
|
|
|
1998-06-29 08:33:16 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_mutex_unlock(&swapfile_mutex);
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
return swap_file->swap_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_remove (gint swap_num)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
SwapFile *swap_file;
|
|
|
|
|
1998-06-29 08:33:16 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_mutex_lock(&swapfile_mutex);
|
|
|
|
#endif
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (initialize)
|
|
|
|
tile_swap_init ();
|
|
|
|
|
|
|
|
swap_file = g_hash_table_lookup (swap_files, &swap_num);
|
|
|
|
if (!swap_file)
|
1998-06-29 08:33:16 +08:00
|
|
|
goto out;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
g_hash_table_remove (swap_files, &swap_num);
|
|
|
|
|
|
|
|
if (swap_file->fd != -1)
|
|
|
|
close (swap_file->fd);
|
|
|
|
|
|
|
|
g_free (swap_file);
|
1998-06-29 08:33:16 +08:00
|
|
|
out:
|
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_mutex_unlock(&swapfile_mutex);
|
|
|
|
#endif
|
1998-10-25 13:55:36 +08:00
|
|
|
return;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
1998-07-13 07:01:33 +08:00
|
|
|
void
|
|
|
|
tile_swap_in_async (Tile *tile)
|
|
|
|
{
|
|
|
|
if (tile->swap_offset == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tile_swap_command (tile, SWAP_IN_ASYNC);
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
void
|
|
|
|
tile_swap_in (Tile *tile)
|
|
|
|
{
|
|
|
|
if (tile->swap_offset == -1)
|
|
|
|
{
|
|
|
|
tile_alloc (tile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tile_swap_command (tile, SWAP_IN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tile_swap_out (Tile *tile)
|
|
|
|
{
|
|
|
|
tile_swap_command (tile, SWAP_OUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tile_swap_delete (Tile *tile)
|
|
|
|
{
|
|
|
|
tile_swap_command (tile, SWAP_DELETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_compress (gint swap_num)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
tile_swap_command (NULL, SWAP_COMPRESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_init (void)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1998-06-29 08:33:16 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (initialize)
|
|
|
|
{
|
|
|
|
initialize = FALSE;
|
|
|
|
|
|
|
|
swap_files = g_hash_table_new ((GHashFunc) tile_swap_hash,
|
|
|
|
(GCompareFunc) tile_swap_compare);
|
1998-07-13 07:01:33 +08:00
|
|
|
|
1998-08-28 20:03:39 +08:00
|
|
|
#ifdef NOTDEF /* USE_PTHREADS */
|
1998-07-13 07:01:33 +08:00
|
|
|
pthread_create (&swapin_thread, NULL, &tile_swap_in_thread, NULL);
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_hash (gint *key)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
return (guint) *key;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_compare (gint *a,
|
|
|
|
gint *b)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
return (*a == *b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_command (Tile *tile,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint command)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
SwapFile *swap_file;
|
1998-06-21 01:09:32 +08:00
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_mutex_lock(&swapfile_mutex);
|
|
|
|
#endif
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (initialize)
|
|
|
|
tile_swap_init ();
|
|
|
|
|
|
|
|
do {
|
|
|
|
swap_file = g_hash_table_lookup (swap_files, &tile->swap_num);
|
|
|
|
if (!swap_file)
|
|
|
|
{
|
1999-06-06 07:41:45 +08:00
|
|
|
g_warning ("could not find swap file for tile");
|
1998-06-21 01:09:32 +08:00
|
|
|
goto out;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (swap_file->fd == -1)
|
|
|
|
{
|
|
|
|
tile_swap_open (swap_file);
|
|
|
|
|
|
|
|
if (swap_file->fd == -1)
|
1998-06-21 01:09:32 +08:00
|
|
|
goto out;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2001-01-23 22:21:37 +08:00
|
|
|
}
|
|
|
|
while ((* swap_file->swap_func) (swap_file->fd,
|
|
|
|
tile, command, swap_file->user_data));
|
|
|
|
|
1998-06-21 01:09:32 +08:00
|
|
|
out:
|
|
|
|
#ifdef USE_PTHREADS
|
|
|
|
pthread_mutex_unlock(&swapfile_mutex);
|
|
|
|
#endif
|
2001-01-23 22:21:37 +08:00
|
|
|
|
1998-10-25 13:55:36 +08:00
|
|
|
return;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_open (SwapFile *swap_file)
|
|
|
|
{
|
|
|
|
SwapFile *tmp;
|
|
|
|
|
|
|
|
if (swap_file->fd != -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (nopen_swap_files == MAX_OPEN_SWAP_FILES)
|
|
|
|
{
|
|
|
|
tmp = open_swap_files->data;
|
|
|
|
close (tmp->fd);
|
|
|
|
tmp->fd = -1;
|
|
|
|
|
|
|
|
open_swap_files = g_list_remove (open_swap_files, open_swap_files->data);
|
|
|
|
nopen_swap_files -= 1;
|
|
|
|
}
|
|
|
|
|
1999-09-01 14:15:59 +08:00
|
|
|
swap_file->fd = open (swap_file->filename,
|
|
|
|
O_CREAT | O_RDWR | _O_BINARY | _O_TEMPORARY,
|
|
|
|
S_IREAD | S_IWRITE);
|
1999-09-01 12:27:30 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (swap_file->fd == -1)
|
|
|
|
{
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("unable to open swap file...BAD THINGS WILL HAPPEN SOON");
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
open_swap_files = g_list_append (open_swap_files, swap_file);
|
|
|
|
nopen_swap_files += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The actual swap file code. The swap file consists of tiles
|
|
|
|
* which have been moved out to disk in order to conserve memory.
|
|
|
|
* The swap file format is free form. Any tile in memory may
|
|
|
|
* end up anywhere on disk.
|
|
|
|
* An actual tile in the swap file consists only of the tile data.
|
|
|
|
* The offset of the tile on disk is stored in the tile data structure
|
|
|
|
* in memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_default (gint fd,
|
1997-11-25 06:05:25 +08:00
|
|
|
Tile *tile,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint cmd,
|
1997-11-25 06:05:25 +08:00
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
DefSwapFile *def_swap_file;
|
|
|
|
|
|
|
|
def_swap_file = (DefSwapFile*) user_data;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SWAP_IN:
|
|
|
|
tile_swap_default_in (def_swap_file, fd, tile);
|
|
|
|
break;
|
1998-07-13 07:01:33 +08:00
|
|
|
case SWAP_IN_ASYNC:
|
|
|
|
tile_swap_default_in_async (def_swap_file, fd, tile);
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
case SWAP_OUT:
|
|
|
|
tile_swap_default_out (def_swap_file, fd, tile);
|
|
|
|
break;
|
|
|
|
case SWAP_DELETE:
|
|
|
|
tile_swap_default_delete (def_swap_file, fd, tile);
|
|
|
|
break;
|
|
|
|
case SWAP_COMPRESS:
|
1999-06-06 07:41:45 +08:00
|
|
|
g_warning ("tile_swap_default: SWAP_COMPRESS: UNFINISHED");
|
1997-11-25 06:05:25 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1998-07-13 07:01:33 +08:00
|
|
|
static void
|
|
|
|
tile_swap_default_in_async (DefSwapFile *def_swap_file,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd,
|
1998-07-13 07:01:33 +08:00
|
|
|
Tile *tile)
|
|
|
|
{
|
1998-08-28 20:03:39 +08:00
|
|
|
#ifdef NOTDEF /* USE_PTHREADS */
|
1998-07-13 07:01:33 +08:00
|
|
|
AsyncSwapArgs *args;
|
|
|
|
|
|
|
|
args = g_new(AsyncSwapArgs, 1);
|
|
|
|
args->def_swap_file = def_swap_file;
|
|
|
|
args->fd = fd;
|
|
|
|
args->tile = tile;
|
|
|
|
|
|
|
|
/* add this tile to the list of tiles for the async swapin task */
|
|
|
|
pthread_mutex_lock (&async_swapin_mutex);
|
1998-08-28 20:03:39 +08:00
|
|
|
g_slist_append (async_swapin_tiles_end, args);
|
1998-07-13 07:01:33 +08:00
|
|
|
|
1998-08-28 20:03:39 +08:00
|
|
|
if (!async_swapin_tiles)
|
|
|
|
async_swapin_tiles = async_swapin_tiles_end;
|
1998-07-13 07:01:33 +08:00
|
|
|
|
|
|
|
pthread_cond_signal (&async_swapin_signal);
|
|
|
|
pthread_mutex_unlock (&async_swapin_mutex);
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* ignore; it's only a hint anyway */
|
|
|
|
/* this could be changed to call out to another program that
|
|
|
|
* tries to make the OS read the data in from disk.
|
|
|
|
*/
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NOTE: if you change this function, check to see if your changes
|
|
|
|
* apply to tile_swap_in_attempt() near the end of the file. The
|
|
|
|
* difference is that this version makes guarantees about what it
|
|
|
|
* provides, but tile_swap_in_attempt() just tries and gives up if
|
|
|
|
* anything goes wrong.
|
|
|
|
*
|
|
|
|
* I'm not sure that it is worthwhile to try to pull out common
|
|
|
|
* bits; I think the two functions are (at least for now) different
|
|
|
|
* enough to keep as two functions.
|
1998-07-22 07:34:28 +08:00
|
|
|
*
|
|
|
|
* N.B. the mutex on the tile must already have been locked on entry
|
|
|
|
* to this function. DO NOT LOCK IT HERE.
|
1998-07-13 07:01:33 +08:00
|
|
|
*/
|
1997-11-25 06:05:25 +08:00
|
|
|
static void
|
|
|
|
tile_swap_default_in (DefSwapFile *def_swap_file,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd,
|
1997-11-25 06:05:25 +08:00
|
|
|
Tile *tile)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
gint bytes;
|
|
|
|
gint err;
|
|
|
|
gint nleft;
|
1997-11-25 06:05:25 +08:00
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
err = -1;
|
|
|
|
|
1998-07-13 07:01:33 +08:00
|
|
|
if (tile->data)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (def_swap_file->cur_position != tile->swap_offset)
|
|
|
|
{
|
|
|
|
def_swap_file->cur_position = tile->swap_offset;
|
|
|
|
|
|
|
|
offset = lseek (fd, tile->swap_offset, SEEK_SET);
|
|
|
|
if (offset == -1)
|
|
|
|
{
|
1998-07-05 08:03:06 +08:00
|
|
|
if (seek_err_msg)
|
1999-12-28 18:30:50 +08:00
|
|
|
g_message ("unable to seek to tile location on disk: %d", err);
|
1998-07-05 08:03:06 +08:00
|
|
|
seek_err_msg = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = tile_size (tile);
|
|
|
|
tile_alloc (tile);
|
|
|
|
|
|
|
|
nleft = bytes;
|
|
|
|
while (nleft > 0)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
err = read (fd, tile->data + bytes - nleft, nleft);
|
|
|
|
}
|
|
|
|
while ((err == -1) && ((errno == EAGAIN) || (errno == EINTR)));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (err <= 0)
|
|
|
|
{
|
1998-07-05 08:03:06 +08:00
|
|
|
if (read_err_msg)
|
2001-01-23 22:21:37 +08:00
|
|
|
g_message ("unable to read tile data from disk: %s (%d/%d bytes read)",
|
|
|
|
g_strerror (errno), err, nleft);
|
1998-07-05 08:03:06 +08:00
|
|
|
read_err_msg = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nleft -= err;
|
|
|
|
}
|
|
|
|
|
|
|
|
def_swap_file->cur_position += bytes;
|
|
|
|
|
|
|
|
/* Do not delete the swap from the file */
|
|
|
|
/* tile_swap_default_delete (def_swap_file, fd, tile); */
|
1998-07-05 08:03:06 +08:00
|
|
|
|
|
|
|
read_err_msg = seek_err_msg = TRUE;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_default_out (DefSwapFile *def_swap_file,
|
|
|
|
int fd,
|
|
|
|
Tile *tile)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
gint bytes;
|
|
|
|
gint rbytes;
|
|
|
|
gint err;
|
|
|
|
gint nleft;
|
1997-11-25 06:05:25 +08:00
|
|
|
off_t offset;
|
1999-10-04 23:31:14 +08:00
|
|
|
off_t newpos;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
bytes = TILE_WIDTH * TILE_HEIGHT * tile->bpp;
|
|
|
|
rbytes = tile_size (tile);
|
|
|
|
|
|
|
|
/* If there is already a valid swap_offset, use it */
|
|
|
|
if (tile->swap_offset == -1)
|
1999-10-04 23:31:14 +08:00
|
|
|
newpos = tile_swap_find_offset (def_swap_file, fd, bytes);
|
|
|
|
else
|
|
|
|
newpos = tile->swap_offset;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-10-04 23:31:14 +08:00
|
|
|
if (def_swap_file->cur_position != newpos)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1999-10-04 23:31:14 +08:00
|
|
|
offset = lseek (fd, newpos, SEEK_SET);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (offset == -1)
|
|
|
|
{
|
1998-07-05 08:03:06 +08:00
|
|
|
if (seek_err_msg)
|
2001-01-23 22:21:37 +08:00
|
|
|
g_message ("unable to seek to tile location on disk: %s",
|
|
|
|
g_strerror (errno));
|
1998-07-05 08:03:06 +08:00
|
|
|
seek_err_msg = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
|
|
|
}
|
1999-10-04 23:31:14 +08:00
|
|
|
def_swap_file->cur_position = newpos;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nleft = rbytes;
|
|
|
|
while (nleft > 0)
|
|
|
|
{
|
1998-01-05 17:30:23 +08:00
|
|
|
err = write (fd, tile->data + rbytes - nleft, nleft);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (err <= 0)
|
|
|
|
{
|
1998-07-05 08:03:06 +08:00
|
|
|
if (write_err_msg)
|
2001-01-23 22:21:37 +08:00
|
|
|
g_message ("unable to write tile data to disk: %s (%d/%d bytes written)",
|
|
|
|
g_strerror (errno), err, nleft);
|
1998-07-05 08:03:06 +08:00
|
|
|
write_err_msg = FALSE;
|
1997-11-25 06:05:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nleft -= err;
|
|
|
|
}
|
|
|
|
|
|
|
|
def_swap_file->cur_position += rbytes;
|
|
|
|
|
1998-07-13 07:01:33 +08:00
|
|
|
/* Do NOT free tile->data because we may be pre-swapping.
|
|
|
|
* tile->data is freed in tile_cache_zorch_next
|
|
|
|
*/
|
1998-06-21 01:09:32 +08:00
|
|
|
tile->dirty = FALSE;
|
1999-10-04 23:31:14 +08:00
|
|
|
tile->swap_offset = newpos;
|
1998-07-05 08:03:06 +08:00
|
|
|
|
|
|
|
write_err_msg = seek_err_msg = TRUE;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_default_delete (DefSwapFile *def_swap_file,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd,
|
1997-11-25 06:05:25 +08:00
|
|
|
Tile *tile)
|
|
|
|
{
|
|
|
|
GList *tmp;
|
|
|
|
GList *tmp2;
|
2001-01-23 22:21:37 +08:00
|
|
|
Gap *gap;
|
|
|
|
Gap *gap2;
|
|
|
|
glong start;
|
|
|
|
glong end;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (tile->swap_offset == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
start = tile->swap_offset;
|
|
|
|
end = start + TILE_WIDTH * TILE_HEIGHT * tile->bpp;
|
|
|
|
tile->swap_offset = -1;
|
|
|
|
|
|
|
|
tmp = def_swap_file->gaps;
|
|
|
|
while (tmp)
|
|
|
|
{
|
|
|
|
gap = tmp->data;
|
|
|
|
|
|
|
|
if (end == gap->start)
|
|
|
|
{
|
|
|
|
gap->start = start;
|
|
|
|
|
|
|
|
if (tmp->prev)
|
|
|
|
{
|
|
|
|
gap2 = tmp->prev->data;
|
|
|
|
if (gap->start == gap2->end)
|
|
|
|
{
|
|
|
|
gap2->end = gap->end;
|
|
|
|
tile_swap_gap_destroy (gap);
|
2001-01-23 22:21:37 +08:00
|
|
|
def_swap_file->gaps =
|
|
|
|
g_list_remove_link (def_swap_file->gaps, tmp);
|
1997-11-25 06:05:25 +08:00
|
|
|
g_list_free (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (start == gap->end)
|
|
|
|
{
|
|
|
|
gap->end = end;
|
|
|
|
|
|
|
|
if (tmp->next)
|
|
|
|
{
|
|
|
|
gap2 = tmp->next->data;
|
|
|
|
if (gap->end == gap2->start)
|
|
|
|
{
|
|
|
|
gap2->start = gap->start;
|
|
|
|
tile_swap_gap_destroy (gap);
|
2001-01-23 22:21:37 +08:00
|
|
|
def_swap_file->gaps =
|
|
|
|
g_list_remove_link (def_swap_file->gaps, tmp);
|
1997-11-25 06:05:25 +08:00
|
|
|
g_list_free (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (end < gap->start)
|
|
|
|
{
|
|
|
|
gap = tile_swap_gap_new (start, end);
|
|
|
|
|
|
|
|
tmp2 = g_list_alloc ();
|
|
|
|
tmp2->data = gap;
|
|
|
|
tmp2->next = tmp;
|
|
|
|
tmp2->prev = tmp->prev;
|
|
|
|
if (tmp->prev)
|
|
|
|
tmp->prev->next = tmp2;
|
|
|
|
tmp->prev = tmp2;
|
|
|
|
|
|
|
|
if (tmp == def_swap_file->gaps)
|
|
|
|
def_swap_file->gaps = tmp2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (!tmp->next)
|
|
|
|
{
|
|
|
|
gap = tile_swap_gap_new (start, end);
|
|
|
|
tmp->next = g_list_alloc ();
|
|
|
|
tmp->next->data = gap;
|
|
|
|
tmp->next->prev = tmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!def_swap_file->gaps)
|
|
|
|
{
|
|
|
|
gap = tile_swap_gap_new (start, end);
|
|
|
|
def_swap_file->gaps = g_list_append (def_swap_file->gaps, gap);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = g_list_last (def_swap_file->gaps);
|
|
|
|
gap = tmp->data;
|
|
|
|
|
|
|
|
if (gap->end == def_swap_file->swap_file_end)
|
|
|
|
{
|
|
|
|
tile_swap_resize (def_swap_file, fd, gap->start);
|
|
|
|
tile_swap_gap_destroy (gap);
|
|
|
|
def_swap_file->gaps = g_list_remove_link (def_swap_file->gaps, tmp);
|
|
|
|
g_list_free (tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_resize (DefSwapFile *def_swap_file,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd,
|
|
|
|
glong new_size)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
if (def_swap_file->swap_file_end > new_size)
|
2000-01-07 01:14:32 +08:00
|
|
|
{
|
|
|
|
ftruncate (fd, new_size);
|
|
|
|
}
|
2001-01-23 22:21:37 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
def_swap_file->swap_file_end = new_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long
|
|
|
|
tile_swap_find_offset (DefSwapFile *def_swap_file,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd,
|
|
|
|
gint bytes)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
GList *tmp;
|
2001-01-23 22:21:37 +08:00
|
|
|
Gap *gap;
|
|
|
|
glong offset;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
tmp = def_swap_file->gaps;
|
|
|
|
while (tmp)
|
|
|
|
{
|
|
|
|
gap = tmp->data;
|
|
|
|
|
|
|
|
if ((gap->end - gap->start) >= bytes)
|
|
|
|
{
|
|
|
|
offset = gap->start;
|
|
|
|
gap->start += bytes;
|
|
|
|
|
|
|
|
if (gap->start == gap->end)
|
|
|
|
{
|
|
|
|
tile_swap_gap_destroy (gap);
|
2001-01-23 22:21:37 +08:00
|
|
|
def_swap_file->gaps =
|
|
|
|
g_list_remove_link (def_swap_file->gaps, tmp);
|
1997-11-25 06:05:25 +08:00
|
|
|
g_list_free (tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = def_swap_file->swap_file_end;
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
tile_swap_resize (def_swap_file, fd,
|
|
|
|
def_swap_file->swap_file_end + swap_file_grow);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if ((offset + bytes) < (def_swap_file->swap_file_end))
|
|
|
|
{
|
|
|
|
gap = tile_swap_gap_new (offset + bytes, def_swap_file->swap_file_end);
|
|
|
|
def_swap_file->gaps = g_list_append (def_swap_file->gaps, gap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
static Gap *
|
|
|
|
tile_swap_gap_new (glong start,
|
|
|
|
glong end)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
Gap *gap;
|
|
|
|
|
|
|
|
gap = g_new (Gap, 1);
|
|
|
|
gap->start = start;
|
|
|
|
gap->end = end;
|
|
|
|
|
|
|
|
return gap;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_gap_destroy (Gap *gap)
|
|
|
|
{
|
|
|
|
g_free (gap);
|
|
|
|
}
|
1998-07-13 07:01:33 +08:00
|
|
|
|
|
|
|
|
1998-08-28 20:03:39 +08:00
|
|
|
#ifdef NOTDEF /* USE_PTHREADS */
|
1998-07-13 07:01:33 +08:00
|
|
|
/* go through the list of tiles that are likely to be used soon and
|
|
|
|
* try to swap them in. If any tile is not in a state to be swapped
|
|
|
|
* in, ignore it, and the error will get dealt with when the tile
|
1998-08-28 20:03:39 +08:00
|
|
|
* is really needed -- assuming that the error still happens.
|
1998-07-13 07:01:33 +08:00
|
|
|
*
|
|
|
|
* Potential future enhancement: for non-threaded systems, we could
|
|
|
|
* fork() a process which merely attempts to bring tiles into the
|
|
|
|
* OS's buffer/page cache, where they will be read into the gimp
|
|
|
|
* more quickly. This would be pretty trivial, actually.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
tile_swap_in_attempt (DefSwapFile *def_swap_file,
|
2001-01-23 22:21:37 +08:00
|
|
|
gint fd,
|
1998-07-13 07:01:33 +08:00
|
|
|
Tile *tile)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
gint bytes;
|
|
|
|
gint err;
|
|
|
|
gint nleft;
|
1998-07-13 07:01:33 +08:00
|
|
|
off_t offset;
|
|
|
|
|
|
|
|
err = -1;
|
|
|
|
|
|
|
|
TILE_MUTEX_LOCK (tile);
|
|
|
|
if (tile->data)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (!tile->swap_num || !tile->swap_offset)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (def_swap_file->cur_position != tile->swap_offset)
|
|
|
|
{
|
|
|
|
def_swap_file->cur_position = tile->swap_offset;
|
|
|
|
|
|
|
|
offset = lseek (fd, tile->swap_offset, SEEK_SET);
|
|
|
|
if (offset == -1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = tile_size (tile);
|
|
|
|
tile_alloc (tile);
|
|
|
|
|
|
|
|
nleft = bytes;
|
|
|
|
while (nleft > 0)
|
|
|
|
{
|
2001-01-23 22:21:37 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
err = read (fd, tile->data + bytes - nleft, nleft);
|
|
|
|
}
|
|
|
|
while ((err == -1) && ((errno == EAGAIN) || (errno == EINTR)));
|
1998-07-13 07:01:33 +08:00
|
|
|
|
|
|
|
if (err <= 0)
|
|
|
|
{
|
|
|
|
g_free (tile->data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nleft -= err;
|
|
|
|
}
|
|
|
|
|
|
|
|
def_swap_file->cur_position += bytes;
|
|
|
|
|
|
|
|
out:
|
|
|
|
TILE_MUTEX_UNLOCK (tile);
|
|
|
|
}
|
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
static gpointer
|
|
|
|
tile_swap_in_thread (gpointer data)
|
1998-07-13 07:01:33 +08:00
|
|
|
{
|
|
|
|
AsyncSwapArgs *args;
|
2001-01-23 22:21:37 +08:00
|
|
|
GSList *free_item;
|
1998-07-13 07:01:33 +08:00
|
|
|
|
2001-01-23 22:21:37 +08:00
|
|
|
while (TRUE)
|
1998-07-13 07:01:33 +08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock (&async_swapin_mutex);
|
|
|
|
|
|
|
|
if (!async_swapin_tiles)
|
|
|
|
{
|
|
|
|
pthread_cond_wait (&async_swapin_signal, &async_swapin_mutex);
|
|
|
|
}
|
|
|
|
args = async_swapin_tiles->data;
|
|
|
|
|
|
|
|
free_item = async_swapin_tiles;
|
|
|
|
async_swapin_tiles = async_swapin_tiles->next;
|
|
|
|
g_slist_free_1(free_item);
|
2001-01-23 22:21:37 +08:00
|
|
|
|
1998-07-13 07:01:33 +08:00
|
|
|
if (!async_swapin_tiles)
|
|
|
|
async_swapin_tiles_end = NULL;
|
|
|
|
|
|
|
|
pthread_mutex_unlock (&async_swapin_mutex);
|
|
|
|
|
|
|
|
tile_swap_in_attempt(args->def_swap_file, args->fd, args->tile);
|
|
|
|
|
|
|
|
g_free(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|