1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/* opaques.c: Opaque memory management test objects
*
* Copyright (c) 2005 Novell, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the Lesser GNU General
* Public License as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "opaques.h"
#include <stdio.h>
static int opserial, refserial;
static gboolean error = FALSE, expect_error = FALSE;
static gboolean check_error (gboolean valid, const char *msg, ...);
GtksharpOpaque *
gtksharp_opaque_new (void)
{
GtksharpOpaque *op = g_new0 (GtksharpOpaque, 1);
op->valid = TRUE;
op->serial = opserial++;
return op;
}
int
gtksharp_opaque_get_serial (GtksharpOpaque *op)
{
check_error (op->valid, "get_serial on freed GtksharpOpaque serial %d\n", op->serial);
return op->serial;
}
void gtksharp_opaque_set_friend (GtksharpOpaque *op, GtksharpOpaque *friend)
{
check_error (op->valid, "set_friend on freed GtksharpOpaque serial %d\n", op->serial);
op->friend = friend;
}
GtksharpOpaque *
gtksharp_opaque_get_friend (GtksharpOpaque *op)
{
check_error (op->valid, "get_friend on freed GtksharpOpaque serial %d\n", op->serial);
return op->friend;
}
GtksharpOpaque *
gtksharp_opaque_copy (GtksharpOpaque *op)
{
check_error (op->valid, "copying freed GtksharpOpaque serial %d\n", op->serial);
return gtksharp_opaque_new ();
}
void
gtksharp_opaque_free (GtksharpOpaque *op)
{
check_error (op->valid, "Double free of GtksharpOpaque serial %d\n", op->serial);
op->valid = FALSE;
/* We don't actually free it */
}
GtksharpOpaque *
gtksharp_opaque_check (GtksharpOpaqueReturnFunc func, GtksharpGCFunc gc)
{
GtksharpOpaque *op = func ();
gc ();
return op;
}
GtksharpOpaque *
gtksharp_opaque_check_free (GtksharpOpaqueReturnFunc func, GtksharpGCFunc gc)
{
GtksharpOpaque *op = func ();
gc ();
gtksharp_opaque_free (op);
gc ();
return op;
}
int
gtksharp_opaque_get_last_serial (void)
{
return opserial - 1;
}
GtksharpRefcounted *
gtksharp_refcounted_new (void)
{
GtksharpRefcounted *ref = g_new0 (GtksharpRefcounted, 1);
ref->valid = TRUE;
ref->refcount = 1;
ref->serial = refserial++;
return ref;
}
int
gtksharp_refcounted_get_serial (GtksharpRefcounted *ref)
{
check_error (ref->valid, "get_serial on freed GtksharpRefcounted serial %d\n", ref->serial);
return ref->serial;
}
void
gtksharp_refcounted_ref (GtksharpRefcounted *ref)
{
if (check_error (ref->valid, "ref on freed GtksharpRefcounted serial %d\n", ref->serial))
return;
ref->refcount++;
}
void
gtksharp_refcounted_unref (GtksharpRefcounted *ref)
{
if (check_error (ref->valid, "unref on freed GtksharpRefcounted serial %d\n", ref->serial))
return;
if (--ref->refcount == 0) {
ref->valid = FALSE;
/* We don't actually free it */
}
}
int
gtksharp_refcounted_get_refcount (GtksharpRefcounted *ref)
{
check_error (ref->valid, "get_refcount on freed GtksharpRefcounted serial %d\n", ref->serial);
return ref->refcount;
}
void
gtksharp_refcounted_set_friend (GtksharpRefcounted *ref, GtksharpRefcounted *friend)
{
check_error (ref->valid, "set_friend on freed GtksharpRefcounted serial %d\n", ref->serial);
if (ref->friend)
gtksharp_refcounted_unref (ref->friend);
ref->friend = friend;
if (ref->friend)
gtksharp_refcounted_ref (ref->friend);
}
GtksharpRefcounted *
gtksharp_refcounted_get_friend (GtksharpRefcounted *ref)
{
check_error (ref->valid, "get_friend on freed GtksharpRefcounted serial %d\n", ref->serial);
return ref->friend;
}
GtksharpRefcounted *
gtksharp_refcounted_check (GtksharpRefcountedReturnFunc func, GtksharpGCFunc gc)
{
GtksharpRefcounted *ref = func ();
gc ();
return ref;
}
GtksharpRefcounted *
gtksharp_refcounted_check_unref (GtksharpRefcountedReturnFunc func, GtksharpGCFunc gc)
{
GtksharpRefcounted *ref = func ();
gc ();
gtksharp_refcounted_unref (ref);
gc ();
return ref;
}
int
gtksharp_refcounted_get_last_serial (void)
{
return refserial - 1;
}
static gboolean
check_error (gboolean valid, const char *msg, ...)
{
va_list ap;
if (valid)
return FALSE;
error = TRUE;
if (expect_error) {
expect_error = FALSE;
return FALSE;
}
expect_error = FALSE;
fprintf (stderr, " UNMANAGED ERROR: ");
va_start (ap, msg);
vfprintf (stderr, msg, ap);
va_end (ap);
return TRUE;
}
gboolean
gtksharp_opaquetest_get_error (void)
{
gboolean old_error = error;
error = expect_error = FALSE;
return old_error;
}
void
gtksharp_opaquetest_set_error (gboolean err)
{
error = err;
}
void
gtksharp_opaquetest_set_expect_error (gboolean err)
{
expect_error = err;
}
|