1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
* 2005-01-10 Paul McCullagh
24
#include "xt_config.h"
26
#include "pthread_xt.h"
28
#include "thread_xt.h"
35
xtPublic XTHeapPtr xt_mm_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize, u_int line, c_char *file, xtBool track)
37
xtPublic XTHeapPtr xt_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize)
40
volatile XTHeapPtr hp;
43
hp = (XTHeapPtr) xt_mm_calloc(self, size, line, file);
46
printf("HEAP: +1 1 %s:%d\n", file, (int) line);
48
hp = (XTHeapPtr) xt_calloc(self, size);
54
xt_spinlock_init_with_autoname(self, &hp->h_lock);
63
hp->h_finalize = finalize;
64
hp->h_onrelease = NULL;
68
xtPublic void xt_check_heap(XTThreadPtr XT_NDEBUG_UNUSED(self), XTHeapPtr XT_NDEBUG_UNUSED(hp))
71
xt_mm_malloc_size(self, hp);
76
xtPublic void xt_mm_heap_reference(XTThreadPtr XT_UNUSED(self), XTHeapPtr hp, u_int line, c_char *file)
78
xtPublic void xt_heap_reference(XTThreadPtr, XTHeapPtr hp)
81
xt_spinlock_lock(&hp->h_lock);
84
printf("HEAP: +1 %d->%d %s:%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count+1, file, (int) line);
87
xt_spinlock_unlock(&hp->h_lock);
90
xtPublic void xt_heap_release(XTThreadPtr self, XTHeapPtr hp)
95
xt_spinlock_lock(&hp->h_lock);
96
ASSERT(hp->h_ref_count != 0);
97
xt_spinlock_unlock(&hp->h_lock);
99
xt_spinlock_lock(&hp->h_lock);
101
(*hp->h_onrelease)(hp);
102
if (hp->h_ref_count > 0) {
105
printf("HEAP: -1 %d->%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count-1);
108
if (hp->h_ref_count == 0) {
110
(*hp->h_finalize)(self, hp);
111
xt_spinlock_unlock(&hp->h_lock);
116
xt_spinlock_unlock(&hp->h_lock);
119
xtPublic void xt_heap_release_ns(XTHeapPtr hp)
124
xt_spinlock_lock(&hp->h_lock);
125
ASSERT_NS(hp->h_ref_count != 0);
126
xt_spinlock_unlock(&hp->h_lock);
128
xt_spinlock_lock(&hp->h_lock);
130
(*hp->h_onrelease)(hp);
132
if (hp->h_ref_count > 0) {
135
printf("HEAP: -1 %d->%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count-1);
138
if (hp->h_ref_count == 0) {
139
XTThreadPtr self = xt_get_self();
143
(*hp->h_finalize)(self, hp);
146
xt_log_and_clear_exception(self);
150
xt_spinlock_unlock(&hp->h_lock);
155
xt_spinlock_unlock(&hp->h_lock);
158
xtPublic void xt_heap_set_release_callback(XTHeapPtr hp, XTOnReleaseFunc onrelease)
160
hp->h_onrelease = onrelease;
163
xtPublic u_int xt_heap_get_ref_count(struct XTThread *XT_UNUSED(self), XTHeapPtr hp)
165
return hp->h_ref_count;