Commit 81f6e840 authored by Vincent Geddes's avatar Vincent Geddes

Performed house cleaning. - Use g_slice_new where appropriate. - Hide

	* glade-id-allocator.c, glade-id-allocator.h: Performed house cleaning.
          - Use g_slice_new where appropriate.
          - Hide private implementation details.
          - Added g_return_if_fail guards.
          
        * gladeui/glade-project.c: Use GladeIDAllocator for allocating unsaved numbers.

svn path=/trunk/; revision=1264
parent e86ce4eb
......@@ -4,6 +4,13 @@
* gladeui/glade-widget.c: Clean up toplevel embedding code, remove
unused embedding implementation. Fix embedding warning.
* glade-id-allocator.c, glade-id-allocator.h: Performed house cleaning.
- Use g_slice_new where appropriate.
- Hide private implementation details.
- Added g_return_if_fail guards.
* gladeui/glade-project.c: Use GladeIDAllocator for allocating unsaved numbers.
2007-04-20 Vincent Geddes <vincent.geddes@gmail.com>
......
......@@ -47,6 +47,7 @@ libgladeui_1_la_SOURCES = \
glade-clipboard-view.c \
glade-command.c \
glade-id-allocator.c \
glade-id-allocator.h \
glade-marshallers.c \
glade-accumulators.c \
glade-parser.c \
......@@ -61,7 +62,6 @@ libgladeui_1_la_SOURCES = \
glade-palette-expander.h \
glade-popup.h \
glade-catalog.h \
glade-id-allocator.h \
glade-marshallers.h \
glade-accumulators.h \
glade-parser.h \
......
......@@ -39,6 +39,8 @@
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkstock.h>
#define GLADE_CONFIG_FILENAME "glade-3.conf"
#define GLADE_APP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GLADE_TYPE_APP, GladeAppPrivate))
enum
......
......@@ -30,8 +30,6 @@
G_BEGIN_DECLS
#define GLADE_CONFIG_FILENAME "glade-3.conf"
#define GLADE_TYPE_APP (glade_app_get_type())
#define GLADE_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GLADE_TYPE_APP, GladeApp))
#define GLADE_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GLADE_TYPE_APP, GladeAppClass))
......
......@@ -2,6 +2,11 @@
/*
* Copyright (C) 2004 Owen Taylor
*
* Authors:
* Owen Taylor <otaylor@redhat.com>
*
* Modified by the Glade developers
*
* 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
......@@ -16,18 +21,22 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors:
* Owen Taylor <otaylor@redhat.com>
*/
#include "config.h"
#include "glade-id-allocator.h"
#include <glib.h>
#include <string.h>
#include "glade-id-allocator.h"
#define INITIAL_WORDS 4
struct _GladeIDAllocator
{
guint n_words;
guint32 *data;
};
/**
* glade_id_allocator_new:
*
......@@ -36,26 +45,29 @@
GladeIDAllocator *
glade_id_allocator_new (void)
{
GladeIDAllocator *allocator = g_new (GladeIDAllocator, 1);
GladeIDAllocator *allocator = g_slice_new (GladeIDAllocator);
allocator->n_words = INITIAL_WORDS;
allocator->data = g_new (guint32, INITIAL_WORDS);
memset (allocator->data, 0xff, INITIAL_WORDS * sizeof (guint32));
return allocator;
}
/**
* glade_id_allocator_free:
* glade_id_allocator_destroy:
* @allocator: a #GladeIDAllocator
*
* Frees @allocator and its associated memory
*/
void
glade_id_allocator_free (GladeIDAllocator *allocator)
glade_id_allocator_destroy (GladeIDAllocator *allocator)
{
g_return_if_fail (allocator != NULL);
g_free (allocator->data);
g_free (allocator);
g_slice_free (GladeIDAllocator, allocator);
}
static inline gint
......@@ -99,8 +111,10 @@ first_set_bit (guint32 word)
* Returns:
*/
guint
glade_id_allocator_alloc (GladeIDAllocator *allocator)
glade_id_allocator_allocate (GladeIDAllocator *allocator)
{
g_return_val_if_fail (allocator != NULL, 0);
guint i;
for (i = 0; i < allocator->n_words; i++)
......@@ -136,8 +150,10 @@ glade_id_allocator_alloc (GladeIDAllocator *allocator)
*/
void
glade_id_allocator_release (GladeIDAllocator *allocator,
guint id)
guint id)
{
g_return_if_fail (allocator != NULL);
id--;
allocator->data[id >> 5] |= 1 << (id & 31);
}
......@@ -151,7 +167,7 @@ int main (int argc, char **argv)
for (i = 0; i < 1000; i++)
{
guint id = glade_id_allocator_alloc (allocator);
guint id = glade_id_allocator_allocate (allocator);
g_assert (id == i);
}
......@@ -167,7 +183,7 @@ int main (int argc, char **argv)
glade_id_allocator_release (allocator, i);
}
glade_id_allocator_free (allocator);
glade_id_allocator_destroy (allocator);
return 0;
}
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2004 Owen Taylor
*
* Authors:
* Owen Taylor <otaylor@redhat.com>
*
* Modified by the Glade developers
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef __GLADE_ID_ALLOCATOR_H__
#define __GLADE_ID_ALLOCATOR_H__
#include <glib.h>
G_BEGIN_DECLS
typedef struct _GladeIDAllocator GladeIDAllocator;
struct _GladeIDAllocator
{
guint n_words;
guint32 *data;
};
GladeIDAllocator *glade_id_allocator_new (void);
void glade_id_allocator_destroy (GladeIDAllocator *allocator);
guint glade_id_allocator_allocate (GladeIDAllocator *allocator);
void glade_id_allocator_release (GladeIDAllocator *allocator,
guint id);
G_END_DECLS
GladeIDAllocator * glade_id_allocator_new (void);
void glade_id_allocator_free (GladeIDAllocator *allocator);
guint glade_id_allocator_alloc (GladeIDAllocator *allocator);
void glade_id_allocator_release (GladeIDAllocator *allocator, guint id);
#endif /* __GLADE_ID_ALLOCATOR_H__ */
#endif
......@@ -61,14 +61,26 @@ enum
PROP_READ_ONLY
};
static guint glade_project_signals[LAST_SIGNAL] = {0};
static GObjectClass *parent_class = NULL;
static GHashTable *allocated_untitled_numbers = NULL;
static guint glade_project_signals[LAST_SIGNAL] = {0};
static GladeIDAllocator *unsaved_number_allocator = NULL;
G_DEFINE_TYPE (GladeProject, glade_project, G_TYPE_OBJECT)
/*******************************************************************
GObjectClass
*******************************************************************/
static GladeIDAllocator *
get_unsaved_number_allocator (void)
{
if (unsaved_number_allocator == NULL)
unsaved_number_allocator = glade_id_allocator_new ();
return unsaved_number_allocator;
}
static void
glade_project_list_unref (GList *original_list)
{
......@@ -80,14 +92,6 @@ glade_project_list_unref (GList *original_list)
g_list_free (original_list);
}
static void
glade_project_release_untitled_number (gint n)
{
g_return_if_fail (allocated_untitled_numbers != NULL);
g_hash_table_remove (allocated_untitled_numbers, GINT_TO_POINTER (n));
}
static void
glade_project_dispose (GObject *object)
{
......@@ -131,7 +135,7 @@ glade_project_dispose (GObject *object)
gtk_object_destroy (GTK_OBJECT (project->tooltips));
project->tooltips = NULL;
G_OBJECT_CLASS (parent_class)->dispose (object);
G_OBJECT_CLASS (glade_project_parent_class)->dispose (object);
}
static void
......@@ -143,14 +147,14 @@ glade_project_finalize (GObject *object)
g_free (project->path);
g_free (project->comment);
if (project->untitled_number > 0)
glade_project_release_untitled_number (project->untitled_number);
if (project->unsaved_number > 0)
glade_id_allocator_release (get_unsaved_number_allocator (), project->unsaved_number);
g_hash_table_destroy (project->widget_names_allocator);
g_hash_table_destroy (project->widget_old_names);
g_hash_table_destroy (project->resources);
G_OBJECT_CLASS (parent_class)->finalize (object);
G_OBJECT_CLASS (glade_project_parent_class)->finalize (object);
}
static void
......@@ -343,7 +347,7 @@ glade_project_init (GladeProject *project)
project->path = NULL;
project->name = NULL;
project->instance = 0;
project->untitled_number = 0;
project->unsaved_number = 0;
project->readonly = FALSE;
project->objects = NULL;
project->selection = NULL;
......@@ -352,7 +356,7 @@ glade_project_init (GladeProject *project)
project->prev_redo_item = NULL;
project->widget_names_allocator =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
(GDestroyNotify) glade_id_allocator_free);
(GDestroyNotify) glade_id_allocator_destroy);
project->widget_old_names =
g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) g_free);
project->tooltips = gtk_tooltips_new ();
......@@ -371,8 +375,6 @@ glade_project_class_init (GladeProjectClass *klass)
object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->get_property = glade_project_get_property;
object_class->finalize = glade_project_finalize;
object_class->dispose = glade_project_dispose;
......@@ -585,56 +587,6 @@ glade_project_class_init (GladeProjectClass *klass)
/*******************************************************************
API
*******************************************************************/
GType
glade_project_get_type (void)
{
static GType type = 0;
if (!type)
{
static const GTypeInfo info = {
sizeof (GladeProjectClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) glade_project_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (GladeProject),
0,
(GInstanceInitFunc) glade_project_init
};
type = g_type_register_static (G_TYPE_OBJECT, "GladeProject", &info, 0);
}
return type;
}
static gint
glade_project_get_untitled_number (void)
{
gint i = 1;
if (allocated_untitled_numbers == NULL)
allocated_untitled_numbers = g_hash_table_new (NULL, NULL);
g_return_val_if_fail (allocated_untitled_numbers != NULL, -1);
while (TRUE)
{
if (g_hash_table_lookup (allocated_untitled_numbers, GINT_TO_POINTER (i)) == NULL)
{
g_hash_table_insert (allocated_untitled_numbers,
GINT_TO_POINTER (i),
GINT_TO_POINTER (i));
return i;
}
++i;
}
}
static void
glade_project_set_readonly (GladeProject *project, gboolean readonly)
......@@ -683,8 +635,8 @@ glade_project_new (gboolean untitled)
if (untitled)
{
project->untitled_number = glade_project_get_untitled_number ();
project->name = g_strdup_printf (_("Unsaved %d"), project->untitled_number);
project->unsaved_number = glade_id_allocator_allocate (get_unsaved_number_allocator ());
project->name = g_strdup_printf (_("Unsaved %d"), project->unsaved_number);
}
return project;
......@@ -1097,7 +1049,7 @@ glade_project_new_widget_name (GladeProject *project, const char *base_name)
while (TRUE)
{
i = glade_id_allocator_alloc (id_allocator);
i = glade_id_allocator_allocate (id_allocator);
name = g_strdup_printf ("%s%u", base_name, i);
/* ok, there is no widget with this name, so return the name */
......@@ -1700,10 +1652,10 @@ glade_project_save (GladeProject *project, const gchar *path, GError **error)
g_object_notify (G_OBJECT (project), "has-unsaved-changes");
}
if (project->untitled_number > 0)
if (project->unsaved_number > 0)
{
glade_project_release_untitled_number (project->untitled_number);
project->untitled_number = 0;
glade_id_allocator_release (get_unsaved_number_allocator (), project->unsaved_number);
project->unsaved_number = 0;
}
g_free (canonical_path);
......
......@@ -29,7 +29,7 @@ struct _GladeProject
gint instance; /* How many projects with this name */
gint untitled_number; /* A unique number for this project if it is untitled */
gint unsaved_number; /* A unique number for this project if it is untitled */
gboolean readonly; /* A flag that is set if the project is readonly */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment