Exemplo n.º 1
0
/*! \relates XsMatrix \brief Init the %XsMatrix and copy the data from \a src into the matrix if \a src is not null */
void XsMatrix_construct(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride)
{
	XsSize r,c;
	XsSize size = rows*stride;

	if (stride == 0)
	{
		stride = cols;
		size = rows * stride;
	}
	if (size)
	{
		// init to size
		*((XsReal**) &thisPtr->m_data) = (XsReal*) xsMathMalloc(size*sizeof(XsReal));
		INC_ALLOC();
	}
	else
		*((XsReal**) &thisPtr->m_data) = 0;
	*((int*) &thisPtr->m_flags) = XSDF_Managed;
	*((XsSize*) &thisPtr->m_rows) = rows;
	*((XsSize*) &thisPtr->m_cols) = cols;
	*((XsSize*) &thisPtr->m_stride) = stride;

	if (src && size)
	{
		if (srcStride == 0 || srcStride == stride)
			memcpy(thisPtr->m_data, src, size*sizeof(XsReal));
		else
		{
			for (r = 0; r < rows; ++r)
				for (c = 0; c < cols; ++c)
					thisPtr->m_data[r*stride+c] = src[r*srcStride + c];
		}
	}
}
Exemplo n.º 2
0
/*! \relates XsArray
	\brief Initializes the XsArray with space for \a count items and copies them from \a src 
	\details This function initializes the object reserving \a count items in the buffer. \a count may
	be 0. If \a src is not 0, \a count items from \a src will be copied.
	\param descriptor The descriptor of the data in the list
	\param count The number of items to reserve space for. When \a src is not NULL, thisArray is also the number of items copied from \a src
	\param src A pointer to an array of objects to copy, may be NULL, ignored when \a count is 0
*/
void XsArray_construct(void* thisPtr, XsArrayDescriptor const* const descriptor, XsSize count, void const* src)
{
	XsArray* thisArray = (XsArray*) thisPtr;
	*((XsArrayDescriptor const**) &thisArray->m_descriptor) = descriptor;
	*((XsSize*) &thisArray->m_size) = count;

	if (thisArray->m_size)
	{
		// init to size
		*((void**) &thisArray->m_data) = malloc(thisArray->m_size*elemSize(thisArray));
		INC_ALLOC();

		// init the configurations
		if (src)
		{
			XsSize i;
			assert(thisArray->m_descriptor->itemCopyConstruct);
			for (i=0; i<thisArray->m_size; ++i)
				thisArray->m_descriptor->itemCopyConstruct(elemAt(thisArray->m_data, i), elemAt(src, i));
		}
		else if (thisArray->m_descriptor->itemConstruct)
		{
			XsSize i;
			for (i=0; i<thisArray->m_size; ++i)
				thisArray->m_descriptor->itemConstruct(elemAt(thisArray->m_data, i));
		}
	}
	else
		*((void**) &thisArray->m_data) = 0;
	*((XsSize*) &thisArray->m_reserved) = thisArray->m_size;
	*((int*) &thisArray->m_flags) = XSDF_Managed;
}
Exemplo n.º 3
0
/*! \relates XsVector
	\brief Initialize the XsVector using \a sz number of items from \a src
	\param sz The desired size of the vector
	\param src 0 or a pointer to a buffer containing \a sz items to copy into the %XsVector
*/
void XsVector_assign(XsVector* thisPtr, XsSize sz, const XsReal* src)
{
	if (thisPtr->m_flags == XSDF_FixedSize)
	{
		if (sz == 0)
		{
			*((int*) &thisPtr->m_flags) |= XSDF_Empty;
			return;
		}
		assert(sz == thisPtr->m_size);
		*((int*) &thisPtr->m_flags) &= ~XSDF_Empty;
	}

	if (sz > thisPtr->m_size || sz == 0)
	{
		XsVector_destruct(thisPtr);
		if (sz)
		{
			// init to size
			XsReal* data = (XsReal*) xsMathMalloc(sz*sizeof(XsReal));
			assert(data);
			*((XsReal**) &thisPtr->m_data) = data;
			*((int*) &thisPtr->m_flags) = XSDF_Managed;
			INC_ALLOC();
		}
	}
	*((XsSize*) &thisPtr->m_size) = sz;
	if (src && sz)
		memcpy(thisPtr->m_data, src, sz*sizeof(XsReal));
}
Exemplo n.º 4
0
//! \relates XsVector \brief Initialize the %XsVector using \a sz number of items from \a src
void XsVector_construct(XsVector* thisPtr, XsSize sz, const XsReal* src)
{
	if (sz)
	{
		// init to size
		*((XsReal**) &thisPtr->m_data) = (XsReal*) xsMathMalloc(sz*sizeof(XsReal));
		INC_ALLOC();
	}
	else
		*((XsReal**) &thisPtr->m_data) = 0;
	*((int*) &thisPtr->m_flags) = XSDF_Managed;
	*((XsSize*) &thisPtr->m_size) = sz;
	if (src && sz)
		memcpy(thisPtr->m_data, src, sz*sizeof(XsReal));
}
Exemplo n.º 5
0
/* Allocates CNT consecutive sectors from the free map and stores
   the first into *SECTORP.
   Returns true if successful, false if not enough consecutive
   sectors were available or if the free_map file could not be
   written. */
