This is the mail archive of the gsl-discuss@sources.redhat.com mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Proposal for non-malloc versions of block, vector, matrix functions


In our "mission critical" software, we try not to explicitly
malloc anything except during the initialization phase. In
this phase, static memory is allocated and re-used during
the most critical run-time phases.

For example, in some file: file1.c:

static int *pi2_list = NULL;

int file1_init(void){

  pi2_list = malloc(500*sizeof(int));

  return 0;
}

int file1_thread_workhorse(void){

  ...here, we use and reuse the pi2_list array...

  return 0;
}

We'd like to support the same concept with gsl data types.
Specifically, we've written "noalloc_from_mem" versions
of all the block, matrix and vector "alloc" functions.

These new functions get passed a pointer which can be used
in place of pointer that would be returned by the "malloc".
Of course, there are many ways to screw this up; that is,
it can be considered more dangerous than a malloc. 

An example where we find it useful though is in some thread
that is called 10 times a second which would need to call
gsl_matrix_alloc. Instead, we would call it once in an initialization
function, and then use the matrix_noalloc_from_mem function in the
thread workhorse and pass to it the pointer from the malloc.

We use GSL version 0.6 (or principle reason for not upgrading
to version 0.7 and beyond is the maintenace of these new
functions). Our question is: would you entertain the idea
of adding functions such as these to the GSL repository?

Thanks,
Bill Brower



---------

Here is an example of our new function for matrix "noalloc"
as would be found in "gsl_matrix_init_source.h" (again, we
are using version 0.6, it looks like this file is now
called "matrix/init_source.c"):

int 
FUNCTION (osl_matrix, noalloc_from_mem)(TYPE( gsl_matrix) * m,
                                        ATOMIC * mem, 
                                        const size_t offset,
					const size_t n1, 
					const size_t n2,
					const size_t d2)
{
/* Fill gsl_matrix so that data points to memory already available.
   Dangerous routine, but useful for real-time.
*/
/*  
  TYPE (gsl_block) * block;
  TYPE (gsl_matrix) * m;
*/
  if (n1 == 0)
    {
      GSL_ERROR_RETURN ("matrix dimension n1 must be positive integer",
			GSL_EDOM, 0);
    }
  else if (n2 == 0)
    {
      GSL_ERROR_RETURN ("matrix dimension n2 must be positive integer",
			GSL_EDOM, 0);
    }
  else if (d2 < n2)
    {
      GSL_ERROR_RETURN ("matrix dimension d2 must be >= n2",
			GSL_EDOM, 0);
    }

/*
  m = (TYPE (gsl_matrix) *) malloc (sizeof (TYPE (gsl_matrix)));
*/
  if (m == 0)
    {
      GSL_ERROR_RETURN ("invalid matrix struct (osl)",
			GSL_ENOMEM, 0);
    }

  /* FIXME: n1*n2 could overflow for large dimensions */

/*
  block = FUNCTION(gsl_block, alloc) (n1 * n2) ;

  if (block == 0)
    {
      GSL_ERROR_RETURN ("failed to allocate space for block",
			GSL_ENOMEM, 0);
    }
*/
  m->data  = mem + offset;
  m->size1 = n1;
  m->size2 = n2;
  m->tda  = d2;
  m->block = 0;

  return GSL_SUCCESS;
}

--
William Brower  MIT Lincoln Laboratory
805.355.1310    KMR Field Site, Kwajalein


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]