Skip to content

Commit

Permalink
extract pdl_pthread_realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Dec 16, 2021
1 parent e24b2cf commit 5e85c08
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
44 changes: 24 additions & 20 deletions Basic/Core/pdlmagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ static int done_pdl_main_pthreadID_init = 0;
* altogether later
*/
static char* pdl_pthread_barf_msgs = NULL;
static int pdl_pthread_barf_msgs_len = 0;
static size_t pdl_pthread_barf_msgs_len = 0;
static char* pdl_pthread_warn_msgs = NULL;
static int pdl_pthread_warn_msgs_len = 0;
static size_t pdl_pthread_warn_msgs_len = 0;

#endif

Expand Down Expand Up @@ -408,39 +408,26 @@ char pdl_pthread_main_thread() {
int pdl_pthread_barf_or_warn(const char* pat, int iswarn, va_list *args)
{
char** msgs;
int* len;
size_t* len;

/* Don't do anything if we are in the main pthread */
if (pdl_pthread_main_thread()) return 0;

if(iswarn)
{
msgs = &pdl_pthread_warn_msgs;
len = &pdl_pthread_warn_msgs_len;
len = &pdl_pthread_warn_msgs_len;
}
else
{
msgs = &pdl_pthread_barf_msgs;
len = &pdl_pthread_barf_msgs_len;
len = &pdl_pthread_barf_msgs_len;
}

/* In the chunk I'm adding I need to store the actual data and trailing newline so add 1. */
int extralen = vsnprintf(NULL, 0, pat, *args) + 1;
size_t extralen = vsnprintf(NULL, 0, pat, *args) + 1;
pdl_pthread_realloc((void **)msgs, *len + extralen + 1);
// add the new complaint to the list
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex );
/* (For windows, we first #undef realloc
so that the system realloc function is used instead of the PerlMem_realloc
macro. This currently works fine, though could conceivably require some
tweaking in the future if it's found to cause any problem.) */
#ifdef WIN32
#undef realloc
#endif
/* FIXME: Common realloc mistake: 'msgs' nulled but not freed upon failure */
*msgs = realloc(*msgs, *len + extralen + 1);
pthread_mutex_unlock( &mutex );
}
vsnprintf( *msgs + *len, extralen + 1, pat, *args);
/* update the length-so-far. This does NOT include the trailing '\0' */
*len += extralen;
Expand All @@ -459,6 +446,23 @@ int pdl_pthread_barf_or_warn(const char* pat, int iswarn, va_list *args)
return 0;
}

/* void ** so update the possibly-global pointer within mutex */
void *pdl_pthread_realloc(void **p, size_t len) {
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex );
/* (For windows, we first #undef realloc
so that the system realloc function is used instead of the PerlMem_realloc
macro. This currently works fine, though could conceivably require some
tweaking in the future if it's found to cause any problem.) */
#ifdef WIN32
#undef realloc
#endif
/* FIXME: Common realloc mistake: 'msgs' nulled but not freed upon failure */
*p = realloc(*p, len);
pthread_mutex_unlock( &mutex );
return *p;
}

/* copied from [email protected]:git/git.git 2.34-ish thread-util.c */
/* changed GIT_WINDOWS_NATIVE to WIN32 */
#if defined(hpux) || defined(__hpux) || defined(_hpux)
Expand Down
1 change: 1 addition & 0 deletions Basic/Core/pdlmagic.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pdl_trans *pdl_find_mutatedtrans(pdl *it);
/* Deferred barfing and warning when pthreading */
char pdl_pthread_main_thread();
int pdl_pthread_barf_or_warn(const char* pat, int iswarn, va_list *args);
void *pdl_pthread_realloc(void **p, size_t len);

pdl_error pdl_add_threading_magic(pdl *,PDL_Indx nthdim, PDL_Indx nthreads);

Expand Down

0 comments on commit 5e85c08

Please sign in to comment.