bool
free_map_allocate (size_t cnt, block_sector_t *sectorp)
{
  block_sector_t sector = bitmap_scan_and_flip (free_map, 0, cnt, false);
  if (sector != BITMAP_ERROR
      && free_map_file != NULL
      && !bitmap_write (free_map, free_map_file))
    {
      bitmap_set_multiple (free_map, sector, cnt, false); 
      sector = BITMAP_ERROR;
    }

  if (sector != BITMAP_ERROR)
    *sectorp = sector;
  else
    PRINT_NUM_ALLOC();

  INC_ALLOC(cnt);
  return sector != BITMAP_ERROR;
}
Exemplo n.º 6
0
/*! \relates XsArray
	\brief Reserves space for \a count items
	\details This function reserves space for exactly \a count items unless \a count is less than the 
	current list size. The function will retain the current data in the list.
	\param count The number of items to reserve space for
	\sa XsArray_assign
*/
void XsArray_reserve(void* thisPtr, XsSize count)
{
	XsArray* thisArray = (XsArray*) thisPtr;
	XsArray tmp = { 0, thisArray->m_size, 0, XSDF_Managed, thisArray->m_descriptor };
	XsSize i;

	if (count < thisArray->m_size)
		count = thisArray->m_size;

	if (count == thisArray->m_reserved)
		return;

	if (!(thisArray->m_flags & XSDF_Managed))
	{
		// attempting thisArray on an unmanaged list is ignored silently
		return;
	}

	if (!count)
	{
		XsArray_destruct(thisArray);
		return;
	}

	*((XsSize*) &tmp.m_reserved) = count;

	// init to size
	*((void**) &tmp.m_data) = malloc(tmp.m_reserved*elemSize(thisArray));
	INC_ALLOC();

	if (thisArray->m_descriptor->itemConstruct)
		for (i=0; i<tmp.m_reserved; ++i)
			thisArray->m_descriptor->itemConstruct(elemAt(tmp.m_data, i));
	
	for (i=0; i<thisArray->m_size; ++i)
		thisArray->m_descriptor->itemSwap(elemAt(thisArray->m_data, i), elemAt(tmp.m_data, i));

	XsArray_destruct(thisArray);
	XsArray_swap(thisArray, &tmp);
}
Exemplo n.º 7
0
/*! \relates XsMatrix \brief Init the %XsMatrix and copy the data from \a src into the matrix if \a src is not null */
void XsMatrix_assign(XsMatrix* thisPtr, XsSize rows, XsSize cols, XsSize stride, const XsReal* src, XsSize srcStride)
{
	XsSize r,c;
	XsSize size = rows*stride;

	if (thisPtr->m_flags & XSDF_FixedSize)
	{
		if (rows == 0 && cols == 0)
		{
			*((int*) &thisPtr->m_flags) |= XSDF_Empty;
			return;
		}

		assert(thisPtr->m_rows == rows && thisPtr->m_cols == cols);
		stride = thisPtr->m_stride;
		size = thisPtr->m_rows * stride;
		*((int*) &thisPtr->m_flags) &= ~XSDF_Empty;
	}
	else
	{
		if (thisPtr->m_rows == rows && thisPtr->m_cols == cols &&
			(stride == 0 || stride == thisPtr->m_stride))
		{
			stride = thisPtr->m_stride;
			size = rows * stride;
		}
		else
		{
			if (stride == 0)
			{
				stride = cols;
				size = rows * stride;
			}
			if (size > thisPtr->m_rows*thisPtr->m_stride || thisPtr->m_rows == 0)
			{
				XsMatrix_destruct(thisPtr);
				if (size)
				{
					// init to size
					*((XsReal**) &thisPtr->m_data) = (XsReal*) xsMathMalloc(size*sizeof(XsReal));
					*((int*) &thisPtr->m_flags) = XSDF_Managed;
					INC_ALLOC();
				}
			}
			*((XsSize*) &thisPtr->m_rows) = rows;
			*((XsSize*) &thisPtr->m_cols) = cols;
			*((XsSize*) &thisPtr->m_stride) = stride;
		}
	}
	if (src && size)
	{
		if (srcStride == 0 || srcStride == stride)
			memcpy(thisPtr->m_data, src, size*sizeof(XsReal));
		else
		{
			for (r = 0; r < rows; ++r)
				for (c = 0; c < cols; ++c)
					thisPtr->m_data[r*stride+c] = src[r*srcStride + c];
		}
	}
}