Skip to content

Commit

Permalink
Temp save. Not complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Aug 8, 2024
1 parent 67ae939 commit ae1b11c
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 48 deletions.
29 changes: 11 additions & 18 deletions aqvm/base/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream) {
return -1;
}

int result = AqvmBaseThreadingMutex_LockMutex(&stream->mutex);
/*int result = AqvmBaseThreadingMutex_LockMutex(&stream->mutex);
if (result != 0) {
// TODO
return -2;
}
}*/
return 0;
}

Expand All @@ -32,11 +32,11 @@ int AqvmBaseFile_UnlockFile(struct AqvmBaseFile_File* stream) {
return -1;
}

int result = AqvmBaseThreadingMutex_UnlockMutex(&stream->mutex);
/*int result = AqvmBaseThreadingMutex_UnlockMutex(&stream->mutex);
if (result != 0) {
// TODO
return -2;
}
}*/
return 0;
}

Expand All @@ -60,10 +60,6 @@ int AqvmBaseFile_LockStream(struct AqvmBaseFile_File* stream) {
// TODO
return -4;
}
if (AqvmBaseFile_LockFile(stream) != 0) {
// TODO
return -5;
}
}

return 0;
Expand All @@ -89,10 +85,6 @@ int AqvmBaseFile_UnlockStream(struct AqvmBaseFile_File* stream) {
// TODO
return -4;
}
if (AqvmBaseFile_UnlockFile(stream) != 0) {
// TODO
return -5;
}
}

return 0;
Expand Down Expand Up @@ -131,10 +123,10 @@ int AqvmBaseFile_fclose(struct AqvmBaseFile_File* stream) {
}

int result = fclose(stream->file);
if (AqvmBaseThreadingMutex_CloseMutex(&stream->mutex)) {
/*if (AqvmBaseThreadingMutex_CloseMutex(&stream->mutex)) {
// TODO
return -2;
}
}*/

if (result != 0) {
// TODO
Expand Down Expand Up @@ -238,11 +230,11 @@ struct AqvmBaseFile_File* AqvmBaseFile_fopen(const char* filename,
return NULL;
}

if (AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex) != 0) {
/*if (AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex) != 0) {
fclose(stream->file);
free(stream);
return NULL;
}
}*/

return stream;
}
Expand Down Expand Up @@ -507,12 +499,13 @@ struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void) {
return NULL;
}

AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex);
/*AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex);*/

return stream;
}

char* AqvmBaseFile_tmpnam(char* str) { // TODO
char* AqvmBaseFile_tmpnam(char* str) {
// TODO
char* result = tmpnam(str);
if (result == NULL) {
// TODO
Expand Down
23 changes: 12 additions & 11 deletions aqvm/base/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include <stdbool.h>
#include <stdio.h>

#include "aqvm/base/threading/mutex/mutex.h"
#include "aqvm/base/file/identifier/identifier.h"
#include "aqvm/base/file/read_write_lock/read_write_lock.h"

struct AqvmBaseFile_File {
FILE* file;
AqvmBaseThreadingMutex_Mutex mutex;
AqvmBaseFileIdentifier_Identifier identifier;
struct AqvmBaseFileReadWriteLock_ReadWriteLock lock;
};

int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream);
Expand All @@ -38,24 +40,23 @@ int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream);
int AqvmBaseFile_fgetpos(struct AqvmBaseFile_File* stream, fpos_t* pos);

struct AqvmBaseFile_File* AqvmBaseFile_fopen(const char* filename,
const char* mode);
const char* mode);

size_t AqvmBaseFile_fread(void* ptr, size_t size, size_t nmemb,
struct AqvmBaseFile_File* stream);
struct AqvmBaseFile_File* stream);

struct AqvmBaseFile_File* AqvmBaseFile_freopen(const char* filename,
const char* mode,
struct AqvmBaseFile_File* stream);
struct AqvmBaseFile_File* AqvmBaseFile_freopen(
const char* filename, const char* mode, struct AqvmBaseFile_File* stream);

int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset,
int whence);
int whence);

int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos);

long int AqvmBaseFile_ftell(struct AqvmBaseFile_File* stream);

size_t AqvmBaseFile_fwrite(const void* ptr, size_t size, size_t nmemb,
struct AqvmBaseFile_File* stream);
struct AqvmBaseFile_File* stream);

int AqvmBaseFile_remove(const char* filename);

Expand All @@ -65,8 +66,8 @@ void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream);

void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer);

int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer, int mode,
size_t size);
int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer,
int mode, size_t size);

struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void);

Expand Down
11 changes: 11 additions & 0 deletions aqvm/base/file/read_write_lock/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 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}/read_write_lock.c)

add_library(AqvmBaseFileReadWriteLock STATIC ${SOURCES})
85 changes: 85 additions & 0 deletions aqvm/base/file/read_write_lock/read_write_lock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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/file/read_write_lock/read_write_lock.h"

