progress on base

This commit is contained in:
2025-02-01 19:08:54 -05:00
parent 102f5af2c7
commit e65974a9b2
12 changed files with 156 additions and 50 deletions
+15
View File
@@ -0,0 +1,15 @@
// base outline
// intended for "As-Is" library usage
#include "base/arch.h"
#include "base/compiler.h"
#include "base/cstd.h"
#include "base/macros.h"
MD_NS_BEGIN
#include "base/base_types.h"
#include "base/memory.h"
#include "base/zpl_memory.h"
MD_NS_END
@@ -1,9 +1,13 @@
#ifdef MD_INTELLISENSE_DIRECTIVES
#pragma once
#include "base_types.h"
#include
#include "strings.h"
#endif
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef BASE_COMMAND_LINE_H
#define BASE_COMMAND_LINE_H
////////////////////////////////
//~ rjf: Parsed Command Line Types
+83
View File
@@ -0,0 +1,83 @@
#ifdef MD_INTELLISENSE_DIRECTIVES
#pragma once
#include "macros.h"
#endif
////////////////////////////////
//~ rjf: Units
#define KB(n) (((U64)(n)) << 10)
#define MB(n) (((U64)(n)) << 20)
#define GB(n) (((U64)(n)) << 30)
#define TB(n) (((U64)(n)) << 40)
#define thousand(n) ((n) * 1000)
#define million(n) ((n) * 1000000)
#define billion(n) ((n) * 1000000000)
////////////////////////////////
//~ rjf: Clamps, Mins, Maxes
#define min(A,B) (((A) < (B)) ? (A) : (B))
#define max(A,B) (((A) > (B)) ? (A) : (B))
#define clamp_top(A,X) Min(A, X)
#define clamp_bot(X,B) Max(X, B)
#define clamp(A,X,B) (((X) < (A)) ? (A) : ((X) > (B)) ? (B) : (X))
////////////////////////////////
//~ rjf: Type -> Alignment
#if MD_COMPILER_MSVC
# define align_of(T) __alignof(T)
#elif MD_COMPILER_CLANG
# define align_of(T) __alignof(T)
#elif MD_COMPILER_GCC
# define align_of(T) __alignof__(T)
#else
# error AlignOf not defined for this compiler.
#endif
////////////////////////////////
//~ rjf: Member Offsets
#define member(T, m) ( ((T*) 0)->m )
#define offset_of(T, m) int_from_ptr(& member(T, m))
#define member_from_offset(T, ptr, off) (T) ((((U8 *) ptr) + (off)))
#define cast_from_member(T, m, ptr) (T*) (((U8*)ptr) - offset_of(T, m))
////////////////////////////////
//~ rjf: For-Loop Construct Macros
#define defer_loop(begin, end) for(int _i_ = ((begin), 0); ! _i_; _i_ += 1, (end))
#define defer_loop_checked(begin, end) for(int _i_ = 2 * ! (begin); (_i_ == 2 ? ((end), 0) : !_i_); _i_ += 1, (end))
#define each_enum_val(type, it) type it = (type) 0; it < type ## _COUNT; it = (type)( it + 1 )
#define each_non_zero_enum_val(type, it) type it = (type) 1; it < type ## _COUNT; it = (type)( it + 1 )
////////////////////////////////
//~ rjf: Memory Operation Macros
#define memory_copy(dst, src, size) memmove((dst), (src), (size))
#define memory_set(dst, byte, size) memset((dst), (byte), (size))
#define memory_compare(a, b, size) memcmp((a), (b), (size))
#define memory_str_len(ptr) cstr_len(ptr)
#define memory_copy_struct(d,s) memory_copy((d),(s),sizeof(*(d)))
#define memory_copy_array(d,s) memory_copy((d),(s),sizeof(d))
#define memory_copy_type(d,s,c) memory_copy((d),(s),sizeof(*(d))*(c))
#define memory_zero(s,z) mem_set((s),0,(z))
#define memory_zero_struct(s) memory_zero((s),sizeof(*(s)))
#define memory_zero_array(a) memroy_zero((a),sizeof(a))
#define memory_zero_type(m,c) memroy_zero((m),sizeof(*(m))*(c))
#define memory_match(a,b,z) (memory_compare((a),(b),(z)) == 0)
#define memory_match_struct(a,b) memory_match((a),(b),sizeof(*(a)))
#define memory_match_array(a,b) memory_match((a),(b),sizeof(a))
#define memory_read(T,p,e) ( ((p)+sizeof(T)<=(e))?(*(T*)(p)):(0) )
#define memory_consume(T,p,e) ( ((p)+sizeof(T)<=(e))?((p)+=sizeof(T),*(T*)((p)-sizeof(T))):((p)=(e),0) )
-12
View File
@@ -1,12 +0,0 @@
#if MD_INTELLISENSE_DIRECTIVES
#pragma once
#include "base_types.h"
#endif
typedef struct String8 String8;
struct String8
{
U8 *str;
U64 size;
};
@@ -1,3 +1,8 @@
#ifdef MD_INTELLISENSE_DIRECTIVES
#pragma once
#include "base/strings.h"
#endif
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
@@ -1,9 +1,12 @@
#ifdef MD_INTELLISENSE_DIRECTIVES
#pragma once
#include "macros.h"
#include "base_types.h"
#endif
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef BASE_STRINGS_H
#define BASE_STRINGS_H
////////////////////////////////
//~ rjf: Third Party Includes
+5
View File
@@ -3,6 +3,11 @@
#include "base_types.h"
#endif
// This provides an alterntive memory strategy to HMH/Casey Muratori/RJF styled arenas
// The library is derived from zpl-c which in-turn
// is related to the gb headers an thus the Odin-lang memory strategy
// Users can override the underlying memory allocator used, even for the HMH arena memory strategy.
#define MD_KILOBYTES( x ) ( ( x ) * ( S64 )( 1024 ) )
#define MD_MEGABYTES( x ) ( MD_KILOBYTES( x ) * ( S64 )( 1024 ) )
#define MD_GIGABYTES( x ) ( MD_MEGABYTES( x ) * ( S64 )( 1024 ) )
View File
View File
+30 -27
View File
@@ -1,6 +1,7 @@
#ifdef MD_INTELLISENSE_DIRECTIVES
#pragma once
#include
#include "base/macros.h"
#include "base/base_types.h"
#endif
// Copyright (c) 2024 Epic Games Tools
@@ -31,7 +32,7 @@
int pthread_setname_np(pthread_t thread, const char *name);
int pthread_getname_np(pthread_t thread, char *name, size_t size);
typedef struct tm tm;
typedef struct tm tm;
typedef struct timespec timespec;
////////////////////////////////
@@ -40,9 +41,9 @@ typedef struct timespec timespec;
typedef struct OS_LNX_FileIter OS_LNX_FileIter;
struct OS_LNX_FileIter
{
DIR *dir;
struct dirent *dp;
String8 path;
DIR* dir;
struct dirent* dp;
String8 path;
};
StaticAssert(sizeof(Member(OS_FileIter, memory)) >= sizeof(OS_LNX_FileIter), os_lnx_file_iter_size_check);
@@ -52,8 +53,8 @@ StaticAssert(sizeof(Member(OS_FileIter, memory)) >= sizeof(OS_LNX_FileIter), os_
typedef struct OS_LNX_SafeCallChain OS_LNX_SafeCallChain;
struct OS_LNX_SafeCallChain
{
OS_LNX_SafeCallChain *next;
OS_ThreadFunctionType *fail_handler;
OS_LNX_SafeCallChain* next;
OS_ThreadFunctionType* fail_handler;
void *ptr;
};
@@ -72,21 +73,23 @@ OS_LNX_EntityKind;
typedef struct OS_LNX_Entity OS_LNX_Entity;
struct OS_LNX_Entity
{
OS_LNX_Entity *next;
OS_LNX_Entity* next;
OS_LNX_EntityKind kind;
union
{
struct
{
pthread_t handle;
OS_ThreadFunctionType *func;
void *ptr;
pthread_t handle;
OS_ThreadFunctionType* func;
void* ptr;
} thread;
pthread_mutex_t mutex_handle;
pthread_mutex_t mutex_handle;
pthread_rwlock_t rwmutex_handle;
struct
{
pthread_cond_t cond_handle;
pthread_cond_t cond_handle;
pthread_mutex_t rwlock_mutex_handle;
} cv;
};
@@ -98,14 +101,14 @@ struct OS_LNX_Entity
typedef struct OS_LNX_State OS_LNX_State;
struct OS_LNX_State
{
Arena *arena;
OS_SystemInfo system_info;
OS_ProcessInfo process_info;
Arena* arena;
OS_SystemInfo system_info;
OS_ProcessInfo process_info;
pthread_mutex_t entity_mutex;
Arena *entity_arena;
OS_LNX_Entity *entity_free;
Arena* entity_arena;
OS_LNX_Entity* entity_free;
};
k
////////////////////////////////
//~ rjf: Globals
@@ -115,18 +118,18 @@ thread_static OS_LNX_SafeCallChain *os_lnx_safe_call_chain = 0;
////////////////////////////////
//~ rjf: Helpers
internal DateTime os_lnx_date_time_from_tm(tm in, U32 msec);
internal tm os_lnx_tm_from_date_time(DateTime dt);
internal timespec os_lnx_timespec_from_date_time(DateTime dt);
internal DenseTime os_lnx_dense_time_from_timespec(timespec in);
internal DateTime os_lnx_date_time_from_tm(tm in, U32 msec);
internal tm os_lnx_tm_from_date_time(DateTime dt);
internal timespec os_lnx_timespec_from_date_time( DateTime dt);
internal DenseTime os_lnx_dense_time_from_timespec(timespec in);
internal FileProperties os_lnx_file_properties_from_stat(struct stat *s);
internal void os_lnx_safe_call_sig_handler(int x);
internal void os_lnx_safe_call_sig_handler (int x);
k
////////////////////////////////
//~ rjf: Entities
internal OS_LNX_Entity *os_lnx_entity_alloc(OS_LNX_EntityKind kind);
internal void os_lnx_entity_release(OS_LNX_Entity *entity);
internal OS_LNX_Entity* os_lnx_entity_alloc(OS_LNX_EntityKind kind);
internal void os_lnx_entity_release(OS_LNX_Entity *entity);
////////////////////////////////
//~ rjf: Thread Entry Point
+5 -5
View File
@@ -1,9 +1,11 @@
#ifdef MD_INTELLISENSE_DIRECTIVES
#pragma once
#include "base/base_types.h"
#endif
// Copyright (c) 2024 Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef OS_CORE_H
#define OS_CORE_H
////////////////////////////////
//~ rjf: System Info
@@ -332,5 +334,3 @@ internal OS_Guid os_make_guid(void);
#if BUILD_ENTRY_DEFINING_UNIT
internal void entry_point(CmdLine *cmdline);
#endif
#endif // OS_CORE_H