#include "replace.h"
#include <talloc.h>
#include "lib/util/talloc_stack.h"
-#include "lib/util/smb_threads.h"
-#include "lib/util/smb_threads_internal.h"
#include "lib/util/fault.h"
#include "lib/util/debug.h"
TALLOC_CTX **talloc_stack;
};
-/*
- * In the single threaded case this is a pointer
- * to the global talloc_stackframe. In the MT-case
- * this is the pointer to the thread-specific key
- * used to look up the per-thread talloc_stackframe
- * pointer.
- */
+static struct talloc_stackframe *global_ts;
-static void *global_ts;
+static struct talloc_stackframe *talloc_stackframe_get_existing(void)
+{
+ struct talloc_stackframe *ts = NULL;
-/* Variable to ensure TLS value is only initialized once. */
-static smb_thread_once_t ts_initialized = SMB_THREAD_ONCE_INIT;
+ ts = global_ts;
-static void talloc_stackframe_init(void * unused)
-{
- if (SMB_THREAD_CREATE_TLS("talloc_stackframe", global_ts)) {
- smb_panic("talloc_stackframe_init create_tls failed");
- }
+ return ts;
}
-static struct talloc_stackframe *talloc_stackframe_create(void)
+static struct talloc_stackframe *talloc_stackframe_get(void)
{
+ struct talloc_stackframe *ts = talloc_stackframe_get_existing();
+
+ if (ts != NULL) {
+ return ts;
+ }
+
#if defined(PARANOID_MALLOC_CHECKER)
#ifdef calloc
#undef calloc
#endif
#endif
- struct talloc_stackframe *ts = (struct talloc_stackframe *)calloc(
+ ts = (struct talloc_stackframe *)calloc(
1, sizeof(struct talloc_stackframe));
#if defined(PARANOID_MALLOC_CHECKER)
#define calloc(n, s) __ERROR_DONT_USE_MALLOC_DIRECTLY
#endif
-
if (!ts) {
smb_panic("talloc_stackframe_init malloc failed");
}
- SMB_THREAD_ONCE(&ts_initialized, talloc_stackframe_init, NULL);
+ global_ts = ts;
- if (SMB_THREAD_SET_TLS(global_ts, ts)) {
- smb_panic("talloc_stackframe_init set_tls failed");
- }
return ts;
}
static int talloc_pop(TALLOC_CTX *frame)
{
- struct talloc_stackframe *ts =
- (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
+ struct talloc_stackframe *ts = talloc_stackframe_get_existing();
size_t blocks;
int i;
size_t poolsize)
{
TALLOC_CTX **tmp, *top;
- struct talloc_stackframe *ts =
- (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
-
- if (ts == NULL) {
- ts = talloc_stackframe_create();
- }
+ struct talloc_stackframe *ts = talloc_stackframe_get();
if (ts->talloc_stack_arraysize < ts->talloc_stacksize + 1) {
tmp = talloc_realloc(NULL, ts->talloc_stack, TALLOC_CTX *,
TALLOC_CTX *_talloc_tos(const char *location)
{
- struct talloc_stackframe *ts =
- (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
+ struct talloc_stackframe *ts = talloc_stackframe_get_existing();
if (ts == NULL || ts->talloc_stacksize == 0) {
- _talloc_stackframe(location);
- ts = (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
+ TALLOC_CTX *ret = _talloc_stackframe(location);
DEBUG(0, ("no talloc stackframe at %s, leaking memory\n",
location));
#ifdef DEVELOPER
smb_panic("No talloc stackframe");
#endif
+ return ret;
}
return ts->talloc_stack[ts->talloc_stacksize-1];
bool talloc_stackframe_exists(void)
{
- struct talloc_stackframe *ts =
- (struct talloc_stackframe *)SMB_THREAD_GET_TLS(global_ts);
+ struct talloc_stackframe *ts = talloc_stackframe_get_existing();
if (ts == NULL || ts->talloc_stacksize == 0) {
return false;