stuffs
authorhgn <hgodden00@gmail.com>
Thu, 1 May 2025 08:54:24 +0000 (09:54 +0100)
committerhgn <hgodden00@gmail.com>
Thu, 1 May 2025 08:54:24 +0000 (09:54 +0100)
src/vg/vg_store.h
vg_console.c
vg_console.h
vg_db.c
vg_db.h
vg_engine.c
vg_mem_pool.c
vg_string.c
vg_tool.c
vg_ui/imgui.c
vg_ui/imgui_impl_opengl.c

index 00a8540568996fb2933f2d21e6f114a20832bd9c..1baf20fa5d915c1e624702ce940f91a5d0ee18bb 100644 (file)
@@ -198,14 +198,14 @@ static aatree_ptr aatree_insert( aatree *tree, aatree_ptr t, aatree_ptr x )
    if( cmp_result <= 0 )
    {
       ptnode->left = aatree_insert( tree, ptnode->left, x );
-      aatree_node *plnode = aatree_get_node( tree, ptnode->left );
-      plnode->parent = t;
+      //aatree_node *plnode = aatree_get_node( tree, ptnode->left );
+      //plnode->parent = t;
    }
    else
    {
       ptnode->right = aatree_insert( tree, ptnode->right, x );
-      aatree_node *prnode = aatree_get_node( tree, ptnode->right );
-      prnode->parent = t;
+      //aatree_node *prnode = aatree_get_node( tree, ptnode->right );
+      //prnode->parent = t;
    }
 
    t = aatree_skew( tree, t );
index e4e41dd8c8c8b01e36618be02b92fa7849f15014..c4179c235682838d9be7729f686cc5da1dd3e276 100644 (file)
@@ -1,6 +1,9 @@
+#ifdef VG_ENGINE
 #include "vg_engine.h"
-#include "vg_console.h"
 #include "vg_ui/imgui.h"
+#endif
+
+#include "vg_console.h"
 #include "vg_log.h"
 #include "vg_string.h"
 #include <string.h>
@@ -9,7 +12,9 @@ struct vg_console vg_console;
 
 void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type, u32 flags )
 {
+#ifdef VG_ENGINE
    THREAD_1;
+#endif
    VG_ASSERT( vg_console.var_count < VG_ARRAY_LEN(vg_console.vars) );
 
    vg_var *var = &vg_console.vars[ vg_console.var_count ++ ];
@@ -21,11 +26,12 @@ void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type, u
    vg_info( "Console variable '%s' registered\n", alias );
 }
 
