/* allocation.hpp */

/*
Copyright (C) 2006 Fabrice Ducos, fabrice.ducos@icare.univ-lille1.fr

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

This program 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 General Public License for more details.

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

#ifndef ALLOCATION_HPP
#define ALLOCATION_HPP

#include <new>
#include <cassert>

#undef ALLOCATION_DEBUG

#ifdef ALLOCATION_DEBUG
#define PDEBUG { std::fprintf(stderr, "call to %s (%s)\n", __PRETTY_FUNCTION__, __FILE__); }
#else
#define PDEBUG
#endif

template <typename data_type>
void allocate(data_type ** &data, int nrows, int ncols) {

  PDEBUG;

  typedef data_type * pdata_type;

  data = new(std::nothrow) pdata_type[nrows] ; assert(data != NULL);
  data[0] = new(std::nothrow) data_type[nrows*ncols]; assert(data[0] != NULL);
  
  for (int irow = 1 ; irow < nrows ; irow++) {
    data[irow] = data[0] + irow*ncols;
  }
}

template <typename data_type>
void deallocate(data_type ** &data) {

  PDEBUG;

  delete data[0];
  delete data;

}

#endif