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

add tb_get_cell #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add tb_get_cell #65

wants to merge 1 commit into from

Conversation

adsr
Copy link
Contributor

@adsr adsr commented Mar 31, 2024

Useful or bloat?

There's currently no way to fetch cells from the front buffer. This does close that gap. Otherwise this is a safer version of tb_cell_buffer which returns a pointer to the entire back buffer.

@adsr adsr changed the title add tb_get_cell (#64) add tb_get_cell Mar 31, 2024
@adsr adsr mentioned this pull request Mar 31, 2024
if_not_init_return();
int rv;
struct tb_cell *cellp = NULL;
rv = cellbuf_get(back ? &global.back : &global.front, x, y, &cellp);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if x or y are out of bounds is it going to fail?
I think it needs check as in tb_set_cell -> if_err_return(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes cellbuf_get will return an error code if coords are out of bounds. There's a test assertion for this case here: https://github.com/termbox/termbox2/pull/65/files#diff-ff257682d1f1e118ed7186fee4abce09fca625c01728795e04d3c9a4fe95eadfR19

@ledinscak
Copy link

I tested change and it works as supposed to.

int rv;
struct tb_cell *cellp = NULL;
rv = cellbuf_get(back ? &global.back : &global.front, x, y, &cellp);
if (cellp) memcpy(cell, cellp, sizeof(*cell));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for better performance can we avoid memcpy and just return pointer to cell like in cellbuf_get function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I figure it's a trade-off between safety and perf. I was going for by-value semantics here (like struct tb_cell tb_get_cell(int x, int y) but reserving the return value for error codes per usual). Then the user can do whatever with their cell and it won't affect tb internals.

If the caller is concerned about an extra memcpy maybe we can assume they'd also want to avoid function overhead per-cell, so maybe tb_cell_buffer is for the perf critical use case? But tb_cell_buffer doesn't support returning the front-buffer currently. We could add another function for that (not my pref to bloat the API -- I already regret adding some like tb_set_func). Or we could change the signature of tb_cell_buffer to int tb_cell_buffer(int back, struct tb_cell **cellbuf) (also not my pref to break ABI).

Or like you suggest maybe tb_get_cell returns the pointer to our memory, and the caller could simply ask for 0, 0 if they want to index the cellbuf themselves for perf. We can warn users in documentation that mutating any memory results in undefined behavior. That seems like the best option to me.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have point, I forgot about safety issues that could appear, but I will also prefer the latter. It is going to be more flexible code where one can get what one needs.

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

Successfully merging this pull request may close these issues.

None yet

2 participants