// File: tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: realize_virt_arrays
METHODDEF(void)
realize_virt_arrays (j_common_ptr cinfo)
/* Allocate the in-memory buffers for any unrealized virtual arrays */
{
  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
  long space_per_minheight, maximum_space, avail_mem;
  long minheights, max_minheights;
  jvirt_sarray_ptr sptr;
  jvirt_barray_ptr bptr;

  /* Compute the minimum space needed (maxaccess rows in each buffer)
   * and the maximum space needed (full image height in each buffer).
   * These may be of use to the system-dependent jpeg_mem_available routine.
   */
  space_per_minheight = 0;
  maximum_space = 0;
  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
    if (sptr->mem_buffer == NULL) { /* if not realized yet */
      space_per_minheight += (long) sptr->maxaccess *
			     (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
      maximum_space += (long) sptr->rows_in_array *
		       (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
    }
  }
  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
    if (bptr->mem_buffer == NULL) { /* if not realized yet */
      space_per_minheight += (long) bptr->maxaccess *
			     (long) bptr->blocksperrow * SIZEOF(JBLOCK);
      maximum_space += (long) bptr->rows_in_array *
		       (long) bptr->blocksperrow * SIZEOF(JBLOCK);
    }
  }

  if (space_per_minheight <= 0)
    return;			/* no unrealized arrays, no work */

  /* Determine amount of memory to actually use; this is system-dependent. */
  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
				 mem->total_space_allocated);

  /* If the maximum space needed is available, make all the buffers full
   * height; otherwise parcel it out with the same number of minheights
   * in each buffer.
   */
  if (avail_mem >= maximum_space)
    max_minheights = 1000000000L;
  else {
    max_minheights = avail_mem / space_per_minheight;
    /* If there doesn't seem to be enough space, try to get the minimum
     * anyway.  This allows a "stub" implementation of jpeg_mem_available().
     */
    if (max_minheights <= 0)
      max_minheights = 1;
  }

  /* Allocate the in-memory buffers and initialize backing store as needed. */

  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
    if (sptr->mem_buffer == NULL) { /* if not realized yet */
      minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
      if (minheights <= max_minheights) {
	/* This buffer fits in memory */
	sptr->rows_in_mem = sptr->rows_in_array;
      } else {
	/* It doesn't fit in memory, create backing store. */
	sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
	jpeg_open_backing_store(cinfo, & sptr->b_s_info,
				(long) sptr->rows_in_array *
				(long) sptr->samplesperrow *
				(long) SIZEOF(JSAMPLE));
	sptr->b_s_open = TRUE;
      }
      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
				      sptr->samplesperrow, sptr->rows_in_mem);
      sptr->rowsperchunk = mem->last_rowsperchunk;
      sptr->cur_start_row = 0;
      sptr->first_undef_row = 0;
      sptr->dirty = FALSE;
    }
  }

  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
    if (bptr->mem_buffer == NULL) { /* if not realized yet */
      minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
      if (minheights <= max_minheights) {
	/* This buffer fits in memory */
	bptr->rows_in_mem = bptr->rows_in_array;
      } else {
	/* It doesn't fit in memory, create backing store. */
	bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
	jpeg_open_backing_store(cinfo, & bptr->b_s_info,
				(long) bptr->rows_in_array *
				(long) bptr->blocksperrow *
				(long) SIZEOF(JBLOCK));
	bptr->b_s_open = TRUE;
      }
      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
				      bptr->blocksperrow, bptr->rows_in_mem);
      bptr->rowsperchunk = mem->last_rowsperchunk;
      bptr->cur_start_row = 0;
      bptr->first_undef_row = 0;
      bptr->dirty = FALSE;
    }
  }
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x28(%rsp)
movq 0x38(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0xfa483
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xfa471
movq 0x8(%rsp), %rax
movl 0x10(%rax), %eax
movq 0x8(%rsp), %rcx
movl 0xc(%rcx), %ecx
imulq %rcx, %rax
shlq $0x0, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rax
movl 0x8(%rax), %eax
movq 0x8(%rsp), %rcx
movl 0xc(%rcx), %ecx
imulq %rcx, %rax
shlq $0x0, %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
jmp 0xfa473
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0xfa41a
movq 0x38(%rsp), %rax
movq 0x90(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
je 0xfa4f4
movq (%rsp), %rax
cmpq $0x0, (%rax)
jne 0xfa4e4
movq (%rsp), %rax
movl 0x10(%rax), %eax
movq (%rsp), %rcx
movl 0xc(%rcx), %ecx
imulq %rcx, %rax
shlq $0x7, %rax
addq 0x30(%rsp), %rax
movq %rax, 0x30(%rsp)
movq (%rsp), %rax
movl 0x8(%rax), %eax
movq (%rsp), %rcx
movl 0xc(%rcx), %ecx
imulq %rcx, %rax
shlq $0x7, %rax
addq 0x28(%rsp), %rax
movq %rax, 0x28(%rsp)
jmp 0xfa4e6
movq (%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, (%rsp)
jmp 0xfa493
cmpq $0x0, 0x30(%rsp)
jg 0xfa501
jmp 0xfa7b7
movq 0x40(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x38(%rsp), %rax
movq 0x98(%rax), %rcx
callq 0xfb4b0
movq %rax, 0x20(%rsp)
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
jl 0xfa53d
movq $0x3b9aca00, 0x10(%rsp) # imm = 0x3B9ACA00
jmp 0xfa561
movq 0x20(%rsp), %rax
cqto
idivq 0x30(%rsp)
movq %rax, 0x10(%rsp)
cmpq $0x0, 0x10(%rsp)
jg 0xfa55f
movq $0x1, 0x10(%rsp)
jmp 0xfa561
movq 0x38(%rsp), %rax
movq 0x88(%rax), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
je 0xfa697
movq 0x8(%rsp), %rax
cmpq $0x0, (%rax)
jne 0xfa682
movq 0x8(%rsp), %rax
movl 0x8(%rax), %eax
subq $0x1, %rax
movq 0x8(%rsp), %rcx
movl 0x10(%rcx), %ecx
cqto
idivq %rcx
addq $0x1, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
jg 0xfa5cd
movq 0x8(%rsp), %rax
movl 0x8(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x14(%rax)
jmp 0xfa621
movq 0x10(%rsp), %rax
movq 0x8(%rsp), %rcx
movl 0x10(%rcx), %ecx
imulq %rcx, %rax
movl %eax, %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rsi
addq $0x38, %rsi
movq 0x8(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
movq 0x8(%rsp), %rax
movl 0xc(%rax), %eax
imulq %rax, %rdx
shlq $0x0, %rdx
callq 0xfb4d0
movq 0x8(%rsp), %rax
movl $0x1, 0x2c(%rax)
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rax
movl 0xc(%rax), %edx
movq 0x8(%rsp), %rax
movl 0x14(%rax), %ecx
movl $0x1, %esi
callq 0xf9ee0
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rax
movl 0xa0(%rax), %ecx
movq 0x8(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x8(%rsp), %rax
movl $0x0, 0x1c(%rax)
movq 0x8(%rsp), %rax
movl $0x0, 0x20(%rax)
movq 0x8(%rsp), %rax
movl $0x0, 0x28(%rax)
jmp 0xfa684
movq 0x8(%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0xfa572
movq 0x38(%rsp), %rax
movq 0x90(%rax), %rax
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
je 0xfa7b7
movq (%rsp), %rax
cmpq $0x0, (%rax)
jne 0xfa7a4
movq (%rsp), %rax
movl 0x8(%rax), %eax
subq $0x1, %rax
movq (%rsp), %rcx
movl 0x10(%rcx), %ecx
cqto
idivq %rcx
addq $0x1, %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rax
cmpq 0x10(%rsp), %rax
jg 0xfa6fc
movq (%rsp), %rax
movl 0x8(%rax), %ecx
movq (%rsp), %rax
movl %ecx, 0x14(%rax)
jmp 0xfa74a
movq 0x10(%rsp), %rax
movq (%rsp), %rcx
movl 0x10(%rcx), %ecx
imulq %rcx, %rax
movl %eax, %ecx
movq (%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x40(%rsp), %rdi
movq (%rsp), %rsi
addq $0x38, %rsi
movq (%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
movq (%rsp), %rax
movl 0xc(%rax), %eax
imulq %rax, %rdx
shlq $0x7, %rdx
callq 0xfb4d0
movq (%rsp), %rax
movl $0x1, 0x2c(%rax)
movq 0x40(%rsp), %rdi
movq (%rsp), %rax
movl 0xc(%rax), %edx
movq (%rsp), %rax
movl 0x14(%rax), %ecx
movl $0x1, %esi
callq 0xfa060
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, (%rax)
movq 0x38(%rsp), %rax
movl 0xa0(%rax), %ecx
movq (%rsp), %rax
movl %ecx, 0x18(%rax)
movq (%rsp), %rax
movl $0x0, 0x1c(%rax)
movq (%rsp), %rax
movl $0x0, 0x20(%rax)
movq (%rsp), %rax
movl $0x0, 0x28(%rax)
jmp 0xfa7a6
movq (%rsp), %rax
movq 0x30(%rax), %rax
movq %rax, (%rsp)
jmp 0xfa6a7
addq $0x48, %rsp
retq
nopl (%rax)