MINOR: obj: provide a safe and an unsafe access to pointed objects

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>".
This commit is contained in:
Willy Tarreau 2013-09-29 09:15:31 +02:00
parent 6fe1541285
commit 66337a0784
2 changed files with 39 additions and 11 deletions

View 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;
}
}

View 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 */