File: TSS_Object.cpp

package info (click to toggle)
kwave 0.7.2-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 9,048 kB
  • ctags: 4,906
  • sloc: cpp: 31,275; ansic: 13,111; sh: 9,511; perl: 2,724; makefile: 786; asm: 145
file content (162 lines) | stat: -rw-r--r-- 5,130 bytes parent folder | download
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
/***************************************************************************
 TSS_Object.cpp  - base class for usage of TSS (supports asynchronous exits)
			     -------------------
    begin                : Sun Oct 01 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"
#include <errno.h>
#include <error.h>        // for strerror
#include <qapplication.h> // for qDebug() and qWarning()
#include <pthread.h>
#include <stdio.h>
#include <limits.h>       // for PTHREAD_KEYS_MAX

#include "mt/TSS_Object.h"

#if 0

### this would require -liberty on SuSE-9.0, or no special linker flag
    on all other distributions.
    so I decided to avoid trouble and leave this feature completely out!
    maybe I re-introduce it in v0.7.2 (THE, 2004-05-11)

#ifdef HAVE_DEMANGLE_H
extern "C" {
#include <demangle.h>
}
#endif // HAVE_DEMANGLE_H

#ifdef HAVE_TYPEINFO
#include <typeinfo>
#endif // HAVE_TYPEINFO

//***************************************************************************

/**
 * Cleanup handler function for the TSS_Object class.
 * @param ptr pointer to a TSS_Object instance
 */
extern "C" void TSS_Object_cleanup_func(void *ptr)
{
    fprintf(stderr, "cleanup handler for %p\n", ptr);
    Q_ASSERT(ptr);
    if (!ptr) {
	qWarning("cleanup handler for NULL pointer ?  => bailing out!");
	return;
    }

#ifdef HAVE_TYPEINFO
//    try {
	TSS_Object *tssobj = (TSS_Object *)ptr;
	const char *obj_name = typeid(*tssobj).name();

	if (obj_name) {
	    /* try to demangle the symbol name */
#ifdef HAVE_DEMANGLE_H
            char *res = cplus_demangle(obj_name,
	                               DMGL_ANSI | DMGL_PARAMS | DMGL_AUTO);
	    if (res) {
		/* use the damangeled name instead */
		qWarning("cleanup handler for class %s", res);
		free(res);
	    } else {
#endif // HAVE_DEMANGLE_H
		qWarning("cleanup handler for class `%s'", obj_name);
#ifdef HAVE_DEMANGLE_H
	    }
#endif // HAVE_DEMANGLE_H
	} else {
	    qWarning("cleanup handler for unknown class `%s'", obj_name);
	}

	if (tssobj) delete tssobj;
//    }
//    catch (...) {
//	qWarning("cleanup handler for %p failed", ptr);
//    }
#else // HAVE_TYPEINFO
//    qWarning("cleanup handler for %p", ptr);

    qWarning("cleanup handler for %s", ((QObject*)ptr)->className());

#endif // HAVE_TYPEINFO

}

//***************************************************************************
// static initializers
unsigned int TSS_Object::m_count(0); // for number of instances

//static Mutex _lock;
#endif /* 0 */

//***************************************************************************
TSS_Object::TSS_Object()
   :m_key(0), m_thread(pthread_self())
{
//    _lock.lock(); // ###
//    m_count++;
//
//    int res = pthread_key_create(&m_key, TSS_Object_cleanup_func);
//    if (res == EAGAIN) {
//	// number of keys exceeded
//	qWarning("TSS_Object: keycreate failed: "
//	    "number of keys exceeded limit: %d (limit=%d)",
//	    m_count, PTHREAD_KEYS_MAX);
//	qDebug("[maybe too many unfreed objects or memory leak?]");
//    } else if (res) {
//	// some other error
//	qWarning("TSS_Object: keycreate failed: %s", strerror(res));
//    } else {
//	// key allocated, associate this object's instance with it
//	res = pthread_setspecific(m_key, (void *)this);
//	if (res) qWarning("TSS_Object::setspecific failed: %s",
//	    strerror(res));
//    }
//
//    qDebug("TSS_Object::TSS_Object():  this=%p, tid=%d, key=%p, count=%d",
//	this, pthread_self(), m_key, m_count);
//
//    _lock.unlock(); // ###
}

//***************************************************************************
TSS_Object::~TSS_Object()
{
//    _lock.lock(); // ###
//
//    Q_ASSERT(pthread_getspecific(m_key) != 0);
//    Q_ASSERT(pthread_getspecific(m_key) == (void *)this);
//    Q_ASSERT(m_thread == pthread_self());
//
//    qDebug("TSS_Object::~TSS_Object(): this=%p, tid=%d, key=%p, count=%d",
//	this, pthread_self(), m_key, m_count);
//
////    pthread_setspecific(m_key, 0);
//
//    int res = pthread_key_delete(m_key);
//    if (res) qWarning(
//	"TSS_Object::~TSS_Object: key deletion failed: %s",
//	strerror(res));
//
//    Q_ASSERT(m_count != 0);
//    m_count--;
//
//    _lock.unlock(); // ###
}

//***************************************************************************
//***************************************************************************
/* end of TSS_Object.cpp */