MODULE iom$allocate_image_requests;

*copyc OSD$DEFAULT_PRAGMATS
*copyc IOE$ST_ERRORS
*copyc OSK$KEYPOINTS
*copyc IOK$KEYPOINTS
*copyc OST$STATUS
*copyc OST$HARDWARE_SUBRANGES
*copyc PMP$ZERO_OUT_TABLE
*copyc IOT$NUMBER_OF_REQUESTS
*copyc IOT$COMMAND
*copyc OSV$PAGE_SIZE
*copyc IOT$IMAGE_REQUEST
*copyc osv$mainframe_wired_cb_heap


  PROCEDURE [XDCL, #GATE] iop$allocate_image_requests (number_of_requests:
    iot$number_of_requests;
        commands_per_request: iot$commands_per_request;
    VAR image_request_area: ^SEQ ( * );
    VAR status: ost$status);

    VAR
      request_area: ^SEQ ( * ),
      image_request: iot$image_request,
      command: iot$command,
      image_length: 0 .. 100,
      command_length: 0 .. 100,
      request_size: 1 .. 1000,
      total_size: integer,
      unusable_size: integer,
      unusable_requests: integer,
      loop: boolean;



    status.normal := TRUE;
    #INLINE ('keypoint', osk$entry, 0, iok$allocate_image_request);

    image_length := #SIZE (image_request);
    command_length := #SIZE (command);
    request_size := image_length + (command_length * commands_per_request);
    total_size := request_size * number_of_requests;
    unusable_size := 0;

    loop := TRUE;
    WHILE loop DO
      unusable_requests := ((total_size + unusable_size) DIV osv$page_size) +
            1;
      unusable_size := unusable_requests * request_size;
      IF unusable_requests = (((total_size + unusable_size) DIV osv$page_size)
            + 1) THEN
        loop := FALSE;
      IFEND;
    WHILEND;

    total_size := total_size + unusable_size;

    ALLOCATE request_area: [[REP total_size OF ost$byte]] IN
          osv$mainframe_wired_cb_heap^;
    IF request_area = NIL THEN
      status.normal := FALSE;
      status.condition := ioc$no_space_to_allocate;
      RETURN;
    IFEND;

    pmp$zero_out_table (#LOC (request_area^), #SIZE (request_area^));

    image_request_area := request_area;

    #INLINE ('keypoint', osk$exit, 0, iok$allocate_image_request);




  PROCEND iop$allocate_image_requests;
MODEND iom$allocate_image_requests;
