msg api, mem api
authorhgn <hgodden00@gmail.com>
Tue, 26 Mar 2024 20:12:14 +0000 (20:12 +0000)
committerhgn <hgodden00@gmail.com>
Tue, 26 Mar 2024 20:12:14 +0000 (20:12 +0000)
vg_mem.c
vg_msg.c
vg_msg.h

index 797407913bd65f36d6ef65d077642a004fdda1f4..0efd3adf484f674daf4d1d6a56723f39321266ef 100644 (file)
--- a/vg_mem.c
+++ b/vg_mem.c
@@ -34,18 +34,16 @@ vg_linear_allocator *vg_linear_header( void *data )
 __attribute__((warn_unused_result))
 void *_vg_linear_alloc( void *buffer, u32 size, const char *constr_name )
 {
-   if( size % 8 ){
-      vg_error( "alloc(%u) is not 8 byte aligned\n", size );
-      vg_print_backtrace();
+   if( size % 8 )
       size = vg_align8( size );
-   }
-   if( ((u64)buffer) % 8 ){
+
+   if( ((u64)buffer) % 8 )
       vg_fatal_error( "unaligned buffer (%p)", buffer );
-   }
 
    vg_linear_allocator *alloc = vg_linear_header( buffer );
 
-   if( (alloc->cur + size) > alloc->size ){
+   if( (alloc->cur + size) > alloc->size )
+   {
       vg_fatal_error( "linear allocator overflow (%u + %u > %u)\n", 
                         alloc->cur, size, alloc->size );
    }
index db7380a91bfacd0f4292a043a1868e3b4fce89b7..65d8ebda49bc0c9a317f33c7b60c7886ddfb8e5c 100644 (file)
--- a/vg_msg.c
+++ b/vg_msg.c
@@ -1,5 +1,8 @@
 #include "vg_msg.h"
 #include "vg_platform.h"
+#include "vg_string.h"
+#include <string.h>
+#include <stdio.h>
 
 /* write a buffer from msg, rang checked. */
 void vg_msg_wbuf( vg_msg *msg, u8 *buf, u32 len )
@@ -383,7 +386,8 @@ int vg_msg_getkvcmd( vg_msg *msg, const char *key, vg_msg_cmd *cmd )
  * Read a integral KV out to dst, and perform conversion if needed
  * dst is always defined, if its not found its set to 0
  */
-int vg_msg_getkvintg( vg_msg *msg, const char *key, u8 type, void *dst )
+int vg_msg_getkvintg( vg_msg *msg, const char *key, u8 type, void *dst, 
+                      void *default_value )
 {
    vg_msg_cmd cmd;
    if( vg_msg_getkvcmd( msg, key, &cmd ) )
@@ -393,7 +397,11 @@ int vg_msg_getkvintg( vg_msg *msg, const char *key, u8 type, void *dst )
    }
    else 
    {
-      memset( dst, 0, vg_msg_cmd_bytecount(type) );
+      if( default_value )
+         memcpy( dst, default_value, vg_msg_cmd_bytecount(type) );
+      else
+         memset( dst, 0, vg_msg_cmd_bytecount(type) );
+
       return 0;
    }
 }
index 91f0dc20a8cc22e59af0ff4517f4ca626e62e3b2..0b1302418ef710f55fa11faa58787e30a0034e42 100644 (file)
--- a/vg_msg.h
+++ b/vg_msg.h
@@ -188,9 +188,9 @@ int vg_msg_getkvcmd( vg_msg *msg, const char *key, vg_msg_cmd *cmd );
 
 /*
  * Read a integral KV out to dst, and perform conversion if needed
- * dst is always defined, if its not found its set to 0
  */
-int vg_msg_getkvintg( vg_msg *msg, const char *key, u8 type, void *dst );
+int vg_msg_getkvintg( vg_msg *msg, const char *key, u8 type, void *dst,
+                      void *default_value );
 
 /* helper for reading string kvs. returns NULL if not found */
 const char *vg_msg_getkvstr( vg_msg *msg, const char *key );