libcm is a C development framework with an emphasis on audio signal processing applications.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

cmLib.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmFileSys.h"
  10. #include "cmLib.h"
  11. #include "cmText.h"
  12. #ifndef NDEF
  13. #include <dlfcn.h>
  14. typedef void* _libH_t;
  15. bool _cmLibIsNull( _libH_t lh )
  16. { return lh == NULL; };
  17. const cmChar_t* _cmLibSysError()
  18. {
  19. const char* msg = dlerror();
  20. if( msg == NULL )
  21. msg = "<none>";
  22. return msg;
  23. }
  24. _libH_t _cmLibOpen( const char* libFn )
  25. { return dlopen(libFn,RTLD_LAZY); }
  26. bool _cmLibClose( _libH_t* lH )
  27. {
  28. if( *lH != NULL )
  29. {
  30. if( dlclose(*lH) == 0 )
  31. *lH = NULL;
  32. else
  33. return false;
  34. }
  35. return true;
  36. }
  37. void* _cmLibSym( _libH_t h, const char* symLabel )
  38. { return dlsym(h,symLabel); }
  39. #endif
  40. typedef struct cmLibNode_str
  41. {
  42. cmChar_t* fn;
  43. unsigned id;
  44. _libH_t lH;
  45. struct cmLibNode_str* link;
  46. } cmLibNode_t;
  47. typedef struct
  48. {
  49. cmErr_t err;
  50. cmLibNode_t* nodes;
  51. unsigned id;
  52. cmFileSysH_t fsH;
  53. } cmLib_t;
  54. cmLib_t* _cmLibHandleToPtr( cmLibH_t h )
  55. {
  56. cmLib_t* p = (cmLib_t*)h.h;
  57. return p;
  58. }
  59. cmLibH_t cmLibNullHandle = cmSTATIC_NULL_HANDLE;
  60. cmLibRC_t _cmLibNodeFree( cmLib_t* p, cmLibNode_t* np )
  61. {
  62. if( !_cmLibClose( &np->lH ) )
  63. return cmErrMsg(&p->err,kCloseFailLibRC,"Library close failed. System Message: %s", _cmLibSysError());
  64. // free np->fn and set np->fn to NULL - so the node may be reused.
  65. cmMemPtrFree(&np->fn);
  66. np->id = cmInvalidId;
  67. return kOkLibRC;
  68. }
  69. cmLibRC_t _cmLibFinalize( cmLib_t* p )
  70. {
  71. cmLibNode_t* np = p->nodes;
  72. while( np != NULL )
  73. {
  74. cmLibNode_t* pp = np->link;
  75. _cmLibNodeFree(p,np);
  76. cmMemFree(np);
  77. np = pp;
  78. }
  79. if( cmFileSysIsValid(p->fsH) )
  80. cmFileSysFinalize(&p->fsH);
  81. cmMemFree(p);
  82. return kOkLibRC;
  83. }
  84. cmLibRC_t cmLibInitialize( cmCtx_t* ctx, cmLibH_t* hp, const cmChar_t* dirStr )
  85. {
  86. cmLibRC_t rc = kOkLibRC;
  87. cmLib_t* p = cmMemAllocZ(cmLib_t,1);
  88. cmErrSetup(&p->err,&ctx->rpt,"cmLib");
  89. cmFileSysInitialize(&p->fsH,ctx,"cmLibFs");
  90. hp->h = p;
  91. if( cmTextIsNotEmpty(dirStr) )
  92. if((rc = cmLibScan(*hp,dirStr)) != kOkLibRC )
  93. hp->h = NULL;
  94. if( rc != kOkLibRC )
  95. _cmLibFinalize(p);
  96. return rc;
  97. }
  98. cmLibRC_t cmLibFinalize( cmLibH_t* hp )
  99. {
  100. cmLibRC_t rc;
  101. if( hp == NULL || hp->h == NULL )
  102. return kOkLibRC;
  103. cmLib_t* p = _cmLibHandleToPtr(*hp);
  104. if((rc = _cmLibFinalize(p)) == kOkLibRC )
  105. hp->h = NULL;
  106. return rc;
  107. }
  108. bool cmLibIsValid( cmLibH_t h )
  109. { return h.h != NULL; }
  110. unsigned cmLibOpen( cmLibH_t h, const cmChar_t* libFn )
  111. {
  112. cmLib_t* p = _cmLibHandleToPtr(h);
  113. _libH_t lH = _cmLibOpen(libFn);
  114. cmLibNode_t* np = p->nodes;
  115. unsigned idx = 0;
  116. if( _cmLibIsNull(lH) )
  117. {
  118. cmErrMsg(&p->err,kOpenFailLibRC,"Library load failed. System Message: %s", _cmLibSysError() );
  119. return cmInvalidId;
  120. }
  121. while( np != NULL )
  122. {
  123. if( np->fn == NULL )
  124. break;
  125. np = np->link;
  126. ++idx;
  127. }
  128. if( np == NULL )
  129. {
  130. np = cmMemAllocZ(cmLibNode_t,1);
  131. np->link = p->nodes;
  132. p->nodes = np;
  133. }
  134. np->fn = cmMemAllocStr(libFn);
  135. np->lH = lH;
  136. np->id = p->id++;
  137. return idx;
  138. }
  139. cmLibNode_t* _cmLibIdToNode( cmLib_t* p, unsigned libId )
  140. {
  141. cmLibNode_t* np = p->nodes;
  142. while( np != NULL )
  143. {
  144. if( np->id == libId )
  145. return np;
  146. np = np->link;
  147. }
  148. return NULL;
  149. }
  150. cmLibRC_t cmLibClose( cmLibH_t h, unsigned libId )
  151. {
  152. cmLib_t* p = _cmLibHandleToPtr(h);
  153. cmLibNode_t* np = _cmLibIdToNode(p,libId);
  154. if( (np == NULL) || _cmLibIsNull(np->lH) )
  155. return cmErrMsg(&p->err,kInvalidIdLibRC,"The library id %i is not valid or the library is closed.",libId);
  156. return kOkLibRC;
  157. }
  158. void* cmLibSym( cmLibH_t h, unsigned libId, const cmChar_t* funcStr )
  159. {
  160. void* f;
  161. cmLib_t* p = _cmLibHandleToPtr(h);
  162. cmLibNode_t* np = _cmLibIdToNode(p,libId);
  163. if( (np == NULL) || _cmLibIsNull(np->lH) )
  164. {
  165. cmErrMsg(&p->err,kInvalidIdLibRC,"The library id %i is not valid or the library is closed.",libId);
  166. return NULL;
  167. }
  168. if((f = _cmLibSym(np->lH,funcStr)) == NULL)
  169. {
  170. cmErrMsg(&p->err,kSymFailLibRC,"The dynamic symbol '%s' was not found. System Message: %s", cmStringNullGuard(funcStr), _cmLibSysError());
  171. return NULL;
  172. }
  173. return f;
  174. }
  175. cmLibRC_t cmLibScan( cmLibH_t h, const cmChar_t* dirStr )
  176. {
  177. cmLib_t* p = _cmLibHandleToPtr(h);
  178. unsigned dirEntryCnt = 0;
  179. cmFileSysDirEntry_t* d = NULL;
  180. cmLibRC_t rc = kOkLibRC;
  181. unsigned i = 0;
  182. if( cmFileSysIsValid(p->fsH) == false )
  183. return cmErrMsg(&p->err,kFileSysFailLibRC,"The file system object was not successfully initialized.");
  184. if((d = cmFileSysDirEntries(p->fsH, dirStr, kFileFsFl | kFullPathFsFl, &dirEntryCnt )) == NULL )
  185. return cmErrMsg(&p->err,kFileSysFailLibRC,"The scan of directory '%s' failed.",cmStringNullGuard(dirStr));
  186. for(i=0; i<dirEntryCnt; ++i)
  187. cmLibOpen(h,d[i].name);
  188. cmFileSysDirFreeEntries(p->fsH,d);
  189. return rc;
  190. }
  191. unsigned cmLibCount( cmLibH_t h )
  192. {
  193. cmLib_t* p = _cmLibHandleToPtr(h);
  194. cmLibNode_t* np = p->nodes;
  195. unsigned n = 0;
  196. while( np != NULL )
  197. {
  198. np = np->link;
  199. ++n;
  200. }
  201. return n;
  202. }
  203. unsigned cmLibIndexToId( cmLibH_t h, unsigned idx )
  204. {
  205. cmLib_t* p = _cmLibHandleToPtr(h);
  206. cmLibNode_t* np = p->nodes;
  207. unsigned i = 0;
  208. while( np != NULL )
  209. {
  210. if( i == idx )
  211. return np->id;
  212. np = np->link;
  213. ++i;
  214. }
  215. if(np == NULL )
  216. {
  217. cmErrMsg(&p->err,kInvalidIdLibRC,"The library index %i is not valid.",idx);
  218. return cmInvalidId;
  219. }
  220. return np->id;
  221. }
  222. const cmChar_t* cmLibName( cmLibH_t h, unsigned libId )
  223. {
  224. cmLib_t* p = _cmLibHandleToPtr(h);
  225. cmLibNode_t* np = _cmLibIdToNode(p,libId);
  226. if( (np == NULL) || _cmLibIsNull(np->lH) )
  227. {
  228. cmErrMsg(&p->err,kInvalidIdLibRC,"The library id %i is not valid or the library is closed.",libId);
  229. return NULL;
  230. }
  231. return np->fn;
  232. }