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.

115 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 1989-2001 Microsoft Corporation
  3. Module Name:
  4. common.h
  5. Abstract:
  6. Platform independent utility functions
  7. Author:
  8. Jiandong Ruan
  9. Revision History:
  10. --*/
  11. #ifndef __COMMON_H__
  12. #define __COMMON_H__
  13. ////////////////////////////////////////////////////////////////////////////////
  14. // The common routines for handling SMB objects
  15. ////////////////////////////////////////////////////////////////////////////////
  16. typedef enum {
  17. SMB_REF_CREATE,
  18. SMB_REF_FIND,
  19. SMB_REF_DPC,
  20. SMB_REF_ASSOCIATE,
  21. SMB_REF_CONNECT,
  22. SMB_REF_DISCONNECT,
  23. SMB_REF_SEND,
  24. SMB_REF_RECEIVE,
  25. SMB_REF_TDI,
  26. SMB_REF_MAX
  27. } SMB_REF_CONTEXT;
  28. //
  29. // Forward declaration of SMB_OBJECT
  30. //
  31. struct _SMB_OBJECT;
  32. typedef struct _SMB_OBJECT SMB_OBJECT, *PSMB_OBJECT;
  33. typedef VOID (*PSMB_OBJECT_CLEANUP)(PSMB_OBJECT);
  34. struct _SMB_OBJECT {
  35. LIST_ENTRY Linkage;
  36. ULONG Tag;
  37. LONG RefCount;
  38. KSPIN_LOCK Lock;
  39. PSMB_OBJECT_CLEANUP CleanupRoutine;
  40. #ifdef REFCOUNT_DEBUG
  41. LONG RefContext[SMB_REF_MAX];
  42. #endif
  43. };
  44. void __inline
  45. SmbInitializeObject(PSMB_OBJECT ob, ULONG Tag, PSMB_OBJECT_CLEANUP cleanup)
  46. {
  47. ASSERT(cleanup);
  48. InitializeListHead(&ob->Linkage);
  49. ob->RefCount = 1;
  50. ob->CleanupRoutine = cleanup;
  51. ob->Tag = Tag;
  52. #ifdef REFCOUNT_DEBUG
  53. RtlZeroMemory(ob->RefContext, sizeof(ob->RefContext));
  54. ob->RefContext[SMB_REF_CREATE] = 1;
  55. #endif
  56. }
  57. void __inline
  58. SmbReferenceObject(PSMB_OBJECT ob, SMB_REF_CONTEXT ctx)
  59. {
  60. //
  61. // When the object is created, the refcount is initialized as 1
  62. // It is impossible for someone to reference an object whose
  63. // creation reference has been removed.
  64. //
  65. ASSERT(ob->RefCount > 0);
  66. InterlockedIncrement(&ob->RefCount);
  67. #ifdef REFCOUNT_DEBUG
  68. ASSERT(ob->RefContext[ctx] >= 0);
  69. InterlockedIncrement(&ob->RefContext[ctx]);
  70. #else
  71. UNREFERENCED_PARAMETER(ctx);
  72. #endif
  73. }
  74. void __inline
  75. SmbDereferenceObject(PSMB_OBJECT ob, SMB_REF_CONTEXT ctx)
  76. {
  77. ASSERT(ob->RefCount > 0);
  78. #ifdef REFCOUNT_DEBUG
  79. ASSERT(ob->RefContext[ctx] > 0);
  80. InterlockedDecrement(&ob->RefContext[ctx]);
  81. #else
  82. UNREFERENCED_PARAMETER(ctx);
  83. #endif
  84. if (0 == InterlockedDecrement(&ob->RefCount)) {
  85. #ifdef REFCOUNT_DEBUG
  86. LONG i;
  87. for (i = 0; i < SMB_REF_MAX; i++) {
  88. ASSERT(ob->RefContext[i] == 0);
  89. }
  90. #endif
  91. ob->CleanupRoutine(ob);
  92. }
  93. }
  94. #endif // __COMMON_H__