/*
  This file is part of the "Base Functions and Types" library, intended to
  simplify and enhance portability, memory and I/O use for scientific codes.

  Copyright (C) 2004-2006  EDF

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

#include "bft_error.h"
#include "bft_mem_usage.h"
#include "bft_mem.h"
#include "bft_sys_info.h"

static void
bft_mem_error_handler_test(const char     *const file_name,
                           const int             line_num,
                           const int             sys_error_code,
                           const char     *const format,
                           const va_list         arg_ptr)
{
  fprintf(stderr, "test memory error handler (empty).\n");
}

int
main (int argc, char *argv[])
{
  bft_error_handler_t *errhandler_save;

  void *p1, *p2, *p3, *p4;

  /* BFT initialization and environment */

  bft_mem_usage_set_options(BFT_MEM_USAGE_TRACK_PR_SIZE |
                            BFT_MEM_USAGE_TRACK_ALLOC_SIZE);
  bft_mem_usage_init();

  bft_mem_init("bft_mem_log_file");

  errhandler_save = bft_mem_error_handler_get();

  bft_mem_error_handler_set(bft_mem_error_handler_test);
  printf("test memory error handler set\n");

  BFT_MALLOC(p1, 100000, long);
  printf("p1 = %p\n", p1);
  BFT_MALLOC(p2, 100000, double);
  printf("p2 = %p\n", p2);

  bft_mem_usage_set_options(BFT_MEM_USAGE_TRACK_PR_SIZE |
                            BFT_MEM_USAGE_TRACK_ALLOC_SIZE);

  p3 = NULL;
  BFT_REALLOC(p3, 100000, double);
  printf("p3 = %p\n", p3);
  BFT_REALLOC(p3, 10000, double);
  printf("p3 = %p\n", p3);

  BFT_MALLOC(p4, 1000000000, double);
  printf("p4 = %p\n", p4);

  printf("default memory error handler set\n");
  bft_mem_error_handler_set(errhandler_save);

  BFT_FREE(p1);
  BFT_FREE(p2);
  BFT_MALLOC(p2, 100000, double);
  printf("p2 = %p\n", p2);
  BFT_FREE(p2);
  BFT_REALLOC(p3, 0, double);
  printf("p3 = %p\n", p3);

  if (bft_mem_have_memalign() == 1) {
    void *pa;
    BFT_MEMALIGN(pa, 128, 100, double);
    printf("pa (aligned 128) = %p\n", pa);
    BFT_FREE(pa);
  }

  bft_mem_end();

  printf("max memory usage: %lu kB\n",
         (unsigned long) bft_mem_usage_max_pr_size());
  printf("max memory usage (alloc): %lu kB\n",
         (unsigned long) bft_mem_usage_max_alloc_size());

  BFT_MALLOC(p1, 10000, long);
  printf("p1 = %p\n", p1);
  BFT_FREE(p1);
  printf("p1 = %p\n", p1);
  BFT_MALLOC(p1, 1000000000, double);
  printf("p1 = %p\n", p1);

  /* End */

  exit (EXIT_SUCCESS);
}


syntax highlighted by Code2HTML, v. 0.9.1