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

Consider adding more HASH_FIND_XX for standard INT/UINT types #229

Open
depler opened this issue Apr 11, 2021 · 1 comment
Open

Consider adding more HASH_FIND_XX for standard INT/UINT types #229

depler opened this issue Apr 11, 2021 · 1 comment

Comments

@depler
Copy link

depler commented Apr 11, 2021

I was breaking my head why this code not working as expected:

uint16_t uid = 0;
HASH_FIND_INT(head, &uid , item);

Until I did this:

#define HASH_FIND_UINT16(head,findint,out) HASH_FIND(hh,head,findint,sizeof(uint16_t),out)
#define HASH_ADD_UINT16(head,intfield,add) HASH_ADD(hh,head,intfield,sizeof(uint16_t),add)

Please append additional macros for standard INT/UINT types

@depler depler changed the title Consider adding more HASH_FIND_XX for standard types Consider adding more HASH_FIND_XX for standard INT/UINT types Apr 11, 2021
@Quuxplusone
Copy link
Collaborator

I'm not inclined to add HASH_FIND_SHORT, HASH_FIND_UINT32_T, etc. etc.; that doesn't seem worth it, since someone who knows about this problem will simply use the general-purpose HASH_FIND in the first place. But it's pretty awful that HASH_FIND_INT(head, &uid, item) compiles silently to the wrong behavior!

We could mitigate this by changing the macro as follows:

 #define HASH_FIND_INT(head,findint,out)                                          \
-    HASH_FIND(hh,head,findint,sizeof(int),out)
+    HASH_FIND(hh,head,findint,sizeof(int[sizeof(*(findint)) == sizeof(int) ? 1 : -1]),out)

This says "If the user gave us a pointer of the form &uid but sizeof(*&uid) isn't the same as sizeof(int), then compile-time fail."
I think the problem with this would be backward compatibility. Today, this works:

    int uid = 42;
    unsigned char *p = (unsigned char*)&uid;
    HASH_FIND_INT(head, p, item);

But it wouldn't work after the above change. Therefore I don't think we can actually make the above change, at least not by default.

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