forked from enlightenment/efl
Cover lazy allocator at 100%. Add error checking capability.
SVN revision: 35320
This commit is contained in:
parent
71e9fe89e9
commit
ba49a3fee1
|
@ -8,16 +8,16 @@
|
|||
* @{
|
||||
*/
|
||||
|
||||
typedef void (*Eina_Lalloc_Alloc) (void *user_data, int num);
|
||||
typedef Eina_Bool (*Eina_Lalloc_Alloc) (void *user_data, int num);
|
||||
#define EINA_LALLOC_ALLOC(function) ((Eina_Lalloc_Alloc)function)
|
||||
typedef void (*Eina_Lalloc_Free) (void *user_data);
|
||||
#define EINA_LALLOC_FREE(function) ((Eina_Lalloc_Free)function)
|
||||
|
||||
typedef struct _Eina_Lalloc Eina_Lalloc;
|
||||
EAPI void eina_lalloc_free(Eina_Lalloc *a);
|
||||
EAPI void eina_lalloc_delete(Eina_Lalloc *a);
|
||||
EAPI Eina_Lalloc *eina_lalloc_new(void *data, Eina_Lalloc_Alloc alloc_cb, Eina_Lalloc_Free free_cb, int num_init);
|
||||
EAPI void eina_lalloc_elements_add(Eina_Lalloc *a, int num);
|
||||
EAPI void eina_lalloc_element_add(Eina_Lalloc *a);
|
||||
EAPI Eina_Bool eina_lalloc_elements_add(Eina_Lalloc *a, int num);
|
||||
EAPI Eina_Bool eina_lalloc_element_add(Eina_Lalloc *a);
|
||||
|
||||
/** @} */
|
||||
|
||||
|
|
|
@ -35,43 +35,64 @@ EAPI Eina_Lalloc * eina_lalloc_new(void *data, Eina_Lalloc_Alloc alloc_cb, Eina_
|
|||
* To be documented
|
||||
* FIXME: To be fixed
|
||||
*/
|
||||
EAPI void eina_lalloc_element_add(Eina_Lalloc *a)
|
||||
EAPI Eina_Bool eina_lalloc_element_add(Eina_Lalloc *a)
|
||||
{
|
||||
if (a->num_elements == a->num_allocated)
|
||||
{
|
||||
a->num_allocated = (1 << a->acc);
|
||||
a->acc++;
|
||||
a->alloc_cb(a->data, a->num_allocated);
|
||||
}
|
||||
a->num_elements++;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be documented
|
||||
* FIXME: To be fixed
|
||||
*/
|
||||
EAPI void eina_lalloc_elements_add(Eina_Lalloc *a, int num)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = a->num_elements + num;
|
||||
if (tmp > a->num_allocated)
|
||||
{
|
||||
while (tmp > a->num_allocated)
|
||||
if (a->alloc_cb(a->data, (1 << a->acc)) == EINA_TRUE)
|
||||
{
|
||||
a->num_allocated = (1 << a->acc);
|
||||
a->acc++;
|
||||
} else {
|
||||
return EINA_FALSE;
|
||||
}
|
||||
a->alloc_cb(a->data, a->num_allocated);
|
||||
}
|
||||
a->num_elements += num;
|
||||
a->num_elements++;
|
||||
|
||||
return EINA_TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be documented
|
||||
* FIXME: To be fixed
|
||||
*/
|
||||
EAPI void eina_lalloc_free(Eina_Lalloc *a)
|
||||
EAPI Eina_Bool eina_lalloc_elements_add(Eina_Lalloc *a, int num)
|
||||
{
|
||||
int tmp;
|
||||
|
||||
tmp = a->num_elements + num;
|
||||
if (tmp > a->num_allocated)
|
||||
{
|
||||
int allocated;
|
||||
int acc;
|
||||
|
||||
allocated = a->num_allocated;
|
||||
acc = a->acc;
|
||||
|
||||
while (tmp > allocated)
|
||||
{
|
||||
allocated = (1 << acc);
|
||||
acc++;
|
||||
}
|
||||
|
||||
if (a->alloc_cb(a->data, allocated) == EINA_TRUE)
|
||||
{
|
||||
a->num_allocated = allocated;
|
||||
a->acc = acc;
|
||||
} else {
|
||||
return EINA_FALSE;
|
||||
}
|
||||
}
|
||||
a->num_elements += num;
|
||||
|
||||
return EINA_TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be documented
|
||||
* FIXME: To be fixed
|
||||
*/
|
||||
EAPI void eina_lalloc_delete(Eina_Lalloc *a)
|
||||
{
|
||||
a->free_cb(a->data);
|
||||
free(a);
|
||||
|
|
|
@ -16,7 +16,8 @@ eina_test_stringshare.c \
|
|||
eina_test_array.c \
|
||||
eina_test_error.c \
|
||||
eina_test_magic.c \
|
||||
eina_test_inlist.c
|
||||
eina_test_inlist.c \
|
||||
eina_test_lalloc.c
|
||||
|
||||
eina_suite_LDADD = @CHECK_LIBS@ $(top_builddir)/src/lib/libeina.la
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ static const Eina_Test_Case etc[] = {
|
|||
{ "Error", eina_test_error },
|
||||
{ "Magic", eina_test_magic },
|
||||
{ "Inlist", eina_test_inlist },
|
||||
{ "Lazy alloc", eina_test_lalloc },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
|
|
|
@ -12,5 +12,6 @@ void eina_test_array(TCase *tc);
|
|||
void eina_test_error(TCase *tc);
|
||||
void eina_test_magic(TCase *tc);
|
||||
void eina_test_inlist(TCase *tc);
|
||||
void eina_test_lalloc(TCase *tc);
|
||||
|
||||
#endif /* EINA_SUITE_H_ */
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "eina_suite.h"
|
||||
#include "eina_lalloc.h"
|
||||
|
||||
typedef struct _Eina_Lazy_Allocator_Test Eina_Lazy_Allocator_Test;
|
||||
struct _Eina_Lazy_Allocator_Test
|
||||
{
|
||||
void *data;
|
||||
int num;
|
||||
};
|
||||
|
||||
static Eina_Bool
|
||||
_test_alloc(Eina_Lazy_Allocator_Test *elat, int num)
|
||||
{
|
||||
fprintf(stderr, "%i %i\n", elat->num, num);
|
||||
|
||||
if (elat->num == 10 && num == 1) return EINA_FALSE;
|
||||
if (elat->num == 122 && num == 128) return EINA_FALSE;
|
||||
|
||||
elat->num += num;
|
||||
elat->data = realloc(elat->data, elat->num);
|
||||
|
||||
return EINA_TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
_test_free(Eina_Lazy_Allocator_Test *elat)
|
||||
{
|
||||
free(elat->data);
|
||||
elat->data = NULL;
|
||||
elat->num = 0;
|
||||
}
|
||||
|
||||
START_TEST(eina_lalloc_simple)
|
||||
{
|
||||
Eina_Lazy_Allocator_Test *elat;
|
||||
Eina_Lalloc *test;
|
||||
int i;
|
||||
|
||||
elat = calloc(1, sizeof (Eina_Lazy_Allocator_Test));
|
||||
fail_if(!elat);
|
||||
|
||||
test = eina_lalloc_new(elat, EINA_LALLOC_ALLOC(_test_alloc), EINA_LALLOC_FREE(_test_free), 10);
|
||||
fail_if(!test);
|
||||
|
||||
for (i = 0; i < 10; ++i)
|
||||
fail_if(eina_lalloc_element_add(test) != EINA_TRUE);
|
||||
fail_if(eina_lalloc_element_add(test) != EINA_FALSE);
|
||||
fail_if(eina_lalloc_elements_add(test, 5) != EINA_TRUE);
|
||||
for (i = 0; i < 21; ++i)
|
||||
fail_if(eina_lalloc_element_add(test) != EINA_TRUE);
|
||||
|
||||
fail_if(eina_lalloc_elements_add(test, 50) != EINA_FALSE);
|
||||
|
||||
eina_lalloc_delete(test);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
void
|
||||
eina_test_lalloc(TCase *tc)
|
||||
{
|
||||
tcase_add_test(tc, eina_lalloc_simple);
|
||||
}
|
Loading…
Reference in New Issue