From 1e57e07926ef6919757ffa4dd53d2e9d7073a5ee Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Tue, 10 Mar 2009 23:56:47 +0000 Subject: [PATCH] - Fixed a double-fclose and memory leaks in zipdir. - Fixed building of LZMA SDK on Linux with some help from p7zip. SVN r1469 (trunk) --- lzma/C/Threads.c | 179 +++++++++++++++++++++++++++++++++++++ lzma/C/Threads.h | 85 ++++++++++++++++-- src/files.cpp | 2 +- src/g_shared/a_weapons.cpp | 2 +- tools/zipdir/zipdir.c | 11 ++- wadsrc/CMakeLists.txt | 2 +- 6 files changed, 268 insertions(+), 13 deletions(-) diff --git a/lzma/C/Threads.c b/lzma/C/Threads.c index 160218312..64d871abe 100644 --- a/lzma/C/Threads.c +++ b/lzma/C/Threads.c @@ -4,6 +4,8 @@ Igor Pavlov Public domain */ #include "Threads.h" + +#ifdef _WIN32 #include static WRes GetError() @@ -111,3 +113,180 @@ WRes CriticalSection_Init(CCriticalSection *p) return 0; } +#else + +#include +#include + +#include + +#if defined(__linux__) +#define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP +#endif + + +WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), void *parameter) +{ + pthread_attr_t attr; + int ret; + + thread->_created = 0; + + ret = pthread_attr_init(&attr); + if (ret) return ret; + + ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE); + if (ret) return ret; + + ret = pthread_create(&thread->_tid, &attr, (void * (*)(void *))startAddress, parameter); + + /* ret2 = */ pthread_attr_destroy(&attr); + + if (ret) return ret; + + thread->_created = 1; + + return 0; // SZ_OK; +} + +WRes Thread_Wait(CThread *thread) +{ + void *thread_return; + int ret; + + if (thread->_created == 0) + return EINVAL; + + ret = pthread_join(thread->_tid,&thread_return); + thread->_created = 0; + + return ret; +} + +WRes Thread_Close(CThread *thread) +{ + if (!thread->_created) return SZ_OK; + + pthread_detach(thread->_tid); + thread->_tid = 0; + thread->_created = 0; + return SZ_OK; +} + +WRes Event_Create(CEvent *p, BOOL manualReset, int initialSignaled) +{ + pthread_mutex_init(&p->_mutex,0); + pthread_cond_init(&p->_cond,0); + p->_manual_reset = manualReset; + p->_state = (initialSignaled ? TRUE : FALSE); + p->_created = 1; + return 0; +} + +WRes Event_Set(CEvent *p) { + pthread_mutex_lock(&p->_mutex); + p->_state = TRUE; + pthread_cond_broadcast(&p->_cond); + pthread_mutex_unlock(&p->_mutex); + return 0; +} + +WRes Event_Reset(CEvent *p) { + pthread_mutex_lock(&p->_mutex); + p->_state = FALSE; + pthread_mutex_unlock(&p->_mutex); + return 0; +} + +WRes Event_Wait(CEvent *p) { + pthread_mutex_lock(&p->_mutex); + while (p->_state == FALSE) + { + pthread_cond_wait(&p->_cond, &p->_mutex); + } + if (p->_manual_reset == FALSE) + { + p->_state = FALSE; + } + pthread_mutex_unlock(&p->_mutex); + return 0; +} + +WRes Event_Close(CEvent *p) { + if (p->_created) + { + p->_created = 0; + pthread_mutex_destroy(&p->_mutex); + pthread_cond_destroy(&p->_cond); + } + return 0; +} + +WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount) +{ + pthread_mutex_init(&p->_mutex,0); + pthread_cond_init(&p->_cond,0); + p->_count = initiallyCount; + p->_maxCount = maxCount; + p->_created = 1; + return 0; +} + +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount) +{ + UInt32 newCount; + + if (releaseCount < 1) return EINVAL; + + pthread_mutex_lock(&p->_mutex); + + newCount = p->_count + releaseCount; + if (newCount > p->_maxCount) + { + pthread_mutex_unlock(&p->_mutex); + return EINVAL; + } + p->_count = newCount; + pthread_cond_broadcast(&p->_cond); + pthread_mutex_unlock(&p->_mutex); + return 0; +} + +WRes Semaphore_Wait(CSemaphore *p) { + pthread_mutex_lock(&p->_mutex); + while (p->_count < 1) + { + pthread_cond_wait(&p->_cond, &p->_mutex); + } + p->_count--; + pthread_mutex_unlock(&p->_mutex); + return 0; +} + +WRes Semaphore_Close(CSemaphore *p) { + if (p->_created) + { + p->_created = 0; + pthread_mutex_destroy(&p->_mutex); + pthread_cond_destroy(&p->_cond); + } + return 0; +} + +WRes CriticalSection_Init(CCriticalSection * lpCriticalSection) +{ + return pthread_mutex_init(&(lpCriticalSection->_mutex),0); +} + +WRes ManualResetEvent_Create(CManualResetEvent *p, int initialSignaled) + { return Event_Create(p, TRUE, initialSignaled); } + +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *p) + { return ManualResetEvent_Create(p, 0); } + +WRes AutoResetEvent_Create(CAutoResetEvent *p, int initialSignaled) + { return Event_Create(p, FALSE, initialSignaled); } +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p) + { return AutoResetEvent_Create(p, 0); } + +#endif diff --git a/lzma/C/Threads.h b/lzma/C/Threads.h index 4235c44c2..c895c7e51 100644 --- a/lzma/C/Threads.h +++ b/lzma/C/Threads.h @@ -6,13 +6,7 @@ #ifdef _WIN32 #include -#endif - -#ifdef _WIN32 typedef DWORD WRes; -#else -typedef int WRes; -#endif #include "Types.h" @@ -74,5 +68,84 @@ WRes CriticalSection_Init(CCriticalSection *p); #define CriticalSection_Enter(p) EnterCriticalSection(p) #define CriticalSection_Leave(p) LeaveCriticalSection(p) +#else +typedef int WRes; + +#include "Types.h" +#include + +/* #define DEBUG_SYNCHRO 1 */ + +typedef struct _CThread +{ + pthread_t _tid; + int _created; + +} CThread; + +#define Thread_Construct(thread) (thread)->_created = 0 +#define Thread_WasCreated(thread) ((thread)->_created != 0) + +typedef unsigned THREAD_FUNC_RET_TYPE; +#define THREAD_FUNC_CALL_TYPE +#define THREAD_FUNC_DECL THREAD_FUNC_RET_TYPE THREAD_FUNC_CALL_TYPE + +WRes Thread_Create(CThread *thread, THREAD_FUNC_RET_TYPE (THREAD_FUNC_CALL_TYPE *startAddress)(void *), void *parameter); +WRes Thread_Wait(CThread *thread); +WRes Thread_Close(CThread *thread); + +typedef struct _CEvent +{ + int _created; + int _manual_reset; + int _state; + pthread_mutex_t _mutex; + pthread_cond_t _cond; +} CEvent; + +typedef CEvent CAutoResetEvent; +typedef CEvent CManualResetEvent; + +#define Event_Construct(event) (event)->_created = 0 +#define Event_IsCreated(event) ((event)->_created) + +WRes ManualResetEvent_Create(CManualResetEvent *event, int initialSignaled); +WRes ManualResetEvent_CreateNotSignaled(CManualResetEvent *event); +WRes AutoResetEvent_Create(CAutoResetEvent *event, int initialSignaled); +WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *event); +WRes Event_Set(CEvent *event); +WRes Event_Reset(CEvent *event); +WRes Event_Wait(CEvent *event); +WRes Event_Close(CEvent *event); + + +typedef struct _CSemaphore +{ + int _created; + UInt32 _count; + UInt32 _maxCount; + pthread_mutex_t _mutex; + pthread_cond_t _cond; +} CSemaphore; + +#define Semaphore_Construct(p) (p)->_created = 0 + +WRes Semaphore_Create(CSemaphore *p, UInt32 initiallyCount, UInt32 maxCount); +WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 num); +#define Semaphore_Release1(p) Semaphore_ReleaseN(p, 1) +WRes Semaphore_Wait(CSemaphore *p); +WRes Semaphore_Close(CSemaphore *p); + +typedef struct { + pthread_mutex_t _mutex; +} CCriticalSection; + +WRes CriticalSection_Init(CCriticalSection *p); +#define CriticalSection_Delete(p) pthread_mutex_destroy(&((p)->_mutex)) +#define CriticalSection_Enter(p) pthread_mutex_lock(&((p)->_mutex)) +#define CriticalSection_Leave(p) pthread_mutex_unlock(&((p)->_mutex)) + +#endif + #endif diff --git a/src/files.cpp b/src/files.cpp index 08e261d1b..6d5beacbb 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -378,7 +378,7 @@ FileReaderLZMA::FileReaderLZMA (FileReader &file, size_t uncompressed_size, bool OutProcessed = 0; // Read zip LZMA properties header - if (File.Read(header, sizeof(header)) < sizeof(header)) + if (File.Read(header, sizeof(header)) < (long)sizeof(header)) { I_Error("FileReaderLZMA: File too shart\n"); } diff --git a/src/g_shared/a_weapons.cpp b/src/g_shared/a_weapons.cpp index a7d83aa78..04854182d 100644 --- a/src/g_shared/a_weapons.cpp +++ b/src/g_shared/a_weapons.cpp @@ -1300,7 +1300,7 @@ void FWeaponSlots::AddSlot(int slot, const PClass *type, bool feedback) { if (type != NULL && !Slots[slot].AddWeapon(type) && feedback) { - Printf ("Could not add %s to slot %zu\n", type->TypeName.GetChars(), slot); + Printf ("Could not add %s to slot %d\n", type->TypeName.GetChars(), slot); } } diff --git a/tools/zipdir/zipdir.c b/tools/zipdir/zipdir.c index 251fb50f2..fbe4d2b0b 100644 --- a/tools/zipdir/zipdir.c +++ b/tools/zipdir/zipdir.c @@ -632,7 +632,11 @@ file_sorted_t *sort_files(dir_tree_t *trees, int num_files) void write_zip(const char *zipname, dir_tree_t *trees, int update) { +#ifdef _WIN32 char tempname[_MAX_PATH]; +#else + char tempname[PATH_MAX]; +#endif EndOfCentralDirectory dirend; int i, num_files; file_sorted_t *sorted; @@ -730,10 +734,6 @@ void write_zip(const char *zipname, dir_tree_t *trees, int update) tempname, zipname, strerror(errno)); } } - else - { - fclose(zip); - } } } free(sorted); @@ -838,10 +838,12 @@ int append_to_zip(FILE *zip_file, file_sorted_t *filep, FILE *ozip, BYTE *odir) i = copy_zip_file(zip_file, file, ozip, dirent); if (i > 0) { + free(readbuf); return 0; } if (i < 0) { + free(readbuf); fprintf(stderr, "Unable to write %s to zip\n", file->path); return 1; } @@ -1426,6 +1428,7 @@ int copy_zip_file(FILE *zip, file_entry_t *file, FILE *ozip, CentralDirectoryEnt free(buf); return -1; } + free(buf); file->date = lfh.ModDate; file->time = lfh.ModTime; file->uncompressed_size = LittleLong(lfh.UncompressedSize); diff --git a/wadsrc/CMakeLists.txt b/wadsrc/CMakeLists.txt index cfe0739c4..1cb85974a 100644 --- a/wadsrc/CMakeLists.txt +++ b/wadsrc/CMakeLists.txt @@ -6,7 +6,7 @@ add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/generated/dehsupp.lmp DEPENDS dehsupp ${CMAKE_CURRENT_SOURCE_DIR}/sources/dehsupp.txt ) add_custom_command( OUTPUT ${ZDOOM_OUTPUT_DIR}/zdoom.pk3 - COMMAND ${CMAKE_BINARY_DIR}/tools/zipdir/zipdir -u ${ZDOOM_OUTPUT_DIR}/zdoom.pk3 ${CMAKE_CURRENT_SOURCE_DIR}/static ${CMAKE_CURRENT_BINARY_DIR}/generated + COMMAND ${CMAKE_BINARY_DIR}/tools/zipdir/zipdir -uf ${ZDOOM_OUTPUT_DIR}/zdoom.pk3 ${CMAKE_CURRENT_SOURCE_DIR}/static ${CMAKE_CURRENT_BINARY_DIR}/generated DEPENDS zipdir ${CMAKE_CURRENT_BINARY_DIR}/generated/dehsupp.lmp ${CMAKE_CURRENT_SOURCE_DIR}/static ) add_custom_target( pk3 ALL