$BCXSTRSIZE posible bug in BCX_TmpStr ???

Started by jbk, June 23, 2024, 05:05:54 PM

Previous topic - Next topic

Robert

Quote from: jbk on June 23, 2024, 09:18:23 PM
sorry MrBcx, I thought that I was careful about my wording
my test program is not working, so I give up

Hi jbk:

Don't give up. Post the broken code. Maybe someone here can see a fix.

MrBcx

Quote from: jbk on June 23, 2024, 09:18:23 PM
sorry MrBcx, I thought that I was careful about my wording
my test program is not working, so I give up

JB,

Do be mindful that $BCXSTRSIZE affects statically dimensioned strings ( from the stack )
as well as dynamically dimensioned strings ( from the heap ). 

jbk

sorry MrBcx, I thought that I was careful about my wording
my test program is not working, so I give up

MrBcx

Quote from: jbk on June 23, 2024, 05:05:54 PM

I am a bit suspicious of the BCX_TmpStr function but I don't understand what exactly the function is doing, would you explain?

I'm a bit suspicious of coders who do not stay current on versions, who don't read my latest entries in the revision list,
and who cast aspersions on the efforts of others before checking themselves.

Reference:  https://bcxbasiccoders.com/smf/index.php?topic=1077.0

REVISIONS:

*********************************************************************************************
2024/06/06       Changes in 8.1.0 from 8.0.9
*********************************************************************************************
Robert Wishlaw : Reported bug with ISFILE() function.  MrBcx corrected/improved/re-tested
                 the ISFILE(), ISFOLDER(), ISREADONLY(), ISHIDDEN() functions.

Kevin Diggins  : Renamed runtime function BCX_TmpStr() to BCX_TempStr().  BCX_TempStr() is now
                 thread-safe after adding a critical section into it.  By doing so, all BCX
                 string functions that utilize BCX_TempStr() inherit some level of thread
                 safety too, depending upon each function's individual implementation.



Reference: From BC.Bas



SUB RunTimeFunctions (FP_WRITE AS FILE)
    FPRINT FP_WRITE, ""
    FPRINT FP_WRITE, "// *************************************************"
    FPRINT FP_WRITE, "//                 ";$BCX_STR_RUNTIME
    FPRINT FP_WRITE, "// *************************************************"
    FPRINT FP_WRITE, ""

    IF Use_BcxTempStr THEN
        '=========================================================================
        ' BCX_TempStr maintains a circular array of 2048 string buffers.  Each
        ' time it's called, it advances the index (StrCnt), frees any previously
        ' allocated memory at that index, allocates a new buffer of the requested
        ' size, and returns a pointer to this buffer. This approach helps manage
        ' memory efficiently and avoids frequently allocating and deallocating
        ' temporary strings.  Using EnterCriticalSection and LeaveCriticalSection
        ' ensures that only one thread can execute the critical section at a time,
        ' making the function thread-safe.  In addition, every BCX string function
        ' that utilizes BCX_TempStr() inherits some level of thread safety also,
        ' depending upon each function's implementation.
        '=========================================================================
        FPRINT FP_WRITE, "char *BCX_TempStr(size_t iBytes)"
        FPRINT FP_WRITE, "{"
        FPRINT FP_WRITE, "  static int StrCnt;"
        FPRINT FP_WRITE, "  static char *StrFunc[2048];"
        FPRINT FP_WRITE, "  static CRITICAL_SECTION cs;"
        FPRINT FP_WRITE, "  static int initialized = 0;"
        FPRINT FP_WRITE, "  // Initialize the critical section once"
        FPRINT FP_WRITE, "  if (!initialized) {"
        FPRINT FP_WRITE, "    InitializeCriticalSection(&cs);"
        FPRINT FP_WRITE, "    initialized = 1;"
        FPRINT FP_WRITE, "  }"
        FPRINT FP_WRITE, "  EnterCriticalSection(&cs);"
        FPRINT FP_WRITE, "  StrCnt = (StrCnt + 1) & 2047;"
        FPRINT FP_WRITE, "  if (StrFunc[StrCnt]) {"
        FPRINT FP_WRITE, "    free(StrFunc[StrCnt]);"
        FPRINT FP_WRITE, "    StrFunc[StrCnt] = NULL;"
        FPRINT FP_WRITE, "  }"
        FPRINT FP_WRITE, "  StrFunc[StrCnt] = (char*)calloc(iBytes + 1, sizeof(char));"
        FPRINT FP_WRITE, "  if (!StrFunc[StrCnt]) {"
        FPRINT FP_WRITE, "    LeaveCriticalSection(&cs);"
        FPRINT FP_WRITE, "    return NULL; // Allocation failed"
        FPRINT FP_WRITE, "  }"
        FPRINT FP_WRITE, "  LeaveCriticalSection(&cs);"
        FPRINT FP_WRITE, "  return StrFunc[StrCnt];"
        FPRINT FP_WRITE, "}\n\n"
    END IF





jbk

#3
I will post after some more testing, it seems that it's stack too small but also a problem with BCX_TmpStr
not 100% sure but I think that the only problem was too small a stack

Robert

Quote from: jbk on June 23, 2024, 05:05:54 PM
I noticed while trying to run some benchmarks using my decfloat routines that changing $BCXSTRSIZE to a high value has no effect from preventing a crash when trying to print a very big string.
if the string length exceeds 16383 or so then there's crash no matter what value I assign to $BCXSTRSIZE
I am a bit suspicious of the BCX_TmpStr function but I don't understand what exactly the function is doing, would you explain?

My S.W.A.G. is that you wore the wrong pair of socks.

Example please.

jbk

#1
I noticed while trying to run some benchmarks using my decfloat routines that changing $BCXSTRSIZE to a high value has no effect from preventing a crash when trying to print a very big string.
if the string length exceeds 16383 or so then there's crash no matter what value I assign to $BCXSTRSIZE
I am a bit suspicious of the BCX_TmpStr function but I don't understand what exactly the function is doing, would you explain?