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.7KB

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