+extern GPrivate *glk_data_key;
+
+/* Internal function: create a fileref using the given parameters. */
+frefid_t
+fileref_new(gchar *filename, glui32 rock, glui32 usage, glui32 orig_filemode)
+{
+ g_return_val_if_fail(filename != NULL, NULL);
+
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+
+ frefid_t f = g_new0(struct glk_fileref_struct, 1);
+ f->magic = MAGIC_FILEREF;
+ f->rock = rock;
+ if(glk_data->register_obj)
+ f->disprock = (*glk_data->register_obj)(f, gidisp_Class_Fileref);
+
+ f->filename = g_strdup(filename);
+ f->usage = usage;
+ f->orig_filemode = orig_filemode;
+
+ /* Add it to the global fileref list */
+ glk_data->fileref_list = g_list_prepend(glk_data->fileref_list, f);
+ f->fileref_list = glk_data->fileref_list;
+
+ return f;
+}
+
+static void
+fileref_close_common(frefid_t fref)
+{
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+
+ glk_data->fileref_list = g_list_delete_link(glk_data->fileref_list, fref->fileref_list);
+
+ if(glk_data->unregister_obj)
+ {
+ (*glk_data->unregister_obj)(fref, gidisp_Class_Fileref, fref->disprock);
+ fref->disprock.ptr = NULL;
+ }
+
+ if(fref->filename)
+ g_free(fref->filename);
+
+ fref->magic = MAGIC_FREE;
+ g_free(fref);
+}