1
/* Copyright (c) 2005 SNAP Innovation 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"
27
#include "xt_thread.h"
33
typedef struct XTHeap {
34
xt_rwlock_type h_lock; /* Prevent concurrent access to the heap memory: */
35
u_int h_ref_count; /* So we know when to free (EVERY pointer reference MUST be counted). */
36
XTFinalizeFunc h_finalize; /* If non-NULL, call before freeing. */
37
XTFinalizeFunc h_onrelease; /* If non-NULL, call on release. */
40
char h_data[XT_VAR_LENGTH]; /* This is the user memory */
42
} XTHeapRec, *XTHeapPtr;
45
xtPublic void *xt_mm_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize, u_int line, char *file)
47
xtPublic void *xt_heap_new(XTThreadPtr self, size_t size, XTFinalizeFunc finalize)
53
hp = xt_mm_calloc(self, offsetof(XTHeapRec, x.h_data) + size, line, file);
55
hp = xt_calloc(self, offsetof(XTHeapRec, x.h_data) + size);
61
xt_init_rwlock(self, &hp->h_lock);
70
hp->h_finalize = finalize;
71
hp->h_onrelease = NULL;
75
xtPublic void xt_check_heap(XTThreadPtr self, void *mem)
77
char *hp = ((char *) mem - offsetof(XTHeapRec, x.h_data));
80
xt_mm_malloc_size(self, hp);
84
xtPublic void xt_heap_reference(XTThreadPtr self, void *mem)
86
XTHeapPtr hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
88
xt_w_lock(self, &hp->h_lock);
90
xt_rw_unlock(self, &hp->h_lock);
93
xtPublic void xt_heap_release(XTThreadPtr self, void *mem)
99
hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
101
xt_r_lock(NULL, &hp->h_lock);
102
ASSERT(hp->h_ref_count != 0);
103
xt_rw_unlock(self, &hp->h_lock);
105
xt_w_lock(self, &hp->h_lock);
107
(*hp->h_onrelease)(self, mem);
108
if (hp->h_ref_count > 0) {
110
if (hp->h_ref_count == 0) {
112
(*hp->h_finalize)(self, mem);
113
xt_rw_unlock(self, &hp->h_lock);
118
xt_rw_unlock(self, &hp->h_lock);
121
xtPublic void xt_heap_set_release_callback(XTThreadPtr self, void *mem, XTFinalizeFunc onrelease)
123
XTHeapPtr hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
125
hp->h_onrelease = onrelease;
128
xtPublic u_int xt_heap_get_ref_count(struct XTThread *self, void *mem)
130
XTHeapPtr hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
132
return hp->h_ref_count;
135
xtPublic void *xt_heap_rlock(XTThreadPtr th, void *mem)
137
XTHeapPtr hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
139
return xt_r_lock(th, &hp->h_lock) ? mem : NULL;
142
xtPublic void *xt_heap_wlock(XTThreadPtr self, void *mem)
144
XTHeapPtr hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
146
return xt_w_lock(self, &hp->h_lock) ? mem : NULL;
149
xtPublic void xt_heap_unlock(XTThreadPtr self, void *mem)
151
XTHeapPtr hp = (XTHeapPtr) ((char *) mem - offsetof(XTHeapRec, x.h_data));
153
xt_rw_unlock(self, &hp->h_lock);