GDB (API)
/home/stan/gdb/src/gdb/registry.h
Go to the documentation of this file.
00001 /* Macros for general registry objects.
00002 
00003    Copyright (C) 2011-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #ifndef REGISTRY_H
00021 #define REGISTRY_H
00022 
00023 /* The macros here implement a template type and functions for
00024    associating some user data with a container object.
00025 
00026    A registry is associated with a struct tag name.  To attach a
00027    registry to a structure, use DEFINE_REGISTRY.  This takes the
00028    structure tag and an access method as arguments.  In the usual
00029    case, where the registry fields appear directly in the struct, you
00030    can use the 'REGISTRY_FIELDS' macro to declare the fields in the
00031    struct definition, and you can pass 'REGISTRY_ACCESS_FIELD' as the
00032    access argument to DEFINE_REGISTRY.  In other cases, use
00033    REGISTRY_FIELDS to define the fields in the appropriate spot, and
00034    then define your own accessor to find the registry field structure
00035    given an instance of your type.
00036 
00037    The API user requests a key from a registry during gdb
00038    initialization.  Later this key can be used to associate some
00039    module-specific data with a specific container object.
00040 
00041    The exported API is best used via the wrapper macros:
00042    
00043    - register_TAG_data(TAG)
00044    Get a new key for the container type TAG.
00045    
00046    - register_TAG_data_with_cleanup(TAG, SAVE, FREE)
00047    Get a new key for the container type TAG.
00048    SAVE and FREE are defined as void (*) (struct TAG *, void *)
00049    When the container is destroyed, first all registered SAVE
00050    functions are called.
00051    Then all FREE functions are called.
00052    Either or both may be NULL.
00053    
00054    - clear_TAG_data(TAG, OBJECT)
00055    Clear all the data associated with OBJECT.  Should be called by the
00056    container implementation when a container object is destroyed.
00057    
00058    - set_TAG_data(TAG, OBJECT, KEY, DATA)
00059    Set the data on an object.
00060    
00061    - TAG_data(TAG, OBJECT, KEY)
00062    Fetch the data for an object; returns NULL if it has not been set.
00063 */
00064 
00065 /* This structure is used in a container to hold the data that the
00066    registry uses.  */
00067 
00068 struct registry_fields
00069 {
00070   void **data;
00071   unsigned num_data;
00072 };
00073 
00074 /* This macro is used in a container struct definition to define the
00075    fields used by the registry code.  */
00076 
00077 #define REGISTRY_FIELDS                         \
00078   struct registry_fields registry_data
00079 
00080 /* A convenience macro for the typical case where the registry data is
00081    kept as fields of the object.  This can be passed as the ACCESS
00082    method to DEFINE_REGISTRY.  */
00083 
00084 #define REGISTRY_ACCESS_FIELD(CONTAINER) \
00085   (CONTAINER)
00086 
00087 /* Opaque type representing a container type with a registry.  This
00088    type is never defined.  This is used to factor out common
00089    functionality of all struct tag names into common code.  IOW,
00090    "struct tag name" pointers are cast to and from "struct
00091    registry_container" pointers when calling the common registry
00092    "backend" functions.  */
00093 struct registry_container;
00094 
00095 /* Registry callbacks have this type.  */
00096 typedef void (*registry_data_callback) (struct registry_container *, void *);
00097 
00098 struct registry_data
00099 {
00100   unsigned index;
00101   registry_data_callback save;
00102   registry_data_callback free;
00103 };
00104 
00105 struct registry_data_registration
00106 {
00107   struct registry_data *data;
00108   struct registry_data_registration *next;
00109 };
00110 
00111 struct registry_data_registry
00112 {
00113   struct registry_data_registration *registrations;
00114   unsigned num_registrations;
00115 };
00116 
00117 /* Registry backend functions.  Client code uses the frontend
00118    functions defined by DEFINE_REGISTRY below instead.  */
00119 
00120 const struct registry_data *register_data_with_cleanup
00121   (struct registry_data_registry *registry,
00122    registry_data_callback save,
00123    registry_data_callback free);
00124 
00125 void registry_alloc_data (struct registry_data_registry *registry,
00126                           struct registry_fields *registry_fields);
00127 
00128 /* Cast FUNC and CONTAINER to the real types, and call FUNC, also
00129    passing DATA.  */
00130 typedef void (*registry_callback_adaptor) (registry_data_callback func,
00131                                            struct registry_container *container,
00132                                            void *data);
00133 
00134 void registry_clear_data (struct registry_data_registry *data_registry,
00135                           registry_callback_adaptor adaptor,
00136                           struct registry_container *container,
00137                           struct registry_fields *fields);
00138 
00139 void registry_container_free_data (struct registry_data_registry *data_registry,
00140                                    registry_callback_adaptor adaptor,
00141                                    struct registry_container *container,
00142                                    struct registry_fields *fields);
00143 
00144 void registry_set_data (struct registry_fields *fields,
00145                         const struct registry_data *data,
00146                         void *value);
00147 
00148 void *registry_data (struct registry_fields *fields,
00149                      const struct registry_data *data);
00150 
00151 /* Define a new registry implementation.  */
00152 
00153 #define DEFINE_REGISTRY(TAG, ACCESS)                                    \
00154 struct registry_data_registry TAG ## _data_registry = { NULL, 0 };      \
00155                                                                         \
00156 const struct TAG ## _data *                                             \
00157 register_ ## TAG ## _data_with_cleanup (void (*save) (struct TAG *, void *), \
00158                                         void (*free) (struct TAG *, void *)) \
00159 {                                                                       \
00160   struct registry_data_registration **curr;                             \
00161                                                                         \
00162   return (struct TAG ## _data *)                                        \
00163     register_data_with_cleanup (&TAG ## _data_registry,                 \
00164                                 (registry_data_callback) save,          \
00165                                 (registry_data_callback) free);         \
00166 }                                                                       \
00167                                                                         \
00168 const struct TAG ## _data *                                             \
00169 register_ ## TAG ## _data (void)                                        \
00170 {                                                                       \
00171   return register_ ## TAG ## _data_with_cleanup (NULL, NULL);           \
00172 }                                                                       \
00173                                                                         \
00174 static void                                                             \
00175 TAG ## _alloc_data (struct TAG *container)                              \
00176 {                                                                       \
00177   struct registry_fields *rdata = &ACCESS (container)->registry_data;   \
00178                                                                         \
00179   registry_alloc_data (&TAG ## _data_registry, rdata);                  \
00180 }                                                                       \
00181                                                                         \
00182 static void                                                             \
00183 TAG ## registry_callback_adaptor (registry_data_callback func,          \
00184                                   struct registry_container *container, \
00185                                   void *data)                           \
00186 {                                                                       \
00187   struct TAG *tagged_container = (struct TAG *) container;              \
00188   struct registry_fields *rdata                                         \
00189     = &ACCESS (tagged_container)->registry_data;                        \
00190                                                                         \
00191   registry_ ## TAG ## _callback tagged_func                             \
00192     = (registry_ ## TAG ## _callback) func;                             \
00193                                                                         \
00194   tagged_func (tagged_container, data);                                 \
00195 }                                                                       \
00196                                                                         \
00197 void                                                                    \
00198 clear_ ## TAG ## _data (struct TAG *container)                          \
00199 {                                                                       \
00200   struct registry_fields *rdata = &ACCESS (container)->registry_data;   \
00201                                                                         \
00202   registry_clear_data (&TAG ## _data_registry,                          \
00203                        TAG ## registry_callback_adaptor,                \
00204                        (struct registry_container *) container,         \
00205                        rdata);                                          \
00206 }                                                                       \
00207                                                                         \
00208 static void                                                             \
00209 TAG ## _free_data (struct TAG *container)                               \
00210 {                                                                       \
00211   struct registry_fields *rdata = &ACCESS (container)->registry_data;   \
00212                                                                         \
00213   registry_container_free_data (&TAG ## _data_registry,                 \
00214                                 TAG ## registry_callback_adaptor,       \
00215                                 (struct registry_container *) container, \
00216                                 rdata);                                 \
00217 }                                                                       \
00218                                                                         \
00219 void                                                                    \
00220 set_ ## TAG ## _data (struct TAG *container,                            \
00221                       const struct TAG ## _data *data,                  \
00222                       void *value)                                      \
00223 {                                                                       \
00224   struct registry_fields *rdata = &ACCESS (container)->registry_data;   \
00225                                                                         \
00226   registry_set_data (rdata,                                             \
00227                      (struct registry_data *) data,                     \
00228                      value);                                            \
00229 }                                                                       \
00230                                                                         \
00231 void *                                                                  \
00232 TAG ## _data (struct TAG *container, const struct TAG ## _data *data)   \
00233 {                                                                       \
00234   struct registry_fields *rdata = &ACCESS (container)->registry_data;   \
00235                                                                         \
00236   return registry_data (rdata,                                          \
00237                         (struct registry_data *) data);                 \
00238 }
00239 
00240 
00241 /* External declarations for the registry functions.  */
00242 
00243 #define DECLARE_REGISTRY(TAG)                                           \
00244 struct TAG ## _data;                                                    \
00245 typedef void (*registry_ ## TAG ## _callback) (struct TAG *, void *);   \
00246 extern const struct TAG ## _data *register_ ## TAG ## _data (void);     \
00247 extern const struct TAG ## _data *register_ ## TAG ## _data_with_cleanup \
00248  (registry_ ## TAG ## _callback save, registry_ ## TAG ## _callback free); \
00249 extern void clear_ ## TAG ## _data (struct TAG *);              \
00250 extern void set_ ## TAG ## _data (struct TAG *,                 \
00251                                   const struct TAG ## _data *data, \
00252                                   void *value);                 \
00253 extern void *TAG ## _data (struct TAG *,                        \
00254                            const struct TAG ## _data *data);
00255 
00256 #endif /* REGISTRY_H */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines