Forums > Dev

atomarray_copyatoms and getbytes ?

December 5, 2010 | 5:29 pm

Hello maxers,

in SDK about myobject_getvalueof ( ) function it is said :

By convention, and to permit the interoperability of objects using the obex API, developers should allocate memory in their getvalueof methods using the getbytes() function.

is it good to use atomarray_copyatoms ( ) to allocate memory ?


December 5, 2010 | 7:54 pm

Hi pizza,

it is easier and cleaner to use the atom_alloc() or atom_alloc_array() functions to implement the getvalueof method.

Example to follow:

t_max_err myobject_getvalueof(t_myobject *x, long *argc, t_atom **argv)
{
    if (argc && argv) {
        char alloc;
        if (atom_alloc(argc, argv, &alloc)) {
            return MAX_ERR_OUT_OF_MEM;
        }
        if (alloc) {
            atom_setfloat(*argv, 0.0);
        }
	return MAX_ERR_NONE;
    }
}

Cheers.

- Luigi


December 6, 2010 | 7:38 am

Hello Luigi Castelli,

yep, but as i have an atomarray x->data in my object’s structure, i thought it was easy like that :

t_max_err uniform_getvalueof (t_uniform *x, long *ac, t_atom **av)
{
    if (ac && av)
         {
	       if (*ac || *av)
			{
			    ;
			}
		else
			{
		            atomarray_copyatoms (x->data, ac, av) ;
			}
	}

   return MAX_ERR_NONE ;
}

Thanks for your example ;

If atom_alloc ( ) is good is suppose atomarray_copyatoms ( ) is good too (concerning getbytes function SDK warning) …


December 6, 2010 | 12:39 pm

ok, then I would do:

t_max_err myobject_getvalueof(t_myobject *x, long *argc, t_atom **argv)
{
    if (argc && argv) {
        char alloc;
        if (atom_alloc_array(x->count, argc, argv, &alloc)) {
            return MAX_ERR_OUT_OF_MEM;
        }
        if (alloc) {
            atom_setatom_array(*argc, *argv, x->count, x->data);
        }
	return MAX_ERR_NONE;
    }
}

Even though conceptually using atomarray_copyatoms() makes sense, you are assuming that its underlying implementation is compatible with the way that getvalueof works under the hood, which might or might not be true.
So, unless you work for C74 and have access to the code, I think it’s safer to use atom_alloc_array()

In any case, I too would be interested to know from C74 if in this context atomarray_copyatoms() could be safely used.

Cheers.

- Luigi


December 6, 2010 | 1:42 pm

Yes it is safe to call atomarray_copyatoms() which actually works how you would want a perfect getvalueof function to work.

1. if memory is passed in use it (making sure to not write beyond the input arg count)
2. allocate memory if there is none passed in.

Here is the internal copyatoms() call which atomarray_copyatoms() uses:

t_max_err copyatoms(long src_ac, t_atom *src_av, long *dst_ac, t_atom **dst_av)
{
	long i;

	if (dst_ac&&dst_av) {
		// memory passed in, use it
		if (*dst_ac&&*dst_av) {
			*dst_ac = MIN(*dst_ac,src_ac);
			for (i=0;i< (*dst_ac);i++) {
				(*dst_av)[i] = src_av[i];
			}
			return MAX_ERR_NONE;
		} else {
			if (*dst_av=(t_atom *)sysmem_newptr(src_ac*sizeof(t_atom))) {
				*dst_ac = src_ac;
				for (i=0;i

December 6, 2010 | 1:45 pm

In fact for such a case, it’s even easier, since you can simply write:

t_max_err uniform_getvalueof (t_uniform *x, long *ac, t_atom **av)
{
        if (ac && av)
                atomarray_copyatoms (x->data, ac, av) ;
        return MAX_ERR_NONE;
}

Viewing 6 posts - 1 through 6 (of 6 total)