summaryrefslogtreecommitdiff
path: root/storage/pbxt/src/heap_xt.cc
blob: 11331f6489fff2a886f23e058f85eeb1a29f1300 (plain)
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
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 *
 * PrimeBase XT
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 *
 * 2005-01-10	Paul McCullagh
 *
 * H&G2JCtL
 */

#include "xt_config.h"

#include "pthread_xt.h"
#include "heap_xt.h"
#include "thread_xt.h"

#ifdef xt_heap_new
#undef xt_heap_new
#endif

#ifdef DEBUG_MEMORY
xtPublic XTHeapPtr xt_mm_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize, u_int line, c_char *file, xtBool track)
#else
xtPublic XTHeapPtr xt_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize)
#endif
{
	volatile XTHeapPtr	hp;
	
#ifdef DEBUG_MEMORY
	hp = (XTHeapPtr) xt_mm_calloc(self, size, line, file);
	hp->h_track = track;
	if (track)
		printf("HEAP: +1  1 %s:%d\n", file, (int) line);
#else
	hp = (XTHeapPtr) xt_calloc(self, size);
#endif
	if (!hp)
		return NULL;

	try_(a) {
		xt_spinlock_init_with_autoname(self, &hp->h_lock);
	}
	catch_(a) {
		xt_free(self, hp);
		throw_();
	}
	cont_(a);

	hp->h_ref_count = 1;
	hp->h_finalize = finalize;
	hp->h_onrelease = NULL;
	return hp;
}

xtPublic void xt_check_heap(XTThreadPtr XT_NDEBUG_UNUSED(self), XTHeapPtr XT_NDEBUG_UNUSED(hp))
{
#ifdef DEBUG_MEMORY
	xt_mm_malloc_size(self, hp);
#endif
}

#ifdef DEBUG_MEMORY
xtPublic void xt_mm_heap_reference(XTThreadPtr XT_UNUSED(self), XTHeapPtr hp, u_int line, c_char *file)
#else
xtPublic void xt_heap_reference(XTThreadPtr, XTHeapPtr hp)
#endif
{
	xt_spinlock_lock(&hp->h_lock);
#ifdef DEBUG_MEMORY
	if (hp->h_track)
		printf("HEAP: +1 %d->%d %s:%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count+1, file, (int) line);
#endif
	hp->h_ref_count++;
	xt_spinlock_unlock(&hp->h_lock);
}

xtPublic void xt_heap_release(XTThreadPtr self, XTHeapPtr hp)
{	
	if (!hp)
		return;
#ifdef DEBUG_MEMORY
	xt_spinlock_lock(&hp->h_lock);
	ASSERT(hp->h_ref_count != 0);
	xt_spinlock_unlock(&hp->h_lock);
#endif
	xt_spinlock_lock(&hp->h_lock);
	if (hp->h_onrelease)
		(*hp->h_onrelease)(self, hp);
	if (hp->h_ref_count > 0) {
#ifdef DEBUG_MEMORY
	if (hp->h_track)
		printf("HEAP: -1 %d->%d\n", (int) hp->h_ref_count, (int) hp->h_ref_count-1);
#endif
		hp->h_ref_count--;
		if (hp->h_ref_count == 0) {
			if (hp->h_finalize)
				(*hp->h_finalize)(self, hp);
			xt_spinlock_unlock(&hp->h_lock);
			xt_spinlock_free(NULL, &hp->h_lock);
			xt_free(self, hp);
			return;
		}
	}
	xt_spinlock_unlock(&hp->h_lock);
}

xtPublic void xt_heap_set_release_callback(XTThreadPtr XT_UNUSED(self), XTHeapPtr hp, XTFinalizeFunc onrelease)
{
	hp->h_onrelease = onrelease;
}

xtPublic u_int xt_heap_get_ref_count(struct XTThread *XT_UNUSED(self), XTHeapPtr hp)
{
	return hp->h_ref_count;
}