// File: tomfunkhouser[P]gaps/pkgs/jpeg/jmemmgr.c
// Function: jinit_memory_mgr
GLOBAL(void)
jinit_memory_mgr (j_common_ptr cinfo)
{
  my_mem_ptr mem;
  long max_to_use;
  int pool;
  size_t test_mac;

  cinfo->mem = NULL;		/* for safety if init fails */

  /* Check for configuration errors.
   * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
   * doesn't reflect any real hardware alignment requirement.
   * The test is a little tricky: for X>0, X and X-1 have no one-bits
   * in common if and only if X is a power of 2, ie has only one one-bit.
   * Some compilers may give an "unreachable code" warning here; ignore it.
   */
  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
    ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
  /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
   * a multiple of SIZEOF(ALIGN_TYPE).
   * Again, an "unreachable code" warning may be ignored here.
   * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
   */
  test_mac = (size_t) MAX_ALLOC_CHUNK;
  if ((long) test_mac != MAX_ALLOC_CHUNK ||
      (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);

  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */

  /* Attempt to allocate memory manager's control block */
  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));

  if (mem == NULL) {
    jpeg_mem_term(cinfo);	/* system-dependent cleanup */
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
  }

  /* OK, fill in the method pointers */
  mem->pub.alloc_small = alloc_small;
  mem->pub.alloc_large = alloc_large;
  mem->pub.alloc_sarray = alloc_sarray;
  mem->pub.alloc_barray = alloc_barray;
  mem->pub.request_virt_sarray = request_virt_sarray;
  mem->pub.request_virt_barray = request_virt_barray;
  mem->pub.realize_virt_arrays = realize_virt_arrays;
  mem->pub.access_virt_sarray = access_virt_sarray;
  mem->pub.access_virt_barray = access_virt_barray;
  mem->pub.free_pool = free_pool;
  mem->pub.self_destruct = self_destruct;

  /* Make MAX_ALLOC_CHUNK accessible to other modules */
  mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;

  /* Initialize working state */
  mem->pub.max_memory_to_use = max_to_use;

  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
    mem->small_list[pool] = NULL;
    mem->large_list[pool] = NULL;
  }
  mem->virt_sarray_list = NULL;
  mem->virt_barray_list = NULL;

  mem->total_space_allocated = SIZEOF(my_memory_mgr);

  /* Declare ourselves open for business */
  cinfo->mem = & mem->pub;

  /* Check for an environment variable JPEGMEM; if found, override the
   * default max_memory setting from jpeg_mem_init.  Note that the
   * surrounding application may again override this value.
   * If your system doesn't support getenv(), define NO_GETENV to disable
   * this feature.
   */
#ifndef NO_GETENV
  { char * memenv;

    if ((memenv = getenv("JPEGMEM")) != NULL) {
      char ch = 'x';

      if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
	if (ch == 'm' || ch == 'M')
	  max_to_use *= 1000L;
	mem->pub.max_memory_to_use = max_to_use * 1000L;
      }
    }
  }
#endif

}

// Assembly:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq $0x0, 0x8(%rax)
movq $0x3b9aca00, 0x10(%rsp) # imm = 0x3B9ACA00
cmpq $0x3b9aca00, 0x10(%rsp) # imm = 0x3B9ACA00
je 0xf98db
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x3, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x30(%rsp), %rdi
callq 0xfb510
movq %rax, 0x20(%rsp)
movq 0x30(%rsp), %rdi
movl $0xa8, %esi
callq 0xfb410
movq %rax, 0x28(%rsp)
cmpq $0x0, 0x28(%rsp)
jne 0xf9940
movq 0x30(%rsp), %rdi
callq 0xfb520
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x36, 0x28(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movl $0x0, 0x2c(%rax)
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rdi
callq *%rax
movq 0x28(%rsp), %rax
leaq 0x1c4(%rip), %rcx       # 0xf9b10
movq %rcx, (%rax)
movq 0x28(%rsp), %rax
leaq 0x435(%rip), %rcx       # 0xf9d90
movq %rcx, 0x8(%rax)
movq 0x28(%rsp), %rax
leaq 0x575(%rip), %rcx       # 0xf9ee0
movq %rcx, 0x10(%rax)
movq 0x28(%rsp), %rax
leaq 0x6e5(%rip), %rcx       # 0xfa060
movq %rcx, 0x18(%rax)
movq 0x28(%rsp), %rax
leaq 0x855(%rip), %rcx       # 0xfa1e0
movq %rcx, 0x20(%rax)
movq 0x28(%rsp), %rax
leaq 0x945(%rip), %rcx       # 0xfa2e0
movq %rcx, 0x28(%rax)
movq 0x28(%rsp), %rax
leaq 0xa35(%rip), %rcx       # 0xfa3e0
movq %rcx, 0x30(%rax)
movq 0x28(%rsp), %rax
leaq 0xe05(%rip), %rcx       # 0xfa7c0
movq %rcx, 0x38(%rax)
movq 0x28(%rsp), %rax
leaq 0x1095(%rip), %rcx      # 0xfaa60
movq %rcx, 0x40(%rax)
movq 0x28(%rsp), %rax
leaq 0x1325(%rip), %rcx      # 0xfad00
movq %rcx, 0x48(%rax)
movq 0x28(%rsp), %rax
leaq 0x1585(%rip), %rcx      # 0xfaf70
movq %rcx, 0x50(%rax)
movq 0x28(%rsp), %rax
movq $0x3b9aca00, 0x60(%rax) # imm = 0x3B9ACA00
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %rax
movq %rcx, 0x58(%rax)
movl $0x1, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jl 0xf9a4c
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq $0x0, 0x68(%rax,%rcx,8)
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rcx
movq $0x0, 0x78(%rax,%rcx,8)
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0xf9a12
movq 0x28(%rsp), %rax
movq $0x0, 0x88(%rax)
movq 0x28(%rsp), %rax
movq $0x0, 0x90(%rax)
movq 0x28(%rsp), %rax
movq $0xa8, 0x98(%rax)
movq 0x28(%rsp), %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x8(%rax)
leaq 0x3eddf(%rip), %rdi     # 0x138870
callq 0xf610
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xf9afe
movb $0x78, 0x7(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x3edc6(%rip), %rsi     # 0x138878
leaq 0x20(%rsp), %rdx
leaq 0x7(%rsp), %rcx
movb $0x0, %al
callq 0xf4a0
cmpl $0x0, %eax
jle 0xf9afc
movsbl 0x7(%rsp), %eax
cmpl $0x6d, %eax
je 0xf9adc
movsbl 0x7(%rsp), %eax
cmpl $0x4d, %eax
jne 0xf9aea
imulq $0x3e8, 0x20(%rsp), %rax # imm = 0x3E8
movq %rax, 0x20(%rsp)
imulq $0x3e8, 0x20(%rsp), %rcx # imm = 0x3E8
movq 0x28(%rsp), %rax
movq %rcx, 0x58(%rax)
jmp 0xf9afe
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)