Commit 980ba7f8 authored by Michael Natterer's avatar Michael Natterer 😴

app: move memsize functions into their own files gimp-memsize.[ch]

parent cafc316a
......@@ -37,6 +37,8 @@ libappcore_a_sources = \
gimp-gradients.h \
gimp-gui.c \
gimp-gui.h \
gimp-memsize.c \
gimp-memsize.h \
gimp-modules.c \
gimp-modules.h \
gimp-parasites.c \
......
/* GIMP - The GNU 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string.h>
#include <cairo.h>
#include <gegl.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpcolor/gimpcolor.h"
#include "core-types.h"
#include "gimp-memsize.h"
#include "gimpparamspecs.h"
gint64
gimp_g_type_instance_get_memsize (GTypeInstance *instance)
{
if (instance)
{
GTypeQuery type_query;
g_type_query (G_TYPE_FROM_INSTANCE (instance), &type_query);
return type_query.instance_size;
}
return 0;
}
gint64
gimp_g_object_get_memsize (GObject *object)
{
if (object)
return gimp_g_type_instance_get_memsize ((GTypeInstance *) object);
return 0;
}
gint64
gimp_g_hash_table_get_memsize (GHashTable *hash,
gint64 data_size)
{
if (hash)
return (2 * sizeof (gint) +
5 * sizeof (gpointer) +
g_hash_table_size (hash) * (3 * sizeof (gpointer) + data_size));
return 0;
}
typedef struct
{
GimpMemsizeFunc func;
gint64 memsize;
gint64 gui_size;
} HashMemsize;
static void
hash_memsize_foreach (gpointer key,
gpointer value,
HashMemsize *memsize)
{
gint64 gui_size = 0;
memsize->memsize += memsize->func (value, &gui_size);
memsize->gui_size += gui_size;
}
gint64
gimp_g_hash_table_get_memsize_foreach (GHashTable *hash,
GimpMemsizeFunc func,
gint64 *gui_size)
{
HashMemsize memsize;
g_return_val_if_fail (func != NULL, 0);
if (! hash)
return 0;
memsize.func = func;
memsize.memsize = 0;
memsize.gui_size = 0;
g_hash_table_foreach (hash, (GHFunc) hash_memsize_foreach, &memsize);
if (gui_size)
*gui_size = memsize.gui_size;
return memsize.memsize + gimp_g_hash_table_get_memsize (hash, 0);
}
gint64
gimp_g_slist_get_memsize (GSList *slist,
gint64 data_size)
{
return g_slist_length (slist) * (data_size + sizeof (GSList));
}
gint64
gimp_g_slist_get_memsize_foreach (GSList *slist,
GimpMemsizeFunc func,
gint64 *gui_size)
{
GSList *l;
gint64 memsize = 0;
g_return_val_if_fail (func != NULL, 0);
for (l = slist; l; l = g_slist_next (l))
memsize += sizeof (GSList) + func (l->data, gui_size);
return memsize;
}
gint64
gimp_g_list_get_memsize (GList *list,
gint64 data_size)
{
return g_list_length (list) * (data_size + sizeof (GList));
}
gint64
gimp_g_list_get_memsize_foreach (GList *list,
GimpMemsizeFunc func,
gint64 *gui_size)
{
GList *l;
gint64 memsize = 0;
g_return_val_if_fail (func != NULL, 0);
for (l = list; l; l = g_list_next (l))
memsize += sizeof (GList) + func (l->data, gui_size);
return memsize;
}
gint64
gimp_g_value_get_memsize (GValue *value)
{
gint64 memsize = 0;
if (! value)
return 0;
if (G_VALUE_HOLDS_STRING (value))
{
memsize += gimp_string_get_memsize (g_value_get_string (value));
}
else if (G_VALUE_HOLDS_BOXED (value))
{
if (GIMP_VALUE_HOLDS_RGB (value))
{
memsize += sizeof (GimpRGB);
}
else if (GIMP_VALUE_HOLDS_MATRIX2 (value))
{
memsize += sizeof (GimpMatrix2);
}
else if (GIMP_VALUE_HOLDS_PARASITE (value))
{
memsize += gimp_parasite_get_memsize (g_value_get_boxed (value),
NULL);
}
else if (GIMP_VALUE_HOLDS_ARRAY (value) ||
GIMP_VALUE_HOLDS_INT8_ARRAY (value) ||
GIMP_VALUE_HOLDS_INT16_ARRAY (value) ||
GIMP_VALUE_HOLDS_INT32_ARRAY (value) ||
GIMP_VALUE_HOLDS_FLOAT_ARRAY (value))
{
GimpArray *array = g_value_get_boxed (value);
if (array)
memsize += (sizeof (GimpArray) +
array->static_data ? 0 : array->length);
}
else if (GIMP_VALUE_HOLDS_STRING_ARRAY (value))
{
GimpArray *array = g_value_get_boxed (value);
if (array)
{
memsize += sizeof (GimpArray);
if (! array->static_data)
{
gchar **tmp = (gchar **) array->data;
gint i;
memsize += array->length * sizeof (gchar *);
for (i = 0; i < array->length; i++)
memsize += gimp_string_get_memsize (tmp[i]);
}
}
}
else
{
g_printerr ("%s: unhandled boxed value type: %s\n",
G_STRFUNC, G_VALUE_TYPE_NAME (value));
}
}
else if (G_VALUE_HOLDS_OBJECT (value))
{
g_printerr ("%s: unhandled object value type: %s\n",
G_STRFUNC, G_VALUE_TYPE_NAME (value));
}
return memsize + sizeof (GValue);
}
gint64
gimp_g_param_spec_get_memsize (GParamSpec *pspec)
{
gint64 memsize = 0;
if (! pspec)
return 0;
if (! (pspec->flags & G_PARAM_STATIC_NAME))
memsize += gimp_string_get_memsize (g_param_spec_get_name (pspec));
if (! (pspec->flags & G_PARAM_STATIC_NICK))
memsize += gimp_string_get_memsize (g_param_spec_get_nick (pspec));
if (! (pspec->flags & G_PARAM_STATIC_BLURB))
memsize += gimp_string_get_memsize (g_param_spec_get_blurb (pspec));
return memsize + gimp_g_type_instance_get_memsize ((GTypeInstance *) pspec);
}
gint64
gimp_gegl_buffer_get_memsize (GeglBuffer *buffer)
{
if (buffer)
{
const Babl *format = gegl_buffer_get_format (buffer);
return (babl_format_get_bytes_per_pixel (format) *
gegl_buffer_get_width (buffer) *
gegl_buffer_get_height (buffer) +
gimp_g_object_get_memsize (G_OBJECT (buffer)));
}
return 0;
}
gint64
gimp_gegl_pyramid_get_memsize (GeglBuffer *buffer)
{
if (buffer)
{
const Babl *format = gegl_buffer_get_format (buffer);
/* The pyramid levels constitute a geometric sum with a ratio of 1/4. */
return ((gint64) babl_format_get_bytes_per_pixel (format) *
(gint64) gegl_buffer_get_width (buffer) *
(gint64) gegl_buffer_get_height (buffer) * 1.33 +
gimp_g_object_get_memsize (G_OBJECT (buffer)));
}
return 0;
}
gint64
gimp_string_get_memsize (const gchar *string)
{
if (string)
return strlen (string) + 1;
return 0;
}
gint64
gimp_parasite_get_memsize (GimpParasite *parasite,
gint64 *gui_size)
{
if (parasite)
return (sizeof (GimpParasite) +
gimp_string_get_memsize (parasite->name) +
parasite->size);
return 0;
}
/* GIMP - The GNU 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef __APP_GIMP_MEMSIZE_H__
#define __APP_GIMP_MEMSIZE_H__
gint64 gimp_g_type_instance_get_memsize (GTypeInstance *instance);
gint64 gimp_g_object_get_memsize (GObject *object);
gint64 gimp_g_hash_table_get_memsize (GHashTable *hash,
gint64 data_size);
gint64 gimp_g_hash_table_get_memsize_foreach (GHashTable *hash,
GimpMemsizeFunc func,
gint64 *gui_size);
gint64 gimp_g_slist_get_memsize (GSList *slist,
gint64 data_size);
gint64 gimp_g_slist_get_memsize_foreach (GSList *slist,
GimpMemsizeFunc func,
gint64 *gui_size);
gint64 gimp_g_list_get_memsize (GList *list,
gint64 data_size);
gint64 gimp_g_list_get_memsize_foreach (GList *slist,
GimpMemsizeFunc func,
gint64 *gui_size);
gint64 gimp_g_value_get_memsize (GValue *value);
gint64 gimp_g_param_spec_get_memsize (GParamSpec *pspec);
gint64 gimp_gegl_buffer_get_memsize (GeglBuffer *buffer);
gint64 gimp_gegl_pyramid_get_memsize (GeglBuffer *buffer);
gint64 gimp_string_get_memsize (const gchar *string);
gint64 gimp_parasite_get_memsize (GimpParasite *parasite,
gint64 *gui_size);
#endif /* __APP_GIMP_MEMSIZE_H__ */
......@@ -53,16 +53,13 @@
#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"
#include "libgimpcolor/gimpcolor.h"
#include "libgimpconfig/gimpconfig.h"
#include "core-types.h"
#include "gimp.h"
#include "gimp-utils.h"
#include "gimpcontainer.h"
#include "gimpcontext.h"
#include "gimperror.h"
#include "gimpparamspecs.h"
#include "gimp-intl.h"
......@@ -70,279 +67,6 @@
#define MAX_FUNC 100
gint64
gimp_g_type_instance_get_memsize (GTypeInstance *instance)
{
if (instance)
{
GTypeQuery type_query;
g_type_query (G_TYPE_FROM_INSTANCE (instance), &type_query);
return type_query.instance_size;
}
return 0;
}
gint64
gimp_g_object_get_memsize (GObject *object)
{
if (object)
return gimp_g_type_instance_get_memsize ((GTypeInstance *) object);
return 0;
}
gint64
gimp_g_hash_table_get_memsize (GHashTable *hash,
gint64 data_size)
{
if (hash)
return (2 * sizeof (gint) +
5 * sizeof (gpointer) +
g_hash_table_size (hash) * (3 * sizeof (gpointer) + data_size));
return 0;
}
typedef struct
{
GimpMemsizeFunc func;
gint64 memsize;
gint64 gui_size;
} HashMemsize;
static void
hash_memsize_foreach (gpointer key,
gpointer value,
HashMemsize *memsize)
{
gint64 gui_size = 0;
memsize->memsize += memsize->func (value, &gui_size);
memsize->gui_size += gui_size;
}
gint64
gimp_g_hash_table_get_memsize_foreach (GHashTable *hash,
GimpMemsizeFunc func,
gint64 *gui_size)
{
HashMemsize memsize;
g_return_val_if_fail (func != NULL, 0);
if (! hash)
return 0;
memsize.func = func;
memsize.memsize = 0;
memsize.gui_size = 0;
g_hash_table_foreach (hash, (GHFunc) hash_memsize_foreach, &memsize);
if (gui_size)
*gui_size = memsize.gui_size;
return memsize.memsize + gimp_g_hash_table_get_memsize (hash, 0);
}
gint64
gimp_g_slist_get_memsize (GSList *slist,
gint64 data_size)
{
return g_slist_length (slist) * (data_size + sizeof (GSList));
}
gint64
gimp_g_slist_get_memsize_foreach (GSList *slist,
GimpMemsizeFunc func,
gint64 *gui_size)
{
GSList *l;
gint64 memsize = 0;
g_return_val_if_fail (func != NULL, 0);
for (l = slist; l; l = g_slist_next (l))
memsize += sizeof (GSList) + func (l->data, gui_size);
return memsize;
}
gint64
gimp_g_list_get_memsize (GList *list,
gint64 data_size)
{
return g_list_length (list) * (data_size + sizeof (GList));
}
gint64
gimp_g_list_get_memsize_foreach (GList *list,
GimpMemsizeFunc func,
gint64 *gui_size)
{
GList *l;
gint64 memsize = 0;
g_return_val_if_fail (func != NULL, 0);
for (l = list; l; l = g_list_next (l))
memsize += sizeof (GList) + func (l->data, gui_size);
return memsize;
}
gint64
gimp_g_value_get_memsize (GValue *value)
{
gint64 memsize = 0;
if (! value)
return 0;
if (G_VALUE_HOLDS_STRING (value))
{
memsize += gimp_string_get_memsize (g_value_get_string (value));
}
else if (G_VALUE_HOLDS_BOXED (value))
{
if (GIMP_VALUE_HOLDS_RGB (value))
{
memsize += sizeof (GimpRGB);
}
else if (GIMP_VALUE_HOLDS_MATRIX2 (value))
{
memsize += sizeof (GimpMatrix2);
}
else if (GIMP_VALUE_HOLDS_PARASITE (value))
{
memsize += gimp_parasite_get_memsize (g_value_get_boxed (value),
NULL);
}
else if (GIMP_VALUE_HOLDS_ARRAY (value) ||
GIMP_VALUE_HOLDS_INT8_ARRAY (value) ||
GIMP_VALUE_HOLDS_INT16_ARRAY (value) ||
GIMP_VALUE_HOLDS_INT32_ARRAY (value) ||
GIMP_VALUE_HOLDS_FLOAT_ARRAY (value))
{
GimpArray *array = g_value_get_boxed (value);
if (array)
memsize += (sizeof (GimpArray) +
array->static_data ? 0 : array->length);
}
else if (GIMP_VALUE_HOLDS_STRING_ARRAY (value))
{
GimpArray *array = g_value_get_boxed (value);
if (array)
{
memsize += sizeof (GimpArray);
if (! array->static_data)
{
gchar **tmp = (gchar **) array->data;
gint i;
memsize += array->length * sizeof (gchar *);
for (i = 0; i < array->length; i++)
memsize += gimp_string_get_memsize (tmp[i]);
}
}
}
else
{
g_printerr ("%s: unhandled boxed value type: %s\n",
G_STRFUNC, G_VALUE_TYPE_NAME (value));
}
}
else if (G_VALUE_HOLDS_OBJECT (value))
{
g_printerr ("%s: unhandled object value type: %s\n",
G_STRFUNC, G_VALUE_TYPE_NAME (value));
}
return memsize + sizeof (GValue);
}
gint64
gimp_g_param_spec_get_memsize (GParamSpec *pspec)
{
gint64 memsize = 0;
if (! pspec)
return 0;
if (! (pspec->flags & G_PARAM_STATIC_NAME))
memsize += gimp_string_get_memsize (g_param_spec_get_name (pspec));
if (! (pspec->flags & G_PARAM_STATIC_NICK))
memsize += gimp_string_get_memsize (g_param_spec_get_nick (pspec));
if (! (pspec->flags & G_PARAM_STATIC_BLURB))
memsize += gimp_string_get_memsize (g_param_spec_get_blurb (pspec));
return memsize + gimp_g_type_instance_get_memsize ((GTypeInstance *) pspec);
}
gint64
gimp_gegl_buffer_get_memsize (GeglBuffer *buffer)
{
if (buffer)
{
const Babl *format = gegl_buffer_get_format (buffer);
return (babl_format_get_bytes_per_pixel (format) *
gegl_buffer_get_width (buffer) *
gegl_buffer_get_height (buffer) +
gimp_g_object_get_memsize (G_OBJECT (buffer)));
}
return 0;
}
gint64
gimp_gegl_pyramid_get_memsize (GeglBuffer *buffer)
{
if (buffer)
{
const Babl *format = gegl_buffer_get_format (buffer);
/* The pyramid levels constitute a geometric sum with a ratio of 1/4. */
return ((gint64) babl_format_get_bytes_per_pixel (format) *
(gint64) gegl_buffer_get_width (buffer) *
(gint64) gegl_buffer_get_height (buffer) * 1.33 +
gimp_g_object_get_memsize (G_OBJECT (buffer)));
}
return 0;
}
gint64
gimp_string_get_memsize (const gchar *string)
{
if (string)
return strlen (string) + 1;
return 0;
}
gint64
gimp_parasite_get_memsize (GimpParasite *parasite,
gint64 *gui_size)
{
if (parasite)
return (sizeof (GimpParasite) +
gimp_string_get_memsize (parasite->name) +
parasite->size);
return 0;
}
gint
gimp_get_pid (void)
{
......
......@@ -32,33 +32,6 @@
#define MAX4(a,b,c,d) MAX (MAX ((a), (b)), MAX ((c), (d)))
gint64 gimp_g_type_instance_get_memsize (GTypeInstance *instance);
gint64 gimp_g_object_get_memsize (GObject *object);
gint64 gimp_g_hash_table_get_memsize (GHashTable *hash,
gint64 data_size);
gint64 gimp_g_hash_table_get_memsize_foreach (GHashTable *hash,
GimpMemsizeFunc func,
gint64 *gui_size);
gint64 gimp_g_slist_get_memsize (GSList *slist,
gint64 data_size);
gint64 gimp_g_slist_get_memsize_foreach (GSList *slist,
GimpMemsizeFunc func,
gint64 *gui_size);
gint64 gimp_g_list_get_memsize (GList *list,
gint64 data_size);
gint64 gimp_g_list_get_memsize_foreach (GList *slist,
GimpMemsizeFunc func,
gint64 *gui_size);
gint64 gimp_g_value_get_memsize (GValue *value);
gint64 gimp_g_param_spec_get_memsize (GParamSpec *pspec);
gint64 gimp_gegl_buffer_get_memsize (GeglBuffer *buffer);
gint64 gimp_gegl_pyramid_get_memsize (GeglBuffer *buffer);
gint64 gimp_string_get_memsize (const gchar *string);
gint64 gimp_parasite_get_memsize (GimpParasite *parasite,
gint64 *gui_size);
gint gimp_get_pid (void);
guint64 gimp_get_physical_memory_size (void);
gchar * gimp_get_backtrace (void);
......
......@@ -45,6 +45,7 @@
#include "gimp.h"
#include "gimp-contexts.h"
#include "gimp-gradients.h"
#include "gimp-memsize.h"
#include "gimp-modules.h"
#include "gimp-parasites.h"
#include "gimp-templates.h"
......
......@@ -27,7 +27,7 @@
#include "gegl/gimp-babl.h"
#include "gimp-utils.h"
#include "gimp-memsize.h"
#include "gimpbuffer.h"
#include "gimpimage.h"
#include "gimptempbuf.h"
......
......@@ -28,7 +28,7 @@
#include "core-types.h"
#include "gimp.h"
#include "gimp-utils.h"
#include "gimp-memsize.h"
#include "gimpcontainer.h"
#include "gimpmarshal.h"
......
......@@ -33,7 +33,7 @@
#include "config/gimpcoreconfig.h"
#include "gimp.h"
#include "gimp-utils.h"
#include "gimp-memsize.h"
#include "gimpbrush.h"
#include "gimpbuffer.h"
#include "gimpcontainer.h"
......