Fix THANDLE_MALLOC_WITH_EXTRA_SIZE to be THANDLE_MALLOC_FLEX (#284)

* Make THANDLE_MALLOC_WITH_EXTRA_SIZE be THANDLE_MALLOC_FLEX

* Fix extra_size instances to be named flex
This commit is contained in:
Dan Cristoloveanu 2023-10-30 09:56:14 -07:00 коммит произвёл GitHub
Родитель 39c4a4c9ec
Коммит 3c77c593c7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 56 добавлений и 56 удалений

Просмотреть файл

@ -89,7 +89,7 @@ MOCKABLE_FUNCTION(, void, THANDLE_ASSIGN(T), THANDLE(T) *, t1, THANDLE(T), t2 );
#define THANDLE_TYPE_DEFINE(T)
```
`THANDLE_TYPE_DEFINE` introduces the implementation for the functions in `THANDLE_TYPE_DECLARE` (`THANDLE_ASSIGN`, `THANDLE_INITIALIZE`, `THANDLE_GET_T`) and three new memory management functions `THANDLE_MALLOC(T)`, `THANDLE_MALLOC_WITH_EXTRA_SIZE(T)` and `THANDLE_FREE(T)`.
`THANDLE_TYPE_DEFINE` introduces the implementation for the functions in `THANDLE_TYPE_DECLARE` (`THANDLE_ASSIGN`, `THANDLE_INITIALIZE`, `THANDLE_GET_T`) and three new memory management functions `THANDLE_MALLOC(T)`, `THANDLE_MALLOC_FLEX(T)` and `THANDLE_FREE(T)`.
### THANDLE_MALLOC_WITH_MALLOC_FUNCTIONS(C)
@ -119,33 +119,33 @@ static T* THANDLE_MALLOC(T)(void(*dispose)(T*))
`THANDLE_MALLOC` behaves as `THANDLE_MALLOC_WITH_MALLOC_FUNCTIONS` when called with `malloc_function` `NULL` and with `free_function` `NULL`.
### THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C)
### THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(C)
```c
static T* THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C)(void(*dispose)(T*), size_t extra_size, THANDLE_LL_MALLOC_FLEX_FUNCTION_POINTER_T malloc_flex_function, THANDLE_LL_FREE_FUNCTION_POINTER_T free_function)
static T* THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(C)(void(*dispose)(T*), size_t, nmemb, size_t, size, THANDLE_LL_MALLOC_FLEX_FUNCTION_POINTER_T malloc_flex_function, THANDLE_LL_FREE_FUNCTION_POINTER_T free_function)
```
`THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS` returns a `T*` pointer that has `extra_size` bytes allocated. This is useful for example when allocating a character string in a flexible array structure.
`THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS` returns a `T*` pointer that has (`nmemb` * `size`) bytes allocated. This is useful for example when allocating a character string in a flexible array structure.
**SRS_THANDLE_02_046: [** If `malloc_flex_function` is not `NULL` then `malloc_flex_function` and `free_function` shall be used to allocate memory. **]**
**SRS_THANDLE_02_047: [** If `malloc_flex_function` from `THANDLE_LL_TYPE_DEFINE_WITH_MALLOC_FUNCTIONS` is not `NULL` then `THANDLE_LL_TYPE_DEFINE_WITH_MALLOC_FUNCTIONS`'s `malloc_flex_function` and `free_function` shall be used to allocate/free memory. **]**
**SRS_THANDLE_02_049: [** If no function can be found to allocate/free memory then `THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS` shall use `malloc_flex` and `free`. **]**
**SRS_THANDLE_02_049: [** If no function can be found to allocate/free memory then `THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS` shall use `malloc_flex` and `free`. **]**
**SRS_THANDLE_02_050: [** `THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS` shall allocate memory. **]**
**SRS_THANDLE_02_050: [** `THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS` shall allocate memory for the `THANDLE` and an additional (`nmemb` * `size`) bytes. **]**
**SRS_THANDLE_02_051: [** `THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS` shall initialize the reference count to 1, store `dispose` and `free_function` succeed and return a non-`NULL` `T*`. **]**
**SRS_THANDLE_02_051: [** `THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS` shall initialize the reference count to 1, store `dispose` and `free_function` succeed and return a non-`NULL` `T*`. **]**
**SRS_THANDLE_02_052: [** If allocating memory fails then `THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS` shall fail and return `NULL`. **]**
**SRS_THANDLE_02_052: [** If allocating memory fails then `THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS` shall fail and return `NULL`. **]**
### THANDLE_MALLOC_WITH_EXTRA_SIZE
### THANDLE_MALLOC_FLEX
```c
static T* THANDLE_MALLOC_WITH_EXTRA_SIZE(T)(void(*dispose)(T*), size_t extra_size)
static T* THANDLE_MALLOC_FLEX(T)(void(*dispose)(T*), size_t, nmemb, size_t, size)
```
`THANDLE_MALLOC_WITH_EXTRA_SIZE` return a pointer to `T`. `dispose` is a function that the `THANDLE` calls when the reference count reaches 0 in order to free the resources allocated by the user in `T`. `dispose` can be `NULL` in which case there are no user resources to be de-allocated. `T` is a type that has a flexible array. `extra_size` is the size in bytes of the flexible array.
`THANDLE_MALLOC_FLEX` return a pointer to `T`. `dispose` is a function that the `THANDLE` calls when the reference count reaches 0 in order to free the resources allocated by the user in `T`. `dispose` can be `NULL` in which case there are no user resources to be de-allocated. `T` is a type that has a flexible array. `nmemb` and `size` determine the size in bytes of the flexible array.
### THANDLE_FREE(T)
@ -153,7 +153,7 @@ static T* THANDLE_MALLOC_WITH_EXTRA_SIZE(T)(void(*dispose)(T*), size_t extra_siz
THANDLE_FREE(T)(T* t)
```
`THANDLE_FREE` frees the allocated memory by `THANDLE_MALLOC` or `THANDLE_MALLOC_WITH_EXTRA_SIZE`. It is called when reference count reaches 0 after `dispose`, or it can be called from the user code to free the memory.
`THANDLE_FREE` frees the allocated memory by `THANDLE_MALLOC` or `THANDLE_MALLOC_FLEX`. It is called when reference count reaches 0 after `dispose`, or it can be called from the user code to free the memory.
**SRS_THANDLE_02_016: [** If `t` is `NULL` then `THANDLE_FREE` shall return. **]**
@ -169,7 +169,7 @@ Given a previously constructed `THANDLE(T)`, `THANDLE_GET_T(T)` returns a pointe
**SRS_THANDLE_02_023: [** If `t` is `NULL` then `THANDLE_GET_T(T)` shall return `NULL`. **]**
**SRS_THANDLE_02_024: [** `THANDLE_GET_T(T)` shall return the same pointer as `THANDLE_MALLOC`/`THANDLE_MALLOC_WITH_EXTRA_SIZE` returned at the handle creation time. **]**
**SRS_THANDLE_02_024: [** `THANDLE_GET_T(T)` shall return the same pointer as `THANDLE_MALLOC`/`THANDLE_MALLOC_FLEX` returned at the handle creation time. **]**
### THANDLE_CREATE_FROM_CONTENT_FLEX_WITH_MALLOC_FUNCTIONS(C)

