]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: obj: provide a safe and an unsafe access to pointed objects
authorWilly Tarreau <w@1wt.eu>
Sun, 29 Sep 2013 07:15:31 +0000 (09:15 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 9 Dec 2013 14:40:22 +0000 (15:40 +0100)
Most of the times, the caller of objt_<type>(ptr) will know that <ptr>
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_<type>".

include/proto/obj_type.h
include/types/obj_type.h

index ba5d534578c7fb7f880a2aed266434f2a10b53bb..09a5ee4e2cdc1e11348b45216861a75cd08340fe 100644 (file)
@@ -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_<type> : converts the pointer without any control of its
+ *     value nor type.
+ *   - objt_<type> : 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;
        }
 }
index cfb3e346332c2cf59875e17bd475ccbb93432b49..b27f375bb14889cdba69d9461feb34990bd242d3 100644 (file)
@@ -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 */