~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbxt/src/heap_xt.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2005 PrimeBase Technologies GmbH
2
 
 *
3
 
 * PrimeBase XT
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
 
 *
19
 
 * 2005-01-10   Paul McCullagh
20
 
 *
21
 
 * H&G2JCtL
22
 
 */
23
 
 
24
 
#include "xt_config.h"
25
 
 
26
 
#include "pthread_xt.h"
27
 
#include "heap_xt.h"
28
 
#include "thread_xt.h"
29
 
 
30
 
#ifdef xt_heap_new
31
 
#undef xt_heap_new
32
 
#endif
33
 
 
34
 
#ifdef DEBUG_MEMORY
35
 
xtPublic XTHeapPtr xt_mm_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize, u_int line, c_char *file, xtBool track)
36
 
#else
37
 
xtPublic XTHeapPtr xt_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize)
38
 
#endif
39
 
{
40
 
        volatile XTHeapPtr      hp;
41
 
        
42
 
#ifdef DEBUG_MEMORY
43
 
        hp = (XTHeapPtr) xt_mm_calloc(self, size, line, file);
44
 
        hp->h_track = track;
45
 
        if (track)
46
 
                printf("HEAP: +1  1 %s:%d\n", file, (int) line);
47
 
#else
48
 
        hp = (XTHeapPtr) xt_calloc(self, size);
49
 
#endif
50
 
        if (!hp)
51
 
                return NULL;
52
 
 
53
 
        try_(a) {
54
 
                xt_spinlock_init_with_autoname(self, &hp->h_lock);
55
 
        }
56
 
        catch_(a) {
57
 
                xt_free(self, hp);
58
 
                throw_();
59
 
        }
60
 
        cont_(a);
61
 
 
62
 
        hp->h_ref_count = 1;
63
 
        hp->h_finalize = finalize;
64
 
        hp->h_onrelease = NULL;
65
 
        return hp;
66
 
}
67
 
 
68
 
xtPublic void xt_check_heap(XTThreadPtr XT_NDEBUG_UNUSED(self), XTHeapPtr XT_NDEBUG_UNUSED(hp))
69
 
{
70
 
#ifdef DEBUG_MEMORY
71
 
        xt_mm_malloc_size(self, hp);
72
 
#endif
73
 
}
74
 
 
75
 
#ifdef DEBUG_MEMORY
76
 
xtPublic void xt_mm_heap_reference(XTThreadPtr XT_UNUSED(self), XTHeapPtr hp, u_int line, c_char *file)
77
 
#else
78
 
xtPublic void xt_heap_reference(XTThreadPtr, XTHeapPtr hp)
79
 
#endif
80
 
{
81
 
        xt_spinlock_lock(&hp->h_lock);
82
 
#ifdef DEBUG_MEMORY
83
 
        if (hp->h_track)
84
 
                printf("HEAP: +1 %d->%d %s:%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count+1, file, (int) line);
85
 
#endif
86
 
        hp->h_ref_count++;
87
 
        xt_spinlock_unlock(&hp->h_lock);
88
 
}
89
 
 
90
 
xtPublic void xt_heap_release(XTThreadPtr self, XTHeapPtr hp)
91
 
{       
92
 
        if (!hp)
93
 
                return;
94
 
#ifdef DEBUG_MEMORY
95
 
        xt_spinlock_lock(&hp->h_lock);
96
 
        ASSERT(hp->h_ref_count != 0);
97
 
        xt_spinlock_unlock(&hp->h_lock);
98
 
#endif
99
 
        xt_spinlock_lock(&hp->h_lock);
100
 
        if (hp->h_onrelease)
101
 
                (*hp->h_onrelease)(hp);
102
 
        if (hp->h_ref_count > 0) {
103
 
#ifdef DEBUG_MEMORY
104
 
                if (hp->h_track)
105
 
                        printf("HEAP: -1 %d->%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count-1);
106
 
#endif
107
 
                hp->h_ref_count--;
108
 
                if (hp->h_ref_count == 0) {
109
 
                        if (hp->h_finalize)
110
 
                                (*hp->h_finalize)(self, hp);
111
 
                        xt_spinlock_unlock(&hp->h_lock);
112
 
                        xt_free(self, hp);
113
 
                        return;
114
 
                }
115
 
        }
116
 
        xt_spinlock_unlock(&hp->h_lock);
117
 
}
118
 
 
119
 
xtPublic void xt_heap_release_ns(XTHeapPtr hp)
120
 
{
121
 
        if (!hp)
122
 
                return;
123
 
#ifdef DEBUG_MEMORY
124
 
        xt_spinlock_lock(&hp->h_lock);
125
 
        ASSERT_NS(hp->h_ref_count != 0);
126
 
        xt_spinlock_unlock(&hp->h_lock);
127
 
#endif
128
 
        xt_spinlock_lock(&hp->h_lock);
129
 
        if (hp->h_onrelease)
130
 
                (*hp->h_onrelease)(hp);
131
 
                
132
 
        if (hp->h_ref_count > 0) {
133
 
#ifdef DEBUG_MEMORY
134
 
                if (hp->h_track)
135
 
                        printf("HEAP: -1 %d->%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count-1);
136
 
#endif
137
 
                hp->h_ref_count--;
138
 
                if (hp->h_ref_count == 0) {
139
 
                        XTThreadPtr self = xt_get_self();
140
 
 
141
 
                        try_(a) {
142
 
                                if (hp->h_finalize)
143
 
                                        (*hp->h_finalize)(self, hp);
144
 
                        }
145
 
                        catch_(a) {
146
 
                                xt_log_and_clear_exception(self);
147
 
                        }
148
 
                        cont_(a);
149
 
 
150
 
                        xt_spinlock_unlock(&hp->h_lock);
151
 
                        xt_free_ns(hp);
152
 
                        return;
153
 
                }
154
 
        }
155
 
        xt_spinlock_unlock(&hp->h_lock);
156
 
}
157
 
 
158
 
xtPublic void xt_heap_set_release_callback(XTHeapPtr hp, XTOnReleaseFunc onrelease)
159
 
{
160
 
        hp->h_onrelease = onrelease;
161
 
}
162
 
 
163
 
xtPublic u_int xt_heap_get_ref_count(struct XTThread *XT_UNUSED(self), XTHeapPtr hp)
164
 
{
165
 
        return hp->h_ref_count;
166
 
}
167
 
 
168