Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions include/dl/dl_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
extern "C" {
#endif // __cplusplus

struct dl_allocator;
/*
Enum: dl_util_file_type_t
Enumeration of possible file types that can be read by util-functions.
Expand Down Expand Up @@ -52,13 +53,16 @@ typedef enum
filetype - Type of file to read, see dl_util_file_type_t.
out_instance - Pointer to fill with read instance.
out_type - TypeID of instance found in file, can be set to 0x0.
allocator - Allocator for doing temp file allocations. 0x0 / nullpointer is also
valid and will default to using malloc (default behavior of dl).

Returns:
DL_ERROR_OK on success.
*/
dl_error_t dl_util_load_from_file( dl_ctx_t dl_ctx, dl_typeid_t type,
const char* filename, dl_util_file_type_t filetype,
void** out_instance, dl_typeid_t* out_type );
void** out_instance, dl_typeid_t* out_type,
dl_allocator *allocator );

/*
Function: dl_util_load_from_stream
Expand All @@ -71,21 +75,23 @@ dl_error_t dl_util_load_from_file( dl_ctx_t dl_ctx, dl_typeid_t
more.

Parameters:
dl_ctx - Context to use for operations.
type - Type expected to be found in file, set to 0 if not known.
stream - Open stream to load from.
filetype - Type of file to read, see dl_util_file_type_t.
out_instance - Pointer to fill with read instance.
out_type - TypeID of instance found in file, can be set to 0x0.
consumed_bytes - Number of bytes read from stream.
dl_ctx - Context to use for operations.
type - Type expected to be found in file, set to 0 if not known.
stream - Open stream to load from.
filetype - Type of file to read, see dl_util_file_type_t.
out_instance - Pointer to fill with read instance.
out_type - TypeID of instance found in file, can be set to 0x0.
consumed_bytes - Number of bytes read from stream.
allocator - Allocator for doing temp file allocations. 0x0 / nullpointer is also
valid and will default to using malloc (default behavior of dl).

Returns:
DL_ERROR_OK on success.
*/
dl_error_t dl_util_load_from_stream( dl_ctx_t dl_ctx, dl_typeid_t type,
FILE* stream, dl_util_file_type_t filetype,
void** out_instance, dl_typeid_t* out_type,
size_t* consumed_bytes );
size_t* consumed_bytes, dl_allocator *allocator );

/*
Function: dl_util_load_from_file_inplace
Expand All @@ -96,20 +102,22 @@ dl_error_t dl_util_load_from_stream( dl_ctx_t dl_ctx, dl_typeid_t
be used accordingly.

Parameters:
dl_ctx - Context to use for operations.
type - Type expected to be found in file.
filename - Path to file to load from.
filetype - Type of file to read, see EDLUtilFileType.
out_instance - Pointer to area to load instance to.
out_instance_size - Size of buffer pointed to by _ppInstance
dl_ctx - Context to use for operations.
type - Type expected to be found in file.
filename - Path to file to load from.
filetype - Type of file to read, see EDLUtilFileType.
out_instance - Pointer to area to load instance to.
out_instance_size - Size of buffer pointed to by _ppInstance
allocator - Allocator for doing temp file allocations. 0x0 / nullpointer is also
valid and will default to using malloc (default behavior of dl).

Returns:
DL_ERROR_OK on success.
*/
dl_error_t dl_util_load_from_file_inplace( dl_ctx_t dl_ctx, dl_typeid_t type,
const char* filename, dl_util_file_type_t filetype,
void* out_instance, size_t out_instance_size,
dl_typeid_t* out_type );
dl_typeid_t* out_type, dl_allocator *allocator );

