From d66900f78a4ad9fe9271896a84aa4afe74e7ddfd Mon Sep 17 00:00:00 2001 From: alexpeshkoff Date: Fri, 5 Sep 2008 13:15:24 +0000 Subject: [PATCH] Refactored code saving strings in remote.cpp --- src/remote/remote.cpp | 74 +++++++------------------------------------ 1 file changed, 12 insertions(+), 62 deletions(-) diff --git a/src/remote/remote.cpp b/src/remote/remote.cpp index 4db1c18893..343b776953 100644 --- a/src/remote/remote.cpp +++ b/src/remote/remote.cpp @@ -35,6 +35,7 @@ #include "../jrd/gds_proto.h" #include "../jrd/thread_proto.h" #include "../common/config/config.h" +#include "../common/classes/init.h" #ifdef DEV_BUILD Firebird::AtomicCounter rem_port::portCounter = 0; @@ -47,8 +48,8 @@ IMPLEMENT_TRACE_ROUTINE(remote_trace, "REMOTE") const SLONG DUMMY_INTERVAL = 60; /* seconds */ const int ATTACH_FAILURE_SPACE = 2048; /* bytes */ -static TEXT* attach_failures = NULL; -static TEXT* attach_failures_ptr; +static Firebird::StringsBuffer* attachFailures = NULL; +static Firebird::GlobalPtr attachFailuresMutex; static void cleanup_memory(void*); @@ -567,69 +568,19 @@ void REMOTE_save_status_strings( ISC_STATUS* vector) * strings to a special buffer. * **************************************/ - if (!attach_failures) + Firebird::MutexLockGuard guard(attachFailuresMutex); + + if (!attachFailures) { - attach_failures = - (TEXT*) gds__alloc((SLONG) ATTACH_FAILURE_SPACE); + attachFailures = FB_NEW(*getDefaultMemoryPool()) Firebird::CircularStringsBuffer; /* FREE: freed by exit handler cleanup_memory() */ - if (!attach_failures) /* NOMEM: don't bother trying to copy */ + if (!attachFailures) /* NOMEM: don't bother trying to copy */ return; -#ifdef DEBUG_GDS_ALLOC - /* This buffer is freed by the exit handler - but some of the - * reporting mechanisms will report it anyway, so flag it as - * "known unfreed" to get the reports quiet. - */ - gds_alloc_flag_unfreed((void*) attach_failures); -#endif /* DEBUG_GDS_ALLOC */ - attach_failures_ptr = attach_failures; + gds__register_cleanup(cleanup_memory, 0); } - TEXT* p; - USHORT l = 0; // silence non initialized warning - while (*vector) - { - const ISC_STATUS status = *vector++; - switch (status) - { - case isc_arg_cstring: - l = (USHORT) *vector++; - - case isc_arg_interpreted: - case isc_arg_string: - case isc_arg_sql_state: - p = (TEXT*) *vector; - if (status != isc_arg_cstring) - l = strlen(p) + 1; - - // if string too long, truncate it - if (l > ATTACH_FAILURE_SPACE / 4) - l = ATTACH_FAILURE_SPACE / 4; - - /* If there isn't any more room in the buffer, - start at the beginning again */ - - if (attach_failures_ptr + l > attach_failures + ATTACH_FAILURE_SPACE) - attach_failures_ptr = attach_failures; - - // copy data - memcpy(attach_failures_ptr, p, l); - - // ensure string is correctly terminated - if (status != isc_arg_cstring) - attach_failures_ptr[l - 1] = 0; - else - vector[-1] = l; - - *vector++ = (ISC_STATUS) attach_failures_ptr; - attach_failures_ptr += l; - break; - - default: - ++vector; - break; - } - } + attachFailures->makePermanentVector(vector, vector); } @@ -646,11 +597,10 @@ static void cleanup_memory( void *block) * **************************************/ - if (attach_failures) - gds__free(attach_failures); + delete attachFailures; + attachFailures = NULL; gds__unregister_cleanup(cleanup_memory, 0); - attach_failures = NULL; }