提交 278b7205 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Decref and remove reference to self->base when allocating new memory

Thanks Fred for the review.
上级 1f8cb171
...@@ -396,32 +396,37 @@ int CudaNdarray_alloc_contiguous(CudaNdarray *self, const int nd, const inttype ...@@ -396,32 +396,37 @@ int CudaNdarray_alloc_contiguous(CudaNdarray *self, const int nd, const inttype
{ {
return 0; return 0;
} }
else
// The structure of self will be reused with newly allocated memory.
// If self was a view, we should remove the reference to its base.
// (If base was already NULL, the following has no effect.)
Py_XDECREF(self->base);
self->base = NULL;
// If self is a view, do not try to free its memory
if (self->data_allocated && device_free(self->devdata))
{ {
// If self is a view, do not try to free its memory self->devdata = NULL;
if (self->data_allocated && device_free(self->devdata)) self->data_allocated = 0;
{ return -1;
self->devdata = NULL; }
self->data_allocated = 0;
return -1;
}
assert(size>0); assert(size>0);
self->devdata = (float*)device_malloc(size*sizeof(real)); self->devdata = (float*)device_malloc(size*sizeof(real));
if (!self->devdata) if (!self->devdata)
{ {
CudaNdarray_set_nd(self,-1); CudaNdarray_set_nd(self,-1);
self->data_allocated = 0; self->data_allocated = 0;
self->devdata = 0; self->devdata = 0;
return -1; return -1;
}
if (0)
fprintf(stderr,
"Allocated devdata %p (self=%p)\n",
self->devdata,
self);
self->data_allocated = size;
} }
if (0)
fprintf(stderr,
"Allocated devdata %p (self=%p)\n",
self->devdata,
self);
self->data_allocated = size;
return 0; return 0;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论