/*
Function: dl_util_store_to_file
Expand All @@ -127,14 +135,16 @@ dl_error_t dl_util_load_from_file_inplace( dl_ctx_t dl_ctx, dl_typeid_
out_endian - Endian of stored instance if binary.
out_ptr_size - Pointer size of stored instance if binary.
out_instance - Pointer to instance to write
allocator - Allocator for doing temp file allocations. 0x0 / nullpointer is also
valid and will default to using malloc (default behavior of dl).

Returns:
DL_ERROR_OK on success.
*/
dl_error_t dl_util_store_to_file( dl_ctx_t dl_ctx, dl_typeid_t type,
const char* filename, dl_util_file_type_t filetype,
dl_endian_t out_endian, size_t out_ptr_size,
const void* out_instance );
const void* out_instance, dl_allocator *allocator );

/*
Function: dl_util_store_to_stream
Expand All @@ -152,14 +162,16 @@ dl_error_t dl_util_store_to_file( dl_ctx_t dl_ctx, dl_typeid_t ty
out_endian - Endian of stored instance if binary.
out_ptr_size - Pointer size of stored instance if binary.
out_instance - Pointer to instance to write
allocator - Allocator for doing temp file allocations. 0x0 / nullpointer is also
valid and will default to using malloc (default behavior of dl).

Returns:
DL_ERROR_OK on success.
*/
dl_error_t dl_util_store_to_stream( dl_ctx_t dl_ctx, dl_typeid_t type,
FILE* stream, dl_util_file_type_t filetype,
dl_endian_t out_endian, size_t out_ptr_size,
const void* out_instance );
const void* out_instance, dl_allocator *allocator );

#ifdef __cplusplus
}
Expand Down
8 changes: 4 additions & 4 deletions src/dl_alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static void dl_internal_free( void* ptr, void* /*ctx*/ )
free( ptr );
}

bool dl_allocator_initialize( dl_allocator* alloc, dl_alloc_func alloc_f, dl_realloc_func realloc_f, dl_free_func free_f, void* alloc_ctx )
int dl_allocator_initialize( dl_allocator* alloc, dl_alloc_func alloc_f, dl_realloc_func realloc_f, dl_free_func free_f, void* alloc_ctx )
{
if( alloc_f == 0x0 && free_f == 0x0 && realloc_f == 0x0 )
{
Expand All @@ -26,19 +26,19 @@ bool dl_allocator_initialize( dl_allocator* alloc, dl_alloc_func alloc_f, dl_rea
alloc->realloc = dl_internal_realloc;
alloc->free = dl_internal_free;
alloc->ctx = 0x0;
return true;
return 1;
}

if( alloc_f == 0x0 || free_f == 0x0 )
{
// invalid setup!
return false;
return 0;
}

alloc->alloc = alloc_f;
alloc->free = free_f;
alloc->realloc = realloc_f;
alloc->ctx = alloc_ctx;

return true;
return 1;
}
16 changes: 13 additions & 3 deletions src/dl_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
#include <dl/dl.h>
#include <string.h>

struct dl_allocator
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

typedef struct dl_allocator
{
dl_alloc_func alloc;
dl_realloc_func realloc;
dl_free_func free;
void* ctx;
};
} dl_allocator;

/**
* Initialize a dl_allocator to be used internally in DL.
*
* If alloc_f and free_f is both NULL, malloc, realloc and free will be used.
* If alloc_f and free_f is not NULL, but realloc_f if NULL a fallback using alloc_f and free_f together with memcpy will be used.
* Returns 0 if initialization fails, 1 if it succeeds.
*/
bool dl_allocator_initialize( dl_allocator* alloc, dl_alloc_func alloc_f, dl_realloc_func realloc_f, dl_free_func free_f, void* alloc_ctx );
int dl_allocator_initialize( dl_allocator* alloc, dl_alloc_func alloc_f, dl_realloc_func realloc_f, dl_free_func free_f, void* alloc_ctx );

/**
* Allocator memory on an allocator.
Expand Down Expand Up @@ -53,5 +58,10 @@ inline void* dl_realloc( dl_allocator* alloc, void* ptr, size_t size, size_t old
return new_ptr;
}

#ifdef __cplusplus
}
#endif // __cplusplus


#endif // DL_ALLOC_H_INCLUDED

Loading