Skip to content

Commit

Permalink
simplify os impl more
Browse files Browse the repository at this point in the history
  • Loading branch information
apache-hb committed Apr 1, 2024
1 parent 64f950e commit aa92669
Show file tree
Hide file tree
Showing 53 changed files with 747 additions and 495 deletions.
37 changes: 6 additions & 31 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project('cthulhu', 'c',
license : 'LGPLv3',
version : '0.2.8',
license : 'LGPL-3.0-only AND GPL-3.0-only',
version : '0.2.9',
license_files : 'LICENSE',
meson_version : '>=1.3.0',
default_options : [
Expand Down Expand Up @@ -93,35 +93,6 @@ if unit_tests.enabled() and is_release and cc.get_id() == 'gcc'
warning('enabling unit tests in release mode with gcc will result in degraded performance')
endif

version = meson.project_version()
host = host_machine.system()
parts = version.split('.')

source_dir = meson.project_source_root() / 'src'
data_dir = meson.project_source_root() / 'data'

config_cdata = configuration_data()

config_cdata.set('CTU_MAJOR', parts[0].to_int())
config_cdata.set('CTU_MINOR', parts[1].to_int())
config_cdata.set('CTU_PATCH', parts[2].to_int())

config_cdata.set10('CTU_DEBUG', is_debug)
config_cdata.set10('CTU_PARANOID', opt_paranoid.allowed())
config_cdata.set10('CTU_BUILD_SHARED', default_library == 'shared')

# we use `__attribute__((const|pure))` heavily, but during unit tests we
# violate function preconditions to test assertions.
# msvc and clang dont mind, but gcc does. so we disable the attributes for gcc
config_cdata.set10('CTU_DISABLE_FN_PURITY', unit_tests.allowed() and cc.get_id() == 'gcc')

# gcc warns about __PRETY_FUNCTION__ not being ISO C when warning_leve=3
# which is weird seeing as we specify gnu11, maybe its a gcc bug?
config_cdata.set10('CTU_HAS_PRETTY_FUNCTION', warning_level < 3)

config_cdata.set10('CTU_TRACE_MEMORY', trace_memory.allowed())
config_cdata.set10('CTU_STB_SPRINTF', opt_stb_sprintf.allowed())

# args for all code
all_args = cc.get_supported_arguments(
# we use flexible array members
Expand Down Expand Up @@ -218,6 +189,7 @@ add_project_arguments(all_args, language : [ 'c' ])
lexargs = []
parseargs = []

host = host_machine.system()
# setup required flex/bison args
if host == 'windows'
lexargs += [ '--wincompat' ]
Expand Down Expand Up @@ -270,6 +242,9 @@ endif
doxygen = find_program('doxygen', required : get_option('doxygen'))

if doxygen.found()
source_dir = meson.project_source_root() / 'src'
data_dir = meson.project_source_root() / 'data'

doxy_sources = [
source_dir / 'common',
source_dir / 'cthulhu',
Expand Down
2 changes: 1 addition & 1 deletion src/common/core/data/module_api.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "core/compiler.h" // IWYU pragma: export

#if CTU_BUILD_SHARED
#if CT_BUILD_SHARED
# if CT_@MOD@_BUILD
# define CT_@MOD@_API CT_EXPORT
# else
Expand Down
2 changes: 2 additions & 0 deletions src/common/core/include/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
#define CT_INNER_STR(x) #x
#define CT_STR(x) CT_INNER_STR(x)

#define CT_VERSION CT_STR(CTU_MAJOR) "." CT_STR(CTU_MINOR) "." CT_STR(CTU_PATCH)

/// @defgroup ansi_colour ANSI Colour macros
/// @brief ANSI escape string colour macros
/// @ingroup core
Expand Down
9 changes: 9 additions & 0 deletions src/common/core/include/ctu_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#define CT_BUILDTYPE_SHARED 1
#define CT_BUILDTYPE_STATIC 2

#include <ctu_core_config.h>

#define CT_BUILD_SHARED (CTU_BUILDTYPE == CT_BUILDTYPE_SHARED)
#define CT_BUILD_STATIC (CTU_BUILDTYPE == CT_BUILDTYPE_STATIC)
28 changes: 27 additions & 1 deletion src/common/core/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
version = meson.project_version()
parts = version.split('.')

config_cdata = configuration_data()

config_cdata.set('CTU_MAJOR', parts[0].to_int())
config_cdata.set('CTU_MINOR', parts[1].to_int())
config_cdata.set('CTU_PATCH', parts[2].to_int())

config_cdata.set10('CTU_DEBUG', is_debug)
config_cdata.set10('CTU_PARANOID', opt_paranoid.allowed())

if default_library == 'shared'
config_cdata.set('CTU_BUILDTYPE', 'CT_BUILDTYPE_SHARED')
else
config_cdata.set('CTU_BUILDTYPE', 'CT_BUILDTYPE_STATIC')
endif

# we use `__attribute__((const|pure))` heavily, but during unit tests we
# violate function preconditions to test assertions.
# msvc and clang dont mind, but gcc does. so we disable the attributes for gcc
config_cdata.set10('CTU_DISABLE_FN_PURITY', unit_tests.allowed() and cc.get_id() == 'gcc')

config_cdata.set10('CTU_TRACE_MEMORY', trace_memory.allowed())
config_cdata.set10('CTU_STB_SPRINTF', opt_stb_sprintf.allowed())

ctu_config_header = configure_file(
output : 'ctu_config.h',
output : 'ctu_core_config.h',
configuration : config_cdata
)

Expand Down
8 changes: 6 additions & 2 deletions src/common/fs/include/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "os/core.h"

#include "fs/impl.h"

#include <stdbool.h>

CT_BEGIN_API
Expand All @@ -21,6 +19,10 @@ typedef struct vector_t vector_t;
/// @ingroup common
/// @{

typedef struct fs_t fs_t;
typedef struct fs_inode_t fs_inode_t;
typedef struct fs_iter_t fs_iter_t;

/// @brief delete a filesystem handle
///
/// @param fs the filesystem to delete
Expand Down Expand Up @@ -128,6 +130,8 @@ typedef void (*fs_dirent_callback_t)(const char *path, const char *name, os_dire

CT_FS_API void fs_iter_dirents(fs_t *fs, const char *path, void *data, fs_dirent_callback_t callback);

CT_FS_API bool fs_inode_is(IN_NOTNULL const fs_inode_t *inode, os_dirent_t type);

CT_FS_API os_error_t fs_iter_begin(
IN_NOTNULL fs_t *fs,
IN_STRING const char *path,
Expand Down
39 changes: 0 additions & 39 deletions src/common/fs/include/fs/impl.h

This file was deleted.

11 changes: 9 additions & 2 deletions src/common/fs/src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ fs_inode_t gInvalidFileNode = {
.type = eOsNodeNone
};

static fs_inode_t *inode_new(os_dirent_t type, const void *data, size_t size, arena_t *arena)
fs_inode_t *inode_new(os_dirent_t type, const void *data, size_t size, arena_t *arena)
{
CTASSERT(type < eOsNodeCount);
CT_ASSERT_RANGE(type, 0, eOsNodeCount - 1);

fs_inode_t *inode = ARENA_MALLOC(sizeof(fs_inode_t) + size, "inode", NULL, arena);
inode->type = type;
Expand All @@ -43,6 +43,13 @@ void *inode_data(fs_inode_t *inode)
return inode->data;
}

void *iter_data(fs_iter_t *iter)
{
CTASSERT(iter != NULL);

return iter->data;
}

bool inode_is(fs_inode_t *inode, os_dirent_t type)
{
CTASSERT(inode != NULL);
Expand Down
26 changes: 26 additions & 0 deletions src/common/fs/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
typedef struct map_t map_t;
typedef struct arena_t arena_t;

typedef struct fs_inode_t
{
os_dirent_t type;
char data[];
} fs_inode_t;

typedef struct fs_iter_t
{
fs_t *fs;
fs_inode_t *current;
char data[];
} fs_iter_t;

typedef struct inode_result_t
{
fs_inode_t *node;
Expand All @@ -25,6 +38,10 @@ typedef os_error_t (*fs_file_delete_t)(fs_t *fs, fs_inode_t *node, const char *n
typedef inode_result_t (*fs_dir_create_t)(fs_t *fs, fs_inode_t *node, const char *name);
typedef os_error_t (*fs_dir_delete_t)(fs_t *fs, fs_inode_t *node, const char *name);

typedef os_error_t (*fs_iter_begin_t)(fs_t *fs, fs_inode_t *dir, fs_iter_t *iter);
typedef os_error_t (*fs_iter_next_t)(fs_iter_t *iter);
typedef os_error_t (*fs_iter_end_t)(fs_iter_t *iter);

/// @brief fs callback to delete the fs
///
/// @param fs the fs to delete
Expand All @@ -43,6 +60,12 @@ typedef struct fs_callbacks_t

fs_file_create_t pfn_create_file;
fs_file_delete_t pfn_delete_file;

fs_iter_begin_t pfn_iter_begin;
fs_iter_next_t pfn_iter_next;
fs_iter_end_t pfn_iter_end;

size_t iter_size;
} fs_callbacks_t;

typedef struct fs_t
Expand All @@ -60,10 +83,13 @@ CT_LOCAL extern fs_inode_t gInvalidFileNode;

CT_LOCAL fs_inode_t *inode_file(const void *data, size_t size, arena_t *arena);
CT_LOCAL fs_inode_t *inode_dir(const void *data, size_t size, arena_t *arena);
CT_LOCAL fs_inode_t *inode_new(os_dirent_t type, const void *data, size_t size, arena_t *arena);

CT_LOCAL void *inode_data(fs_inode_t *inode);
CT_LOCAL bool inode_is(fs_inode_t *inode, os_dirent_t type);

CT_LOCAL void *iter_data(fs_iter_t *iter);

// helpers
CT_LOCAL os_error_t mkdir_recursive(const char *path, arena_t *arena);

Expand Down
Loading

0 comments on commit aa92669

Please sign in to comment.