Source code of Windows XP (NT5)
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.

104 lines
4.9 KiB

  1. /*************************************************************************
  2. * *
  3. * DYNARRAY.C *
  4. * *
  5. * Copyright (C) Microsoft Corporation 1994-1995 *
  6. * All Rights reserved. *
  7. * *
  8. * This library implements a robust dynamic array scheme. It replaces the *
  9. * old DRG structure and should probably also replace the MR. *
  10. * The total size of the array is NOT limited to 64K since it uses *
  11. * Binh's threaded block memory manager to add new elements. Therefore it *
  12. * should work correctly on both 16- and 32-bit platforms for any number *
  13. * of elements. *
  14. * *
  15. * Items in the array are assumed to be of a fixed size, which is defined *
  16. * when the array is first initialized. To speed up the common operation *
  17. * of forward enumeration, the next element function/macro is provided. *
  18. * *
  19. * There are two types of lists possible: normal, and single-block *
  20. * Use single-block for better space and possible more efficient time *
  21. * usage when you know that your entire array will fit within a block. *
  22. * Allocations beyond the one block limit will return NULL. *
  23. * *
  24. * The first member of any element in the array MUST be an LPVOID. * *
  25. * UNIMPLEMENTED: Insert into, delete *
  26. **************************************************************************
  27. * *
  28. **************************************************************************
  29. * *
  30. * Written By : KevynCT *
  31. * Current Owner: KevynCT *
  32. * *
  33. **************************************************************************/
  34. #ifndef __DYNARRAY_H__
  35. #define __DYNARRAY_H__
  36. #if defined( __cplusplus )
  37. extern "C" {
  38. #endif
  39. #define CALC_BLOCK_SIZE(x, y) (sizeof(x) * (y))
  40. #define DEFAULT_BLOCK_MAX ((WORD)0xFFFF)
  41. #define DynArrayValid(lpdl) ((lpdl)->lpBlockHead)
  42. #define DynArrayGetNumElts(lpdl) ((lpdl)->cFoo)
  43. #define DynArrayEmpty(lpdl) ((lpdl)->cFoo == 0)
  44. #define DynArraySingleBlockGetNumElts(lpdl) ((lpdl)->cFoo)
  45. #define DynArrayGetFirstElt(lpdl) ((lpdl)->lpFooCache = (lpdl)->lpFooHead)
  46. #define DynArraySingleBlockGetFirstElt(lpdl) ((lpdl)->lpFooHead)
  47. #define DynArrayGetCurElt(lpdl) ((lpdl)->lpFooCache)
  48. #define DynArraySingleBlockGetCurElt(lpdl) ((lpdl)->lpFooCache)
  49. #define DynArraySetCurElt(lpdl, lpv) ((lpdl)->lpFooCache = (lpv))
  50. #define DynArraySingleBlockSetCurElt(lpdl, lpv) ((lpdl)->lpFooCache = (lpv))
  51. #define DynArrayNextElt(lpdl) ((lpdl)->lpFooCache = *(LPVOID FAR *)((lpdl)->lpFooCache))
  52. #define DYN_ARRAY_SINGLEBLOCK 0x00000001
  53. #define DynArrayEltCopy(lpdest, lpsrc, size) MEMCPY(((LPBYTE)(lpdest)) + sizeof(LPVOID), \
  54. ((LPBYTE)(lpsrc)) + sizeof(LPVOID), size - sizeof(LPVOID))
  55. // init using some default settings
  56. #define DynArrayInitDef(lpdl, s, wAvg) \
  57. DynArrayInit(lpdl, CALC_BLOCK_SIZE(s, wAvg), DEFAULT_BLOCK_MAX, sizeof(s), 0)
  58. // DFL elements:
  59. typedef struct tagDL
  60. {
  61. LPBLK lpBlockHead; // the linked list of blocks for this list
  62. WORD cbFoo; // the (fixed) size of the list elements
  63. WORD cFooPerBlock;
  64. DWORD cFoo;
  65. DWORD dwFlags;
  66. LPVOID lpFooHead; // the first element of the linked list
  67. LPVOID lpFooFree; // the next free element of the linked list
  68. LPVOID lpFooCache; // the last element accessed by an element method.
  69. } DL, FAR *LPDL;
  70. ///// ordinary (multi-block) arrays
  71. BOOL PASCAL FAR DynArrayInit(LPDL lpdl, DWORD BlockSize, WORD cMaxBlock, WORD wElemSize, DWORD dwFlags);
  72. LPVOID PASCAL FAR DynArrayAppendElt(LPDL lpdl);
  73. LPVOID PASCAL FAR DynArrayGetOrdinalElt(LPDL lpdl, DWORD dwEltIndex);
  74. //_inline LPVOID PASCAL FAR DynArrayNextElt(LPDL lpdl);
  75. LPVOID PASCAL FAR DynArrayClearElt(LPDL lpdl);
  76. VOID PASCAL FAR DynArrayFree(LPDL lpdl);
  77. VOID PASCAL FAR DynArrayReset(LPDL lpdl);
  78. ///// single-block arrays
  79. #ifdef SINGLE_BLOCK
  80. BOOL PASCAL FAR DynArraySingleBlockInit(LPDL lpdl, DWORD BlockSize, WORD wElemSize, DWORD dwFlags);
  81. LPVOID PASCAL FAR DynArraySingleBlockAppendElt(LPDL lpdl);
  82. LPVOID PASCAL FAR DynArraySingleBlockGetOrdinalElt(LPDL lpdl, DWORD dwEltIndex);
  83. _inline LPVOID PASCAL FAR DynArraySingleBlockNextElt(LPDL lpdl);
  84. #define DynArraySingleBlockClearElt(lpdl) DynArrayClearElt(lpdl)
  85. #define DynArraySingleBlockFree(lpdl) DynArrayFree(lpdl)
  86. #define DynArraySingleBlockReset(lpdl) DynArrayReset(lpdl)
  87. #endif
  88. #if defined( __cplusplus )
  89. }
  90. #endif
  91. #endif // __DYNARRAY_H__