libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmProcObj.c 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "cmSymTbl.h"
  10. #include "cmFloatTypes.h"
  11. #include "cmComplexTypes.h"
  12. #include "cmProcObj.h"
  13. //------------------------------------------------------------------------------------------------------------
  14. void* _cmObjAlloc( void* ap, const char* label, struct cmCtx_str* c, unsigned objByteCnt )
  15. {
  16. unsigned flags = (ap == NULL) ? kDynObjFl : 0;
  17. if( ap == NULL )
  18. ap = cmMemMalloc(objByteCnt);
  19. memset(ap,0,objByteCnt);
  20. assert( objByteCnt >= sizeof(cmObj));
  21. // the first field in the object must be an cmObj field
  22. cmObj* p = (cmObj*)ap;
  23. p->flags = flags;
  24. p->ctx = c;
  25. if( c != NULL )
  26. cmErrSetup(&p->err,c->obj.err.rpt,label);
  27. return ap;
  28. }
  29. // if pp is non-NULL then *pp is set to NULL on return
  30. void _cmObjFree( void** pp, cmObj* op )
  31. {
  32. // pp and op are both NULL or neither is NULL
  33. assert( (pp==NULL) == (op==NULL) );
  34. if( op != NULL )
  35. if( cmIsFlag(op->flags,kDynObjFl) )
  36. cmMemPtrFree( pp );
  37. }
  38. //------------------------------------------------------------------------------------------------------------
  39. cmCtx* cmCtxAlloc( cmCtx* p, cmRpt_t* rpt, cmLHeapH_t lhH, cmSymTblH_t stH )
  40. {
  41. cmRC_t rc;
  42. cmCtx* op = cmObjAlloc( cmCtx, NULL, p );
  43. op->obj.ctx = op;
  44. if( (rc = cmCtxInit( op, rpt, lhH, stH )) != cmOkRC )
  45. cmCtxFree(&op);
  46. return op;
  47. }
  48. cmRC_t cmCtxFree( cmCtx** pp )
  49. {
  50. if( pp != NULL && *pp != NULL )
  51. {
  52. cmCtxFinal(*pp);
  53. cmObjFree(pp);
  54. }
  55. return cmOkRC;
  56. }
  57. cmRC_t cmCtxInit( cmCtx* c, cmRpt_t* rpt, cmLHeapH_t lhH, cmSymTblH_t stH )
  58. {
  59. cmRC_t rc;
  60. if((rc = cmCtxFinal(c)) != cmOkRC )
  61. return rc;
  62. c->obj.err.rpt = rpt;
  63. c->lhH = lhH;
  64. c->stH = stH;
  65. return rc;
  66. }
  67. cmRC_t cmCtxFinal( cmCtx* c )
  68. { return cmOkRC; }
  69. cmRC_t cmCtxPrint( cmCtx* c, const char* fmt, ... )
  70. {
  71. va_list vl;
  72. va_start(vl,fmt);
  73. cmRptVPrintf(c->obj.err.rpt,fmt,vl);
  74. va_end(vl);
  75. return cmOkRC;
  76. }
  77. cmRC_t cmCtxRtCondition( cmObj* p, unsigned code, const char* fmt, ... )
  78. {
  79. va_list vl;
  80. va_start(vl,fmt);
  81. cmErrVMsg(&p->err,code,fmt,vl);
  82. va_end(vl);
  83. return code;
  84. }
  85. cmRC_t cmCtxRtAssertFailed( cmObj* p, unsigned code, const char* fmt, ... )
  86. {
  87. va_list vl;
  88. va_start(vl,fmt);
  89. cmErrVMsg(&p->err, code,fmt,vl);
  90. va_end(vl);
  91. return code;
  92. }
  93. //------------------------------------------------------------------------------------------------------------
  94. cmMtxFile* cmMtxFileAlloc( cmCtx* c, cmMtxFile* p, const char* fn )
  95. {
  96. cmMtxFile* op = cmObjAlloc( cmMtxFile, c, p );
  97. if( fn != NULL )
  98. if( cmMtxFileCreate( op, fn ) != cmOkRC && p == NULL )
  99. cmMtxFileFree(&op);
  100. return op;
  101. }
  102. cmRC_t cmMtxFileFree( cmMtxFile** pp )
  103. {
  104. cmRC_t rc = cmOkRC;
  105. if( pp != NULL && *pp != NULL)
  106. {
  107. cmLHeapFree((*pp)->obj.ctx->lhH,*pp);
  108. if((rc = cmMtxFileClose(*pp)) == cmOkRC )
  109. cmObjFree(pp);
  110. }
  111. return rc;
  112. }
  113. cmRC_t cmMtxFileCreate(cmMtxFile* p, const char* fn )
  114. {
  115. assert(fn != NULL );
  116. cmRC_t rc;
  117. if((rc = cmMtxFileClose(p)) != cmOkRC )
  118. return rc;
  119. if((p->fp = fopen(fn,"wt")) == NULL )
  120. return cmCtxRtCondition( &p->obj, cmSystemErrorRC, "Unable to create the matrix text file:'%s'.",fn);
  121. unsigned n = strlen(fn)+1;
  122. p->fn = cmLhResizeN(p->obj.ctx->lhH,cmChar_t,p->fn,n);
  123. strncpy(p->fn,fn,n);
  124. return rc;
  125. }
  126. cmRC_t cmMtxFileClose( cmMtxFile* p )
  127. {
  128. if( p != NULL )
  129. {
  130. if( p->fp != NULL )
  131. {
  132. fclose(p->fp);
  133. p->fp = NULL;
  134. }
  135. }
  136. return cmOkRC;
  137. }
  138. cmRC_t cmMtxFileFloatExec( cmMtxFile* p, const float* inPtr, unsigned inCnt, unsigned inStride )
  139. {
  140. const float* ep = inPtr + (inCnt * inStride);
  141. for(; inPtr < ep; inPtr+=inStride )
  142. fprintf(p->fp,"%e ",*inPtr);
  143. fprintf(p->fp,"\n");
  144. return cmOkRC;
  145. }
  146. cmRC_t cmMtxFileDoubleExec( cmMtxFile* p, const double* inPtr, unsigned inCnt, unsigned inStride )
  147. {
  148. const double* ep = inPtr + (inCnt * inStride);
  149. for(; inPtr < ep; inPtr+=inStride )
  150. fprintf(p->fp,"%e ",*inPtr);
  151. fprintf(p->fp,"\n");
  152. return cmOkRC;
  153. }
  154. cmRC_t cmMtxFileComplexExec( cmMtxFile* p, const cmComplexR_t* inPtr, unsigned inCnt, unsigned inStride )
  155. {
  156. const cmComplexR_t* sp = inPtr;
  157. const cmComplexR_t* ep = inPtr + (inCnt * inStride);
  158. for(; sp < ep; sp += inStride )
  159. fprintf(p->fp,"%e ",cmCrealR(*sp));
  160. fprintf(p->fp,"\n");
  161. sp = inPtr;
  162. for(; sp < ep; sp += inStride )
  163. fprintf(p->fp,"%e ",cmCimagR(*sp));
  164. fprintf(p->fp,"\n");
  165. return cmOkRC;
  166. }