int posix_memalign(void **memptr, size_t alignment, size_t size)
{
  int ret;
  if( !chpl_mem_inited() ) {
    *memptr = NULL;
    ret = chpl_posix_memalign_check_valid(alignment);
    if( ret ) return ret;
    *memptr = __libc_memalign(alignment, size);
    if( ! *memptr ) return ENOMEM;
    if( DEBUG_REPLACE_MALLOC ) 
      printf("in early posix_memalign %p = system posix_memalign(%#x)\n",
             *memptr, (int) size);
    track_system_allocated(*memptr, size, __libc_malloc);
    return 0;
  }

  if( DEBUG_REPLACE_MALLOC ) 
    printf("in posix_memalign\n");

  ret = chpl_posix_memalign(memptr, alignment, size);

  if( DEBUG_REPLACE_MALLOC ) 
    printf("%p = chpl_posix_memalign(%#x, %#x) returned %i\n",
           *memptr, (int) alignment, (int) size, ret);

  return ret;
}
void chpl_memhook_check_pre(size_t number, size_t size,
                            chpl_mem_descInt_t description,
                            int32_t lineno, c_string filename) {
  if (!chpl_mem_inited())
    chpl_error("memory routine called before the memory layer is initialized",
               lineno, filename);

  if (number > 0 && size > SIZE_MAX/number)
    chpl_error("Attempting to allocate > max(size_t) bytes of memory",
               lineno, filename);
}
void* memalign(size_t alignment, size_t size)
{
  if( !chpl_mem_inited() ) {
    void* ret = __libc_memalign(alignment, size);
    if( DEBUG_REPLACE_MALLOC ) 
      printf("in early memalign %p = system memalign(%#x)\n", ret, (int) size);
    track_system_allocated(ret, size, __libc_malloc);
    return ret;
  }
  if( DEBUG_REPLACE_MALLOC ) 
    printf("in memalign\n");

  return chpl_memalign(alignment, size);
}
void* pvalloc(size_t size)
{
  if( !chpl_mem_inited() ) {
    void* ret = __libc_pvalloc(size);
    if( DEBUG_REPLACE_MALLOC ) 
      printf("in early pvalloc %p = system pvalloc(%#x)\n",
             ret, (int) size);
    track_system_allocated(ret, size, __libc_malloc);
    return ret;
  }
  if( DEBUG_REPLACE_MALLOC ) 
    printf("in pvalloc\n");

  return chpl_pvalloc(size);
}
void free(void* ptr)
{
  if( ! ptr ) return;
  if( DEBUG_REPLACE_MALLOC ) 
    printf("in free(%p)\n", ptr);
  // check to see if we're freeing a pointer that was allocated
  // before the our allocator came up.
  if( !chpl_mem_inited() || is_system_allocated(ptr, NULL) ) {
    if( DEBUG_REPLACE_MALLOC ) 
      printf("calling system free\n");
    __libc_free(ptr);
    return;
  }

  if( DEBUG_REPLACE_MALLOC ) 
    printf("calling chpl_free\n");
  chpl_free(ptr);
}
void* realloc(void* ptr, size_t size)
{
  if( !chpl_mem_inited() ) {
    void* ret = __libc_realloc(ptr, size);
    if( DEBUG_REPLACE_MALLOC ) 
      printf("in early realloc %p = system realloc(%p,%#x)\n",
             ret, ptr, (int) size);
    track_system_allocated(ret, size, __libc_malloc);
    return ret;
  } else {
    void* ret = NULL;
    size_t allocated_len = 0;
    size_t copy_size;

    if( DEBUG_REPLACE_MALLOC )
      printf("in realloc(%p,%#x)\n", ptr, (int) size);

    // check to see if we're realloc'ing a pointer that was allocated
    // before the our allocator came up.
    if( is_system_allocated(ptr, &allocated_len) ) {
      if( DEBUG_REPLACE_MALLOC ) {
        printf("in realloc, ptr %p was system allocated to size %#x\n",
               ptr, (int) allocated_len);
      }

      // allocate some new memory on the Chapel heap
      ret = chpl_malloc(size);

      // copy the minimum of allocated_len and size
      // to handle realloc expanding or shrinking the allocation
      copy_size = allocated_len;
      if( size < copy_size ) copy_size = size;

      memcpy(ret, ptr, copy_size);

      // free the old pointer from the system heap.
      __libc_free(ptr);
      return ret;
    } else {
      return chpl_realloc(ptr, size);
    }
  }
}
void* calloc(size_t n, size_t size)
{
  void* ret;
  if( !chpl_mem_inited() ) {
    ret = __libc_calloc(n, size);
    if( DEBUG_REPLACE_MALLOC ) 
      printf("in early calloc %p = system calloc(%#x)\n", ret, (int) (n*size));
    track_system_allocated(ret, n*size, __libc_malloc);
    return ret;
  }
  if( DEBUG_REPLACE_MALLOC ) 
    printf("in calloc\n");

  ret = chpl_calloc(n, size);

  if( DEBUG_REPLACE_MALLOC ) 
    printf("%p = chpl_calloc(%#x)\n", ret, (int) (n*size));

  return ret;
}