DataBlade API Programmer's Manual
Chapter 11: Developing Server Functions
Home
Contents
Index
Master Index
New Book
Stack Space Allocation
Stacks are allocated from a common region in shared memory. Like all memory that user-defined routines use, stack segments can be overrun. The database server can only check for stack violations when the user-defined routine yields. Therefore, you must ensure that you:
allocate sufficient stack space when you declare the user-defined routine.
A user-defined routine needs to have enough stack space for all its local variables. By default, the database server allocates the stack with the size that the
STACKSIZE
configuration parameter specifies (32 kilobytes, if
STACKSIZE
is not set). To change this stack size for a user-defined routine, use the
STACK
routine modifier of the
CREATE FUNCTION
or
CREATE PROCEDURE
statement when you register your user-defined routine.
use the
mi_call()
function for user-defined routines that potentially use unlimited recursion.
The DataBlade API provides the
mi_call()
function to manage stack space. This function checks the amount of unused stack space and allocates additional stack segments if necessary.
The
mi_call()
function is not available for client applications.
The following example illustrates stack space allocation with
mi_call()
:
#include <mi.h>
#include <string.h>
/* Function Declarations */
mi_integer
binsearch1(mi_string *my_word, mi_string *my_array[], mi_integer n);
mi_integer
my_test1(mi_string **my_array, mi_integer nelem);
mi_integer
micalltest(int i)
{
/* Data Declarations */
MI_CONNECTION *conn;
mi_integer ret = 0;
mi_string *my_array[] = {"Apr", "Aug", "Dec","Feb","Jan",
"Jul", "Jun", "Mar", "May","Nov", "Oct", "Sep"};
conn = mi_open( NULL, NULL, NULL );
/* Make a call to test mi_call_stack */
ret = my_test1( my_array, 12 );
mi_close( conn );
return( ret );
}
mi_integer my_test1( mi_string **my_array, mi_integer nelem )
{
mi_integer status;
mi_integer rc;
mi_string *my_word = "Dec";
status = mi_call( (mi_integer *) &rc, /* address of return value */
binsearch1,
(mi_integer) 3,
my_word,
my_array,
nelem );
switch (status)
{
case MI_TOOMANY:
return sqerr(-722);
case MI_CONTINUE:
break;
case MI_NOMEM:
return sqerr(-722);
case MI_DONE:
return rc;
}
/* Enough stack space to execute the routine */
rc = binsearch1( my_word, my_array, nelem );
return( rc );
}
mi_integer binsearch1( mi_string *my_word,
mi_string *my_array[],
mi_integer n)
{
mi_integer cond;
mi_integer low,high,mid;
low = 0;
high = n - 1;
while (low <= high)
{
mid = (low + high)/2;
if ((cond = strcmp(my_word, my_array[mid])) < 0)
high = mid - 1;
else if (cond > 0 )
low = mid + 1;
else
return mid;
}
return -1;
}
DataBlade API Programmer's Manual
, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.