InitializeDateLogHandle

Create and initialize a new DATE_LOG_HANDLE for date collection management

Overview

InitializeDateLogHandle creates and initializes a new DATE_LOG_HANDLE for managing date collections with support for three distinct handle types: DateLog, DateRange, and Holidays. The function establishes a thread-safe, shared context with mutex-based synchronization and reference counting to enable concurrent access from multiple threads.

Purpose

This is the primary entry point for creating date collection handles. It allocates necessary structures, initializes synchronization primitives, and establishes the foundation for all date operations within the MDTS library.

Key Features

Function Signature


                HRESULT _Must_inspect_result_ _Success_(SUCCEEDED(return)) InitializeDateLogHandle(
                 _In_opt_  PSECURITY_ATTRIBUTES                                                pSecAttr,
                 _In_opt_ _Pre_satisfies_(dwMaximumLength <= DATE_LOG_MAX) const DWORD         dwMaximumLength,
                 _In_opt_ _When_(HandleType == HandleIsForDatelog, _Notnull_ _Valid_)          HOLIDAYS_HANDLE hHolidays,
                 _Out_ DATE_LOG_HANDLE                                                         *hDateLogHandle,
                 _In_ DateHandleType                                                           HandleType
                );
            

Calling Convention

Standard C calling convention with SAL annotations for parameter validation and return value inspection.

Parameters

pSecAttr

[In, Optional] PSECURITY_ATTRIBUTES

Security attributes for the mutexes created by this function. Can be NULL to use default security.

  • If NULL: default process security is used
  • If provided: security descriptor determines mutex access rights
  • Used for all mutex creation within the handle
dwMaximumLength

[In, Optional] DWORD

Maximum number of date items this collection can hold. Determines pre-allocated capacity.

  • Must not exceed DATE_LOG_MAX (368,072 items)
  • If 0: automatic size is used based on handle type
  • For DateLog: defaults to DATE_LOG_MAX if not specified
  • For Holidays: defaults to MAX_HOLIDAYS (341 items) if not specified
  • Affects memory allocation and performance
hHolidays

[In, Optional] HOLIDAYS_HANDLE

Handle to holidays collection for market day validation. Required for DateLog and DateRange types.

  • For DateLog: Must be non-NULL and valid
  • For DateRange: Must be non-NULL and valid
  • For Holidays: Must be NULL (holidays don't reference other holidays)
  • Used internally for business day calculations and holiday validation
  • Handle is duplicated internally; original can be freed after this call
hDateLogHandle

[Out] DATE_LOG_HANDLE*

Pointer to receive the newly created DATE_LOG_HANDLE on success.

  • Must not be NULL
  • On success: receives valid handle pointer
  • On failure: receives NULL
  • Must be freed via FreeDateCollectionHandle() when no longer needed
HandleType

[In] DateHandleType

Specifies the type of collection handle to create. Determines behavior and requirements.

Handle Type Purpose hHolidays Required Max Size
HandleIsForDatelog Full date log with all market dates Yes (non-NULL) DATE_LOG_MAX
HandleIsForRange Temporary date range for merge operations Yes (non-NULL) DATE_LOG_MAX
HandleIsForHolidays Collection of holiday and weekend dates No (must be NULL) MAX_HOLIDAYS

Return Values

Success

S_OK (0x00000000)

Handle created successfully. hDateLogHandle contains valid pointer to new handle.

Validation Errors

ERROR_INVALID_PARAMETER (0x80070057)

Invalid parameter provided:

ERROR_INVALID_HANDLE (0x80070006)

hHolidays handle is invalid or corrupted. Handle must be previously created by InitializeDateLogHandle.

Resource Allocation Errors

ERROR_OUTOFMEMORY (0x80000002)

Insufficient memory to allocate handle structures, context, or mutexes. Check available system memory.

GetLastError()

System error from mutex creation or security validation. Wrapped as HRESULT.

Behavior and Operations

Initialization Process

  1. Validate input parameters
  2. Allocate DATE_CONTEXT structure
  3. Create primary mutex for context protection
  4. Initialize critical section for handle reference counting
  5. Allocate DATE_LOG_CORE for date item storage
  6. Create core protection mutex
  7. Set magic number based on handle type
  8. If holidays provided: duplicate holidays handle
  9. Create DATE_LOG structure (public handle)
  10. Return new handle to caller

Memory Allocation

The function allocates the following structures:

Synchronization

Three levels of synchronization are established:

Usage Examples

Example 1: Create DateLog Handle

                    // First, create holidays handle
                    HOLIDAYS_HANDLE hHolidays = NULL;
                    HRESULT hr = InitializeDateLogHandle(
                        NULL,                          // Default security
                        0,                             // Default size (MAX_HOLIDAYS)
                        NULL,                          // No parent holidays needed
                        &hHolidays,
                        HandleIsForHolidays
                    );

                    if (SUCCEEDED(hr)) {
                        // Now create DateLog handle
                        DATE_LOG_HANDLE hDateLog = NULL;
                        hr = InitializeDateLogHandle(
                            NULL,                      // Default security
                            100000,                    // Max 100,000 dates
                            hHolidays,                 // Holidays for validation
                            &hDateLog,
                            HandleIsForDatelog
                        );

                        if (SUCCEEDED(hr)) {
                            printf("DateLog handle created successfully\n");
                            FreeDateCollectionHandle(hDateLog);
                        }

                        FreeDateCollectionHandle(hHolidays);
                    }
               
Example 2: Create DateRange Handle

                    // Create a temporary date range for merging
                    DATE_RANGE_HANDLE hDateRange = NULL;

                    HRESULT hr = InitializeDateLogHandle(
                         NULL,                          // Default security
                        10000,                         // Max 10,000 items for range
                        hHolidays,                     // Holidays handle
                        &hDateRange,
                        HandleIsForRange
                    );

                    if (SUCCEEDED(hr)) {
                        printf("DateRange created successfully\n");
                        FreeDateCollectionHandle(hDateRange);
                    }
                
Example 3: Error Handling

                    DATE_LOG_HANDLE hDateLog = NULL;

                    HRESULT hr = InitializeDateLogHandle(
                        NULL,                      // pSecAttr
                        500000,                    // dwMaximumLength - EXCEEDS LIMIT!
                        hHolidays,
                        &hDateLog,
                        HandleIsForDatelog
                    );

                    if (hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER)) {
                            printf("Maximum length exceeds DATE_LOG_MAX\n");
                            // Use default size instead
                        hr = InitializeDateLogHandle(
                            NULL,
                            0,                     // Use default
                            hHolidays,
                            &hDateLog,
                            HandleIsForDatelog
                        );
                    }
               

Thread Safety

Synchronization Primitives

Reference Counting

The context uses reference counting to manage lifetime:

Important Notes

Handle Lifetime: Returned handles must be freed via FreeDateCollectionHandle() to properly decrement reference counts and release resources.
Holidays Dependency: For DateLog and DateRange handles, the holidays handle is duplicated internally and held for the lifetime of the created handle.
Size Limits: The dwMaximumLength parameter must not exceed DATE_LOG_MAX.
Security Attributes: If security restrictions are required, pass appropriate SECURITY_ATTRIBUTES structure. Default NULL uses process security context.

Related Functions