From: Willy Tarreau Date: Sun, 29 Sep 2013 07:15:31 +0000 (+0200) Subject: MINOR: obj: provide a safe and an unsafe access to pointed objects X-Git-Tag: v1.5-dev20~125 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=66337a0784974c15b87b970f519c5b2e61e5294d;p=thirdparty%2Fhaproxy.git MINOR: obj: provide a safe and an unsafe access to pointed objects Most of the times, the caller of objt_(ptr) will know that is valid and of the correct type (eg: in an "if" condition). Let's provide an unsafe variant that does not perform the check again for these usages. The new functions are called "__objt_". --- diff --git a/include/proto/obj_type.h b/include/proto/obj_type.h index ba5d534578..09a5ee4e2c 100644 --- a/include/proto/obj_type.h +++ b/include/proto/obj_type.h @@ -2,7 +2,7 @@ * include/proto/obj_type.h * This file contains function prototypes to manipulate object types * - * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu + * Copyright (C) 2000-2013 Willy Tarreau - w@1wt.eu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -32,7 +32,7 @@ static inline enum obj_type obj_type(enum obj_type *t) { - if (!t || *t > OBJ_TYPE_APPLET) + if (!t || *t >= OBJ_TYPE_ENTRIES) return OBJ_TYPE_NONE; return *t; } @@ -48,41 +48,68 @@ static inline const char *obj_type_name(enum obj_type *t) } } +/* Note: for convenience, we provide two versions of each function : + * - __objt_ : converts the pointer without any control of its + * value nor type. + * - objt_ : same as above except that if the pointer is NULL + * or points to a non-matching type, NULL is returned instead. + */ + +static inline struct listener *__objt_listener(enum obj_type *t) +{ + return container_of(t, struct listener, obj_type); +} + static inline struct listener *objt_listener(enum obj_type *t) { if (!t || *t != OBJ_TYPE_LISTENER) return NULL; - return container_of(t, struct listener, obj_type); + return __objt_listener(t); +} + +static inline struct proxy *__objt_proxy(enum obj_type *t) +{ + return container_of(t, struct proxy, obj_type); } static inline struct proxy *objt_proxy(enum obj_type *t) { if (!t || *t != OBJ_TYPE_PROXY) return NULL; - return container_of(t, struct proxy, obj_type); + return __objt_proxy(t); +} + +static inline struct server *__objt_server(enum obj_type *t) +{ + return container_of(t, struct server, obj_type); } static inline struct server *objt_server(enum obj_type *t) { if (!t || *t != OBJ_TYPE_SERVER) return NULL; - return container_of(t, struct server, obj_type); + return __objt_server(t); +} + +static inline struct si_applet *__objt_applet(enum obj_type *t) +{ + return container_of(t, struct si_applet, obj_type); } static inline struct si_applet *objt_applet(enum obj_type *t) { if (!t || *t != OBJ_TYPE_APPLET) return NULL; - return container_of(t, struct si_applet, obj_type); + return __objt_applet(t); } static inline void *obj_base_ptr(enum obj_type *t) { switch (obj_type(t)) { - case OBJ_TYPE_LISTENER: return objt_listener(t); - case OBJ_TYPE_PROXY: return objt_proxy(t); - case OBJ_TYPE_SERVER: return objt_server(t); - case OBJ_TYPE_APPLET: return objt_applet(t); + case OBJ_TYPE_LISTENER: return __objt_listener(t); + case OBJ_TYPE_PROXY: return __objt_proxy(t); + case OBJ_TYPE_SERVER: return __objt_server(t); + case OBJ_TYPE_APPLET: return __objt_applet(t); default: return NULL; } } diff --git a/include/types/obj_type.h b/include/types/obj_type.h index cfb3e34633..b27f375bb1 100644 --- a/include/types/obj_type.h +++ b/include/types/obj_type.h @@ -2,7 +2,7 @@ * include/types/obj_type.h * This file declares some object types for use in various structures. * - * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu + * Copyright (C) 2000-2013 Willy Tarreau - w@1wt.eu * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -37,6 +37,7 @@ enum obj_type { OBJ_TYPE_PROXY, /* object is a struct proxy */ OBJ_TYPE_SERVER, /* object is a struct server */ OBJ_TYPE_APPLET, /* object is a struct si_applet */ + OBJ_TYPE_ENTRIES /* last one : number of entries */ }; #endif /* _TYPES_OBJ_TYPE_H */