예제 #1
0
파일: pyev.c 프로젝트: Spencerx/pyev
/* allocate memory from the Python heap - this is a bit messy */
static void *
pyev_allocator(void *ptr, long size)
{
#ifdef PYMALLOC_DEBUG
    PyGILState_STATE gstate = PyGILState_Ensure();
#endif
    void *result = NULL;

    if (size > 0) {
#if SIZEOF_LONG > SIZEOF_SIZE_T
        if (size <= PY_SIZE_MAX) {
            result = PyMem_Realloc(ptr, (size_t)size);
        }
#else
        result = PyMem_Realloc(ptr, (size_t)size);
#endif
    }
    else if (!size) {
        PyMem_Free(ptr);
    }
#ifdef PYMALLOC_DEBUG
    PyGILState_Release(gstate);
#endif
    return result;
}
예제 #2
0
static int
join_check_rec_size(WriterObj *self, int rec_len)
{

    if (rec_len < 0 || rec_len > INT_MAX - MEM_INCR) {
        PyErr_NoMemory();
        return 0;
    }

    if (rec_len > self->rec_size) {
        if (self->rec_size == 0) {
            self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
            if (self->rec != NULL)
                PyMem_Free(self->rec);
            self->rec = PyMem_Malloc(self->rec_size);
        }
        else {
            char *old_rec = self->rec;

            self->rec_size = (rec_len / MEM_INCR + 1) * MEM_INCR;
            self->rec = PyMem_Realloc(self->rec, self->rec_size);
            if (self->rec == NULL)
                PyMem_Free(old_rec);
        }
        if (self->rec == NULL) {
            PyErr_NoMemory();
            return 0;
        }
    }
    return 1;
}
예제 #3
0
static PyObject*
PyLocale_strxfrm(PyObject* self, PyObject* args)
{
    char *s, *buf;
    size_t n1, n2;
    PyObject *result;

    if (!PyArg_ParseTuple(args, "s:strxfrm", &s))
        return NULL;

    /* assume no change in size, first */
    n1 = strlen(s) + 1;
    buf = PyMem_Malloc(n1);
    if (!buf)
        return PyErr_NoMemory();
    n2 = strxfrm(buf, s, n1);
    if (n2 > n1) {
        /* more space needed */
        buf = PyMem_Realloc(buf, n2);
        if (!buf)
            return PyErr_NoMemory();
        strxfrm(buf, s, n2);
    }
    result = PyString_FromString(buf);
    PyMem_Free(buf);
    return result;
}
예제 #4
0
/* Extend the partials array p[] by doubling its size. */
static int                          /* non-zero on error */
_fsum_realloc(double **p_ptr, Py_ssize_t  n,
             double  *ps,    Py_ssize_t *m_ptr)
{
	void *v = NULL;
	Py_ssize_t m = *m_ptr;

	m += m;  /* double */
	if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) {
		double *p = *p_ptr;
		if (p == ps) {
			v = PyMem_Malloc(sizeof(double) * m);
			if (v != NULL)
				memcpy(v, ps, sizeof(double) * n);
		}
		else
			v = PyMem_Realloc(p, sizeof(double) * m);
	}
	if (v == NULL) {        /* size overflow or no memory */
		PyErr_SetString(PyExc_MemoryError, "math.fsum partials");
		return 1;
	}
	*p_ptr = (double*) v;
	*m_ptr = m;
	return 0;
}
예제 #5
0
파일: greenlet.c 프로젝트: vietlq/greenlet
static int g_save(PyGreenlet* g, char* stop)
{
	/* Save more of g's stack into the heap -- at least up to 'stop'

	   g->stack_stop |________|
	                 |        |
			 |    __ stop       . . . . .
	                 |        |    ==>  .       .
			 |________|          _______
			 |        |         |       |
			 |        |         |       |
	  g->stack_start |        |         |_______| g->stack_copy

	 */
	intptr_t sz1 = g->stack_saved;
	intptr_t sz2 = stop - g->stack_start;
	assert(g->stack_start != NULL);
	if (sz2 > sz1) {
		char* c = (char*)PyMem_Realloc(g->stack_copy, sz2);
		if (!c) {
			PyErr_NoMemory();
			return -1;
		}
		memcpy(c+sz1, g->stack_start+sz1, sz2-sz1);
		g->stack_copy = c;
		g->stack_saved = sz2;
	}
	return 0;
}
예제 #6
0
// Reallocate space. Returns:
//      NULL    ... reallocation failed
//   pointer ... address of reallocated space
// Arguments:
//   debugMode   ... debug messages flag
//   ptr         ... pointer to already allocated space
//   size        ... new size in bytes
void *reallocate(int debugMode, void *ptr, int size)
{
    // Allocate space for raw data.
    void *tmp = PyMem_Realloc(ptr, size);
    if(tmp == NULL && debugMode)
        fprintf(debugFile, "HSpiceRead: cannot allocate.\n");
    return tmp;
}
예제 #7
0
int
DataStack_grow(Stats *pstats, DataStack *pdata_stack)
{
    pdata_stack->depth++;
    if (pdata_stack->depth >= pdata_stack->alloc) {
        /* We've outgrown our data_stack array: make it bigger. */
        int bigger = pdata_stack->alloc + STACK_DELTA;
        DataStackEntry * bigger_data_stack = PyMem_Realloc(pdata_stack->stack, bigger * sizeof(DataStackEntry));
        STATS( pstats->stack_reallocs++; )
        if (bigger_data_stack == NULL) {
예제 #8
0
파일: stack.c 프로젝트: 7kbird/chrome
extern void
stack_push(node_stack_t *stack, node_t *node)
{
	stack->stack[stack->stackptr++] = node;
	if (stack->stackptr >= stack->size) {
		stack->size *= 2;
		stack->stack = PyMem_Realloc(stack->stack,
				sizeof(node_t *) * stack->size);
	}
}
예제 #9
0
static void *py_lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
    (void)ud;
    (void)osize;
    if (nsize == 0) {
        PyMem_Free(ptr);
        return NULL;
    } else {
        return PyMem_Realloc(ptr, nsize);
    }
}
예제 #10
0
static void
set_zcache(void)
{
    TRACE("Entering set_zcache\n");
    if (in_zcache > global.cache_size) {
        int i;
        for(i = global.cache_size; i < in_zcache; ++i)
            mpz_clear(zcache[i]);
        in_zcache = global.cache_size;
    }
    zcache = PyMem_Realloc(zcache, sizeof(mpz_t) * global.cache_size);
}
예제 #11
0
파일: atexitmodule.c 프로젝트: 1st1/cpython
static PyObject *
atexit_register(PyObject *self, PyObject *args, PyObject *kwargs)
{
    atexitmodule_state *modstate;
    atexit_callback *new_callback;
    PyObject *func = NULL;

    modstate = GET_ATEXIT_STATE(self);

    if (modstate->ncallbacks >= modstate->callback_len) {
        atexit_callback **r;
        modstate->callback_len += 16;
        r = (atexit_callback**)PyMem_Realloc(modstate->atexit_callbacks,
                                      sizeof(atexit_callback*) * modstate->callback_len);
        if (r == NULL)
            return PyErr_NoMemory();
        modstate->atexit_callbacks = r;
    }

    if (PyTuple_GET_SIZE(args) == 0) {
        PyErr_SetString(PyExc_TypeError,
                "register() takes at least 1 argument (0 given)");
        return NULL;
    }

    func = PyTuple_GET_ITEM(args, 0);
    if (!PyCallable_Check(func)) {
        PyErr_SetString(PyExc_TypeError,
                "the first argument must be callable");
        return NULL;
    }

    new_callback = PyMem_Malloc(sizeof(atexit_callback));
    if (new_callback == NULL)
        return PyErr_NoMemory();

    new_callback->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
    if (new_callback->args == NULL) {
        PyMem_Free(new_callback);
        return NULL;
    }
    new_callback->func = func;
    new_callback->kwargs = kwargs;
    Py_INCREF(func);
    Py_XINCREF(kwargs);

    modstate->atexit_callbacks[modstate->ncallbacks++] = new_callback;

    Py_INCREF(func);
    return func;
}
예제 #12
0
static void
set_pympzcache(void)
{
    TRACE("Entering set_pympzcache\n");
    if (in_pympzcache > global.cache_size) {
        int i;
        for (i = global.cache_size; i < in_pympzcache; ++i) {
            mpz_cloc(pympzcache[i]->z);
            PyObject_Del(pympzcache[i]);
        }
        in_pympzcache = global.cache_size;
    }
    pympzcache = PyMem_Realloc(pympzcache, sizeof(PympzObject)*global.cache_size);
}
예제 #13
0
void *
psi_realloc(void *ptr, size_t size)
{
    void *value;

#ifdef PYMALLOC
    value = PyMem_Realloc(ptr, size);
#else
    value = realloc(ptr, size);
#endif
    if (value == NULL)
        PyErr_NoMemory();
    return value;
}
예제 #14
0
static int
to_points_and_codes_expand_buffer(
    DecomposeToPointsAndCodesData *data, size_t len)
{
    size_t new_len = len + data->cursor;

    if (new_len > data->buffer_size) {
        data->buffer_size = (((new_len / BUFFER_CHUNK_SIZE) + 1) *
                             BUFFER_CHUNK_SIZE);
        data->points = PyMem_Realloc(
            data->points, data->buffer_size * sizeof(double) * 2);
        if (data->points == NULL) {
            return -1;
        }
        data->codes = PyMem_Realloc(
            data->codes, data->buffer_size);
        if (data->codes == NULL) {
            return -1;
        }
    }

    return 0;
}
예제 #15
0
static PyObject*
PyLocale_strxfrm(PyObject* self, PyObject* args)
{
    Py_UNICODE *s0;
    Py_ssize_t n0;
    wchar_t *s, *buf = NULL;
    size_t n1, n2;
    PyObject *result = NULL;
#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
    Py_ssize_t i;
#endif

    if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0))
        return NULL;

#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
    s = (wchar_t *) s0;
#else
    s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
    if (!s)
        return PyErr_NoMemory();
    for (i=0; i<=n0; i++)
        s[i] = s0[i];
#endif

    /* assume no change in size, first */
    n1 = wcslen(s) + 1;
    buf = PyMem_Malloc(n1*sizeof(wchar_t));
    if (!buf) {
        PyErr_NoMemory();
        goto exit;
    }
    n2 = wcsxfrm(buf, s, n1);
    if (n2 >= n1) {
        /* more space needed */
        buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
        if (!buf) {
            PyErr_NoMemory();
            goto exit;
        }
        n2 = wcsxfrm(buf, s, n2+1);
    }
    result = PyUnicode_FromWideChar(buf, n2);
 exit:
    if (buf) PyMem_Free(buf);
#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
    PyMem_Free(s);
#endif
    return result;
}
예제 #16
0
static int
to_string_expand_buffer(
    DecomposeToStringData *data, size_t len)
{
    size_t new_len = len + 2 + data->cursor;

    if (new_len > data->buffer_size) {
        data->buffer_size = (((new_len / BUFFER_CHUNK_SIZE) + 1) *
                             BUFFER_CHUNK_SIZE);
        data->buffer = PyMem_Realloc(data->buffer, data->buffer_size);
        if (data->buffer == NULL) {
            return -1;
        }
    }

    return 0;
}
/* Performs calculation of auxilliary variables after integration, event finding
   are complete. This is the only place where memory is allocated outside of
   an "Init" function. */
void AuxVarCalc( IData *GS ) {
  int i;
  
  assert(GS);

  if( (GS->nAuxVars > 0) && (GS->pointsIdx > 0 ) ) {
    
    /* Allocate space for aux variable calculations if this is the first
       time we have done aux calculations */
    if( GS->gAuxPoints == NULL ) {
      GS->gAuxPoints = (double **)PyMem_Malloc(GS->pointsIdx*sizeof(double *));
      assert(GS->gAuxPoints);
      
      for( i = 0; i < GS->pointsIdx; i++ ) {
	GS->gAuxPoints[i] = (double *)PyMem_Malloc(GS->nAuxVars*sizeof(double));
	assert(GS->gAuxPoints[i]);
      }
    }
    /* Otherwise, realloc space for the additional pointers, PyMem_Malloc
       space for the new aux points */
    else {
      double **temp = NULL;
      temp = (double **)PyMem_Realloc(GS->gAuxPoints,GS->pointsIdx*sizeof(double *));
      assert(temp);
      GS->gAuxPoints = temp;
      for( i = GS->auxIdx; i < GS->pointsIdx; i++ ) {
	GS->gAuxPoints[i] = (double *)PyMem_Malloc(GS->nAuxVars*sizeof(double));
	assert(GS->gAuxPoints[i]);
      }
    }
    
   /* Perform calculation on each integrated point */
    for( i = GS->auxIdx; i < GS->pointsIdx; i++ ) {
      if( GS->nExtInputs > 0 ) {
	FillCurrentExtInputValues(GS, GS->gTimeV[i]);
      }
      auxvars((unsigned) GS->phaseDim, (unsigned) GS->paramDim, GS->gTimeV[i], 
	      GS->gPoints[i], GS->gParams, GS->gAuxPoints[i], 
	      (unsigned) GS->extraSpaceSize, GS->gExtraSpace, 
	      (unsigned) GS->nExtInputs, GS->gCurrentExtInputVals);
    }
    
    GS->auxIdx = GS->pointsIdx;    
  }
}
예제 #18
0
static int
parse_grow_buff(ReaderObj *self)
{
	if (self->field_size == 0) {
		self->field_size = 4096;
		if (self->field != NULL)
			PyMem_Free(self->field);
		self->field = PyMem_Malloc(self->field_size);
	}
	else {
		self->field_size *= 2;
		self->field = PyMem_Realloc(self->field, self->field_size);
	}
	if (self->field == NULL) {
		PyErr_NoMemory();
		return 0;
	}
	return 1;
}
예제 #19
0
static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) {
    int pos, i;
    __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;
    if (unlikely(!code_line)) {
        return;
    }
    if (unlikely(!entries)) {
        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry));
        if (likely(entries)) {
            __pyx_code_cache.entries = entries;
            __pyx_code_cache.max_count = 64;
            __pyx_code_cache.count = 1;
            entries[0].code_line = code_line;
            entries[0].code_object = code_object;
            Py_INCREF(code_object);
        }
        return;
    }
    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);
    if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) {
        PyCodeObject* tmp = entries[pos].code_object;
        entries[pos].code_object = code_object;
        Py_DECREF(tmp);
        return;
    }
    if (__pyx_code_cache.count == __pyx_code_cache.max_count) {
        int new_max = __pyx_code_cache.max_count + 64;
        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc(
            __pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry));
        if (unlikely(!entries)) {
            return;
        }
        __pyx_code_cache.entries = entries;
        __pyx_code_cache.max_count = new_max;
    }
    for (i=__pyx_code_cache.count; i>pos; i--) {
        entries[i] = entries[i-1];
    }
    entries[pos].code_line = code_line;
    entries[pos].code_object = code_object;
    __pyx_code_cache.count++;
    Py_INCREF(code_object);
}
예제 #20
0
static PyObject*
PyLocale_strxfrm(PyObject* self, PyObject* args)
{
    PyObject *str;
    Py_ssize_t n1;
    wchar_t *s = NULL, *buf = NULL;
    size_t n2;
    PyObject *result = NULL;

    if (!PyArg_ParseTuple(args, "U:strxfrm", &str))
        return NULL;

    s = PyUnicode_AsWideCharString(str, &n1);
    if (s == NULL)
        goto exit;

    /* assume no change in size, first */
    n1 = n1 + 1;
    buf = PyMem_New(wchar_t, n1);
    if (!buf) {
        PyErr_NoMemory();
        goto exit;
    }
    n2 = wcsxfrm(buf, s, n1);
    if (n2 >= (size_t)n1) {
        /* more space needed */
        wchar_t * new_buf = PyMem_Realloc(buf, (n2+1)*sizeof(wchar_t));
        if (!new_buf) {
            PyErr_NoMemory();
            goto exit;
        }
        buf = new_buf;
        n2 = wcsxfrm(buf, s, n2+1);
    }
    result = PyUnicode_FromWideChar(buf, n2);
exit:
    if (buf)
        PyMem_Free(buf);
    if (s)
        PyMem_Free(s);
    return result;
}
예제 #21
0
int
_PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
{
    PyInterpreterState *interp = PyThreadState_Get()->interp;

    if (!PyCode_Check(code) || index < 0 ||
            index >= interp->co_extra_user_count) {
        PyErr_BadInternalCall();
        return -1;
    }

    PyCodeObject *o = (PyCodeObject*) code;
    _PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;

    if (co_extra == NULL || co_extra->ce_size <= index) {
        Py_ssize_t i = (co_extra == NULL ? 0 : co_extra->ce_size);
        co_extra = PyMem_Realloc(
                co_extra,
                sizeof(_PyCodeObjectExtra) +
                (interp->co_extra_user_count-1) * sizeof(void*));
        if (co_extra == NULL) {
            return -1;
        }
        for (; i < interp->co_extra_user_count; i++) {
            co_extra->ce_extras[i] = NULL;
        }
        co_extra->ce_size = interp->co_extra_user_count;
        o->co_extra = co_extra;
    }

    if (co_extra->ce_extras[index] != NULL) {
        freefunc free = interp->co_extra_freefuncs[index];
        if (free != NULL) {
            free(co_extra->ce_extras[index]);
        }
    }

    co_extra->ce_extras[index] = extra;
    return 0;
}
예제 #22
0
파일: buffer.c 프로젝트: garsue/pydrizzle
buffer_result
write2buf(buffer_t *buf, const char *c, size_t l) {

    size_t newl;
    char *newbuf;
    buffer_result ret = WRITE_OK;
    newl = buf->len + l;

    if (l == 0) {
        return ret;
    }

    if (newl >= buf->buf_size) {
        buf->buf_size *= 2;
        if(buf->buf_size <= newl) {
            buf->buf_size = (int)(newl + 1);
        }
        if(buf->buf_size > buf->limit){
            buf->buf_size = buf->limit + 1;
        }
        newbuf = (char*)PyMem_Realloc(buf->buf, buf->buf_size);
        if (!newbuf) {
            PyErr_SetString(PyExc_MemoryError,"out of memory");
            PyMem_Free(buf->buf);
            buf->buf = 0;
            buf->buf_size = buf->len = 0;
            return MEMORY_ERROR;
        }
        buf->buf = newbuf;
    }
    if(newl >= buf->buf_size){
        l = buf->buf_size - buf->len -1;
        ret = LIMIT_OVER;
    }
    memcpy(buf->buf + buf->len, c , l);
    buf->len += (int)l;
    return ret;
}
예제 #23
0
/* return new pos */
static unsigned char *buf_enlarge(struct Buf *buf, unsigned need_room)
{
	unsigned alloc = buf->alloc;
	unsigned need_size = buf->pos + need_room;
	unsigned char *ptr;

	/* no alloc needed */
	if (need_size < alloc)
		return buf->ptr + buf->pos;

	if (alloc <= need_size / 2)
		alloc = need_size;
	else
		alloc = alloc * 2;

	ptr = PyMem_Realloc(buf->ptr, alloc);
	if (!ptr)
		return NULL;

	buf->ptr = ptr;
	buf->alloc = alloc;
	return buf->ptr + buf->pos;
}
예제 #24
0
// construct and fill a CPT
CPT*
_buildcpt(PyArrayObject *obs, PyListObject *arities, int num_parents) {
    register int i,j;
    register int qi,ri;
    register int nx,ny;
    CPT *cpt;

    // child arity
    ri = PyInt_AsSsize_t(PyList_GET_ITEM(arities, 0));

    // parent configurations
    qi = 1;
    for (i=0; i < num_parents; i++) {
        qi *= PyInt_AsSsize_t(PyList_GET_ITEM(arities, i+1));
    }

    int len_offsets = (num_parents==0)?1:num_parents;

    // use existing cpt?
    if (_oldcpt != NULL) {
        cpt = _oldcpt;
        _oldcpt = NULL;

        // reallocate mem for new offsets and counts
        cpt->offsets = PyMem_Realloc(cpt->offsets, sizeof(int) * len_offsets);
        
        // qi > max_qi, so allocate more arrays
        if (qi > cpt->max_qi) {
            cpt->counts = PyMem_Realloc(cpt->counts, sizeof(int *) * qi);
            for (i=cpt->max_qi; i < qi; i++) {
                cpt->counts[i] = PyMem_Malloc(sizeof(int) * (ri+1));
            }
            cpt->max_qi = qi;
        }
        
        // reallocate and initialize arrays we need
        for (i=0; i < qi; i++) {
            cpt->counts[i] = PyMem_Realloc(cpt->counts[i], sizeof(int) * (ri+1));
            for (j=0; j < ri+1; j++) 
                cpt->counts[i][j] = 0;
        }
    } else {
        // create new cpt
        cpt = (CPT*) PyMem_Malloc(sizeof(CPT));
        cpt->max_qi = qi;
        cpt->offsets = PyMem_Malloc(sizeof(int) * len_offsets);
        cpt->counts = PyMem_Malloc(sizeof(int *) * qi);
        
        for (i=0; i<qi; i++) {
            cpt->counts[i] = PyMem_Malloc(sizeof(int) * (ri+1));
            for (j=0; j < ri+1; j++)
                cpt->counts[i][j] = 0;
        }
    }
  
    // update cpt
    cpt->ri = ri;
    cpt->qi = qi;
    cpt->num_parents = num_parents;

    // create offsets
    cpt->offsets[0] = 1;
    for (i=1; i<num_parents; i++)
        cpt->offsets[i] = cpt->offsets[i-1]*PyInt_AsSsize_t(PyList_GET_ITEM(arities, i));
    
    // adding to nij and nijk
    nx = PyArray_DIM(obs, 0);
    ny = PyArray_DIM(obs, 1);

    for (i=0; i<nx; i++) {
        j = cptindex(obs, i, cpt->offsets, num_parents);
        cpt->counts[j][0]++;
        cpt->counts[j][*(int*)PyArray_GETPTR2(obs,i,0) + 1]++;
    }

    // return the cpt
    return cpt;
}
예제 #25
0
void *mem_realloc_mem(void *pp, size_t size, int lineNo, char *funName,
                      char *fileName, char *dirName)
{
  char *p = (char *) pp;
  size_t hsize = sizeof(AllocSpaceAlign);
  size_t tsize, aux;
  float64 *endptr;
  AllocSpace *head;
  char *phead;

  if (p == 0) return(0);

  if (size == 0) {
    errput("%s, %s, %s, %d: zero allocation!\n",
           dirName, fileName, funName, lineNo);
    ERR_GotoEnd(1);
  }

  // 1. almost as mem_free_mem().
  mem_check_ptr(p, lineNo, funName, fileName, dirName);
  if (ERR_Chk) {
    ERR_GotoEnd(1);
  }

  phead = p - hsize;
  head = (AllocSpace *) phead;
  head->cookie = AL_AlreadyFreed;

  endptr = (float64 *) (p + head->size);
  endptr[0] = (float64) AL_AlreadyFreed;

  al_curUsage -= head->size;
  al_frags--;
  mem_list_remove(head, al_head);

  // 2. realloc.
  aux = size % sizeof(float64);
  size += (aux) ? sizeof(float64) - aux : 0;
  tsize = size + hsize + sizeof(float64);
  if ((p = (char *) PyMem_Realloc(phead, tsize)) == 0) {
    errput("%s, %s, %s, %d: error re-allocating to %zu bytes (current: %zu).\n",
           dirName, fileName, funName, lineNo, size, al_curUsage);
    ERR_GotoEnd(1);
  }

  // 3. almost as mem_alloc_mem().
  p += hsize;
  mem_list_new(p, size, al_head, lineNo, funName, fileName, dirName);

  al_curUsage += size;
  if (al_curUsage > al_maxUsage) {
    al_maxUsage = al_curUsage;
  }
  al_frags++;

  return((void *) p);

 end_label:
  if (ERR_Chk) {
    errput(ErrHead "error exit!\n");
  }

  return(0);
}
예제 #26
0
int SetRunParams( IData *GS, double *Pars, double *ICs, double **Bds, double *y0, double gTime, 
		  double *GlobalTime0, double tstart, double tend,
		  int refine, int nSpecTimes, double *specTimes, double *upperBounds, double *lowerBounds) {

  int i;

  assert( GS );

  /* We only check initBasic because we don't rely on 
     any other mallocs having been performed.  */
  assert( GS->isInitBasic == 1 );

  /* Copy in parameters, ICs, etc. for this run */
  /* Parameters were malloc'd in InitBasic */
  if( GS->paramDim > 0 ) {
    assert(GS->gParams);
    for( i = 0; i < GS->paramDim; i++ ) {
      GS->gParams[i] = Pars[i];
    }
  }
  
  /* We expect IC to be global ICs, initialized ahead of time (initialize basic) */
  /* Save initial conditions */
  for( i = 0; i < GS->phaseDim; i++ ) {
    ICs[i] = y0[i];
  }
  
  /* We expect Bds to be global Bounds, initialized ahead of time (initialize basic) */
  /* Save the current upper and lower bounds on parameters. */
  for( i = 0; i < GS->phaseDim + GS->paramDim; i++ ) {
    Bds[0][i] = lowerBounds[i];
    Bds[1][i] = upperBounds[i];
  }

  /* Set the global time (also a global variable) */
  *GlobalTime0 = gTime;

  /* Make sure that all of our global variables are set to initial values at beginning of
     call. NB: This was the source of a bug that popped up when the program was run
     twice in succession. */
  GS->tStart = tstart;
  GS->tEnd = tend;

  GS->lastTime = tstart; 

  /* Set the direction of integration */
  GS->direction = (GS->tStart < GS->tEnd ) ? 1 : -1; 

  /* Calling SetRunParams forces a new run -- this means we must reset the indices */
  GS->hasRun = 0;
  GS->timeIdx = 0; GS->pointsIdx = 0; GS->auxIdx = 0;  
  GS->eventFound = 0;

  nSpecTimes = ( nSpecTimes < 0 ) ? 0 : nSpecTimes;
  if( nSpecTimes > 0 ) {
    if( GS->specTimesLen > 0 ) {
      double *temp = NULL;
      assert(GS->gSpecTimes);
      temp = (double *)PyMem_Realloc(GS->gSpecTimes, nSpecTimes*sizeof(double));
      assert(temp);
    }
    else {
      if( GS->gSpecTimes != NULL ) {
	PyMem_Free( GS->gSpecTimes );
	GS->gSpecTimes = NULL;
      }
      GS->gSpecTimes = (double *)PyMem_Malloc(nSpecTimes*sizeof(double));
      assert(GS->gSpecTimes);
    }
    GS->specTimesLen = nSpecTimes;
    for( i = 0; i < GS->specTimesLen; i++ ) {
      GS->gSpecTimes[i] = specTimes[i];
    }
  }
  else if ( GS->specTimesLen > 0 ) {
    if( GS->gSpecTimes != NULL ) {
      PyMem_Free(GS->gSpecTimes);
      GS->gSpecTimes = NULL;
    }
    GS->specTimesLen = nSpecTimes;
  }


  /* If refinement is on, allocate space for buffering refinement 
     points and times */
  refine = ( refine < 0 ) ? 0 : refine;
  GS->refineBufIdx = 0;
  if( refine > 0 ) {
    if( refine > GS->refine ) {
      if( GS->gRefineTimes != NULL ) {
	for( i = 0; i < GS->refine; i++ ) {
	  if( GS->gRefinePoints[i] != NULL ) {
	    PyMem_Free( GS->gRefinePoints[i] );
	    GS->gRefinePoints[i] = NULL;
	  }
	}
	PyMem_Free( GS->gRefinePoints );
	GS->gRefinePoints = NULL;
      }

      GS->refine = refine;
      
      if( GS->gRefineTimes == NULL ) {
	GS->gRefinePoints = (double **)PyMem_Malloc(GS->refine*sizeof(double *));
	assert(GS->gRefinePoints);
	for( i = 0; i < GS->refine; i++ ) {
	  GS->gRefinePoints[i] = (double *)PyMem_Malloc(GS->phaseDim*sizeof(double));
	  assert(GS->gRefinePoints[i]);
	}
	GS->gRefineTimes = (double *)PyMem_Malloc(GS->refine*sizeof(double));
	assert(GS->gRefineTimes);
      }
    }
    /* Note in this case we are not reallocing the refine times. 
       Since this is a 1-D double array, free will take care of it easily. */
    else if (refine < GS->refine) {
      for( i = refine; i < GS->refine; i++ ) {
	if( GS->gRefinePoints[i] != NULL ) {
	  PyMem_Free(GS->gRefinePoints[i]);
	  GS->gRefinePoints[i] = NULL;
	}
      }
      GS->refine = refine;
    }
  }
  else {
    if ( refine < GS->refine ) {
      for( i = 0; i < GS->refine; i++ ) {                                                      
        if( GS->gRefinePoints[i] != NULL ) {                                                       
	  PyMem_Free(GS->gRefinePoints[i]);                                                        
          GS->gRefinePoints[i] = NULL;                                                             
        }                                                                                          
      }                                                                                            
      PyMem_Free(GS->gRefinePoints);
      GS->gRefinePoints = NULL;
    }
  }
      
  GS->isInitRunParams = 1;

  return SUCCESS;
}