1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
5 * Copyright (C) 2002-2006 Morten Welinder (terra@diku.dk)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #include <gsf-config.h>
23 #include <gsf/gsf-shared-memory.h>
24 #include <gsf/gsf-impl-utils.h>
27 #include <sys/types.h>
29 #elif defined(G_OS_WIN32)
34 GObjectClass g_object_class;
35 } GsfSharedMemoryClass;
37 static GObjectClass *parent_class;
40 gsf_shared_memory_new (void *buf, gsf_off_t size, gboolean needs_free)
42 GsfSharedMemory *mem = g_object_new (GSF_SHARED_MEMORY_TYPE, NULL);
43 if (G_UNLIKELY (NULL == mem)) return NULL;
47 mem->needs_free = needs_free;
48 mem->needs_unmap = FALSE;
53 gsf_shared_memory_mmapped_new (void *buf, gsf_off_t size)
55 #if defined(HAVE_MMAP) || defined(G_OS_WIN32)
57 if ((gsf_off_t)msize != size) {
58 g_warning ("memory buffer size too large");
61 GsfSharedMemory *mem = gsf_shared_memory_new (buf, size, FALSE);
62 mem->needs_unmap = TRUE;
71 gsf_shared_memory_finalize (GObject *obj)
73 GsfSharedMemory *mem = (GsfSharedMemory *) (obj);
75 if (mem->buf != NULL) {
78 else if (mem->needs_unmap) {
80 munmap (mem->buf, mem->size);
81 #elif defined(G_OS_WIN32)
82 UnmapViewOfFile (mem->buf);
84 g_assert_not_reached ();
89 G_OBJECT_CLASS (parent_class)->finalize (obj);
93 gsf_shared_memory_init (GObject *obj)
95 GsfSharedMemory *mem = (GsfSharedMemory *) (obj);
100 gsf_shared_memory_class_init (GObjectClass *gobject_class)
102 parent_class = g_type_class_peek_parent (gobject_class);
104 gobject_class->finalize = gsf_shared_memory_finalize;
107 GSF_CLASS (GsfSharedMemory, gsf_shared_memory,
108 gsf_shared_memory_class_init, gsf_shared_memory_init,