Hash :
51f42bae
Author :
Thomas de Grivel
Date :
2023-04-07T08:28:47
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include "array.h"
#include "tag.h"
void array_clean (s_array *a)
{
assert(a);
free(a->dimensions);
free(a->data);
}
s_array * array_init (s_array *a, e_tag_type type, uw dimension,
const uw *dimensions)
{
uw i = 0;
assert(a);
assert(dimension);
assert(dimensions);
if (! dimension) {
errx(1, "zero dimension");
return NULL;
}
#ifdef DEBUG
while (i < dimension) {
assert(dimensions[i]);
i++;
}
#endif
a->dimension = dimension;
a->dimensions = calloc(dimension, sizeof(s_array_dimension));
i = 0;
while (i < dimension) {
a->dimensions[i].count = dimensions[i];
i++;
}
i--;
a->type = type;
a->dimensions[i].item_size = tag_type_size(type);
while (i > 0) {
i--;
a->dimensions[i].item_size = a->dimensions[i + 1].count *
a->dimensions[i + 1].item_size;
}
a->size = a->dimensions[0].count * a->dimensions[0].item_size;
a->data = calloc(1, a->size);
return a;
}
void * array_data (const s_array *a, const uw *address)
{
uw i = 0;
uw offset = 0;
assert(a);
assert(address);
while (i < a->dimension) {
offset += address[i] * a->dimensions[i].item_size;
}
return (s8 *) a->data + offset;
}