-void vg_console_reg_cmd( const char *alias,
-                         int (*function)(int argc, const char *argv[]),
-                         void (*poll_suggest)(int argc, const char *argv[]) )
+void vg_console_reg_cmd( const char *alias, int (*function)(int argc, const char *argv[]),
+                                            void (*poll_suggest)(int argc, const char *argv[]) )
 {
+#ifdef VG_ENGINE
    THREAD_1;
+#endif
    VG_ASSERT( vg_console.function_count < VG_ARRAY_LEN(vg_console.functions) );
 
    vg_cmd *cmd = &vg_console.functions[ vg_console.function_count ++ ];
@@ -48,9 +54,7 @@ static int _vg_console_list( int argc, char const *argv[] )
        for( int i=0; i<vg_console.var_count; i ++ )
    {
                struct vg_var *cv = &vg_console.vars[ i ];
-               vg_info( "%s %s\n", 
-               (const char *[]){ "i32","u32","f32","str" }[cv->data_type],
-               cv->name );
+               vg_info( "%s %s\n", (const char *[]){ "i32","u32","f32","str" }[cv->data_type], cv->name );
        }
        
        return 0;
@@ -58,16 +62,16 @@ static int _vg_console_list( int argc, char const *argv[] )
 
 static void vg_console_write_persistent(void)
 {
+#ifdef VG_ENGINE
    enum engine_status status = SDL_AtomicGet( &vg.engine_status );
-
    if( status < k_engine_status_running )
    {
       vg_low( "Not writing auto.conf due to early exit.\n" );
       return;
    }
+#endif
 
        FILE *fp = fopen( "cfg/auto.conf", "w" );
-
    if( !fp )
    {
       vg_error( "Cannot open cfg/auto.conf\n" );
@@ -81,17 +85,11 @@ static void vg_console_write_persistent(void)
                if( cv->flags & VG_VAR_PERSISTENT )
       {
          if( cv->data_type == k_var_dtype_i32 )
-         {
             fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
-         }
          else if( cv->data_type == k_var_dtype_u32 )
-         {
             fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
-         }
          else if( cv->data_type == k_var_dtype_f32 )
-         {
             fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
-         }
          else if( cv->data_type == k_var_dtype_str )
          {
             vg_str *str = cv->data;
@@ -104,18 +102,19 @@ static void vg_console_write_persistent(void)
        fclose( fp );
 }
 
+#ifdef VG_ENGINE
 static void _vg_console_free(void)
 {
        vg_console_write_persistent();
 }
+#endif
 
 /*
  * splits src into tokens and fills out args as pointers to those tokens
  * returns number of tokens
  * dst must be as long as src
  */
-static int vg_console_tokenize( const char *src, char *dst, 
-                                   const char *args[8] )
+static int vg_console_tokenize( const char *src, char *dst, const char *args[8] )
 {
        int arg_count = 0,
        in_token = 0;
@@ -155,7 +154,7 @@ static int vg_console_tokenize( const char *src, char *dst,
    return arg_count;
 }
 
-vg_var *vg_console_match_var( const char *kw )
+static vg_var *vg_console_match_var( const char *kw )
 {
        for( int i=0; i<vg_console.var_count; i ++ )
    {
@@ -169,7 +168,7 @@ vg_var *vg_console_match_var( const char *kw )
    return NULL;
 }
 
-vg_cmd *vg_console_match_cmd( const char *kw )
+static vg_cmd *vg_console_match_cmd( const char *kw )
 {
        for( int i=0; i<vg_console.function_count; i ++ )
    {
@@ -200,17 +199,19 @@ void vg_execute_console_input( const char *cmd, bool silent )
       /* Cvar Matched, try get value */
       if( arg_count >= 2 )
       {
+#ifdef VG_ENGINE
          if( cv->flags & VG_VAR_CHEAT )
          {
-            if( !vg_console.cheats && !silent )
+            bool cheats = vg_console.cheats;
+            if( !cheats && !silent )
             {
                vg_error( "variable is cheat protected\n" );
                return;
             }
          }
+#endif
 
-         if( (cv->data_type == k_var_dtype_u32) ||
-             (cv->data_type == k_var_dtype_i32) )
+         if( (cv->data_type == k_var_dtype_u32) || (cv->data_type == k_var_dtype_i32) )
          {
             int *ptr = cv->data;
             *ptr = atoi( args[1] ); 
@@ -259,6 +260,7 @@ void vg_execute_console_input( const char *cmd, bool silent )
       vg_error( "No command/var named '%s'. Use 'list' to view all\n",args[0]);
 }
 
+#ifdef VG_ENGINE
 u32 str_lev_distance( const char *s1, const char *s2 )
 {
    u32 m = strlen( s1 ),
@@ -323,8 +325,7 @@ u32 str_lcs( const char *s1, const char *s2 )
 }
 
 /* str must not fuckoff ever! */
-void console_suggest_score_text( const char *str, const char *input,
-                                 int minscore )
+void console_suggest_score_text( const char *str, const char *input, int minscore )
 {
    if( !str )
       return;
@@ -348,8 +349,7 @@ void console_suggest_score_text( const char *str, const char *input,
    /* insert if good score */
    if( best_pos < VG_ARRAY_LEN( vg_console.suggestions ) )
    {
-      int start = VG_MIN( vg_console.suggestion_count, 
-                          VG_ARRAY_LEN( vg_console.suggestions )-1 );
+      int start = VG_MIN( vg_console.suggestion_count, VG_ARRAY_LEN( vg_console.suggestions )-1 );
       for( int j=start; j>best_pos; j -- )
          vg_console.suggestions[j] = vg_console.suggestions[j-1];
 
@@ -357,8 +357,7 @@ void console_suggest_score_text( const char *str, const char *input,
       vg_console.suggestions[ best_pos ].len = strlen( str );
       vg_console.suggestions[ best_pos ].lev_score = score;
 
-      if( vg_console.suggestion_count < 
-            VG_ARRAY_LEN( vg_console.suggestions ) )
+      if( vg_console.suggestion_count < VG_ARRAY_LEN( vg_console.suggestions ) )
          vg_console.suggestion_count ++;
    }
 }
@@ -584,23 +583,23 @@ static void _vg_console_on_enter( ui_context *ctx, char *buf, u32 len, void *use
 
       vg_console.history_pos = -1;
       vg_execute_console_input( vg_console.input, 0 );
-      _ui_textbox_move_cursor( ctx,
-                               &ctx->textbox.cursor_user,
-                               &ctx->textbox.cursor_pos, -10000, 1 );
-
+      _ui_textbox_move_cursor( ctx, &ctx->textbox.cursor_user, &ctx->textbox.cursor_pos, -10000, 1 );
       vg_console.input[0] = '\0';
       console_update_suggestions( ctx );
    }
 
    vg_console.auto_focus = 1;
 }
+#endif
 
-int vg_console_exec( int argc, const char *argv[] )
+static int vg_console_exec( int argc, const char *argv[] )
 {
-   if( argc < 1 ) return 0;
+   if( argc < 1 ) 
+      return 0;
 
    int silent=0;
-   if( argc == 2 ) silent=1;
+   if( argc == 2 ) 
+      silent=1;
 
    char path[256];
    strcpy( path, "cfg/" );
@@ -614,22 +613,14 @@ int vg_console_exec( int argc, const char *argv[] )
                while( fgets( line, sizeof( line ), fp ) )
       {
                        line[ strcspn( line, "\r\n#" ) ] = 0x00;
-
                        if( line[0] != 0x00 )
-         {
-            strcpy( vg_console.input, line );
                                vg_execute_console_input( line, silent );
-                       }
                }
 
                fclose( fp );
        }
    else
-   {
       vg_error( "Could not open '%s'\n", path );
-   }
-
-   vg_console.input[0] = '\0';
    return 0;
 }
 
@@ -638,6 +629,7 @@ void vg_console_init(void)
 {
    vg_console_reg_cmd( "list", _vg_console_list, NULL );
    vg_console_reg_cmd( "exec", vg_console_exec, NULL );
+#ifdef VG_ENGINE
    vg_console_reg_var( "cheats", &vg_console.cheats, k_var_dtype_i32, 
 #ifdef VG_DEVWINDOW
                        VG_VAR_PERSISTENT
@@ -645,8 +637,10 @@ void vg_console_init(void)
                        0
 #endif
                        );
+#endif
 }
 
+#ifdef VG_ENGINE
 void vg_console_load_autos(void)
 {
    vg_console_exec( 2, (const char *[]){ "auto.conf", "silent" } );
@@ -738,3 +732,4 @@ void vg_console_draw( ui_context *ctx )
    if( SDL_UnlockMutex( vg_log.mutex ) )
       vg_fatal_error( "" );
 }
+#endif
index 386ddb1c6e845f6ad032be297eaa66dddf1aa782..e3ccf4f5df3bd8c9d14e49aeb76c68f3a564abfe 100644 (file)
@@ -19,11 +19,12 @@ typedef struct vg_cmd vg_cmd;
 
 struct vg_console
 {
-       struct vg_var{
+       struct vg_var
+   {
                void *data;
                const char *name;
-               
-               enum vg_var_dtype{
+               enum vg_var_dtype
+      {
                        k_var_dtype_i32,
                        k_var_dtype_u32,
                        k_var_dtype_f32,
@@ -34,13 +35,15 @@ struct vg_console
        } 
        vars[ 128 ];
        
-       struct vg_cmd{
+       struct vg_cmd
+   {
                int  (*function)( int argc, char const *argv[] );
       void (*poll_suggest)( int argc, char const *argv[] );
                const char *name;
        }
        functions[ 32 ];
 
+#ifdef VG_ENGINE
    struct {
       const char *str;
       int len;
@@ -52,34 +55,35 @@ struct vg_console
    int suggestion_select,
        suggestion_pastepos,
        suggestion_maxlen;
+#endif
 
    u32 var_count, function_count;
-       
+
+#ifdef VG_ENGINE
        char input[96],
         input_copy[96];
-       
        char history[32][96];
        int history_last, history_pos, history_count;
        
        i32 enabled, cheats;
    bool auto_focus;
+#endif
 }
 extern vg_console;
 
 void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type, u32 flags );
 
-void vg_console_reg_cmd( const char *alias,
-                         int (*function)(int argc, const char *argv[]),
+void vg_console_reg_cmd( const char *alias, int (*function)(int argc, const char *argv[]),
                          void (*poll_suggest)(int argc, const char *argv[]) );
+
+#ifdef VG_ENGINE
 void vg_console_load_autos(void);
 void vg_console_draw( ui_context *ctx );
-void vg_console_init(void);
-int vg_console_exec( int argc, const char *argv[] );
-void vg_execute_console_input( const char *cmd, bool silent );
 static void vg_console_write_persistent(void);
-void console_suggest_score_text( const char *str, const char *input,
-                                 int minscore );
-vg_var *vg_console_match_var( const char *kw );
-vg_cmd *vg_console_match_cmd( const char *kw );
+void console_suggest_score_text( const char *str, const char *input, int minscore );
 void console_suggest_next( ui_context *ctx );
 void console_suggest_prev( ui_context *ctx );
+#endif
+
+void vg_console_init(void);
+void vg_execute_console_input( const char *cmd, bool silent );
diff --git a/vg_db.c b/vg_db.c
index 69dec03a6fce5b0a10c896d8bd181eaef1658801..8c2ce45c471281aea45c489dc008e6fabfb99784 100644 (file)
--- a/vg_db.c
+++ b/vg_db.c
@@ -2,6 +2,9 @@
 #include <stddef.h>
 #include <stdarg.h>
 
+/* util
+ * ------------------------------------------------------------------------------------------------------------------ */
+
 static void vg_db_abort( vg_db *db, const char *message, ... )
 {
    fclose( db->fp );
@@ -23,6 +26,55 @@ static u32 vg_dbhash( u8 *buf, u32 len )
    return hash;
 }
 
+static u64 vg_db_allocate_physical_page( vg_db *db );
+static void vg_db_sync_page( vg_db *db, u16 cache_id );
+
+/* API
+ * ------------------------------------------------------------------------------------------------------------------ */
+
+void vg_db_open( vg_db *db, const char *path )
+{
+   u32 k_ident = 0xf32b1a00;
+   vg_rand_seed( &db->rand, k_ident + time(NULL) );
+   db->fp = fopen( path, "rb+" );
+   db->page_data = malloc( VG_PAGE_SIZE*VG_MAX_CACHED_PAGES );
+   db->cache_count = 0;
+   db->lru_old = 0;
+   db->lru_young = 0;
+   if( db->fp )
+   {
+      u32 ident;
+      vg_db_read( db, 0, &ident, 4 );
+      if( ident != k_ident )
+         vg_db_abort( db, "Ident not found in db file '%s'\n", path );
+      vg_db_read( db, offsetof(vg_db_header,userdata_address), &db->userdata_address, 8 );
+   }
+   else
+   {
+      db->fp = fopen( path, "wb+" );
+      if( !db->fp )
+         vg_db_abort( db, "fopen(wb+) failed for '%s'\n", path );
+      vg_db_allocate_physical_page( db ); // Allocate header as 0
+      vg_db_write( db, offsetof(vg_db_header,ident), &k_ident, 4 );
+      db->userdata_address = vg_db_virtual_allocate( db, VG_1GB );
+      vg_db_write( db, offsetof(vg_db_header,userdata_address), &db->userdata_address, 8 );
+      vg_db_tree_init( db, offsetof(vg_db_header,address_tree) );
+   }
+}
+
+void vg_db_close( vg_db *db )
+{
+   for( u32 i=0; i<VG_MAX_CACHED_PAGES; i ++ )
+      vg_db_sync_page( db, i+1 );
+   fclose( db->fp );
+   db->fp = NULL;
+   free( db->page_data );
+   db->page_data = NULL;
+}
+
+/* paging
+ * ------------------------------------------------------------------------------------------------------------------ */
+
 static u64 vg_db_allocate_physical_page( vg_db *db )
 {
    if( fseek( db->fp, 0, SEEK_END ) )
@@ -48,6 +100,141 @@ static void vg_db_sync_page( vg_db *db, u16 cache_id )
    }
 }
 
+static void vg_db_touch( vg_db *db, u16 cache_id )
+{
+   vg_db_page *page = &db->page_cache[ cache_id-1 ];
+   /* unlink entirely */
+   if( page->lru_younger )
+      db->page_cache[ page->lru_younger-1 ].lru_older = page->lru_older;
+   else if( db->lru_young == cache_id )
+      db->lru_young = page->lru_older;
+   if( page->lru_older )
+      db->page_cache[ page->lru_older-1 ].lru_younger = page->lru_younger;
+   else if( db->lru_old == cache_id )
+      db->lru_old = page->lru_younger;
+   /* re-link */
+   page->lru_younger = 0;
+   page->lru_older = db->lru_young;
+   if( db->lru_young )
+   {
+      page->lru_older = db->lru_young;
+      db->page_cache[ db->lru_young-1 ].lru_younger = cache_id;
+   }
+   db->lru_young = cache_id;
+   if( !db->lru_old )
+      db->lru_old = cache_id;
+}
+
+static void *vg_db_devirtualize( vg_db *db, u64 address, bool write )
+{
+   u64 page_base = address & ~(VG_PAGE_SIZE-1lu),
+       inner_offset = address & (VG_PAGE_SIZE-1lu);
+
+   /* Check hash table for our page */
+   u32 hash = vg_dbhash( (void *)(&page_base), sizeof(page_base) ) & (VG_PAGE_CACHE_HASH_WIDTH-1u);
+   u16 current = db->hash_table[ hash ];
+   while( current )
+   {
+      vg_db_page *page = &db->page_cache[ current-1 ];
+      if( page->virtual_id == page_base )
+      {
+         page->unwritten |= write;
+         vg_db_touch( db, current );
+         return db->page_data + ((u64)(current-1)*VG_PAGE_SIZE + inner_offset);
+      }
+      else
+         current = page->hash_prev;
+   }
+
+   /* Translate address & create page if need be */
+   u64 translated_page_base = page_base;
+   if( address & VG_VIRTUAL_ADDRESS_BIT )
+   {
+      u64 tree_address = offsetof(vg_db_header,address_tree);
+      translated_page_base = vg_db_translate( db, tree_address, page_base );
+      if( translated_page_base == 0 )
+      {
+         u64 new_page = vg_db_allocate_physical_page( db );
+         vg_db_tree_map( db, tree_address, page_base, new_page );
+         translated_page_base = new_page;
+      }
+   }
+
+   /* Allocate cache ID */
+   u16 cache_id = 0;
+   vg_db_page *page = NULL;
+   if( db->cache_count < VG_MAX_CACHED_PAGES ) 
+   {
+      cache_id = ++db->cache_count;
+      page = &db->page_cache[ cache_id-1 ];
+      memset( page, 0, sizeof(vg_db_page) );
+   }
+   else
+   {
+      cache_id = db->lru_old;
+      vg_db_sync_page( db, cache_id );
+      page = &db->page_cache[ cache_id-1 ];
+      u32 old_hash = vg_dbhash( (void *)(&page->virtual_id), sizeof(page->virtual_id) ) & (VG_PAGE_CACHE_HASH_WIDTH-1u);
+      u16 current = db->hash_table[ old_hash ], before = 0;
+      while( current != cache_id )
+      {
+         before = current;
+         current = db->page_cache[ current-1 ].hash_prev;
+      }
+      if( before )
+         db->page_cache[ before-1 ].hash_prev = page->hash_prev;
+      else 
+         db->hash_table[ old_hash ] = 0;
+   }
+   page->hash_prev = db->hash_table[ hash ];
+   db->hash_table[ hash ] = cache_id;
+   vg_db_touch( db, cache_id );
+   page->virtual_id = page_base;
+   page->physical_offset = translated_page_base;
+   page->unwritten = write;
+
+   /* read into memory */
+   void *page_data = db->page_data + (u64)(cache_id-1)*VG_PAGE_SIZE;
+   if( fseek( db->fp, translated_page_base, SEEK_SET ) )
+      vg_db_abort( db, "SEEK_SET (%lx) failed\n", translated_page_base );
+   if( !fread( page_data, VG_PAGE_SIZE, 1, db->fp ) )
+      vg_db_abort( db, "fread page failed\n" );
+   return page_data + inner_offset;
+}
+
+void vg_db_xch( vg_db *db, u64 base_address, void *buf, u32 length, bool write )
+{
+   u64 address = base_address,
+       end     = base_address + (u64)length;
+
+   while( address != end )
+   {
+      u64 byte_count = VG_PAGE_SIZE - (address & (VG_PAGE_SIZE-1lu));
+      if( address + byte_count > end )
+         byte_count = end - address;
+
+      void *cache_buffer = vg_db_devirtualize( db, address, write ),
+           *user_buffer  = buf + (address-base_address);
+      if( write ) memcpy( cache_buffer, user_buffer, byte_count );
+      else        memcpy( user_buffer, cache_buffer, byte_count );
+      address += byte_count;
+   }
+}
+
+u64 vg_db_virtual_allocate( vg_db *db, u64 bytes )
+{
+   u64 page_count = 0;
+   vg_db_read( db, 0x0lu + offsetof(vg_db_header,virtual_pages), &page_count, sizeof(page_count) );
+   u64 pages = (bytes + (VG_PAGE_SIZE-1lu)) >> VG_PAGE_BITS,
+       addr  = page_count * VG_PAGE_SIZE;
+   page_count += pages;
+   vg_db_write( db,0x0lu + offsetof(vg_db_header,virtual_pages), &page_count, sizeof(page_count) );
+   return VG_VIRTUAL_ADDRESS_BIT | addr;
+}
+
+/* AA search tree
+ * ------------------------------------------------------------------------------------------------------------------ */
+
 static u64 vg_db_skew( vg_db *db, u64 t_offset )
 {
    if( t_offset == 0 )
@@ -148,31 +335,6 @@ void vg_db_tree_map( vg_db *db, u64 tree_address, u64 key, u64 value )
    vg_db_write( db, tree_address, &tree, sizeof(tree) );
 }
 
-static void vg_db_touch( vg_db *db, u16 cache_id )
-{
-   vg_db_page *page = &db->page_cache[ cache_id-1 ];
-   /* unlink entirely */
-   if( page->lru_younger )
-      db->page_cache[ page->lru_younger-1 ].lru_older = page->lru_older;
-   else if( db->lru_young == cache_id )
-      db->lru_young = page->lru_older;
-   if( page->lru_older )
-      db->page_cache[ page->lru_older-1 ].lru_younger = page->lru_younger;
-   else if( db->lru_old == cache_id )
-      db->lru_old = page->lru_younger;
-   /* re-link */
-   page->lru_younger = 0;
-   page->lru_older = db->lru_young;
-   if( db->lru_young )
-   {
-      page->lru_older = db->lru_young;
-      db->page_cache[ db->lru_young-1 ].lru_younger = cache_id;
-   }
-   db->lru_young = cache_id;
-   if( !db->lru_old )
-      db->lru_old = cache_id;
-}
-
 u64 vg_db_translate( vg_db *db, u64 tree_address, u64 key )
 {
    vg_db_address_tree tree;
@@ -212,6 +374,9 @@ void vg_db_tree_init( vg_db *db, u64 tree_address )
    }
 }
 
+/* Randomized skiplist 
+ * ------------------------------------------------------------------------------------------------------------------ */
+
 void vg_db_skipper_init( vg_db *db, u64 skipper_address, u32 max_entries )
 {
    vg_db_skipper skipper;
@@ -350,12 +515,6 @@ void vg_db_skipper_unplace( vg_db *db, vg_skipper_context *ctx, u16 item_index,
    }
 }
 
-void vg_db_skipper_replace( vg_db *db, vg_skipper_context *ctx, u16 item_index, void *comparand_old, void *comparand_new )
-{
-   vg_db_skipper_unplace( db, ctx, item_index, comparand_old );
-   vg_db_skipper_placement( db, ctx, item_index, comparand_new );
-}
-
 void vg_db_skipper_iter_start( vg_db *db, vg_skipper_context *ctx )
 {
    vg_db_skipper skipper;
@@ -379,6 +538,9 @@ bool vg_db_skipper_iter( vg_db *db, vg_skipper_context *ctx, u16 *out_index )
       return 1;
    }
 }
+
+/* Dumb table
+ * ------------------------------------------------------------------------------------------------------------------ */
                             
 void vg_db_dumb_table_init( vg_db *db, u64 table_address, u32 structure_size, u32 max_entries )
 {
@@ -420,150 +582,3 @@ u64 vg_db_dumb_table_append( vg_db *db, u64 table_address )
    }
    else return 0;
 }
-
-static void *vg_db_devirtualize( vg_db *db, u64 address, bool write )
-{
-   u64 page_base = address & ~(VG_PAGE_SIZE-1lu),
-       inner_offset = address & (VG_PAGE_SIZE-1lu);
-
-   /* Check hash table for our page */
-   u32 hash = vg_dbhash( (void *)(&page_base), sizeof(page_base) ) & (VG_PAGE_CACHE_HASH_WIDTH-1u);
-   u16 current = db->hash_table[ hash ];
-   while( current )
-   {
-      vg_db_page *page = &db->page_cache[ current-1 ];
-      if( page->virtual_id == page_base )
-      {
-         page->unwritten |= write;
-         vg_db_touch( db, current );
-         return db->page_data + ((u64)(current-1)*VG_PAGE_SIZE + inner_offset);
-      }
-      else
-         current = page->hash_prev;
-   }
-
-   /* Translate address & create page if need be */
-   u64 translated_page_base = page_base;
-   if( address & VG_VIRTUAL_ADDRESS_BIT )
-   {
-      u64 tree_address = offsetof(vg_db_header,address_tree);
-      translated_page_base = vg_db_translate( db, tree_address, page_base );
-      if( translated_page_base == 0 )
-      {
-         u64 new_page = vg_db_allocate_physical_page( db );
-         vg_db_tree_map( db, tree_address, page_base, new_page );
-         translated_page_base = new_page;
-      }
-   }
-
-   /* Allocate cache ID */
-   u16 cache_id = 0;
-   vg_db_page *page = NULL;
-   if( db->cache_count < VG_MAX_CACHED_PAGES ) 
-   {
-      cache_id = ++db->cache_count;
-      page = &db->page_cache[ cache_id-1 ];
-      memset( page, 0, sizeof(vg_db_page) );
-   }
-   else
-   {
-      cache_id = db->lru_old;
-      vg_db_sync_page( db, cache_id );
-      page = &db->page_cache[ cache_id-1 ];
-      u32 old_hash = vg_dbhash( (void *)(&page->virtual_id), sizeof(page->virtual_id) ) & (VG_PAGE_CACHE_HASH_WIDTH-1u);
-      u16 current = db->hash_table[ old_hash ], before = 0;
-      while( current != cache_id )
-      {
-         before = current;
-         current = db->page_cache[ current-1 ].hash_prev;
-      }
-      if( before )
-         db->page_cache[ before-1 ].hash_prev = page->hash_prev;
-      else 
-         db->hash_table[ old_hash ] = 0;
-   }
-   page->hash_prev = db->hash_table[ hash ];
-   db->hash_table[ hash ] = cache_id;
-   vg_db_touch( db, cache_id );
-   page->virtual_id = page_base;
-   page->physical_offset = translated_page_base;
-   page->unwritten = write;
-
-   /* read into memory */
-   void *page_data = db->page_data + (u64)(cache_id-1)*VG_PAGE_SIZE;
-   if( fseek( db->fp, translated_page_base, SEEK_SET ) )
-      vg_db_abort( db, "SEEK_SET (%lx) failed\n", translated_page_base );
-   if( !fread( page_data, VG_PAGE_SIZE, 1, db->fp ) )
-      vg_db_abort( db, "fread page failed\n" );
-   return page_data + inner_offset;
-}
-
-void vg_db_xch( vg_db *db, u64 base_address, void *buf, u32 length, bool write )
-{
-   u64 address = base_address,
-       end     = base_address + (u64)length;
-
-   while( address != end )
-   {
-      u64 byte_count = VG_PAGE_SIZE - (address & (VG_PAGE_SIZE-1lu));
-      if( address + byte_count > end )
-         byte_count = end - address;
-
-      void *cache_buffer = vg_db_devirtualize( db, address, write ),
-           *user_buffer  = buf + (address-base_address);
-      if( write ) memcpy( cache_buffer, user_buffer, byte_count );
-      else        memcpy( user_buffer, cache_buffer, byte_count );
-      address += byte_count;
-   }
-}
-
-void vg_db_open( vg_db *db, const char *path )
-{
-   u32 k_ident = 0xf32b1a00;
-   vg_rand_seed( &db->rand, k_ident + time(NULL) );
-   db->fp = fopen( path, "rb+" );
-   db->page_data = malloc( VG_PAGE_SIZE*VG_MAX_CACHED_PAGES );
-   db->cache_count = 0;
-   db->lru_old = 0;
-   db->lru_young = 0;
-   if( db->fp )
-   {
-      u32 ident;
-      vg_db_read( db, 0, &ident, 4 );
-      if( ident != k_ident )
-         vg_db_abort( db, "Ident not found in db file '%s'\n", path );
-      vg_db_read( db, offsetof(vg_db_header,userdata_address), &db->userdata_address, 8 );
-   }
-   else
-   {
-      db->fp = fopen( path, "wb+" );
-      if( !db->fp )
-         vg_db_abort( db, "fopen(wb+) failed for '%s'\n", path );
-      vg_db_allocate_physical_page( db ); // Allocate header as 0
-      vg_db_write( db, offsetof(vg_db_header,ident), &k_ident, 4 );
-      db->userdata_address = vg_db_virtual_allocate( db, VG_1GB );
-      vg_db_write( db, offsetof(vg_db_header,userdata_address), &db->userdata_address, 8 );
-      vg_db_tree_init( db, offsetof(vg_db_header,address_tree) );
-   }
-}
-
-void vg_db_close( vg_db *db )
-{
-   for( u32 i=0; i<VG_MAX_CACHED_PAGES; i ++ )
-      vg_db_sync_page( db, i+1 );
-   fclose( db->fp );
-   db->fp = NULL;
-   free( db->page_data );
-   db->page_data = NULL;
-}
-
-u64 vg_db_virtual_allocate( vg_db *db, u64 bytes )
-{
-   u64 page_count = 0;
-   vg_db_read( db, 0x0lu + offsetof(vg_db_header,virtual_pages), &page_count, sizeof(page_count) );
-   u64 pages = (bytes + (VG_PAGE_SIZE-1lu)) >> VG_PAGE_BITS,
-       addr  = page_count * VG_PAGE_SIZE;
-   page_count += pages;
-   vg_db_write( db,0x0lu + offsetof(vg_db_header,virtual_pages), &page_count, sizeof(page_count) );
-   return VG_VIRTUAL_ADDRESS_BIT | addr;
-}
diff --git a/vg_db.h b/vg_db.h
index ffade0c9714f3ec690bfafa49e20845521a649fa..498bbbdee7a8e4818fc08f1f4027bd396d4727ac 100644 (file)
--- a/vg_db.h
+++ b/vg_db.h
@@ -107,10 +107,10 @@ typedef struct vg_skipper_context vg_skipper_context;
 typedef i32 (*fn_skipper_comparator)( vg_skipper_context *ctx, void *comparand, u16 item_index );
 struct vg_skipper_context
 {
-   u64 address;
+   u64 address, table_address;
    fn_skipper_comparator fn_compare;
-   void *info;
 
+   /* internal */
    u64 iter_array_address;
    u16 iter_index;
 };
@@ -119,6 +119,5 @@ void vg_db_skipper_init( vg_db *db, u64 skipper_address, u32 max_entries );
 bool vg_db_skipper_find( vg_db *db, vg_skipper_context *ctx, u16 *out_index, void *comparand );
 void vg_db_skipper_placement( vg_db *db, vg_skipper_context *ctx, u16 item_index, void *comparand );
 void vg_db_skipper_unplace( vg_db *db, vg_skipper_context *ctx, u16 item_index, void *comparand );
-void vg_db_skipper_replace( vg_db *db, vg_skipper_context *ctx, u16 item_index, void *comparand_old, void *comparand_new );
 void vg_db_skipper_iter_start( vg_db *db, vg_skipper_context *ctx );
 bool vg_db_skipper_iter( vg_db *db, vg_skipper_context *ctx, u16 *out_index );
index 3848789bba3e24022517f0a1480dbad55aa8884b..e5b286f36d4b175d06c73609a79feb9607052ce1 100644 (file)
@@ -805,12 +805,10 @@ void vg_fatal_exit(void)
 void vg_fatal_error( const char *fmt, ... )
 {
    vg_fatal_condition();
-
    va_list args;
    va_start( args, fmt );
    _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args );
    va_end( args );
-
    vg_fatal_exit();
 }
 
index 01b5e27567f6006167bb5d5a4b90d775242a0636..5fcdbbd1b5cc101db289e3527d869466f3a92b93 100644 (file)
@@ -6,11 +6,12 @@
 /* implementation 
  * -------------------------------------------------------------------------- */
 
-vg_pool_node *vg_pool_nodeptr( vg_pool *pool, u16 id ){
-   if( !id ) return NULL;
-   else {
+vg_pool_node *vg_pool_nodeptr( vg_pool *pool, u16 id )
+{
+   if( !id ) 
+      return NULL;
+   else
       return pool->buffer + (pool->stride*(id-1)) + pool->offset;
-   }
 }
 
 /* is thread safe */
@@ -26,9 +27,9 @@ void vg_pool_init( vg_pool *pool )
 {
    pool->head = 1;
    pool->tail = pool->count;
-   for( u16 ib=1; ib <= pool->count; ib++ ){
+   for( u16 ib=1; ib <= pool->count; ib++ )
+   {
       vg_pool_node *nb = vg_pool_nodeptr( pool, ib );
-
       u16 ia = ib-1, ic = ib+1;
       nb->l = ia;
       nb->r = ic<=pool->count? ic: 0;
@@ -60,8 +61,8 @@ static void vg_pool_unlink( vg_pool *pool, u16 id )
 u16 vg_pool_lru( vg_pool *pool )
 {
    u16 head = pool->head;
-   if( !head ) return 0;
-
+   if( !head ) 
+      return 0;
    vg_pool_unlink( pool, head );
    return head;
 }
@@ -92,7 +93,6 @@ bool vg_pool_unwatch( vg_pool *pool, u16 id )
    {
       vg_pool_node *head = vg_pool_nodeptr( pool, pool->head ),
                    *tail = vg_pool_nodeptr( pool, pool->tail );
-      
       if( tail ) 
          tail->r = id;
       node->l = pool->tail;
index b62ba6e2f316eef6ff61db86e433b8a495074621..2c0d24ea6c59b134c189b53642530e097f94b8e1 100644 (file)
@@ -186,9 +186,14 @@ char *vg_strch( vg_str *str, char c )
    return ptr;
 }
 
-u32 vg_strncpy( const char *src, char *dst, u32 len,
-                enum strncpy_behaviour behaviour )
+u32 vg_strncpy( const char *src, char *dst, u32 len, enum strncpy_behaviour behaviour )
 {
+   if( src == NULL )
+   {
+      dst[0] = '\0';
+      return 0;
+   }
+
    for( u32 i=0; i<len; i++ )
    {
       dst[i] = src[i];
index ce45fa30f7a250c0b625fb7eb8796bba57d75c3e..c9ac3a8607a53dbbe905c421c3961ebf4d6da1ca 100644 (file)
--- a/vg_tool.c
+++ b/vg_tool.c
@@ -8,9 +8,30 @@
 #include "vg_mem.c"
 #include "vg_mem_queue.c"
 #include "vg_io.c"
+#if !defined(_WIN32)
+ #include <execinfo.h>
+#endif
 
 void vg_fatal_exit(void)
 {
+#if !defined(_WIN32)
+   void *array[20];
+   char **strings;
+   int size, i;
+
+   size = backtrace( array, 20 );
+   strings = backtrace_symbols( array, size );
+
+   if( strings != NULL )
+   {
+      vg_error( "\n---------------- gnu backtrace -------------\n" );
+
+      for( int i=0; i<size; i++ )
+         vg_low( "%s\n", strings[i] );
+   }
+
+   free( strings );
+#endif
    exit(1);
 }
 
index e6462080f7221c024fca63a2d4b73b5a095237b1..051e0ba3bfd1fb16d7a4e4c9c28a9c34c3eafc5b 100644 (file)
@@ -7,9 +7,7 @@
 #include "vg_log.h"
 #include <string.h>
 
-void ui_init( ui_context *ctx,
-              ui_vert *verts_buf, u32 verts_max,
-              u16 *indices_buf, u32 indices_max )
+void ui_init( ui_context *ctx, ui_vert *verts_buf, u32 verts_max, u16 *indices_buf, u32 indices_max )
 {
    ctx->vertex_buffer = verts_buf;
    ctx->max_verts = verts_max;
index 2d68999dfe80d9ab9474a62588aeba7fbcc3b816..0810ec7c251f3bac3ec864d292140a778d75d978 100644 (file)
@@ -162,7 +162,7 @@ static struct vg_shader _shader_ui_image = {
        "void main()"
        "{"
                "vec4 colour = texture( uTexImage, aTexCoords );"
-               "FragColor = colour * uColour;"
+               "FragColor = colour * aColour * uColour;"
        "}"
    }
 };