Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default data reflection #169

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Empty file modified include/dl/dl.h
100755 → 100644
Empty file.
Empty file modified include/dl/dl_defines.h
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions include/dl/dl_reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct dl_member_info
{
const char* name;
const char* comment;
const void* default_data;
dl_type_atom_t atom;
dl_type_storage_t storage;
dl_typeid_t type_id;
Expand Down
84 changes: 17 additions & 67 deletions src/dl.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "dl_types.h"
#include "dl_store.h"
#include "dl_swap.h"
#include "dl_binary_writer.h"
#include "dl_patch_ptr.h"
Expand Down Expand Up @@ -142,63 +143,6 @@ dl_error_t DL_DLL_EXPORT dl_instance_load_inplace( dl_ctx_t dl_ctx,
return DL_ERROR_OK;
}

struct CDLBinStoreContext
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to new private header: "dl_store.h"

{
CDLBinStoreContext( uint8_t* out_data, size_t out_data_size, bool is_dummy, dl_allocator alloc )
: written_ptrs(alloc)
, ptrs(alloc)
, strings(alloc)
{
dl_binary_writer_init( &writer, out_data, out_data_size, is_dummy, DL_ENDIAN_HOST, DL_ENDIAN_HOST, DL_PTR_SIZE_HOST );
}

uintptr_t FindWrittenPtr( void* ptr )
{
if( ptr == 0 )
return (uintptr_t)0;

size_t len = written_ptrs.Len();
for( size_t i = 0; i < len; ++i )
if( written_ptrs[i].ptr == ptr )
return written_ptrs[i].pos;

return (uintptr_t)0;
}

void AddWrittenPtr( const void* ptr, uintptr_t pos )
{
written_ptrs.Add( { pos, ptr } );
}

uint32_t GetStringOffset(const char* str, uint32_t length, uint32_t hash)
{
for (size_t i = 0; i < strings.Len(); ++i)
if (strings[i].hash == hash && strings[i].length == length && strcmp(str, strings[i].str) == 0)
return strings[i].offset;

return 0;
}

dl_binary_writer writer;

struct SWrittenPtr
{
uintptr_t pos;
const void* ptr;
};
CArrayStatic<SWrittenPtr, 128> written_ptrs;
CArrayStatic<uintptr_t, 256> ptrs;

struct SString
{
const char* str;
uint32_t length;
uint32_t hash;
uint32_t offset;
};
CArrayStatic<SString, 128> strings;
};

static void dl_internal_store_string( const uint8_t* instance, CDLBinStoreContext* store_ctx )
{
char* str = *(char**)instance;
Expand All @@ -216,17 +160,17 @@ static void dl_internal_store_string( const uint8_t* instance, CDLBinStoreContex
dl_binary_writer_seek_end(&store_ctx->writer);
offset = dl_binary_writer_tell(&store_ctx->writer);
dl_binary_writer_write(&store_ctx->writer, str, length + 1);
store_ctx->strings.Add( {str, length, hash, (uint32_t) offset} );
store_ctx->strings.Add( {{str, length}, hash, (uint32_t) offset} );
dl_binary_writer_seek_set(&store_ctx->writer, pos);
}
if( !store_ctx->writer.dummy )
store_ctx->ptrs.Add( dl_binary_writer_tell( &store_ctx->writer ) );
dl_binary_writer_write( &store_ctx->writer, &offset, sizeof(uintptr_t) );
}

static dl_error_t dl_internal_instance_store( dl_ctx_t dl_ctx, const dl_type_desc* type, uint8_t* instance, CDLBinStoreContext* store_ctx );
static dl_error_t dl_internal_instance_store( dl_ctx_t dl_ctx, const dl_type_desc* type, const uint8_t* instance, CDLBinStoreContext* store_ctx );

static dl_error_t dl_internal_store_ptr( dl_ctx_t dl_ctx, uint8_t* instance, const dl_type_desc* sub_type, CDLBinStoreContext* store_ctx )
static dl_error_t dl_internal_store_ptr( dl_ctx_t dl_ctx, const uint8_t* instance, const dl_type_desc* sub_type, CDLBinStoreContext* store_ctx )
{
uint8_t* data = *(uint8_t**)instance;
uintptr_t offset = store_ctx->FindWrittenPtr( data );
Expand Down Expand Up @@ -265,7 +209,7 @@ static dl_error_t dl_internal_store_ptr( dl_ctx_t dl_ctx, uint8_t* instance, con
return DL_ERROR_OK;
}

static dl_error_t dl_internal_store_array( dl_ctx_t dl_ctx, dl_type_storage_t storage_type, const dl_type_desc* sub_type, uint8_t* instance, uint32_t count, uintptr_t size, CDLBinStoreContext* store_ctx )
static dl_error_t dl_internal_store_array( dl_ctx_t dl_ctx, dl_type_storage_t storage_type, const dl_type_desc* sub_type, const uint8_t* instance, uint32_t count, uintptr_t size, CDLBinStoreContext* store_ctx )
{
switch( storage_type )
{
Expand Down Expand Up @@ -304,7 +248,7 @@ static dl_error_t dl_internal_store_array( dl_ctx_t dl_ctx, dl_type_storage_t st
return DL_ERROR_OK;
}

static dl_error_t dl_internal_store_member( dl_ctx_t dl_ctx, const dl_member_desc* member, uint8_t* instance, CDLBinStoreContext* store_ctx )
dl_error_t dl_internal_store_member( dl_ctx_t dl_ctx, const dl_member_desc* member, const uint8_t* instance, CDLBinStoreContext* store_ctx )
{
dl_type_atom_t atom_type = member->AtomType();
dl_type_storage_t storage_type = member->StorageType();
Expand Down Expand Up @@ -377,8 +321,8 @@ static dl_error_t dl_internal_store_member( dl_ctx_t dl_ctx, const dl_member_des
uintptr_t size = 0;
const dl_type_desc* sub_type = 0x0;

uint8_t* data_ptr = instance;
uint32_t count = *(uint32_t*)( data_ptr + sizeof(void*) );
const uint8_t* data_ptr = instance;
uint32_t count = *(const uint32_t*)( data_ptr + sizeof(void*) );

uintptr_t offset = 0;

Expand Down Expand Up @@ -417,14 +361,16 @@ static dl_error_t dl_internal_store_member( dl_ctx_t dl_ctx, const dl_member_des
dl_binary_writer_seek_set( &store_ctx->writer, pos );

if( !store_ctx->writer.dummy )
store_ctx->ptrs.Add( dl_binary_writer_tell( &store_ctx->writer ) );
store_ctx->ptrs.Add( dl_binary_writer_tell( &store_ctx->writer ) );
}

// make room for ptr
dl_binary_writer_write( &store_ctx->writer, &offset, sizeof(uintptr_t) );

// write count
dl_binary_writer_write( &store_ctx->writer, &count, sizeof(uint32_t) );
if DL_CONSTANT_EXPRESSION(sizeof(uintptr_t) == 8)
dl_binary_writer_write_zero( &store_ctx->writer, sizeof(uint32_t) );
}
return DL_ERROR_OK;

Expand All @@ -440,7 +386,7 @@ static dl_error_t dl_internal_store_member( dl_ctx_t dl_ctx, const dl_member_des
return DL_ERROR_OK;
}

static dl_error_t dl_internal_instance_store( dl_ctx_t dl_ctx, const dl_type_desc* type, uint8_t* instance, CDLBinStoreContext* store_ctx )
static dl_error_t dl_internal_instance_store( dl_ctx_t dl_ctx, const dl_type_desc* type, const uint8_t* instance, CDLBinStoreContext* store_ctx )
{
bool last_was_bitfield = false;

Expand Down Expand Up @@ -499,7 +445,11 @@ dl_error_t dl_instance_store( dl_ctx_t dl_ctx, dl_typeid_t type_id,
return DL_ERROR_TYPE_NOT_FOUND;

bool store_ctx_is_dummy = out_buffer_size == 0;
CDLBinStoreContext store_context( out_buffer, out_buffer_size, store_ctx_is_dummy, dl_ctx->alloc );
dl_binary_writer writer;
dl_binary_writer_init( &writer, out_buffer, out_buffer_size, store_ctx_is_dummy, DL_ENDIAN_HOST, DL_ENDIAN_HOST, DL_PTR_SIZE_HOST );
CArrayStatic<uintptr_t, 256> ptrs( dl_ctx->alloc );
CArrayStatic<CDLBinStoreContext::SString, 128> strings( dl_ctx->alloc );
CDLBinStoreContext store_context( writer, dl_ctx->alloc, ptrs, strings );

dl_data_header* header = (dl_data_header*)out_buffer;
size_t header_plus_alignment = dl_internal_align_up( sizeof( dl_data_header ), type->alignment[DL_PTR_SIZE_HOST] );
Expand Down
3 changes: 1 addition & 2 deletions src/dl_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ static dl_error_t dl_internal_convert_collect_instances_from_member( dl_ctx_t
dl_internal_find_type(ctx, member->type_id),
base_data,
convert_ctx );
break;
case DL_TYPE_STORAGE_STR:
dl_internal_convert_collect_instances_from_str_array( member_data,
member->inline_array_cnt(),
Expand Down Expand Up @@ -513,7 +512,7 @@ static dl_error_t dl_internal_convert_write_member( dl_ctx_t ctx,
case DL_TYPE_STORAGE_PTR:
{
uintptr_t offset = dl_internal_read_ptr_data(member_data, conv_ctx.src_endian, conv_ctx.src_ptr_size);
dl_internal_convert_save_patch_pos( &conv_ctx, writer, dl_binary_writer_tell( writer), offset );
dl_internal_convert_save_patch_pos( &conv_ctx, writer, dl_binary_writer_tell(writer), offset );
}
break;
default:
Expand Down
Loading
Loading