Просмотреть файл

@ -47,7 +47,7 @@
/*thandle_ll_malloc can come from 3 sources from lowest to highest priority:
1. globally: malloc/malloc_flex/free as found at the time of the macro expansion.
2. at type definition time: when the user uses THANDLE_LL_TYPE_DEFINE_WITH_MALLOC_FUNCTIONS
3. at instance creation time: when the user decides to use at instance level of THANDLE specific allocation function by calling THANDLE_LL_MALLOC_WITH_MALLOC_FUNCTIONS / THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS / THANDLE_CREATE_FROM_CONTENT_FLEX_WITH_MALLOC_FUNCTIONS
3. at instance creation time: when the user decides to use at instance level of THANDLE specific allocation function by calling THANDLE_LL_MALLOC_WITH_MALLOC_FUNCTIONS / THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS / THANDLE_CREATE_FROM_CONTENT_FLEX_WITH_MALLOC_FUNCTIONS
*/
typedef void* (*THANDLE_LL_MALLOC_FUNCTION_POINTER_T)(size_t);
@ -85,9 +85,9 @@ typedef struct THANDLE_LL_TYPE_STRUCT_TYPE_TAG(T)
/*given a previous type T, THANDLE_INSPECT introduces a new name that if a function that returns the THANDLE_WRAPPER_TYPE_NAME(T) - useful in debugging. The function has no side-effects.*/
#define THANDLE_INSPECT(T) MU_C2(T,_INSPECT)
/*given a previous type T, THANDLE_MALLOC_WITH_EXTRA_SIZE introduces a new name that mimics "malloc for T, where T is a flex type"*/
/*given a previous type T, THANDLE_MALLOC_FLEX introduces a new name that mimics "malloc for T, where T is a flex type"*/
/*the new name is used to define the name of a static function that allocates memory*/
#define THANDLE_MALLOC_WITH_EXTRA_SIZE(T) MU_C2(T,_MALLOC_WITH_EXTRA_SIZE)
#define THANDLE_MALLOC_FLEX(T) MU_C2(T,_MALLOC_WITH_EXTRA_SIZE)
/*given a previous type T, THANDLE_CREATE_FROM_CONTENT introduces a new name for the function that makes of copy of T and returns a THANDLE to the caller*/
#define THANDLE_CREATE_FROM_CONTENT(T) MU_C2(T,_CREATE_FROM_CONTENT)
@ -99,7 +99,7 @@ typedef struct THANDLE_LL_TYPE_STRUCT_TYPE_TAG(T)
#define THANDLE_GET_SIZEOF(T) MU_C2(T,_GET_SIZE_OF)
/*given a previous type T, THANDLE_FREE introduces a new name that mimics "free for T"*/
/*the new name is used to define the name of a static function that frees the memory allocated by THANDLE_MALLOC/THANDLE_MALLOC_WITH_EXTRA_SIZE*/
/*the new name is used to define the name of a static function that frees the memory allocated by THANDLE_MALLOC/THANDLE_MALLOC_FLEX*/
#define THANDLE_FREE(T) MU_C2(T,_FREE)
/*given a previous type T, THANDLE_DEC_REF introduces a new name that mimics "dec_ref for T"*/
@ -189,11 +189,11 @@ const THANDLE_WRAPPER_TYPE_NAME(T)* const THANDLE_INSPECT(C)(THANDLE(T) t)
return CONTAINING_RECORD(t, THANDLE_WRAPPER_TYPE_NAME(T), data); \
} \
#define THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C) MU_C2(THANDLE_LL_MALLOC_WITH_EXTRA_SIZE_MACRO_WITH_MALLOC_FUNCTIONS_, C)
#define THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(C) MU_C2(THANDLE_LL_MALLOC_WITH_EXTRA_SIZE_MACRO_WITH_MALLOC_FUNCTIONS_, C)
/*THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS returns a new T* with extra size using malloc_flex_function (if not NULL) or THANDLE_LL_TYPE_STRUCT_VAR.malloc_flex (if not NULL) or malloc_flex*/
/*THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS returns a new T* with flex semantics using malloc_flex_function (if not NULL) or THANDLE_LL_TYPE_STRUCT_VAR.malloc_flex (if not NULL) or malloc_flex*/
#define THANDLE_LL_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS_MACRO(C, T) \
static T* THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C)(void(*dispose)(T*), size_t extra_size, THANDLE_LL_MALLOC_FLEX_FUNCTION_POINTER_T malloc_flex_function, THANDLE_LL_FREE_FUNCTION_POINTER_T free_function) \
static T* THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(C)(void(*dispose)(T*), size_t nmemb, size_t size, THANDLE_LL_MALLOC_FLEX_FUNCTION_POINTER_T malloc_flex_function, THANDLE_LL_FREE_FUNCTION_POINTER_T free_function) \
{ \
T* result; \
THANDLE_LL_MALLOC_FLEX_FUNCTION_POINTER_T malloc_flex_function_used; \
@ -212,23 +212,23 @@ static T* THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C)(void(*dispose)
} \
else \
{ \
/*Codes_SRS_THANDLE_02_049: [ If no function can be found to allocate/free memory then THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall use malloc_flex and free. ]*/ \
/*Codes_SRS_THANDLE_02_049: [ If no function can be found to allocate/free memory then THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall use malloc_flex and free. ]*/ \
malloc_flex_function_used = malloc_flex; \
free_function_used = free; \
} \
\
/*Codes_SRS_THANDLE_02_050: [ THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall allocate memory. ]*/ \
THANDLE_WRAPPER_TYPE_NAME(T)* handle_impl = malloc_flex_function_used(sizeof(THANDLE_WRAPPER_TYPE_NAME(T)), extra_size, 1); \
/*Codes_SRS_THANDLE_02_050: [ THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall allocate memory for the THANDLE and an additional (nmemb * size) bytes. ]*/ \
THANDLE_WRAPPER_TYPE_NAME(T)* handle_impl = malloc_flex_function_used(sizeof(THANDLE_WRAPPER_TYPE_NAME(T)), nmemb, size); \
if (handle_impl == NULL) \
{ \
/*Codes_SRS_THANDLE_02_052: [ If allocating memory fails then THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall fail and return NULL. ]*/ \
/*Codes_SRS_THANDLE_02_052: [ If allocating memory fails then THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall fail and return NULL. ]*/ \
LogError("error in malloc_flex_function_used=%p(sizeof(THANDLE_WRAPPER_TYPE_NAME(" MU_TOSTRING(T) "))=%zu)", \
malloc_flex_function_used, sizeof(THANDLE_WRAPPER_TYPE_NAME(T))); \
result = NULL; \
} \
else \
{ \
/*Codes_SRS_THANDLE_02_051: [ THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall initialize the reference count to 1, store dispose and free_function succeed and return a non-NULL T*. ]*/ \
/*Codes_SRS_THANDLE_02_051: [ THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall initialize the reference count to 1, store dispose and free_function succeed and return a non-NULL T*. ]*/ \
THANDLE_DEBUG_COPY_NAME(T, handle_impl->name); \
handle_impl->dispose = dispose; \
handle_impl->free_function = free_function_used; \
@ -239,9 +239,9 @@ static T* THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C)(void(*dispose)
} \
#define THANDLE_LL_MALLOC_WITH_EXTRA_SIZE_MACRO(C, T) \
static T* THANDLE_MALLOC_WITH_EXTRA_SIZE(C)(void(*dispose)(T*), size_t extra_size) \
static T* THANDLE_MALLOC_FLEX(C)(void(*dispose)(T*), size_t nmemb, size_t size) \
{ \
return THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(C)(dispose, extra_size, NULL, NULL); \
return THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(C)(dispose, nmemb, size, NULL, NULL); \
} \
#define THANDLE_CREATE_FROM_CONTENT_FLEX_WITH_MALLOC_FUNCTIONS(C) MU_C2(THANDLE_CREATE_FROM_CONTENT_FLEX_WITH_MALLOC_FUNCTIONS_, C)
@ -462,7 +462,7 @@ void THANDLE_INITIALIZE(C)(THANDLE(T) * lvalue, THANDLE(T) rvalue)
static T* THANDLE_GET_T(C)(THANDLE(T) t) \
{ \
/*Codes_SRS_THANDLE_02_023: [ If t is NULL then THANDLE_GET_T(T) shall return NULL. ]*/ \
/*Codes_SRS_THANDLE_02_024: [ THANDLE_GET_T(T) shall return the same pointer as THANDLE_MALLOC/THANDLE_MALLOC_WITH_EXTRA_SIZE returned at the handle creation time. ]*/ \
/*Codes_SRS_THANDLE_02_024: [ THANDLE_GET_T(T) shall return the same pointer as THANDLE_MALLOC/THANDLE_MALLOC_FLEX returned at the handle creation time. ]*/ \
return (T*)t; \
}

