Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Void pointers / Operator casting #1

Closed
EduardoGRocha opened this issue Apr 13, 2020 · 4 comments
Closed

Void pointers / Operator casting #1

EduardoGRocha opened this issue Apr 13, 2020 · 4 comments

Comments

@EduardoGRocha
Copy link

EduardoGRocha commented Apr 13, 2020

Hi, first of all congrats on the framework. It's quite interesting

My issue is the following: I have a Buffer which allows to store tuples (for operators that need materialization). It works by calling a method void* store_tuple(size_t tuple_size) which allocates memory of size "tuple_size" and returns a pointer to the beginning of this region.

In normal, non-generated code, I would cast this void pointer to the type I need. However, I don't know how to do it in coat. One could template the function to store tuples, like the following

template<class T>
T* store_tuple(size_t tuple_size)

However, this would still be problematic when trying to store tuples with different datatypes

@tetzank
Copy link
Owner

tetzank commented Apr 14, 2020

Is this function just a fancy malloc, or is it doing anything else besides allocating?

You can always pretend that the function signature looks different for the current context, see:

// call realloc with increased size
using realloc_type = T *(*)(T*,size_t); // fix realloc void* type issue, coat has no cast
auto vr_newstart = coat::FunctionCall(self.cc, (realloc_type)realloc, "realloc", vr_start, vr_size << 1);

Here, both void* in realloc()'s signature are casted to T*, T being the element type of the vector in this case. So, in COAT, we use T*, but it calls the function which uses void*. You can probably use something very similar.

@EduardoGRocha
Copy link
Author

That wouldn't work if you want to write multiple datatypes to the same Buffer. Suppose you have a table of form (uiint64_t, uint32_t, uint8_t).

Then if you want to materialize tuples of this table (to sort for example), I would expect something like the following:

// Buffer store returns a uint8_t*
coat::Ptr<CC, coat::Value<CC, uint8_t>> store_address = coat::FunctionCall(fn, buffer.store_tuple, "tuple_store",16);

// Registers with values to be stored
register0 = coat::Value<CC, uint64_t>(fn, val0);
register1 = coat::Value<CC, uint32_t>(fn, val1);
register2 = coat::Value<CC, uint8_t>(fn, val2);

auto ptr0 = store_address.cast<CC, coat::Value<CC, uint64_t>>();
*ptr0 = register0;
store_address +=8
auto ptr1 = store_address.cast<CC, coat::Value<CC, uint32_t>>();
*ptr1 = register1;
store_address +=4
auto ptr2 = store_address.cast<CC, coat::Value<CC, uint8_t>>();
*ptr2 = register2;

@tetzank
Copy link
Owner

tetzank commented Apr 15, 2020

Well, then we need a cast function. It was on my todo list anyway. I pushed a change, see the example:

coat/examples/cast.cpp

Lines 36 to 42 in 94cc783

auto first = coat::cast<uint64_t*>(buf);
*first = 0xCAFEBABE'CAFED00Dul;
buf += sizeof(uint64_t);
auto second = coat::cast<uint32_t*>(buf);
*second = 0xDEADC0DEu;
buf += sizeof(uint32_t);
*buf = 42;

@EduardoGRocha
Copy link
Author

EduardoGRocha commented Apr 18, 2020

I just tested it and seems to work. Thanks a lot!
How can one contribute to the project?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants