/* * * @return 0 if success * -1 if cannot allocate this quantity of memory * -2 if the requested size is smaller than the minimal one * -3 unable to create (or resize) the stack (probably a malloc error */ static int setStacksize(unsigned long newsize) { if (newsize != getCurrentStacksize()) { if ((newsize >= MIN_STACKSIZE)) { if ((newsize <= get_max_memory_for_scilab_stack())) { unsigned long ptr = 0; C2F(scimem) (&newsize, &ptr); if (ptr) { LhsVar(1) = 0; C2F(putlhsvar) (); C2F(adjuststacksize) (&newsize, &ptr); return 0; } //sciprint(" malloc error\n"); return -3; /* We haven't been able to create (or resize) the stack (probably a malloc error */ } /* Not possible to assign that amount of memory */ //sciprint(" Not Enough Minerals !\n"); return -1; } /* Trying to create a too small stack */ //sciprint(" < MIN_STACKSIZE\n"); return -2; } /* Set the stacksize to the same size... No need to do anything */ //sciprint(" same size\n"); return 0; }
/*--------------------------------------------------------------------------*/ static int setGStacksize(unsigned long newsize) { if (newsize != getCurrentGStacksize()) { if ((newsize >= MIN_GSTACKSIZE) && (newsize <= get_max_memory_for_scilab_stack())) { unsigned long ptr = 0; int l = 0; C2F(scigmem) (&newsize, &ptr); l = C2F(vstk).lstk[C2F(vstk).gtop] - C2F(vstk).lstk[C2F(vstk).isiz + 1]; if (ptr) { LhsVar(1) = 0; C2F(putlhsvar) (); C2F(adjustgstacksize) (&newsize, &ptr, &l); return 1; } } return 0; } return 1; }
/*--------------------------------------------------------------------------*/ static int setStacksizeMax(char *fname) { /* we backup previous size */ unsigned long backupSize = getCurrentStacksize(); /* Bug 5495 on Windows 2000 -- WONT FIX GetLargestFreeMemoryRegion */ /* it works on XP, Vista, S7ven */ /* GetLargestFreeMemoryRegion() returns a superior size to real value */ unsigned long maxmemfree = (GetLargestFreeMemoryRegion()) / sizeof(double); /* We have already max */ if (maxmemfree <= backupSize) { LhsVar(1) = 0; C2F(putlhsvar) (); return 0; } /* we do a stacksize('min') */ if (setStacksizeMin(fname) == 0) { unsigned long memmaxavailablebyscilab = get_max_memory_for_scilab_stack(); unsigned long newMemSizeMax = maxmemfree; int errCode; if (memmaxavailablebyscilab < newMemSizeMax) { newMemSizeMax = memmaxavailablebyscilab; } if (newMemSizeMax < MIN_STACKSIZE) { newMemSizeMax = MIN_STACKSIZE; } errCode = setStacksize(newMemSizeMax); if (errCode != 0) { setStacksize(backupSize); Scierror(10001, _("%s: Cannot allocate memory.\n%s\n"), fname, getStackCreationErrorMessage(errCode)); } return 0; } else { /* stacksize('min') fails */ /* restore previous size */ setStacksize(backupSize); Scierror(10001, _("%s: Cannot allocate memory.\n"), fname); } return 0; }
/*--------------------------------------------------------------------------*/ static int sci_stacksizeOneRhs(char *fname) { int l1 = 0, n1 = 0, m1 = 0; int errCode = 0; if (GetType(1) == sci_matrix) { GetRhsVar(1, MATRIX_OF_DOUBLE_DATATYPE, &m1, &n1, &l1); if ((m1 == 1) && (n1 == 1)) { unsigned long NEWMEMSTACKSIZE = (unsigned long) * stk(l1); /* add 1 for alignment problems */ if (is_a_valid_size_for_scilab_stack(NEWMEMSTACKSIZE + 1)) { if ((NEWMEMSTACKSIZE >= MIN_STACKSIZE) && (NEWMEMSTACKSIZE <= get_max_memory_for_scilab_stack())) { /* we backup previous size */ unsigned long backupSize = getCurrentStacksize(); errCode = setStacksizeMin(fname); if (errCode == 0) { errCode = setStacksize(NEWMEMSTACKSIZE); if (errCode == 0) { LhsVar(1) = 0; PutLhsVar(); return 0; } else { /* restore previous size */ setStacksize(backupSize); Scierror(10001, _("%s: Cannot allocate memory.\n%s\n"), fname, getStackCreationErrorMessage(errCode)); } } else { /* restore previous size */ setStacksize(backupSize); Scierror(10001, _("%s: Cannot allocate memory.\n%s\n"), fname, getStackCreationErrorMessage(errCode)); } } else { Scierror(1504, _("%s: Out of bounds value. Not in [%lu,%lu].\n"), fname, MIN_STACKSIZE, get_max_memory_for_scilab_stack() - 1); } } else { Scierror(1504, _("%s: Out of bounds value. Not in [%lu,%lu].\n"), fname, MIN_STACKSIZE, get_max_memory_for_scilab_stack() - 1); } } else { Scierror(204, _("%s: Wrong size for input argument #%d: Scalar expected.\n"), fname, 1); } } else if (GetType(1) == sci_strings) { char *param = NULL; GetRhsVar(1, STRING_DATATYPE, &m1, &n1, &l1); param = cstk(l1); if (strcmp(PARAM_MAX_STR, param) == 0) { return sci_stacksizeMax(fname); } else if (strcmp(PARAM_MIN_STR, param) == 0) { return sci_stacksizeMin(fname); } else { Scierror(204, _("%s: Wrong type for input argument #%d: Scalar, '%s' or '%s'.\n"), fname, 1, "min", "max"); } } else { Scierror(204, _("%s: Wrong type for input argument #%d: Scalar, '%s' or '%s'.\n"), fname, 1, "min", "max"); } return 0; }