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

Align types of i, n, and _ut_i #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/utringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "utarray.h" // for "UT_icd"

typedef struct {
unsigned i; /* index of next available slot; wraps at n */
unsigned n; /* capacity */
size_t i; /* index of next available slot; wraps at n */
size_t n; /* capacity */
mterron marked this conversation as resolved.
Show resolved Hide resolved
unsigned char f; /* full */
UT_icd icd; /* initializer, copy and destructor functions */
char *d; /* n slots of size icd->sz */
Expand All @@ -50,12 +50,12 @@ typedef struct {
#define utringbuffer_clear(a) do { \
if ((a)->icd.dtor) { \
if ((a)->f) { \
unsigned _ut_i; \
size_t _ut_i; \
for (_ut_i = 0; _ut_i < (a)->n; ++_ut_i) { \
(a)->icd.dtor(utringbuffer_eltptr(a, _ut_i)); \
} \
} else { \
unsigned _ut_i; \
size_t _ut_i; \
for (_ut_i = 0; _ut_i < (a)->i; ++_ut_i) { \
(a)->icd.dtor(utringbuffer_eltptr(a, _ut_i)); \
} \
Expand Down Expand Up @@ -94,7 +94,7 @@ typedef struct {

#define _utringbuffer_real_idx(a,j) ((a)->f ? ((j) + (a)->i) % (a)->n : (j))
#define _utringbuffer_internalptr(a,j) ((void*)((a)->d + ((a)->icd.sz * (j))))
#define utringbuffer_eltptr(a,j) ((0 <= (j) && (j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
#define utringbuffer_eltptr(a,j) (((j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here j could certainly be negative if it were, for example, -1, or an int that happened to have the value -1.
If you're doing this to work around a compiler warning, please file a bug/issue with a minimal compilable example that demonstrates the warning and maybe we can find a way to silence it.

Copy link
Author

Choose a reason for hiding this comment

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

Hi, here's the output from the compiler and seem to disagree with that assertion but it might be wrong.

swuniq.c: In function ‘lookup’:
uthash/src/utringbuffer.h:97:38: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
   97 | #define utringbuffer_eltptr(a,j) ((0 <= (j) && (j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
      |                                      ^~
swuniq.c:133:25: note: in expansion of macro ‘utringbuffer_eltptr’
  133 |    XXH64_hash_t *item = utringbuffer_eltptr(rbuffer, i);
      |                         ^~~~~~~~~~~~~~~~~~~
swuniq.c: In function ‘main’:
uthash/src/utringbuffer.h:46:12: warning: conversion from ‘size_t’ {aka ‘long unsigned int’} to ‘unsigned int’ may change value [-Wconversion]
   46 |   (a)->n = (_n);                                                          \
      |            ^
uthash/src/utringbuffer.h:76:3: note: in expansion of macro ‘utringbuffer_init’
   76 |   utringbuffer_init(a, n, _icd);                                          \
      |   ^~~~~~~~~~~~~~~~~
swuniq.c:176:2: note: in expansion of macro ‘utringbuffer_new’
  176 |  utringbuffer_new(history, window_size, &ut_xxh64_hash_t_icd);
      |  ^~~~~~~~~~~~~~~~
uthash/src/utringbuffer.h:97:38: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
   97 | #define utringbuffer_eltptr(a,j) ((0 <= (j) && (j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
      |                                      ^~
uthash/src/utringbuffer.h:55:23: note: in expansion of macro ‘utringbuffer_eltptr’
   55 |         (a)->icd.dtor(utringbuffer_eltptr(a, _ut_i));                     \
      |                       ^~~~~~~~~~~~~~~~~~~
uthash/src/utringbuffer.h:69:3: note: in expansion of macro ‘utringbuffer_clear’
   69 |   utringbuffer_clear(a);                                                  \
      |   ^~~~~~~~~~~~~~~~~~
uthash/src/utringbuffer.h:80:3: note: in expansion of macro ‘utringbuffer_done’
   80 |   utringbuffer_done(a);                                                   \
      |   ^~~~~~~~~~~~~~~~~
swuniq.c:189:2: note: in expansion of macro ‘utringbuffer_free’
  189 |  utringbuffer_free(history);
      |  ^~~~~~~~~~~~~~~~~
uthash/src/utringbuffer.h:97:38: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
   97 | #define utringbuffer_eltptr(a,j) ((0 <= (j) && (j) < utringbuffer_len(a)) ? _utringbuffer_internalptr(a,_utringbuffer_real_idx(a,j)) : NULL)
      |                                      ^~
uthash/src/utringbuffer.h:60:23: note: in expansion of macro ‘utringbuffer_eltptr’
   60 |         (a)->icd.dtor(utringbuffer_eltptr(a, _ut_i));                     \
      |                       ^~~~~~~~~~~~~~~~~~~
uthash/src/utringbuffer.h:69:3: note: in expansion of macro ‘utringbuffer_clear’
   69 |   utringbuffer_clear(a);                                                  \
      |   ^~~~~~~~~~~~~~~~~~
uthash/src/utringbuffer.h:80:3: note: in expansion of macro ‘utringbuffer_done’
   80 |   utringbuffer_done(a);                                                   \
      |   ^~~~~~~~~~~~~~~~~
swuniq.c:189:2: note: in expansion of macro ‘utringbuffer_free’
  189 |  utringbuffer_free(history);
      |  ^~~~~~~~~~~~~~~~~


#define _utringbuffer_fake_idx(a,j) ((a)->f ? ((j) + (a)->n - (a)->i) % (a)->n : (j))
#define _utringbuffer_internalidx(a,e) (((char*)(e) >= (a)->d) ? (((char*)(e) - (a)->d)/(a)->icd.sz) : -1)
Expand Down