vxwSymLib.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:12k
源码类别:

VxWorks

开发平台:

C/C++

  1. // VXWSymTab/vxwSymLib.h - symbol table class
  2. // Copyright 1995-1999 Wind River Systems, Inc.
  3. // modification history
  4. // --------------------
  5. // 01c,09mar99,jdi  doc: fixed wrong cross-references.
  6. // 01b,23feb99,fle  doc : made it refgen compliant
  7. // 01c,21feb99,jdi  added library section, checked in documentation.
  8. // 01b,30sep95,rhp  documented.
  9. // 01a,15jun95,srh  written.
  10. // DESCRIPTION
  11. // This class library provides facilities for managing symbol tables.  A symbol
  12. // table associates a name and type with a value.  A name is simply an
  13. // arbitrary, null-terminated string.  A symbol type is a small integer
  14. // (typedef SYM_TYPE), and its value is a character pointer.  Though commonly
  15. // used as the basis for object loaders, symbol tables may be used whenever
  16. // efficient association of a value with a name is needed.
  17. //
  18. // If you use the VXWSymTab class to manage symbol tables local to
  19. // your own applications, the values for SYM_TYPE objects are completely
  20. // arbitrary; you can use whatever one-byte integers are appropriate for
  21. // your application.
  22. //
  23. // If the VxWorks system symbol table is configured into your target
  24. // system, you can use the VXWSymTab class to manipulate it based on
  25. // its symbol-table ID, recorded in the global f3sysSymTblf1; see
  26. // VXWSymTab::VXWSymTab() to construct an object based on this global.
  27. // In the VxWorks target-resident global symbol table, the values for
  28. // SYM_TYPE are N_ABS, N_TEXT, N_DATA, and N_BSS (defined in a_out.h);
  29. // these are all even numbers, and any of them may be combined (via
  30. // boolean or) with N_EXT (1).  These values originate in the section
  31. // names for a.out object code format, but the VxWorks system symbol
  32. // table uses them as symbol types across all object formats.  (The
  33. // VxWorks system symbol table also occasionally includes additional
  34. // types, in some object formats.)
  35. //
  36. // All operations on a symbol table are interlocked by means of a
  37. // mutual-exclusion semaphore in the symbol table structure.
  38. //
  39. // Symbols are added to a symbol table with VXWSymTab::add().  Each
  40. // symbol in the symbol table has a name, a value, and a type.
  41. // Symbols are removed from a symbol table with VXWSymTab::remove().
  42. //
  43. // Symbols can be accessed by either name or value.  The routine
  44. // VXWSymTab::findByName() searches the symbol table for a symbol of a
  45. // specified name.  The routine VXWSymTab::findByValue() finds the
  46. // symbol with the value closest to a specified value.  The routines
  47. // VXWSymTab::findByNameAndType() and VXWSymTab::findByValueAndType()
  48. // allow the symbol type to be used as an additional criterion in the
  49. // searches.
  50. //
  51. // Symbols in the symbol table are hashed by name into a hash table
  52. // for fast look-up by name, for instance with VXWSymTab::findByName().  The
  53. // size of the hash table is specified during the creation of a symbol
  54. // table.  Look-ups by value, such as with VXWSymTab::findByValue(), must
  55. // search the table linearly; these look-ups can thus be much slower.
  56. //
  57. // The routine VXWSymTab::each() allows each symbol in the symbol
  58. // table to be examined by a user-specified function.
  59. //
  60. // Name clashes occur when a symbol added to a table is identical in
  61. // name and type to a previously added symbol.  Whether or not symbol
  62. // tables can accept name clashes is set by a parameter when the
  63. // symbol table is created with VXWSymTab::VXWSymTab().  If name
  64. // clashes are not allowed, VXWSymTab::add() returns an error if
  65. // there is an attempt to add a symbol with identical name and type.
  66. // If name clashes are allowed, adding multiple symbols with the same
  67. // name and type is not an error.  In such cases,
  68. // VXWSymTab::findByName() returns the value most recently added,
  69. // although all versions of the symbol can be found by
  70. // VXWSymTab::each().
  71. //
  72. // INCLUDE FILES: vxwSymLib.h
  73. //
  74. // SEE ALSO: VXWModule
  75. //
  76. #ifndef vxwSymLib_h
  77. #define vxwSymLib_h
  78. #include "vxWorks.h"
  79. #include "symLib.h"
  80. #include "vxwObject.h"
  81. #include "vxwErr.h"
  82. class VXWSymTab : virtual public VXWIdObject
  83.     {
  84.   public:
  85. //_ VXWSymTab Public Constructors
  86. ///////////////////////////////////////////////////////////////////////////////
  87. //
  88. // VXWSymTab::VXWSymTab - create a symbol table
  89. //
  90. // This constructor creates and initializes a symbol table with a hash table of
  91. // a specified size.  The size of the hash table is specified as a power of two.
  92. // For example, if <hashSizeLog2> is 6, a 64-entry hash table is created.
  93. //
  94. // If <sameNameOk> is FALSE, attempting to add a symbol with
  95. // the same name and type as an already-existing symbol results in an error.
  96. //
  97. // Memory for storing symbols as they are added to the symbol table will be
  98. // allocated from the memory partition <symPartId>.  The ID of the system
  99. // memory partition is stored in the global variable `memSysPartId', which
  100. // is declared in memLib.h.
  101. //
  102. // RETURNS: N/A
  103.     VXWSymTab (int hashSizeLog2, BOOL sameNameOk, PART_ID symPartId)
  104. : stid_ (symTblCreate (hashSizeLog2, sameNameOk, symPartId))
  105. {
  106. if (stid_ == 0)
  107.     vxwThrowErrno ();
  108. }
  109. ///////////////////////////////////////////////////////////////////////////////
  110. //
  111. // VXWSymTab::VXWSymTab - create a symbol-table object 
  112. // 
  113. // This constructor creates a symbol table object based on an existing
  114. // symbol table.  For example, the following statement creates a
  115. // symbol-table object for the VxWorks system symbol table (assuming
  116. // you have configured a target-resident symbol table into your
  117. // VxWorks system):
  118. // .CS
  119. // VXWSymTab sSym;
  120. // ...
  121. // sSym = VXWSymTab (sysSymTbl);
  122. // .CE
  123.     VXWSymTab (SYMTAB_ID aSymTabId)
  124. : stid_ (aSymTabId)
  125. {
  126. }
  127. ///////////////////////////////////////////////////////////////////////////////
  128. //
  129. // VXWSymTab::~VXWSymTab - delete a symbol table
  130. // 
  131. // This routine deletes a symbol table; it deallocates all memory
  132. // associated with its symbol table, including the hash table, and
  133. // marks the table as invalid.
  134. //
  135. // Deletion of a table that still contains symbols throws an error.
  136. // Successful deletion includes the deletion of the internal hash table and
  137. // the deallocation of memory associated with the table.  The table is marked
  138. // invalid to prohibit any future references.
  139. //
  140. // RETURNS: OK, or ERROR if the table still contains symbols.
  141.     ~VXWSymTab ()
  142. {
  143. if (symTblDelete (stid_) != OK)
  144.     vxwThrowErrno ();
  145. }
  146. //_ VXWSymTab Public Member Functions
  147. ///////////////////////////////////////////////////////////////////////////////
  148. //
  149. // VXWSymTab::add - create and add a symbol to a symbol table, including a group number
  150. //
  151. // This routine allocates a symbol <name> and adds it to its symbol
  152. // table with the specified parameters <value>, <type>, and <group>.
  153. // The <group> parameter specifies the group number assigned to a module when
  154. // it is loaded on the target; see the manual entry for moduleLib.
  155. //
  156. // RETURNS: OK, or ERROR if there is insufficient memory for the symbol to be
  157. // allocated.
  158. //
  159. // SEE ALSO: moduleLib
  160.     STATUS add (char *name, char *value, SYM_TYPE type, UINT16 group)
  161. {
  162. return symAdd (stid_, name, value, type, group);
  163. }
  164. ///////////////////////////////////////////////////////////////////////////////
  165. //
  166. // VXWSymTab::each - call a routine to examine each entry in a symbol table
  167. //
  168. // This routine calls a user-supplied routine to examine each entry in the
  169. // symbol table; it calls the specified routine once for each entry.  The
  170. // routine must have the following type signature:
  171. // .CS
  172. //     BOOL routine
  173. //         (
  174. //         char * name, /@ entry name                  @/
  175. //         int val, /@ value associated with entry @/
  176. //         SYM_TYPE type, /@ entry type                  @/
  177. //         int arg, /@ arbitrary user-supplied arg @/
  178. //         UINT16 group /@ group number                @/
  179. //         )
  180. // .CE
  181. // The user-supplied routine must return TRUE if VXWSymTab::each() is to
  182. // continue calling it for each entry, or FALSE if it is done and
  183. // VXWSymTab::each() can exit.
  184. //
  185. // RETURNS: A pointer to the last symbol reached, or NULL if all symbols are
  186. // reached.
  187.     SYMBOL * each (FUNCPTR routine, int routineArg)
  188. {
  189. return symEach (stid_, routine, routineArg);
  190. }
  191. ///////////////////////////////////////////////////////////////////////////////
  192. //
  193. // VXWSymTab::findByName - look up a symbol by name
  194. //
  195. // This routine searches its symbol table for a symbol matching a specified
  196. // name.  If the symbol is found, its value and type are copied to <pValue>
  197. // and <pType>.  If multiple symbols have the same name but differ in type,
  198. // the routine chooses the matching symbol most recently added to the symbol
  199. // table.
  200. //
  201. // RETURNS: OK, or ERROR if the symbol cannot be found.
  202.     STATUS findByName (char *name, char **pValue, SYM_TYPE *pType) const
  203. {
  204. return symFindByName (stid_, name, pValue, pType);
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. //
  208. // VXWSymTab::findByNameAndType - look up a symbol by name and type
  209. //
  210. // This routine searches its symbol table for a symbol matching both name and
  211. // type (<name> and <goalType>).  If the symbol is found, its value and type are
  212. // copied to <pValue> and <pType>.  The <mask> parameter can be used to match
  213. // sub-classes of type.
  214. //
  215. // RETURNS: OK, or ERROR if the symbol is not found.
  216.     STATUS findByNameAndType (char *name, char **pValue, SYM_TYPE *pType,
  217.       SYM_TYPE goalType, SYM_TYPE mask) const
  218. {
  219. return symFindByNameAndType (stid_, name, pValue,
  220.        pType, goalType, mask);
  221. }
  222. ///////////////////////////////////////////////////////////////////////////////
  223. //
  224. // VXWSymTab::findByValue - look up a symbol by value
  225. //
  226. // This routine searches its symbol table for a symbol matching a specified
  227. // value.  If there is no matching entry, it chooses the table entry with the
  228. // next lower value.  The symbol name (with terminating EOS), the actual
  229. // value, and the type are copied to <name>, <pValue>, and <pType>.
  230. //
  231. // RETURNS: OK, or ERROR if <value> is less than the lowest value in the table.
  232.     STATUS findByValue (UINT value, char *name, int *pValue,
  233. SYM_TYPE *pType) const
  234. {
  235. return symFindByValue (stid_, value, name, pValue, pType);
  236. }
  237. ///////////////////////////////////////////////////////////////////////////////
  238. //
  239. // VXWSymTab::findByValueAndType - look up a symbol by value and type
  240. // 
  241. // This routine searches a symbol table for a symbol matching both
  242. // value and type (<value> and <goalType>).  If there is no matching
  243. // entry, it chooses the table entry with the next lower value.  The
  244. // symbol name (with terminating EOS), the actual value, and the type
  245. // are copied to <name>, <pValue>, and <pType>.  The <mask> parameter
  246. // can be used to match sub-classes of type.
  247. //
  248. // RETURNS: OK, or ERROR if <value> is less than the lowest value in the table.
  249.     STATUS findByValueAndType (UINT value, char *name, int *pValue,
  250.        SYM_TYPE *pType, SYM_TYPE goalType,
  251.        SYM_TYPE mask) const
  252. {
  253. return symFindByValueAndType (stid_, value, name, pValue,
  254. pType, goalType, mask);
  255. }
  256. ///////////////////////////////////////////////////////////////////////////////
  257. //
  258. // VXWSymTab::remove - remove a symbol from a symbol table
  259. //
  260. // This routine removes a symbol of matching name and type from
  261. // its symbol table.  The symbol is deallocated if found.
  262. // Note that VxWorks symbols in a standalone VxWorks image (where the 
  263. // symbol table is linked in) cannot be removed.
  264. //
  265. // RETURNS: OK, or ERROR if the symbol is not found
  266. // or could not be deallocated.
  267.     STATUS remove (char *name, SYM_TYPE type)
  268. {
  269. return symRemove (stid_, name, type);
  270. }
  271.     
  272.   protected:
  273.     VXWSymTab ()
  274. {
  275. }
  276.     VXWSymTab (const VXWSymTab &)
  277. {
  278. }
  279.     VXWSymTab & operator = (const VXWSymTab &)
  280. {
  281. return *this;
  282. }
  283.     virtual void * myValue ();
  284.     SYMTAB_ID stid_;
  285.     };
  286. #endif /* ifndef vxwSymLib_h */