Leaked source code of windows server 2003
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.

107 lines
2.5 KiB

  1. //
  2. // strary.cpp
  3. //
  4. // CStructArray
  5. //
  6. #include "private.h"
  7. #include "strary.h"
  8. #include "mem.h"
  9. #define StrPB(x) (_pb + ((x) * _iElemSize))
  10. //+---------------------------------------------------------------------------
  11. //
  12. // Insert(int iIndex, int cElems)
  13. //
  14. // Grows the array to accomodate cElems at offset iIndex.
  15. //
  16. // The new cells are NOT initialized!
  17. //
  18. //----------------------------------------------------------------------------
  19. BOOL CVoidStructArray::Insert(int iIndex, int cElems)
  20. {
  21. BYTE *pb;
  22. int iSizeNew;
  23. Assert(iIndex >= 0);
  24. Assert(iIndex <= _cElems);
  25. Assert(cElems > 0);
  26. // allocate space if necessary
  27. if (_iSize < _cElems + cElems)
  28. {
  29. // allocate 1.5x what we need to avoid future allocs
  30. iSizeNew = max(_cElems + cElems, _cElems + _cElems / 2);
  31. if ((pb = (_pb == NULL) ?
  32. (BYTE *)cicMemAlloc(iSizeNew*_iElemSize) :
  33. (BYTE *)cicMemReAlloc(_pb, iSizeNew* _iElemSize))
  34. == NULL)
  35. {
  36. return FALSE;
  37. }
  38. _pb = pb;
  39. _iSize = iSizeNew;
  40. }
  41. if (iIndex < _cElems)
  42. {
  43. // make room for the new addition
  44. memmove(StrPB(iIndex + cElems),
  45. StrPB(iIndex),
  46. (_cElems - iIndex)*_iElemSize);
  47. #ifdef DEBUG
  48. memset(StrPB(iIndex), 0xFE, cElems * _iElemSize);
  49. #endif
  50. }
  51. _cElems += cElems;
  52. Assert(_iSize >= _cElems);
  53. return TRUE;
  54. }
  55. //+---------------------------------------------------------------------------
  56. //
  57. // Remove(int Index, int cElems)
  58. //
  59. // Removes cElems at offset iIndex.
  60. //
  61. //----------------------------------------------------------------------------
  62. void CVoidStructArray::Remove(int iIndex, int cElems)
  63. {
  64. BYTE *pb;
  65. int iSizeNew;
  66. Assert(cElems > 0);
  67. Assert(iIndex >= 0);
  68. Assert(iIndex + cElems <= _cElems);
  69. if (iIndex + cElems < _cElems)
  70. {
  71. // shift following eles left
  72. memmove(StrPB(iIndex),
  73. StrPB(iIndex + cElems),
  74. (_cElems - iIndex - cElems) * _iElemSize);
  75. #ifdef DEBUG
  76. memset(StrPB(_cElems - cElems), 0xFE, cElems * _iElemSize);
  77. #endif
  78. }
  79. _cElems -= cElems;
  80. // free mem when array contents uses less than half alloc'd mem
  81. iSizeNew = _iSize / 2;
  82. if (iSizeNew > _cElems)
  83. {
  84. if ((pb = (BYTE *)cicMemReAlloc(_pb, iSizeNew * _iElemSize)) != NULL)
  85. {
  86. _pb = pb;
  87. _iSize = iSizeNew;
  88. }
  89. }
  90. }