diff --git a/CMakeLists.txt b/CMakeLists.txt index 689784e..fc781f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,5 +7,5 @@ project(AQ C) include_directories(${PROJECT_SOURCE_DIR}) -add_subdirectory(aqvm) -add_subdirectory(aq) \ No newline at end of file +add_subdirectory(aq) +add_subdirectory(aqvm) \ No newline at end of file diff --git a/aqvm/base/file/file.c b/aqvm/base/file/file.c index d8a294a..1213dd1 100644 --- a/aqvm/base/file/file.c +++ b/aqvm/base/file/file.c @@ -47,12 +47,12 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) { return -1; } if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_inputMutex) != 0) { + if (AqvmBaseThreadingMutex_LockMutex(AqvmBaseIo_inputMutex) != 0) { // TODO return -2; } } else if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_outputMutex) != 0) { + if (AqvmBaseThreadingMutex_LockMutex(AqvmBaseIo_outputMutex) != 0) { // TODO return -3; } @@ -72,12 +72,12 @@ int AqvmBaseFile_UnlockStream(struct AqvmBaseFile_File* stream) { return -1; } if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_inputMutex) != 0) { + if (AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseIo_inputMutex) != 0) { // TODO return -2; } } else if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_outputMutex) != 0) { + if (AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseIo_outputMutex) != 0) { // TODO return -3; } diff --git a/aqvm/base/file/read_write_lock/read_write_lock.c b/aqvm/base/file/read_write_lock/read_write_lock.c index 7c3e8e2..99627f3 100644 --- a/aqvm/base/file/read_write_lock/read_write_lock.c +++ b/aqvm/base/file/read_write_lock/read_write_lock.c @@ -4,29 +4,33 @@ #include "aqvm/base/file/read_write_lock/read_write_lock.h" -int AqvmBaseFileReadWriteLock_InitializeReadWriteLock( - struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) { +struct AqvmBaseFileReadWriteLock_ReadWriteLock* +AqvmBaseFileReadWriteLock_CreateReadWriteLock() { + struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock = + (struct AqvmBaseFileReadWriteLock_ReadWriteLock*)malloc( + sizeof(struct AqvmBaseFileReadWriteLock_ReadWriteLock)); if (read_write_lock == NULL) { // TODO - return -1; + return NULL; } - - if (AqvmBaseThreadingMutex_InitializeMutex(&read_write_lock->mutex) != 0) { + if (AqvmBaseThreadingMutex_CreateMutex(&read_write_lock->mutex) == NULL) { // TODO - return -2; + return NULL; } read_write_lock->read_count = 0; return 0; } -int AqvmBaseFileReadWriteLock_CloseReadWriteLock( +int AqvmBaseFileReadWriteLock_DestroyReadWriteLock( struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) { if (read_write_lock == NULL) { // TODO return -1; } - if (AqvmBaseThreadingMutex_CloseMutex(&read_write_lock->mutex) != 0) { + int result = AqvmBaseThreadingMutex_DestoryMutex(&read_write_lock->mutex); + free(read_write_lock); + if (result != 0) { // TODO return -2; } diff --git a/aqvm/base/file/read_write_lock/read_write_lock.h b/aqvm/base/file/read_write_lock/read_write_lock.h index d13aa30..2ee52dc 100644 --- a/aqvm/base/file/read_write_lock/read_write_lock.h +++ b/aqvm/base/file/read_write_lock/read_write_lock.h @@ -13,10 +13,10 @@ struct AqvmBaseFileReadWriteLock_ReadWriteLock { int read_count; }; -int AqvmBaseFileReadWriteLock_InitializeReadWriteLock( - struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock); +struct AqvmBaseFileReadWriteLock_ReadWriteLock* +AqvmBaseFileReadWriteLock_CreateReadWriteLock(); -int AqvmBaseFileReadWriteLock_CloseReadWriteLock( +int AqvmBaseFileReadWriteLock_DestroyReadWriteLock( struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock); int AqvmBaseFileReadWriteLock_LockReadLock( diff --git a/aqvm/base/hash/CMakeLists.txt b/aqvm/base/hash/CMakeLists.txt index cd1a3a6..2f7034b 100644 --- a/aqvm/base/hash/CMakeLists.txt +++ b/aqvm/base/hash/CMakeLists.txt @@ -8,8 +8,10 @@ include_directories(${PROJECT_SOURCE_DIR}) set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hash.c) +add_subdirectory(map) add_subdirectory(table) add_library(AqvmBaseHash STATIC ${SOURCES}) +target_link_libraries(AqvmBaseHash PRIVATE AqvmBaseHashMap) target_link_libraries(AqvmBaseHash PRIVATE AqvmBaseHashTable) \ No newline at end of file diff --git a/aqvm/base/hash/map/CMakeLists.txt b/aqvm/base/hash/map/CMakeLists.txt new file mode 100644 index 0000000..341bb75 --- /dev/null +++ b/aqvm/base/hash/map/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright 2024 AQ author, All Rights Reserved. +# This program is licensed under the AQ License. You can find the AQ license in +# the root directory. + +cmake_minimum_required(VERSION 3.10) + +include_directories(${PROJECT_SOURCE_DIR}) + +set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/map.c) + +add_library(AqvmBaseHashMap STATIC ${SOURCES}) + +target_link_libraries(AqvmBaseHashMap PRIVATE AqvmBaseHash) +target_link_libraries(AqvmBaseHashMap PRIVATE AqvmBaseHashTable) \ No newline at end of file diff --git a/aqvm/base/hash/map/map.c b/aqvm/base/hash/map/map.c new file mode 100644 index 0000000..1d6e8b9 --- /dev/null +++ b/aqvm/base/hash/map/map.c @@ -0,0 +1,10 @@ +// Copyright 2024 AQ author, All Rights Reserved. +// This program is licensed under the AQ License. You can find the AQ license in +// the root directory. + +#include "aqvm/base/hash/map/map.h" + +#include +#include + +#include "aqvm/base/hash/table/table.h" \ No newline at end of file diff --git a/aqvm/base/hash/map/map.h b/aqvm/base/hash/map/map.h new file mode 100644 index 0000000..3d44e99 --- /dev/null +++ b/aqvm/base/hash/map/map.h @@ -0,0 +1,22 @@ +// Copyright 2024 AQ author, All Rights Reserved. +// This program is licensed under the AQ License. You can find the AQ license in +// the root directory. + +#ifndef AQ_AQVM_BASE_HASH_TABLE_TABLE_H_ +#define AQ_AQVM_BASE_HASH_TABLE_TABLE_H_ + +#include + +#include "aqvm/base/hash/table/table.h" + +struct AqvmBaseHashMap_HashMap { + size_t capacity; + size_t size; + struct AqvmBaseHashTable_HashTable* data; +}; + +struct AqvmBaseHashMap_HashMap* AqvmBaseHashMap_CreateHashMap(size_t capacity); + +int AqvmBaseHashMap_DestroyHashMap(struct AqvmBaseHashMap_HashMap* hash_map); + +#endif \ No newline at end of file diff --git a/aqvm/base/io/io.c b/aqvm/base/io/io.c index eadb933..3ba00fc 100644 --- a/aqvm/base/io/io.c +++ b/aqvm/base/io/io.c @@ -10,8 +10,8 @@ #include "aqvm/base/file/file.h" -AqvmBaseThreadingMutex_Mutex AqvmBaseIo_inputMutex; -AqvmBaseThreadingMutex_Mutex AqvmBaseIo_outputMutex; +AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_inputMutex; +AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_outputMutex; struct AqvmBaseFile_File AqvmBaseIo_stdoutStream; struct AqvmBaseFile_File* AqvmBaseIo_stdout = &AqvmBaseIo_stdoutStream; @@ -27,23 +27,22 @@ int AqvmBaseIo_InitializeIo() { AqvmBaseIo_stdinStream.file = stdin; AqvmBaseIo_stderrStream.file = stderr; - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_inputMutex) != 0) { + AqvmBaseIo_inputMutex = AqvmBaseThreadingMutex_CreateMutex(); + AqvmBaseIo_outputMutex = AqvmBaseThreadingMutex_CreateMutex(); + if (AqvmBaseIo_inputMutex == NULL || AqvmBaseIo_outputMutex == NULL) { // TODO return -1; } - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_outputMutex) != 0) { - // TODO - return -2; - } + return 0; } int AqvmBaseIo_CloseIo() { - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_inputMutex) != 0) { + if (AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseIo_inputMutex) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_outputMutex) != 0) { + if (AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseIo_outputMutex) != 0) { // TODO return -1; } diff --git a/aqvm/base/io/io.h b/aqvm/base/io/io.h index 715c102..a873604 100644 --- a/aqvm/base/io/io.h +++ b/aqvm/base/io/io.h @@ -11,8 +11,8 @@ #include "aqvm/base/file/file.h" #include "aqvm/base/threading/mutex/mutex.h" -extern AqvmBaseThreadingMutex_Mutex AqvmBaseIo_inputMutex; -extern AqvmBaseThreadingMutex_Mutex AqvmBaseIo_outputMutex; +extern AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_inputMutex; +extern AqvmBaseThreadingMutex_Mutex* AqvmBaseIo_outputMutex; extern struct AqvmBaseFile_File* AqvmBaseIo_stdout; extern struct AqvmBaseFile_File* AqvmBaseIo_stdin; diff --git a/aqvm/base/threading/mutex/mutex.c b/aqvm/base/threading/mutex/mutex.c index 611105f..a4fa4b9 100644 --- a/aqvm/base/threading/mutex/mutex.c +++ b/aqvm/base/threading/mutex/mutex.c @@ -4,52 +4,45 @@ #include "aqvm/base/threading/mutex/mutex.h" -int AqvmBaseThreadingMutex_InitializeMutex( - AqvmBaseThreadingMutex_Mutex* mutex) { - if (mutex == NULL) { - // TODO - return -1; - } - +AqvmBaseThreadingMutex_Mutex* AqvmBaseThreadingMutex_CreateMutex() { #ifdef __unix__ - if (AqvmBaseThreadingMutexUnix_InitializeMutex(mutex) != 0) { - // TODO - return -2; - } - return 0; + return AqvmBaseThreadingMutexUnix_CreateMutex(); #elif _WIN32 - if (AqvmBaseThreadingMutexWindows_InitializeMutex(mutex) != 0) { - // TODO - return -2; - } - return 0; + return AqvmBaseThreadingMutexWindows_CreateMutex(); #else // TODO(Threading): When Threading is developed, rewrite that code. + AqvmBaseThreadingMutex_Mutex* mutex = (AqvmBaseThreadingMutex_Mutex*)malloc( + sizeof(AqvmBaseThreadingMutex_Mutex)); + if (mutex == NULL) { + // TODO + return NULL; + } *mutex = false; - return 0; + return mutex; #endif } -int AqvmBaseThreadingMutex_CloseMutex(AqvmBaseThreadingMutex_Mutex* mutex) { +int AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseThreadingMutex_Mutex* mutex) { if (mutex == NULL) { // TODO return -1; } #ifdef __unix__ - if (AqvmBaseThreadingMutexUnix_CloseMutex(mutex) != 0) { + if (AqvmBaseThreadingMutexUnix_DestroyMutex(mutex) != 0) { // TODO return -2; } return 0; #elif _WIN32 - if (AqvmBaseThreadingMutexWindows_CloseMutex(mutex) != 0) { + if (AqvmBaseThreadingMutexWindows_DestroyMutex(mutex) != 0) { // TODO return -2; } return 0; #else // TODO(Threading): When Threading is developed, rewrite that code. + free(mutex); return 0; #endif } diff --git a/aqvm/base/threading/mutex/mutex.h b/aqvm/base/threading/mutex/mutex.h index 6ea221c..cd40ed5 100644 --- a/aqvm/base/threading/mutex/mutex.h +++ b/aqvm/base/threading/mutex/mutex.h @@ -20,14 +20,14 @@ typedef AqvmBaseThreadingMutexWindows_Mutex AqvmBaseThreadingMutex_Mutex; typedef bool AqvmBaseThreadingMutex_Mutex; #endif -int AqvmBaseThreadingMutex_InitializeMutex(AqvmBaseThreadingMutex_Mutex *mutex); +AqvmBaseThreadingMutex_Mutex* AqvmBaseThreadingMutex_CreateMutex(); -int AqvmBaseThreadingMutex_CloseMutex(AqvmBaseThreadingMutex_Mutex *mutex); +int AqvmBaseThreadingMutex_DestroyMutex(AqvmBaseThreadingMutex_Mutex* mutex); -int AqvmBaseThreadingMutex_LockMutex(AqvmBaseThreadingMutex_Mutex *mutex); +int AqvmBaseThreadingMutex_LockMutex(AqvmBaseThreadingMutex_Mutex* mutex); -int AqvmBaseThreadingMutex_TryLockMutex(AqvmBaseThreadingMutex_Mutex *mutex); +int AqvmBaseThreadingMutex_TryLockMutex(AqvmBaseThreadingMutex_Mutex* mutex); -int AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseThreadingMutex_Mutex *mutex); +int AqvmBaseThreadingMutex_UnlockMutex(AqvmBaseThreadingMutex_Mutex* mutex); #endif \ No newline at end of file diff --git a/aqvm/base/threading/mutex/unix/mutex.c b/aqvm/base/threading/mutex/unix/mutex.c index 75eab4f..0f472fd 100644 --- a/aqvm/base/threading/mutex/unix/mutex.c +++ b/aqvm/base/threading/mutex/unix/mutex.c @@ -5,22 +5,23 @@ #include "aqvm/base/threading/mutex/unix/mutex.h" -int AqvmBaseThreadingMutexUnix_InitializeMutex( - AqvmBaseThreadingMutexUnix_Mutex *mutex) { +AqvmBaseThreadingMutexUnix_Mutex *AqvmBaseThreadingMutexUnix_CreateMutex() { + AqvmBaseThreadingMutexUnix_Mutex *mutex = + (AqvmBaseThreadingMutexUnix_Mutex *)malloc( + sizeof(AqvmBaseThreadingMutexUnix_Mutex)); if (mutex == NULL) { // TODO - return -1; + return NULL; } - int result = pthread_mutex_init(mutex, NULL); if (result != 0) { // TODO - return -2; + return NULL; } - return 0; + return mutex; } -int AqvmBaseThreadingMutexUnix_CloseMutex( +int AqvmBaseThreadingMutexUnix_DestroyMutex( AqvmBaseThreadingMutexUnix_Mutex *mutex) { if (mutex == NULL) { // TODO @@ -28,6 +29,7 @@ int AqvmBaseThreadingMutexUnix_CloseMutex( } int result = pthread_mutex_destroy(mutex); + free(mutex); if (result != 0) { // TODO return -2; diff --git a/aqvm/base/threading/mutex/unix/mutex.h b/aqvm/base/threading/mutex/unix/mutex.h index 51892c2..85da079 100644 --- a/aqvm/base/threading/mutex/unix/mutex.h +++ b/aqvm/base/threading/mutex/unix/mutex.h @@ -10,11 +10,10 @@ typedef pthread_mutex_t AqvmBaseThreadingMutexUnix_Mutex; -int AqvmBaseThreadingMutexUnix_InitializeMutex( - AqvmBaseThreadingMutexUnix_Mutex* mutex); +AqvmBaseThreadingMutexUnix_Mutex *AqvmBaseThreadingMutexUnix_CreateMutex(); -int AqvmBaseThreadingMutexUnix_CloseMutex( - AqvmBaseThreadingMutexUnix_Mutex* mutex); +int AqvmBaseThreadingMutexUnix_DestroyMutex( + AqvmBaseThreadingMutexUnix_Mutex *mutex); int AqvmBaseThreadingMutexUnix_LockMutex( AqvmBaseThreadingMutexUnix_Mutex *mutex); diff --git a/aqvm/base/threading/mutex/windows/mutex.c b/aqvm/base/threading/mutex/windows/mutex.c index f10471a..d139ee9 100644 --- a/aqvm/base/threading/mutex/windows/mutex.c +++ b/aqvm/base/threading/mutex/windows/mutex.c @@ -8,38 +8,40 @@ #include #include -int AqvmBaseThreadingMutexWindows_InitializeMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex) { +AqvmBaseThreadingMutexWindows_Mutex* +AqvmBaseThreadingMutexWindows_CreateMutex() { + AqvmBaseThreadingMutexWindows_Mutex* mutex = + (AqvmBaseThreadingMutexWindows_Mutex*)malloc( + sizeof(AqvmBaseThreadingMutexWindows_Mutex)); if (mutex == NULL) { // TODO - return -1; + return NULL; } - *mutex = CreateMutex(NULL, FALSE, NULL); - if (mutex == NULL) { + if (*mutex == NULL) { // TODO - return -2; + return NULL; } return 0; } -int AqvmBaseThreadingMutexWindows_CloseMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex) { - if (mutex == NULL || *mutex == NULL) { +int AqvmBaseThreadingMutexWindows_DestroyMutex( + AqvmBaseThreadingMutexWindows_Mutex* mutex) { + if (mutex == NULL) { // TODO return -1; } - if (!CloseHandle(*mutex)) { + if (*mutex != NULL && !CloseHandle(*mutex)) { // TODO return -2; } - *mutex = NULL; + free(mutex); return 0; } int AqvmBaseThreadingMutexWindows_LockMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex) { + AqvmBaseThreadingMutexWindows_Mutex* mutex) { if (mutex == NULL || *mutex == NULL) { // TODO return -1; @@ -54,7 +56,7 @@ int AqvmBaseThreadingMutexWindows_LockMutex( } int AqvmBaseThreadingMutexWindows_TryLockMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex) { + AqvmBaseThreadingMutexWindows_Mutex* mutex) { if (mutex == NULL || *mutex == NULL) { // TODO return -1; @@ -69,7 +71,7 @@ int AqvmBaseThreadingMutexWindows_TryLockMutex( } int AqvmBaseThreadingMutexWindows_UnlockMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex) { + AqvmBaseThreadingMutexWindows_Mutex* mutex) { if (mutex == NULL || *mutex == NULL) { // TODO return -1; diff --git a/aqvm/base/threading/mutex/windows/mutex.h b/aqvm/base/threading/mutex/windows/mutex.h index db846bb..779db41 100644 --- a/aqvm/base/threading/mutex/windows/mutex.h +++ b/aqvm/base/threading/mutex/windows/mutex.h @@ -10,20 +10,20 @@ typedef HANDLE AqvmBaseThreadingMutexWindows_Mutex; -int AqvmBaseThreadingMutexWindows_InitializeMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex); +AqvmBaseThreadingMutexWindows_Mutex* +AqvmBaseThreadingMutexWindows_CreateMutex(); -int AqvmBaseThreadingMutexWindows_CloseMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex); +int AqvmBaseThreadingMutexWindows_DestroyMutex( + AqvmBaseThreadingMutexWindows_Mutex* mutex); int AqvmBaseThreadingMutexWindows_LockMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex); + AqvmBaseThreadingMutexWindows_Mutex* mutex); int AqvmBaseThreadingMutexWindows_TryLockMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex); + AqvmBaseThreadingMutexWindows_Mutex* mutex); int AqvmBaseThreadingMutexWindows_UnlockMutex( - AqvmBaseThreadingMutexWindows_Mutex *mutex); + AqvmBaseThreadingMutexWindows_Mutex* mutex); #endif #endif \ No newline at end of file