Switch values to having their data refcounted separately.

This allows for values to be "cast" and have a different type, while
still referencing the same data.
This commit is contained in:
2018-05-14 17:41:41 -07:00
parent 3e8c814215
commit cf3136a237
5 changed files with 72 additions and 20 deletions

View File

@@ -80,7 +80,15 @@ libab_result libab_create_table(libab_ref* into, libab_ref* parent);
* @param type the type to give the value.
* @return the result of necessary allocations.
*/
libab_result libab_create_value(libab_ref* into, void* data, libab_ref* type);
libab_result libab_create_value_ref(libab_ref* into, libab_ref* data, libab_ref* type);
/**
* Allocates a new reference counted value with the given type and data.
* @param into the reference to store the allocated data into.
* @param data the type-specific data this value holds.
* @param type the type to give the value.
* @return the result of necessary allocations.
*/
libab_result libab_create_value_raw(libab_ref* into, void* data, libab_ref* type);
/**
* Creates a function list object, storing it in to the given reference.
* @param into the reference to store into.

View File

@@ -15,19 +15,28 @@ struct libab_value_s {
/**
* The data that is specific to this value.
*/
void* data;
libab_ref data;
};
typedef struct libab_value_s libab_value;
/**
* Initializes a new value with the given allocated memory for the data,
* Initializes a new value with the given reference counted data
* and the given type.
* @param data the data for this value. It is freed when the value is released
* according to the free function of the base type.
* @param value the value to initialize.
* @param data the data for this value. Its refcount is decreased when the value is freed.
* @param type the type of this value.
*/
void libab_value_init(libab_value* value, void* data, libab_ref* type);
void libab_value_init_ref(libab_value* value, libab_ref* data, libab_ref* type);
/**
* Initializes a new value with the given raw allocated data, and a type,
* whose basetype's free function is used to release the data when the value is freed.
* @param value the value to initialize.
* @param data the data this value holds.
* @param type the type of this value.
* @return the result of any necessary allocations.
*/
libab_result libab_value_init_raw(libab_value* value, void* data, libab_ref* type);
/**
* Frees the given value.
* @param value the value to free.