
  FUNCTION [INLINE, UNSAFE] nlp$cc_obtain_credits
    (    connection: ^nlt$cc_connection): nlt$cc_credits;

?? PUSH (LISTEXT := ON) ??
*copy nlh$cc_obtain_credits

{ This function will return the number of additional credits to be assigned to
{ the specified connection. If no additional credits can be assigned and no
{ credits are outstanding, then a one minute timer will be selected to "wake up"
{ the connection. If credits are available a credit allocation PDU will be sent
{ to the peer process; if credits are still unavailable the timer will be reset.

    CONST
      sixty_seconds = 60000000; { Time in microseconds.

    VAR
      allocated_buffer_threshold: integer,
      available_buffers: integer,
      connection_limit: integer,
      credits: nlt$cc_credits,
      system_limit: integer;

    credits := 0;
    connection_limit := (nlv$cc_maximum_receive_window - connection^.accumulated_message_buffers);
    IF connection_limit > connection^.receive_credits THEN
      allocated_buffer_threshold := nlv$bm_allocat_buffer_threshold;
      available_buffers := nlv$bm_buffer_pool [nlc$bm_large_buffer_index].count +
            (allocated_buffer_threshold - nlv$bm_buffer_pool [nlc$bm_large_buffer_index].dynamic_buffers);

{ One buffer is reserved for each priority connection.
{ Note: Active connections include both normal and priority connections.

      IF connection^.class = nlc$cc_normal_class THEN
        available_buffers := available_buffers - nlv$cl_priority_connect_count;
      IFEND;

{ If at least half of the allowed buffers are available, no system limit will be imposed.

      IF available_buffers > (allocated_buffer_threshold DIV 2) THEN
        credits := connection_limit;
      ELSE

{ When the number of available network buffers becomes less than the number reserved for priority
{ connections the normal flow control window is closed.
{ If buffers are available, reserve some based on number of active connections to allow for
{ over commitment. Ensure that at least one credit is available of buffers are available.

        IF available_buffers >= connection^.buffers_per_credit THEN
          system_limit := ((available_buffers - (nlp$cl_active_connections () DIV 2)) DIV
                connection^.buffers_per_credit);
          IF system_limit <= 0 THEN
            system_limit := 1;
          IFEND;
          IF (connection_limit > system_limit) THEN
            credits := system_limit;
          ELSE
            credits := connection_limit;
          IFEND;
        IFEND;
      IFEND;

      IF credits > connection^.receive_credits THEN
        credits := credits - connection^.receive_credits;
      ELSE
        credits := 0;
        IF connection^.receive_credits = 0 THEN
          nlp$select_timer (sixty_seconds, 0, connection^.no_buffers_for_peer_credit);
        IFEND;
      IFEND;
    IFEND;

    nlp$cc_obtain_credits := credits;

  FUNCEND nlp$cc_obtain_credits;

*copyc nlc$bm_buffer_pool_index
*copyc nlv$cc_maximum_receive_window
*copyc nlt$bm_buffer_count
*copyc nlt$cc_connection
*copyc nlt$cc_credits
*copyc nlp$cl_active_connections
*copyc nlp$select_timer
*copyc nlv$bm_allocat_buffer_threshold
*copyc nlv$bm_buffer_pool
*copyc nlv$cl_priority_connect_count
?? POP ??
