"Initial commit to Gerrit"
[profile/ivi/libgsf.git] / gsf / gsf-shared-memory.c
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-shared-memory.c: 
4  *
5  * Copyright (C) 2002-2006 Morten Welinder (terra@diku.dk)
6  *
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.
10  *
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.
15  *
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
19  * USA
20  */
21
22 #include <gsf-config.h>
23 #include <gsf/gsf-shared-memory.h>
24 #include <gsf/gsf-impl-utils.h>
25
26 #ifdef HAVE_MMAP
27 #include <sys/types.h>
28 #include <sys/mman.h>
29 #elif defined(G_OS_WIN32)
30 #include <windows.h>
31 #endif
32
33 typedef struct {
34         GObjectClass g_object_class;
35 } GsfSharedMemoryClass;
36
37 static GObjectClass *parent_class;
38
39 GsfSharedMemory *
40 gsf_shared_memory_new (void *buf, gsf_off_t size, gboolean needs_free)
41 {
42         GsfSharedMemory *mem = g_object_new (GSF_SHARED_MEMORY_TYPE, NULL);
43         if (G_UNLIKELY (NULL == mem)) return NULL;
44
45         mem->buf = buf;
46         mem->size = size;
47         mem->needs_free = needs_free;
48         mem->needs_unmap = FALSE;
49         return mem;
50 }
51
52 GsfSharedMemory *
53 gsf_shared_memory_mmapped_new (void *buf, gsf_off_t size)
54 {
55 #if defined(HAVE_MMAP) || defined(G_OS_WIN32)
56         size_t msize = size;
57         if ((gsf_off_t)msize != size) {
58                 g_warning ("memory buffer size too large");
59                 return NULL;
60         } else {
61                 GsfSharedMemory *mem = gsf_shared_memory_new (buf, size, FALSE);
62                 mem->needs_unmap = TRUE;
63                 return mem;
64         }
65 #else
66         return NULL;
67 #endif
68 }
69
70 static void
71 gsf_shared_memory_finalize (GObject *obj)
72 {
73         GsfSharedMemory *mem = (GsfSharedMemory *) (obj);
74         
75         if (mem->buf != NULL) {
76                 if (mem->needs_free)
77                         g_free (mem->buf);
78                 else if (mem->needs_unmap) {
79 #ifdef HAVE_MMAP
80                         munmap (mem->buf, mem->size);
81 #elif defined(G_OS_WIN32)
82                         UnmapViewOfFile (mem->buf);
83 #else
84                         g_assert_not_reached ();
85 #endif
86                 }
87         }
88
89         G_OBJECT_CLASS (parent_class)->finalize (obj);
90 }
91
92 static void
93 gsf_shared_memory_init (GObject *obj)
94 {
95         GsfSharedMemory *mem = (GsfSharedMemory *) (obj);
96         mem->buf = NULL;
97 }
98
99 static void
100 gsf_shared_memory_class_init (GObjectClass *gobject_class)
101 {
102         parent_class = g_type_class_peek_parent (gobject_class);
103
104         gobject_class->finalize = gsf_shared_memory_finalize;
105 }
106
107 GSF_CLASS (GsfSharedMemory, gsf_shared_memory,
108            gsf_shared_memory_class_init, gsf_shared_memory_init,
109            G_TYPE_OBJECT)