// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.

#ifndef tools_cmemT
#define tools_cmemT

#include <cstdlib>
#include <cstring> //memcpy

namespace tools {

template <class T>
inline void cmem_free(T*& a_p){
  if(!a_p) return;
  ::free(a_p);
  a_p = NULL;
}

template <class T>
inline T* cmem_alloc(size_t a_num){
  if(a_num<=0) return 0;
  T* p = (T*)::malloc(a_num*sizeof(T));
  if(!p) return 0;
  return p;
}

template <class T>
inline bool cmem_realloc(T*& a_pointer,size_t a_new_size,size_t a_old_size,bool a_init = false) {
  if(!a_new_size) {
    delete [] a_pointer;
    a_pointer = 0;
    return true;
  }
  if(!a_pointer) {
    a_pointer = new T[a_new_size];
    if(!a_pointer) return false;
    return true;
  }
  if(a_old_size==a_new_size) return true;
  T* pointer = new T[a_new_size];
  if(!pointer) {
    delete [] a_pointer;
    a_pointer = 0;
    return false;
  }
  if(a_new_size>a_old_size) {
    ::memcpy(pointer,a_pointer,a_old_size*sizeof(T));
    if(a_init){
      size_t num = a_new_size-a_old_size;
      T* pos = pointer+a_old_size;
      for(size_t i=0;i<num;i++,pos++) *pos = T();
    }
  } else {
    ::memcpy(pointer,a_pointer,a_new_size*sizeof(T));
  }
  delete [] a_pointer;
  a_pointer = pointer;
  return true;
}

}

#endif
