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.

cmDspStore.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmRpt.h"
  5. #include "cmErr.h"
  6. #include "cmCtx.h"
  7. #include "cmMem.h"
  8. #include "cmMallocDebug.h"
  9. #include "cmLinkedHeap.h"
  10. #include "cmSymTbl.h"
  11. #include "cmJson.h"
  12. #include "cmDspValue.h"
  13. #include "cmDspCtx.h"
  14. #include "cmDspClass.h"
  15. #include "cmDspStore.h"
  16. typedef struct
  17. {
  18. unsigned symId;
  19. cmDspValue_t value;
  20. } cmDspStoreVar_t;
  21. typedef struct
  22. {
  23. cmErr_t err;
  24. cmDspStoreVar_t* array;
  25. unsigned allocCnt;
  26. unsigned growCnt;
  27. unsigned curCnt;
  28. } cmDspStore_t;
  29. cmDspStoreH_t cmDspStoreNullHandle = cmSTATIC_NULL_HANDLE;
  30. cmDspStore_t* _cmDspStoreHandleToPtr( cmDspStoreH_t h )
  31. {
  32. cmDspStore_t* p = (cmDspStore_t*)h.h;
  33. assert( p != NULL );
  34. return p;
  35. }
  36. cmDspRC_t _cmDspStoreFree( cmDspStore_t* p )
  37. {
  38. cmMemFree(p->array);
  39. cmMemFree(p);
  40. return kOkDspRC;
  41. }
  42. cmDspRC_t cmDspStoreAlloc( cmCtx_t* ctx, cmDspStoreH_t* hp, unsigned initStoreCnt, unsigned growStoreCnt )
  43. {
  44. cmDspRC_t rc;
  45. if((rc = cmDspStoreFree(hp)) != kOkDspRC )
  46. return rc;
  47. cmDspStore_t* p = cmMemAllocZ(cmDspStore_t,1);
  48. cmErrSetup(&p->err,&ctx->rpt,"cmDspStore");
  49. p->array = cmMemAllocZ(cmDspStoreVar_t,initStoreCnt);
  50. p->allocCnt = initStoreCnt;
  51. p->curCnt = 0;
  52. p->growCnt = growStoreCnt;
  53. hp->h = p;
  54. return rc;
  55. }
  56. cmDspRC_t cmDspStoreFree( cmDspStoreH_t *hp )
  57. {
  58. cmDspRC_t rc = kOkDspRC;
  59. if(hp==NULL || cmDspStoreIsValid(*hp)==false )
  60. return rc;
  61. cmDspStore_t* p = _cmDspStoreHandleToPtr(*hp);
  62. if((rc = _cmDspStoreFree(p)) != kOkDspRC )
  63. return rc;
  64. hp->h = NULL;
  65. return rc;
  66. }
  67. bool cmDspStoreIsValid( cmDspStoreH_t h )
  68. { return h.h; }
  69. cmDspStoreVar_t* _cmDspStoreSymToPtr( cmDspStore_t* p, unsigned symId )
  70. {
  71. cmDspStoreVar_t* vp = p->array;
  72. cmDspStoreVar_t* ep = p->array + p->curCnt;
  73. for(; vp<ep; ++vp)
  74. if( vp->symId == symId )
  75. return vp;
  76. return NULL;
  77. }
  78. cmDspStoreVar_t* _cmDspStoreIdToPtr( cmDspStore_t* p, unsigned id )
  79. {
  80. if( id < p->curCnt )
  81. return p->array + id;
  82. return NULL;
  83. }
  84. cmDspStoreVar_t* _cmDspStoreAppend( cmDspStore_t* p )
  85. {
  86. cmDspStoreVar_t* vp = NULL;
  87. if( p->curCnt >= p->allocCnt )
  88. {
  89. p->allocCnt += p->growCnt;
  90. p->array = cmMemResizePZ(cmDspStoreVar_t,p->array,p->allocCnt);
  91. }
  92. vp = p->array + p->curCnt;
  93. p->curCnt += 1;
  94. return vp;
  95. }
  96. unsigned cmDspStoreSymToId( cmDspStoreH_t h, unsigned symId )
  97. {
  98. cmDspStore_t* p = _cmDspStoreHandleToPtr(h);
  99. const cmDspStoreVar_t* vp;
  100. if((vp = _cmDspStoreSymToPtr(p,symId)) == NULL )
  101. return cmInvalidId;
  102. return vp - p->array;
  103. }
  104. unsigned cmDspStoreIdToSym( cmDspStoreH_t h, unsigned id )
  105. {
  106. cmDspStore_t* p = _cmDspStoreHandleToPtr(h);
  107. const cmDspStoreVar_t* vp;
  108. if((vp = _cmDspStoreIdToPtr(p,id)) == NULL )
  109. return cmInvalidId;
  110. return vp->symId;
  111. }
  112. const cmDspValue_t* cmDspStoreIdToValue( cmDspStoreH_t h, unsigned id )
  113. {
  114. cmDspStore_t* p = _cmDspStoreHandleToPtr(h);
  115. const cmDspStoreVar_t* vp;
  116. if((vp = _cmDspStoreIdToPtr(p,id)) == NULL )
  117. return NULL;
  118. return &vp->value;
  119. }
  120. cmDspRC_t cmDspStoreSetValueViaId( cmDspStoreH_t h, unsigned id, const cmDspValue_t* val )
  121. {
  122. cmDspRC_t rc = kOkDspRC;
  123. cmDspStore_t* p = _cmDspStoreHandleToPtr(h);
  124. cmDspStoreVar_t* vp = NULL;
  125. if((vp = _cmDspStoreIdToPtr(p,id)) == NULL )
  126. return cmErrMsg(&p->err,kVarNotFoundDspRC,"There is not global variable at with id:%i\n",id);
  127. cmDsvCopy(&vp->value,val);
  128. return rc;
  129. }
  130. unsigned cmDspStoreSetValueViaSym( cmDspStoreH_t h, unsigned symId, const cmDspValue_t* val )
  131. {
  132. cmDspStore_t* p = _cmDspStoreHandleToPtr(h);
  133. cmDspStoreVar_t* vp = NULL;
  134. if((vp = _cmDspStoreSymToPtr(p,symId)) == NULL )
  135. vp = _cmDspStoreAppend(p);
  136. assert(vp != NULL );
  137. cmDsvCopy(&vp->value,val);
  138. vp->symId = symId;
  139. return vp - p->array;
  140. }