From cd1b95aea34de3415fc4a33ecc73e9ebfdbaaa3a Mon Sep 17 00:00:00 2001 From: "Justin R. Cutler" Date: Fri, 8 Apr 2016 21:16:07 -0400 Subject: [PATCH] Add container_of macro --- threadless/container_of.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 threadless/container_of.h diff --git a/threadless/container_of.h b/threadless/container_of.h new file mode 100644 index 0000000..9bd65a8 --- /dev/null +++ b/threadless/container_of.h @@ -0,0 +1,27 @@ +/* threadless.io + * Copyright (c) 2016 Justin R. Cutler + * Licensed under the MIT License. See LICENSE file in the project root for + * full license information. + */ +/** @file + * container_of macro definition + * @author Justin R. Cutler + */ +#ifndef THREADLESS_CONTAINER_OF_H +#define THREADLESS_CONTAINER_OF_H + +/* offsetof */ +#include + +/** Get a pointer to the containing structure of a member + * @param ptr address of @p member within a @p type + * @param type type of container + * @param member name of the member in @p type + * @returns pointer to containing @p type + */ +#define container_of(ptr, type, member) ( \ + (type *)((char *)(1 ? (ptr) : &((type *)0)->member) \ + - offsetof(type, member)) \ +) + +#endif /* THREADLESS_CONTAINER_OF_H */