shithub: lpa

ref: ad872eeb19b4fcc41a5d34750ca6cdcf88a39795
dir: /array.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>

#include "dat.h"
#include "fns.h"

/* This file is the only file that knows how arrays are stored.
 * In theory, that allows us to experiment with other representations later.
 */

struct Array
{
	int rank;
	usize *shape;
	union {
		void *data;
		vlong *intdata;
		Rune *chardata;
	};
};

void 
initarrays(void)
{
	dataspecs[DataArray].size = sizeof(Array);
}

Array *
allocarray(int type, int rank, usize size)
{
	Array *a = alloc(DataArray);
	a->rank = rank;

	switch(type){
	case TypeNumber:
		size *= sizeof(vlong);
		break;
	case TypeChar:
		size *= sizeof(Rune);
		break;
	}

	a->shape = allocextra(a, (sizeof(usize) * rank) + size);
	a->data = (void*)(a->shape+rank);

	return a;
}

void
setint(Array *a, usize offset, vlong v)
{
	a->intdata[offset] = v;
}

void
setshape(Array *a, int dim, usize size)
{
	a->shape[dim] = size;
}