Просмотреть файл

@ -40,9 +40,9 @@ THANDLE(T_OFF_DUMMY) T_OFF_create_with_malloc_functions(int x)
}
/*uses global*/
THANDLE(T_OFF_DUMMY) T_OFF_create_with_extra_size(int x, const char* s)
THANDLE(T_OFF_DUMMY) T_OFF_create_flex(int x, const char* s)
{
T_OFF_DUMMY* d = THANDLE_MALLOC_WITH_EXTRA_SIZE(T_OFF_DUMMY)(NULL, strlen(s)+1); /*uses global malloc functions*/
T_OFF_DUMMY* d = THANDLE_MALLOC_FLEX(T_OFF_DUMMY)(NULL, strlen(s)+1, 1); /*uses global malloc functions*/
if (d != NULL)
{
d->x = x;
@ -53,9 +53,9 @@ THANDLE(T_OFF_DUMMY) T_OFF_create_with_extra_size(int x, const char* s)
}
/*uses instance*/
THANDLE(T_OFF_DUMMY) T_OFF_create_with_extra_size_with_malloc_functions(int x, const char* s)
THANDLE(T_OFF_DUMMY) T_OFF_create_flex_with_malloc_functions(int x, const char* s)
{
T_OFF_DUMMY* d = THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(T_OFF_DUMMY)(NULL, strlen(s) + 1, var_malloc_flex, var_free);
T_OFF_DUMMY* d = THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(T_OFF_DUMMY)(NULL, strlen(s) + 1, 1, var_malloc_flex, var_free);
if (d != NULL)
{
d->x = x;

Просмотреть файл

@ -25,8 +25,8 @@ T_OFF will use THANDLE_LL_TYPE_DEFINE (will not use THANDLE_TYPE_DEFINE_WITH_MAL
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create, int, x);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_with_malloc_functions, int, x);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_with_extra_size, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_with_extra_size_with_malloc_functions, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_flex, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_flex_with_malloc_functions, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_from_content_flex, const T_OFF_DUMMY*, origin);
MOCKABLE_FUNCTION(, THANDLE(T_OFF_DUMMY), T_OFF_create_from_content_flex_with_malloc_functions, const T_OFF_DUMMY*, origin);

Просмотреть файл

@ -40,9 +40,9 @@ THANDLE(T_ON_DUMMY) T_ON_create_with_malloc_functions(int x)
}
/*uses type defined functions*/
THANDLE(T_ON_DUMMY) T_ON_create_with_extra_size(int x, const char* s)
THANDLE(T_ON_DUMMY) T_ON_create_flex(int x, const char* s)
{
T_ON_DUMMY* d = THANDLE_MALLOC_WITH_EXTRA_SIZE(T_ON_DUMMY)(NULL, strlen(s) + 1);
T_ON_DUMMY* d = THANDLE_MALLOC_FLEX(T_ON_DUMMY)(NULL, strlen(s) + 1, 1);
if (d != NULL)
{
d->x = x;
@ -53,9 +53,9 @@ THANDLE(T_ON_DUMMY) T_ON_create_with_extra_size(int x, const char* s)
}
/*uses instance*/
THANDLE(T_ON_DUMMY) T_ON_create_with_extra_size_with_malloc_functions(int x, const char* s)
THANDLE(T_ON_DUMMY) T_ON_create_flex_with_malloc_functions(int x, const char* s)
{
T_ON_DUMMY* d = THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS(T_ON_DUMMY)(NULL, strlen(s) + 1, var_malloc_flex, var_free);
T_ON_DUMMY* d = THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS(T_ON_DUMMY)(NULL, strlen(s) + 1, 1, var_malloc_flex, var_free);
if (d != NULL)
{
d->x = x;

Просмотреть файл

@ -26,8 +26,8 @@ T_ON will use THANDLE_LL_TYPE_DEFINE (will not use THANDLE_TYPE_DEFINE_WITH_MALL
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create, int, x);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_with_malloc_functions, int, x);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_with_extra_size, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_with_extra_size_with_malloc_functions, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_flex, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_flex_with_malloc_functions, int, x, const char*, s);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_from_content_flex, const T_ON_DUMMY*, origin);
MOCKABLE_FUNCTION(, THANDLE(T_ON_DUMMY), T_ON_create_from_content_flex_with_malloc_functions, const T_ON_DUMMY*, origin);

