Commit 82e6a73e authored by Swann Perarnau's avatar Swann Perarnau

[refactor] add inner-malloc to layouts

Layout are a bit more complex as they contain extra arrays at the end
of the struct, so new macros are required.
parent c2379329
Pipeline #8187 passed with stages
in 28 minutes and 38 seconds
......@@ -28,7 +28,7 @@ make:generic:
- /^wip.*/
- /^WIP.*/
variables:
CFLAGS: "-std=c99 -pedantic -Wall -Wextra -Werror -Wno-pointer-arith -Wno-unused-but-set-parameter"
CFLAGS: "-std=c99 -pedantic -Wall -Wextra -Werror -Wno-unused-but-set-parameter"
script:
- ./autogen.sh
- mkdir build
......@@ -48,7 +48,7 @@ make:out-of-tree:
- /^wip.*/
- /^WIP.*/
variables:
CFLAGS: "-std=c99 -pedantic -Wall -Wextra -Werror -Wno-pointer-arith -Wno-unused-but-set-parameter"
CFLAGS: "-std=c99 -pedantic -Wall -Wextra -Werror -Wno-unused-but-set-parameter"
script:
- ./autogen.sh
- mkdir out
......
......@@ -45,6 +45,14 @@
**/
#define AML_INNER_MALLOC_2(a, b) calloc(1, AML_SIZEOF_ALIGNED(a, b))
/** Allocate a pointer that can be used to contain two types plus an extra area
* aligned on a third type.
*
**/
#define AML_INNER_MALLOC_EXTRA(a, b, c, sz) \
calloc(1, AML_SIZEOF_ALIGNED(struct { a __f1; b __f2; }, c) + \
(sizeof(c)*sz))
/** Returns the next pointer after an AML_INNER_MALLOC.
*
* Can be used to iterate over the pointers we need, using the last two types as
......@@ -53,6 +61,15 @@
#define AML_INNER_MALLOC_NEXTPTR(ptr, a, b) \
(void *)(((intptr_t) ptr) + AML_OFFSETOF_ALIGNED(a, b))
/** Returns a pointer inside the extra zone after an AML_INNER_MALLOC_EXTRA.
*
* Can be used to iterate over the pointers we need.
**/
#define AML_INNER_MALLOC_EXTRA_NEXTPTR(ptr, a, b, c, off) \
(void *)(((intptr_t) ptr) + \
AML_OFFSETOF_ALIGNED(struct { a __f1; b __f2; }, c) + \
((off)*sizeof(c)))
/**
* @}
**/
......
......@@ -12,41 +12,47 @@
#include "aml/layout/native.h"
#include "aml/layout/dense.h"
static struct aml_layout *aml_layout_dense_alloc(const size_t ndims)
static int aml_layout_dense_alloc(struct aml_layout **ret,
const size_t ndims)
{
struct aml_layout_dense *data;
struct aml_layout *layout;
char *l;
layout = malloc((sizeof(struct aml_layout) +
sizeof(struct aml_layout_dense) +
(ndims * 4) * sizeof(size_t)));
struct aml_layout_dense *data;
layout = AML_INNER_MALLOC_EXTRA(struct aml_layout,
struct aml_layout_dense,
size_t, 4*ndims);
if (layout == NULL) {
perror("malloc");
return NULL;
*ret = NULL;
return -AML_ENOMEM;
}
l = (char *) layout;
l += sizeof(*layout);
data = (struct aml_layout_dense *) l;
l += sizeof(*data);
data->dims = (size_t *) l;
l += sizeof(*data->dims) * ndims;
data->stride = (size_t *) l;
data = AML_INNER_MALLOC_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_dense);
layout->data = (struct aml_layout_data *) data;
data->dims = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_dense,
size_t, 0);
data->stride = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_dense,
size_t, ndims);
for (size_t i = 0; i < ndims; i++)
data->stride[i] = 1;
l += sizeof(*data->stride) * ndims;
data->pitch = (size_t *) l;
l += sizeof(*data->pitch) * ndims;
data->cpitch = (size_t *) l;
data->pitch = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_dense,
size_t, ndims*2);
data->cpitch = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_dense,
size_t, ndims*3);
data->ptr = NULL;
data->ndims = ndims;
layout->data = (struct aml_layout_data *) data;
return layout;
*ret = layout;
return AML_SUCCESS;
}
static
......@@ -79,16 +85,16 @@ int aml_layout_dense_create(struct aml_layout **layout,
struct aml_layout *l;
struct aml_layout_dense *data;
int err;
if (layout == NULL)
return -AML_EINVAL;
l = aml_layout_dense_alloc(ndims);
if (l == NULL)
return -AML_ENOMEM;
err = aml_layout_dense_alloc(&l, ndims);
if (err)
return err;
data = (struct aml_layout_dense *)l->data;
data->ptr = ptr;
data->cpitch[0] = element_size;
......@@ -305,9 +311,9 @@ int aml_layout_column_reshape(const struct aml_layout_data *data,
d = (const struct aml_layout_dense *)data;
layout = aml_layout_dense_alloc(ndims);
if (layout == NULL)
return -AML_ENOMEM;
err = aml_layout_dense_alloc(&layout, ndims);
if (err)
return err;
err = reshape_dims(d, ndims, dims, stride, cpitch);
if (err != AML_SUCCESS) {
......@@ -336,13 +342,14 @@ int aml_layout_column_slice(const struct aml_layout_data *data,
struct aml_layout *layout;
const struct aml_layout_dense *d;
void *ptr;
int err;
d = (const struct aml_layout_dense *)data;
ptr = aml_layout_column_deref(data, offsets);
layout = aml_layout_dense_alloc(d->ndims);
if (layout == NULL)
return -AML_ENOMEM;
err = aml_layout_dense_alloc(&layout, d->ndims);
if (err)
return err;
size_t cpitch[d->ndims + 1];
size_t new_strides[d->ndims];
......@@ -432,9 +439,9 @@ int aml_layout_row_reshape(const struct aml_layout_data *data,
int err;
d = (const struct aml_layout_dense *)data;
layout = aml_layout_dense_alloc(ndims);
if (layout == NULL)
return -AML_ENOMEM;
err = aml_layout_dense_alloc(&layout, ndims);
if (err)
return err;
for (size_t i = 0; i < ndims; i++)
n_dims[ndims - i - 1] = dims[i];
......@@ -466,6 +473,7 @@ int aml_layout_row_slice(const struct aml_layout_data *data,
struct aml_layout *layout;
const struct aml_layout_dense *d;
void *ptr;
int err;
d = (const struct aml_layout_dense *)data;
......@@ -474,9 +482,9 @@ int aml_layout_row_slice(const struct aml_layout_data *data,
size_t n_dims[d->ndims];
size_t n_strides[d->ndims];
layout = aml_layout_dense_alloc(d->ndims);
if (layout == NULL)
return -AML_ENOMEM;
err = aml_layout_dense_alloc(&layout, d->ndims);
if (err)
return err;
for (size_t i = 0; i < d->ndims; i++) {
n_offsets[i] = offsets[d->ndims - i - 1];
......@@ -513,15 +521,16 @@ int aml_layout_row_slice_native(const struct aml_layout_data *data,
struct aml_layout *layout;
const struct aml_layout_dense *d;
void *ptr;
int err;
d = (const struct aml_layout_dense *)data;
size_t cpitch[d->ndims + 1];
size_t new_strides[d->ndims];
layout = aml_layout_dense_alloc(d->ndims);
if (layout == NULL)
return -AML_ENOMEM;
err = aml_layout_dense_alloc(&layout, d->ndims);
if (err)
return err;
cpitch[d->ndims] = d->cpitch[d->ndims];
for (size_t i = 0; i < d->ndims; i++) {
......
......@@ -14,35 +14,43 @@
#include "aml/layout/dense.h"
#include "aml/layout/reshape.h"
static struct aml_layout *aml_layout_reshape_alloc(const size_t ndims,
const size_t target_ndims)
static int aml_layout_reshape_alloc(struct aml_layout **ret,
const size_t ndims,
const size_t target_ndims)
{
struct aml_layout *l;
struct aml_layout_data_reshape *r;
char *c;
l = malloc(sizeof(struct aml_layout) +
sizeof(struct aml_layout_data_reshape) +
2 * ndims * sizeof(size_t) +
target_ndims * sizeof(size_t));
if (l == NULL)
return NULL;
c = (char *)l;
c += sizeof(*l);
r = (struct aml_layout_data_reshape *)c;
c += sizeof(*r);
r->dims = (size_t *)c;
c += ndims * sizeof(*r->dims);
r->coffsets = (size_t *)c;
c += ndims * sizeof(*r->coffsets);
r->target_dims = (size_t *)c;
r->target = NULL;
r->target_ndims = target_ndims;
r->ndims = ndims;
l->data = (struct aml_layout_data *) r;
return l;
struct aml_layout *layout;
struct aml_layout_data_reshape *data;
layout = AML_INNER_MALLOC_EXTRA(struct aml_layout,
struct aml_layout_data_reshape,
size_t, (2*ndims)+target_ndims);
if (layout == NULL) {
*ret = NULL;
return -AML_ENOMEM;
}
data = AML_INNER_MALLOC_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_data_reshape);
layout->data = (struct aml_layout_data *)data;
data->dims = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_data_reshape,
size_t, 0);
data->coffsets = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_data_reshape,
size_t, ndims);
data->target_dims = AML_INNER_MALLOC_EXTRA_NEXTPTR(layout,
struct aml_layout,
struct aml_layout_data_reshape,
size_t, 2*ndims);
data->target = NULL;
data->target_ndims = target_ndims;
data->ndims = ndims;
*ret = layout;
return AML_SUCCESS;
}
int aml_layout_reshape_create(struct aml_layout **layout,
......@@ -56,14 +64,15 @@ int aml_layout_reshape_create(struct aml_layout **layout,
size_t target_ndims;
size_t prod;
size_t target_prod;
int err;
if (layout == NULL || target == NULL || ndims == 0)
return -AML_EINVAL;
target_ndims = aml_layout_ndims(target);
output = aml_layout_reshape_alloc(ndims, target_ndims);
if (output == NULL)
return -AML_ENOMEM;
err = aml_layout_reshape_alloc(&output, ndims, target_ndims);
if (err)
return err;
data = (struct aml_layout_data_reshape *)output->data;
data->target = target;
......
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