int AqvmBaseFileReadWriteLock_InitializeReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
if (read_write_lock == NULL) {
// TODO
return -1;
}

if (AqvmBaseThreadingMutex_InitializeMutex(&read_write_lock->mutex) != 0) {
// TODO
return -2;
}
read_write_lock->read_count = 0;
return 0;
}

int AqvmBaseFileReadWriteLock_CloseReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
if (read_write_lock == NULL) {
// TODO
return -1;
}

if (AqvmBaseThreadingMutex_CloseMutex(&read_write_lock->mutex) != 0) {
// TODO
return -2;
}
return 0;
}

int AqvmBaseFileReadWriteLock_LockReadLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
if (read_write_lock == NULL) {
// TODO
return -1;
}

if (AqvmBaseThreadingMutex_LockMutex(&read_write_lock->mutex) != 0) {
// TODO
return -2;
}
++read_write_lock->read_count;
return 0;
}

int AqvmBaseFileReadWriteLock_LockWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
if (read_write_lock == NULL) {
// TODO
return -1;
}

if (AqvmBaseThreadingMutex_LockMutex(&read_write_lock->mutex) != 0) {
// TODO
return -2;
}
return 0;
}

int AqvmBaseFileReadWriteLock_UnlockLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock) {
if (read_write_lock == NULL) {
// TODO
return -1;
}

if (read_write_lock->read_count == 0 &&
AqvmBaseThreadingMutex_UnlockMutex(&read_write_lock->mutex) != 0) {
// TODO
return -2;
} else {
--read_write_lock->read_count;
if (read_write_lock->read_count == 0 &&
AqvmBaseThreadingMutex_UnlockMutex(&read_write_lock->mutex) != 0) {
// TODO
return -3;
}
}
return 0;
}
30 changes: 30 additions & 0 deletions aqvm/base/file/read_write_lock/read_write_lock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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_FILE_READ_WRITE_LOCK_READ_WRITE_LOCK_H_
#define AQ_AQVM_BASE_FILE_READ_WRITE_LOCK_READ_WRITE_LOCK_H_

#include "aqvm/base/threading/mutex/mutex.h"

struct AqvmBaseFileReadWriteLock_ReadWriteLock {
AqvmBaseThreadingMutex_Mutex mutex;
int read_count;
};

int AqvmBaseFileReadWriteLock_InitializeReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);

int AqvmBaseFileReadWriteLock_CloseReadWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);

int AqvmBaseFileReadWriteLock_LockReadLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);

int AqvmBaseFileReadWriteLock_LockWriteLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);

int AqvmBaseFileReadWriteLock_UnlockLock(
struct AqvmBaseFileReadWriteLock_ReadWriteLock* read_write_lock);

#endif
48 changes: 47 additions & 1 deletion aqvm/base/hash/table/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,50 @@
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "aqvm/base/hash/table/table.h"
#include "aqvm/base/hash/table/table.h"

#include <stddef.h>
#include <stdlib.h>

#include "aqvm/base/linked_list/linked_list.h"

int AqvmBaseHashTable_CloseHashTable(
struct AqvmBaseHashTable_HashTable* hash_table) {
if (hash_table == NULL) {
// TODO
return -1;
}

for (size_t i = 0; i < hash_table->capacity; i++) {
AqvmBaseLinkedList_CloseLinkedList(&hash_table->data[i]);
}
free(hash_table->data);
hash_table->data = NULL;
hash_table->capacity = 0;
hash_table->size = 0;
return 0;
}

int AqvmBaseHashTable_InitializeHashTable(
struct AqvmBaseHashTable_HashTable* hash_table, size_t capacity) {
if (hash_table == NULL || capacity == 0) {
// TODO
return -1;
}

hash_table->capacity = capacity;
hash_table->size = 0;
hash_table->data = (struct AqvmBaseLinkedList_LinkedList*)malloc(
sizeof(struct AqvmBaseLinkedList_LinkedList) * capacity);
if (hash_table->data == NULL) {
// TODO
return -2;
}
for (size_t i = 0; i < capacity; i++) {
if (AqvmBaseLinkedList_InitializeLinkedList(&hash_table->data[i])) {
// TODO
return -3;
}
}
return 0;
}
16 changes: 16 additions & 0 deletions aqvm/base/hash/table/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@
#ifndef AQ_AQVM_BASE_HASH_TABLE_TABLE_H_
#define AQ_AQVM_BASE_HASH_TABLE_TABLE_H_

#include <stddef.h>

#include "aqvm/base/linked_list/linked_list.h"

struct AqvmBaseHashTable_HashTable {
size_t capacity;
size_t size;
struct AqvmBaseLinkedList_LinkedList* data;
};

int AqvmBaseHashTable_CloseHashTable(
struct AqvmBaseHashTable_HashTable* hash_table);

int AqvmBaseHashTable_InitializeHashTable(
struct AqvmBaseHashTable_HashTable* hash_table, size_t capacity);

#endif
Loading

0 comments on commit ae1b11c

Please sign in to comment.