Просмотреть файл

@ -139,13 +139,13 @@ TEST_FUNCTION(T_ON_create_calls_var_malloc_unappy_path)
}
/*Tests_SRS_THANDLE_02_047: [ If malloc_flex_function from THANDLE_LL_TYPE_DEFINE_WITH_MALLOC_FUNCTIONS is not NULL then THANDLE_LL_TYPE_DEFINE_WITH_MALLOC_FUNCTIONS's malloc_flex_function and free_function shall be used to allocate/free memory. ]*/
TEST_FUNCTION(T_ON_create_with_extra_size_calls_type_malloc)
TEST_FUNCTION(T_ON_create_flex_calls_type_malloc)
{
///arrange
STRICT_EXPECTED_CALL(type_malloc_flex(IGNORED_ARG, IGNORED_ARG, IGNORED_ARG));
///act
THANDLE(T_ON_DUMMY) dummy = T_ON_create_with_extra_size(5, "a");
THANDLE(T_ON_DUMMY) dummy = T_ON_create_flex(5, "a");
///assert
ASSERT_IS_NOT_NULL(dummy);
@ -159,13 +159,13 @@ TEST_FUNCTION(T_ON_create_with_extra_size_calls_type_malloc)
}
/*Tests_SRS_THANDLE_02_046: [ If malloc_flex_function is not NULL then malloc_flex_function and free_function shall be used to allocate memory. ]*/
TEST_FUNCTION(T_ON_create_with_extra_size_with_malloc_functions_calls_var_malloc)
TEST_FUNCTION(T_ON_create_flex_with_malloc_functions_calls_var_malloc)
{
///arrange
STRICT_EXPECTED_CALL(var_malloc_flex(IGNORED_ARG, IGNORED_ARG, IGNORED_ARG));
///act
THANDLE(T_ON_DUMMY) dummy = T_ON_create_with_extra_size_with_malloc_functions(6, "ab");
THANDLE(T_ON_DUMMY) dummy = T_ON_create_flex_with_malloc_functions(6, "ab");
///assert
ASSERT_IS_NOT_NULL(dummy);
@ -184,7 +184,7 @@ TEST_FUNCTION(T_ON_create_from_content_flex_calls_type_malloc)
{
///arrange
STRICT_EXPECTED_CALL(type_malloc_flex(IGNORED_ARG, IGNORED_ARG, IGNORED_ARG));
THANDLE(T_ON_DUMMY) origin = T_ON_create_with_extra_size(7, "abc");
THANDLE(T_ON_DUMMY) origin = T_ON_create_flex(7, "abc");
ASSERT_IS_NOT_NULL(origin);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
@ -211,7 +211,7 @@ TEST_FUNCTION(T_ON_create_from_content_flex_with_malloc_functions_calls_var_mall
{
///arrange
STRICT_EXPECTED_CALL(type_malloc_flex(IGNORED_ARG, IGNORED_ARG, IGNORED_ARG));
THANDLE(T_ON_DUMMY) origin = T_ON_create_with_extra_size(7, "abc");
THANDLE(T_ON_DUMMY) origin = T_ON_create_flex(7, "abc");
ASSERT_IS_NOT_NULL(origin);
ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
@ -251,7 +251,7 @@ TEST_FUNCTION(T_OFF_uses_malloc_when_no_function_is_specified_1)
THANDLE_ASSIGN(T_OFF_DUMMY)(&dummy, NULL);
}
/*Tests_SRS_THANDLE_02_049: [ If no function can be found to allocate/free memory then THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall use malloc_flex and free. ]*/
/*Tests_SRS_THANDLE_02_049: [ If no function can be found to allocate/free memory then THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall use malloc_flex and free. ]*/
TEST_FUNCTION(T_OFF_uses_malloc_flex_when_no_function_is_specified_2)
{
///arrange
@ -260,7 +260,7 @@ TEST_FUNCTION(T_OFF_uses_malloc_flex_when_no_function_is_specified_2)
//act
THANDLE(T_OFF_DUMMY) dummy = T_OFF_create_with_extra_size(9, "abcde");
THANDLE(T_OFF_DUMMY) dummy = T_OFF_create_flex(9, "abcde");
///assert
ASSERT_IS_NOT_NULL(dummy);

Просмотреть файл

@ -34,10 +34,10 @@ static void frees_the_string(LL_FLEX* ll)
THANDLE(LL_FLEX) ll_flex_create(int a, const char* s, size_t howMany)
{
LL_FLEX* result = THANDLE_MALLOC_WITH_EXTRA_SIZE(LL_FLEX)(frees_the_string, howMany * sizeof(int));
LL_FLEX* result = THANDLE_MALLOC_FLEX(LL_FLEX)(frees_the_string, howMany, sizeof(int));
if (result == NULL)
{
LogError("failure in THANDLE_MALLOC_WITH_EXTRA_SIZE(LL_FLEX)(frees_the_string, howMany=%zu * sizeof(int)=%zu)",
LogError("failure in THANDLE_MALLOC_FLEX(LL_FLEX)(frees_the_string, howMany=%zu, sizeof(int)=%zu)",
howMany, sizeof(int));
/*return as is*/
}

Просмотреть файл

@ -206,10 +206,10 @@ TEST_FUNCTION(thandle_user_create_fails_when_thandle_malloc_fails)
///cleanup
}
/* THANDLE_MALLOC_WITH_EXTRA_SIZE */
/* THANDLE_MALLOC_FLEX */
/*Tests_SRS_THANDLE_02_050: [ THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall allocate memory. ]*/
/*Tests_SRS_THANDLE_02_051: [ THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall initialize the reference count to 1, store dispose and free_function succeed and return a non-NULL T*. ]*/
/*Tests_SRS_THANDLE_02_050: [ THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall allocate memory for the THANDLE and an additional (nmemb * size) bytes. ]*/
/*Tests_SRS_THANDLE_02_051: [ THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall initialize the reference count to 1, store dispose and free_function succeed and return a non-NULL T*. ]*/
TEST_FUNCTION(thandle_flex_user_create_succeeds)
{
///arrange
@ -227,12 +227,12 @@ TEST_FUNCTION(thandle_flex_user_create_succeeds)
THANDLE_ASSIGN(LL_FLEX)(&ll, NULL);
}
/*Tests_SRS_THANDLE_02_052: [ If allocating memory fails then THANDLE_MALLOC_WITH_EXTRA_SIZE_WITH_MALLOC_FUNCTIONS shall fail and return NULL. ]*/
/*Tests_SRS_THANDLE_02_052: [ If allocating memory fails then THANDLE_MALLOC_FLEX_WITH_MALLOC_FUNCTIONS shall fail and return NULL. ]*/
TEST_FUNCTION(thandle_flex_user_create_fails_when_thandle_malloc_fails)
{
///arrange
STRICT_EXPECTED_CALL(malloc_flex(IGNORED_ARG, IGNORED_ARG, IGNORED_ARG))
.SetReturn(NULL); /*this is THANDLE_MALLOC_WITH_EXTRA_SIZE*/
.SetReturn(NULL); /*this is THANDLE_MALLOC_FLEX*/
///act
THANDLE(LL_FLEX) ll = ll_flex_create(TEST_A, TEST_S, 10);
@ -473,7 +473,7 @@ TEST_FUNCTION(THANDLE_GET_T_with_t_NULL_returns_NULL)
///cleanup
}
/*Tests_SRS_THANDLE_02_024: [ THANDLE_GET_T(T) shall return the same pointer as THANDLE_MALLOC/THANDLE_MALLOC_WITH_EXTRA_SIZE returned at the handle creation time. ]*/
/*Tests_SRS_THANDLE_02_024: [ THANDLE_GET_T(T) shall return the same pointer as THANDLE_MALLOC/THANDLE_MALLOC_FLEX returned at the handle creation time. ]*/
TEST_FUNCTION(THANDLE_GET_T_with_t_not_NULL_returns_original_pointer) /*direct testing is not really possible (GET_T is static) but shall be inferred by the actions of _increment_a and _get_a*/
{
///arrange