Commit c2379329 authored by Swann Perarnau's avatar Swann Perarnau

[refactor] use inner-malloc across the library

Use the newly added INNER_MALLOC for all the relevant creators.
parent 28cae4f8
......@@ -197,16 +197,13 @@ int aml_area_linux_create(struct aml_area **area, const int mmap_flags,
return -AML_EINVAL;
}
ret = calloc(1, sizeof(struct aml_area));
ret = AML_INNER_MALLOC_2(struct aml_area, struct aml_area_linux_data);
if (ret == NULL)
return -AML_ENOMEM;
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_area,
struct aml_area_linux_data);
ret->ops = &aml_area_linux_ops;
ret->data = calloc(1, sizeof(struct aml_area_linux_data));
if (ret->data == NULL) {
err = -AML_ENOMEM;
goto err_f_ret;
}
data = (struct aml_area_linux_data *)ret->data;
/* set area_data and area */
......@@ -217,7 +214,7 @@ int aml_area_linux_create(struct aml_area **area, const int mmap_flags,
data->nodeset = numa_get_mems_allowed();
if (data->nodeset == NULL) {
err = -AML_ENOMEM;
goto err_f_data;
goto err_f_ret;
}
/* check if the nodemask is compatible with the nodeset */
......@@ -240,8 +237,6 @@ int aml_area_linux_create(struct aml_area **area, const int mmap_flags,
return AML_SUCCESS;
err_f_node:
numa_free_nodemask(data->nodeset);
err_f_data:
free(ret->data);
err_f_ret:
free(ret);
return err;
......@@ -255,12 +250,13 @@ void aml_area_linux_destroy(struct aml_area **area)
if (area == NULL)
return;
a = *area;
if (a == NULL || a->data == NULL)
if (a == NULL)
return;
/* with our creators it should not happen */
assert(a->data != NULL);
data = (struct aml_area_linux_data *) a->data;
numa_free_nodemask(data->nodeset);
free(data);
free(a);
*area = NULL;
}
......
......@@ -206,17 +206,13 @@ int aml_dma_linux_par_create(struct aml_dma **dma, size_t nbreqs,
*dma = NULL;
/* alloc */
ret = calloc(1, sizeof(struct aml_dma));
ret = AML_INNER_MALLOC_2(struct aml_dma, struct aml_dma_linux_par);
if (ret == NULL)
return -AML_ENOMEM;
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_dma,
struct aml_dma_linux_par);
ret->ops = &aml_dma_linux_par_ops;
ret->data = calloc(1, sizeof(struct aml_dma_linux_par));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
d = (struct aml_dma_linux_par *)ret->data;
d->ops = aml_dma_linux_par_inner_ops;
......@@ -247,8 +243,10 @@ void aml_dma_linux_par_destroy(struct aml_dma **dma)
if (dma == NULL)
return;
d = *dma;
if (d == NULL || d->data == NULL)
if (d == NULL)
return;
assert(d->data != NULL);
l = (struct aml_dma_linux_par *)d->data;
for (size_t i = 0; i < aml_vector_size(l->data.requests); i++) {
struct aml_dma_request_linux_par *req =
......@@ -259,7 +257,6 @@ void aml_dma_linux_par_destroy(struct aml_dma **dma)
aml_vector_destroy(&l->data.requests);
pthread_mutex_destroy(&l->data.lock);
free(l);
free(d);
*dma = NULL;
}
......@@ -164,16 +164,12 @@ int aml_dma_linux_seq_create(struct aml_dma **dma, size_t nbreqs)
*dma = NULL;
/* alloc */
ret = calloc(1, sizeof(struct aml_dma));
ret = AML_INNER_MALLOC_2(struct aml_dma, struct aml_dma_linux_seq);
if (ret == NULL)
return -AML_ENOMEM;
ret->data = calloc(1, sizeof(struct aml_dma_linux_seq));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_dma,
struct aml_dma_linux_seq);
ret->ops = &aml_dma_linux_seq_ops;
d = (struct aml_dma_linux_seq *)ret->data;
......@@ -196,12 +192,13 @@ void aml_dma_linux_seq_destroy(struct aml_dma **dma)
if (dma == NULL)
return;
d = *dma;
if (d == NULL || d->data == NULL)
if (d == NULL)
return;
assert(d->data != NULL);
l = (struct aml_dma_linux_seq *)d->data;
aml_vector_destroy(&l->data.requests);
pthread_mutex_destroy(&l->data.lock);
free(l);
free(d);
*dma = NULL;
}
......@@ -253,17 +253,13 @@ int aml_scratch_par_create(struct aml_scratch **scratch,
*scratch = NULL;
/* alloc */
ret = calloc(1, sizeof(struct aml_scratch));
ret = AML_INNER_MALLOC_2(struct aml_scratch, struct aml_scratch_par);
if (ret == NULL)
return -AML_ENOMEM;
ret->ops = &aml_scratch_par_ops;
ret->data = calloc(1, sizeof(struct aml_scratch_par));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_scratch,
struct aml_scratch_par);
s = (struct aml_scratch_par *)ret->data;
s->ops = aml_scratch_par_inner_ops;
......@@ -300,8 +296,10 @@ void aml_scratch_par_destroy(struct aml_scratch **scratch)
if (scratch == NULL)
return;
s = *scratch;
if (s == NULL || s->data == NULL)
if (s == NULL)
return;
assert(s->data != NULL);
inner = (struct aml_scratch_par *)s->data;
aml_vector_destroy(&inner->data.requests);
aml_vector_destroy(&inner->data.tilemap);
......@@ -309,7 +307,6 @@ void aml_scratch_par_destroy(struct aml_scratch **scratch)
inner->data.sch_ptr,
inner->data.scratch_size);
pthread_mutex_destroy(&inner->data.lock);
free(inner);
free(s);
*scratch = NULL;
}
......@@ -253,17 +253,13 @@ int aml_scratch_seq_create(struct aml_scratch **scratch,
*scratch = NULL;
/* alloc */
ret = calloc(1, sizeof(struct aml_scratch));
ret = AML_INNER_MALLOC_2(struct aml_scratch, struct aml_scratch_seq);
if (ret == NULL)
return -AML_ENOMEM;
ret->ops = &aml_scratch_seq_ops;
ret->data = calloc(1, sizeof(struct aml_scratch_seq));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_scratch,
struct aml_scratch_seq);
s = (struct aml_scratch_seq *)ret->data;
s->ops = aml_scratch_seq_inner_ops;
......@@ -300,8 +296,10 @@ void aml_scratch_seq_destroy(struct aml_scratch **scratch)
if (scratch == NULL)
return;
s = *scratch;
if (s == NULL || s->data == NULL)
if (s == NULL)
return;
assert(s->data != NULL);
inner = (struct aml_scratch_seq *)s->data;
aml_vector_destroy(&inner->data.requests);
aml_vector_destroy(&inner->data.tilemap);
......@@ -309,7 +307,6 @@ void aml_scratch_seq_destroy(struct aml_scratch **scratch)
inner->data.sch_ptr,
inner->data.scratch_size);
pthread_mutex_destroy(&inner->data.lock);
free(inner);
free(s);
*scratch = NULL;
}
......@@ -128,16 +128,14 @@ int aml_tiling_1d_create_iterator(struct aml_tiling_data *tiling,
*it = NULL;
ret = calloc(1, sizeof(struct aml_tiling_iterator));
ret = AML_INNER_MALLOC_2(struct aml_tiling_iterator,
struct aml_tiling_iterator_1d_data);
if (ret == NULL)
return -AML_ENOMEM;
ret->ops = &aml_tiling_iterator_1d_ops;
ret->data = calloc(1, sizeof(struct aml_tiling_iterator_1d_data));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_tiling_iterator,
struct aml_tiling_iterator_1d_data);
data = (struct aml_tiling_iterator_1d_data *)ret->data;
data->i = 0;
data->tiling = (struct aml_tiling_1d_data *)tiling;
......@@ -154,9 +152,8 @@ int aml_tiling_1d_destroy_iterator(struct aml_tiling_data *t,
if (iter == NULL)
return -AML_EINVAL;
it = *iter;
if (it == NULL || it->data == NULL)
if (it == NULL)
return -AML_EINVAL;
free(it->data);
free(it);
*iter = NULL;
return AML_SUCCESS;
......@@ -186,17 +183,13 @@ int aml_tiling_1d_create(struct aml_tiling **tiling,
*tiling = NULL;
/* alloc */
ret = calloc(1, sizeof(struct aml_tiling));
ret = AML_INNER_MALLOC_2(struct aml_tiling, struct aml_tiling_1d_data);
if (ret == NULL)
return -AML_ENOMEM;
ret->ops = &aml_tiling_1d_ops;
ret->data = calloc(1, sizeof(struct aml_tiling_1d_data));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_tiling,
struct aml_tiling_1d_data);
t = (struct aml_tiling_1d_data *) ret->data;
t->blocksize = tilesize;
......@@ -213,9 +206,8 @@ void aml_tiling_1d_destroy(struct aml_tiling **tiling)
if (tiling == NULL)
return;
t = *tiling;
if (t == NULL || t->data == NULL)
if (t == NULL)
return;
free(t->data);
free(t);
*tiling = NULL;
}
......
......@@ -136,16 +136,14 @@ int aml_tiling_2d_create_iterator(struct aml_tiling_data *tiling,
*it = NULL;
ret = calloc(1, sizeof(struct aml_tiling_iterator));
ret = AML_INNER_MALLOC_2(struct aml_tiling_iterator,
struct aml_tiling_iterator_2d_data);
if (ret == NULL)
return -AML_ENOMEM;
ret->ops = &aml_tiling_iterator_2d_ops;
ret->data = calloc(1, sizeof(struct aml_tiling_iterator_2d_data));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_tiling_iterator,
struct aml_tiling_iterator_2d_data);
data = (struct aml_tiling_iterator_2d_data *)ret->data;
data->i = 0;
data->tiling = (struct aml_tiling_2d_data *)tiling;
......@@ -162,9 +160,8 @@ int aml_tiling_2d_destroy_iterator(struct aml_tiling_data *t,
if (iter == NULL)
return -AML_EINVAL;
it = *iter;
if (it == NULL || it->data == NULL)
if (it == NULL)
return -AML_EINVAL;
free(it->data);
free(it);
*iter = NULL;
return AML_SUCCESS;
......@@ -211,8 +208,7 @@ int aml_tiling_2d_create(struct aml_tiling **tiling, int type,
*tiling = NULL;
/* alloc */
ret = calloc(1, sizeof(struct aml_tiling));
ret = AML_INNER_MALLOC_2(struct aml_tiling, struct aml_tiling_2d_data);
if (ret == NULL)
return -AML_ENOMEM;
......@@ -221,11 +217,8 @@ int aml_tiling_2d_create(struct aml_tiling **tiling, int type,
else
ret->ops = &aml_tiling_2d_colmajor_ops;
ret->data = calloc(1, sizeof(struct aml_tiling_2d_data));
if (ret->data == NULL) {
free(ret);
return -AML_ENOMEM;
}
ret->data = AML_INNER_MALLOC_NEXTPTR(ret, struct aml_tiling,
struct aml_tiling_2d_data);
data = (struct aml_tiling_2d_data *)ret->data;
data->blocksize = tilesize;
......@@ -244,9 +237,8 @@ void aml_tiling_2d_destroy(struct aml_tiling **tiling)
if (tiling == NULL)
return;
t = *tiling;
if (t == NULL || t->data == NULL)
if (t == NULL)
return;
free(t->data);
free(t);
*tiling = NULL;
}
......
......@@ -13,13 +13,9 @@
int main(void)
{
assert(AML_SIZEOF_ALIGN_GENERIC(16, size_t) % 16 == 0);
assert(AML_SIZEOF_ALIGN_GENERIC(16, char) % 16 == 0);
assert(AML_SIZEOF_ALIGN_DEFAULT(struct aml_area) %
AML_DEFAULT_INNER_MALLOC_ALIGN == 0);
intptr_t *ptr = AML_INNER_MALLOC_2(void *, void *);
assert(ptr != NULL);
void *b = AML_INNER_MALLOC_NEXTPTR(ptr, void *);
void *b = AML_INNER_MALLOC_NEXTPTR(ptr, void *, void *);
assert(b == &ptr[1]);
free(ptr);
return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment