⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 storage.c

📁 蒙特卡罗模拟光子成像C语言版,代码简洁专业
💻 C
字号:
/***********************************************************************
 *
 * Handle storage allocation and deallocation for multi-dimensional
 *  arrays.
 *
 * Allocation routines return NULL on error.  Deallocation routines
 *  fail silently.
 */

/*
 * This file is part of tMCimg.
 * 
 * tMCimg 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.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


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

#include "config.h"

void *alloc_vector(int nelem, size_t selem);
void free_vector(void *p);

/* ****************************************************************** */
/* There's really only one allocation routine, these here are just
 * wrappers and for book-keeping. */

void *alloc_1d_array(int n1, size_t nb)
{ 
  void *p;

  return ((void *)p = alloc_vector(n1, nb));
}

void *alloc_2d_array(int n1, int n2, size_t nb)
{ 
  void **p;
  int i;

  p = (void **)alloc_vector(n1, sizeof(void *));

  if (p != NULL)
    for (i = 0; i < n1; i++)
      p[i] = alloc_1d_array(n2, nb);

  return (void *)p;
}

void *alloc_3d_array(int n1, int n2, int n3, size_t nb)
{ 
  void ***p;
  int i;

  p = (void ***)alloc_vector(n1, sizeof(void **));

  if (p != NULL)
    for (i = 0; i < n1; i++)
      p[i] = alloc_2d_array(n2, n3, nb);

  return (void *)p;
}

void *alloc_4d_array(int n1, int n2, int n3, int n4, size_t nb)
{ 
  void ****p;
  int i;

  p = (void ****)alloc_vector(n1, sizeof(void ***));

  if (p != NULL)
    for (i = 0; i < n1; i++)
      p[i] = alloc_3d_array(n2, n3, n4, nb);

  return (void *)p;
}

/* ****************************************************************** */
/* Likewise, there's really only one deallocation function */

void free_1d_array(void *p, int n1)
{
  free_vector(p);
}

void free_2d_array(void *p, int n1, int n2)
{
  void **q = (void **)p;
  int i;

  if (q != NULL)
    {
      for (i = 0; i < n1; i++)
        free_1d_array(q[i], n2);

      free_vector(q);
    }

  return;
}

void free_3d_array(void *p, int n1, int n2, int n3)
{
  void ***q = (void ***)p;
  int i;

  if (q != NULL)
    {
      for (i = 0; i < n1; i++)
        free_2d_array(q[i], n2, n3);

      free_vector(q);
    }

  return;
}

void free_4d_array(void *p, int n1, int n2, int n3, int n4)
{
  void ****q = (void ****)p;
  int i;

  if (q != NULL)
    {
      for (i = 0; i < n1; i++)
        free_3d_array(q[i], n2, n3, n4);

      free_vector(q);
    }

  return;
}

/* ********************************************************************
 * The real allocation and deallocation routines.
 * *******************************************************************/

void *alloc_vector(int nelem, size_t selem)
{
  void *p;

  if ((p = (void *)malloc(nelem * selem)) == NULL)
    {
#if (HAVE_PERROR)
      perror("malloc() failed");
#endif

      fprintf(stderr, "Unable to allocate %lu bytes\n", 
              (unsigned long)(nelem * selem));
    }

  return p;
}

void free_vector(void *p)
{
  if (p != NULL)
    free(p);

  return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -