diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/compat.c | 6 | ||||
-rw-r--r-- | src/common/util.h | 23 |
2 files changed, 25 insertions, 4 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index 697a32c091..3833c396e8 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -174,8 +174,7 @@ tor_mmap_file(const char *filename) void tor_munmap_file(tor_mmap_t *handle) { - tor_mmap_impl_t *h = (tor_mmap_impl_t*) - (((char*)handle) - STRUCT_OFFSET(tor_mmap_impl_t, base)); + tor_mmap_impl_t *h = SUBTYPE_P(handle, tor_mmap_impl_t, base); munmap((char*)h->base.data, h->mapping_size); tor_free(h); } @@ -244,8 +243,7 @@ tor_mmap_file(const char *filename) void tor_munmap_file(tor_mmap_t *handle) { - win_mmap_t *h = (win_mmap_t*) - (((char*)handle) - STRUCT_OFFSET(win_mmap_t, base)); + win_mmap_t *h = SUBTYPE_P(handle, win_mmap_t, base); if (handle->data) /* This is an ugly cast, but without it, "data" in struct tor_mmap_t would have to be redefined as non-const. */ diff --git a/src/common/util.h b/src/common/util.h index 9c94925004..d64ffb8e88 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -110,6 +110,29 @@ extern int dmalloc_free(const char *file, const int line, void *pnt, ((off_t) (((char*)&((tp*)0)->member)-(char*)0)) #endif +/** Macro: yield a pointer to the field at position <b>off</b> within the + * structure <b>st</b>. Example: + * <pre> + * struct a { int foo; int bar; } x; + * off_t bar_offset = STRUCT_OFFSET(struct a, bar); + * int *bar_p = STRUCT_VAR_P(&x, bar_offset); + * *bar_p = 3; + * </pre> + */ +#define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) ) + +/** Macro: yield a pointer to an enclosing structure given a pointer to + * a substructure at offset <b>off</b>. Example: + * <pre> + * struct base { ... }; + * struct subtype { int x; struct base b; } x; + * struct base *bp = &x.base; + * struct *sp = SUBTYPE_P(bp, struct subtype, b); + * </pre> + */ +#define SUBTYPE_P(p, subtype, basemember) \ + ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) )) + /* String manipulation */ /** Allowable characters in a hexadecimal string. */ |