length;
MagickBooleanType
- mapped;
+ mapped,
+ synchronize;
char
path[MaxTextExtent];
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
-% AcquireMatrixInfo() allocates the MatrixInfo structure.
+% AcquireMatrixInfo() allocates the ImageInfo structure.
%
% The format of the AcquireMatrixInfo method is:
%
% o exception: return any errors or warnings in this structure.
%
*/
+
+static inline MagickSizeType MagickMin(const MagickSizeType x,
+ const MagickSizeType y)
+{
+ if (x < y)
+ return(x);
+ return(y);
+}
+
+#if defined(SIGBUS)
+static void MatrixSignalHandler(int status)
+{
+ ThrowFatalException(CacheFatalError,"UnableToExtendMatrixCache");
+}
+#endif
+
+static inline MagickOffsetType WriteMatrixElements(
+ const MatrixInfo *restrict matrix_info,const MagickOffsetType offset,
+ const MagickSizeType length,const unsigned char *restrict buffer)
+{
+ register MagickOffsetType
+ i;
+
+ ssize_t
+ count;
+
+#if !defined(MAGICKCORE_HAVE_PWRITE)
+ if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
+ return((MagickOffsetType) -1);
+#endif
+ count=0;
+ for (i=0; i < (MagickOffsetType) length; i+=count)
+ {
+#if !defined(MAGICKCORE_HAVE_PWRITE)
+ count=write(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
+ (MagickSizeType) SSIZE_MAX));
+#else
+ count=pwrite(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
+ (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
+#endif
+ if (count <= 0)
+ {
+ count=0;
+ if (errno != EINTR)
+ break;
+ }
+ }
+ return(i);
+}
+
+static MagickBooleanType SetMatrixExtent(MatrixInfo *restrict matrix_info,
+ MagickSizeType length)
+{
+ MagickOffsetType
+ count,
+ extent,
+ offset;
+
+ if (length != (MagickSizeType) ((MagickOffsetType) length))
+ return(MagickFalse);
+ offset=(MagickOffsetType) lseek(matrix_info->file,0,SEEK_END);
+ if (offset < 0)
+ return(MagickFalse);
+ if ((MagickSizeType) offset >= length)
+ return(MagickTrue);
+ extent=(MagickOffsetType) length-1;
+ count=WriteMatrixElements(matrix_info,extent,1,(const unsigned char *) "");
+#if defined(MAGICKCORE_HAVE_POSIX_FALLOCATE)
+ if (matrix_info->synchronize != MagickFalse)
+ {
+ int
+ status;
+
+ status=posix_fallocate(matrix_info->file,offset+1,extent-offset);
+ if (status != 0)
+ return(MagickFalse);
+ }
+#endif
+#if defined(SIGBUS)
+ (void) signal(SIGBUS,MatrixSignalHandler);
+#endif
+ return(count != (MagickOffsetType) 1 ? MagickFalse : MagickTrue);
+}
+
MagickExport MatrixInfo *AcquireMatrixInfo(const size_t columns,
const size_t rows,const size_t stride,ExceptionInfo *exception)
{
+ char
+ *synchronize;
+
MagickBooleanType
status;
matrix_info->columns=columns;
matrix_info->rows=rows;
matrix_info->stride=stride;
+ synchronize=GetEnvironmentValue("MAGICK_SYNCHRONIZE");
+ if (synchronize != (const char *) NULL)
+ {
+ matrix_info->synchronize=IsStringTrue(synchronize);
+ synchronize=DestroyString(synchronize);
+ }
matrix_info->length=(MagickSizeType) columns*rows*stride;
if (matrix_info->columns != (size_t) (matrix_info->length/rows/stride))
{
status=AcquireMagickResource(MemoryResource,matrix_info->length);
if (status != MagickFalse)
{
+ matrix_info->mapped=MagickFalse;
matrix_info->elements=AcquireMagickMemory((size_t)
matrix_info->length);
if (matrix_info->elements == NULL)
status=AcquireMagickResource(MapResource,matrix_info->length);
if (status != MagickFalse)
{
- status=ResetMatrixInfo(matrix_info);
+ status=SetMatrixExtent(matrix_info,matrix_info->length);
if (status != MagickFalse)
{
matrix_info->elements=(void *) MapBlob(matrix_info->file,IOMode,0,
%
*/
-static inline size_t MagickMin(const size_t x,const size_t y)
-{
- if (x < y)
- return(x);
- return(y);
-}
-
-static inline ssize_t ReadMatrixInfo(const MatrixInfo *matrix_info,
- const MagickOffsetType offset,const size_t length,unsigned char *buffer)
+static inline MagickOffsetType ReadMatrixElements(
+ const MatrixInfo *restrict matrix_info,const MagickOffsetType offset,
+ const MagickSizeType length,unsigned char *restrict buffer)
{
- register ssize_t
+ register MagickOffsetType
i;
ssize_t
count;
-#if !defined(MAGICKCORE_HAVE_PPREAD)
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp critical (MagickCore_ReadMatrixInfo)
+#if !defined(MAGICKCORE_HAVE_PREAD)
+ if (lseek(matrix_info->file,offset,SEEK_SET) < 0)
+ return((MagickOffsetType) -1);
#endif
+ count=0;
+ for (i=0; i < (MagickOffsetType) length; i+=count)
{
- i=(-1);
- if (lseek(matrix_info->file,offset,SEEK_SET) >= 0)
- {
-#endif
- count=0;
- for (i=0; i < (ssize_t) length; i+=count)
- {
-#if !defined(MAGICKCORE_HAVE_PPREAD)
- count=read(matrix_info->file,buffer+i,MagickMin(length-i,(size_t)
- SSIZE_MAX));
+#if !defined(MAGICKCORE_HAVE_PREAD)
+ count=read(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
+ (MagickSizeType) SSIZE_MAX));
#else
- count=pread(matrix_info->file,buffer+i,MagickMin(length-i,(size_t)
- SSIZE_MAX),offset+i);
+ count=pread(matrix_info->file,buffer+i,(size_t) MagickMin(length-i,
+ (MagickSizeType) SSIZE_MAX),(off_t) (offset+i));
#endif
- if (count > 0)
- continue;
- count=0;
- if (errno != EINTR)
- {
- i=(-1);
- break;
- }
- }
-#if !defined(MAGICKCORE_HAVE_PPREAD)
+ if (count <= 0)
+ {
+ count=0;
+ if (errno != EINTR)
+ break;
}
}
-#endif
return(i);
}
const ssize_t x,const ssize_t y,void *value)
{
MagickOffsetType
+ count,
i;
- ssize_t
- count;
-
assert(matrix_info != (const MatrixInfo *) NULL);
assert(matrix_info->signature == MagickSignature);
i=(MagickOffsetType) matrix_info->rows*x+y;
matrix_info->stride);
return(MagickTrue);
}
- count=ReadMatrixInfo(matrix_info,i*matrix_info->stride,matrix_info->stride,
- value);
- if (count != matrix_info->stride)
+ count=ReadMatrixElements(matrix_info,i*matrix_info->stride,
+ matrix_info->stride,value);
+ if (count != (MagickOffsetType) matrix_info->stride)
return(MagickFalse);
return(MagickTrue);
}
% %
% %
% %
-% R e l i n q u i s h M a g i c k M a t r i x %
+% N u l l M a t r i x %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
-% RelinquishMagickMatrix() frees the previously acquired matrix (array of
-% pointers to arrays of doubles).
-%
-% The format of the RelinquishMagickMatrix method is:
-%
-% double **RelinquishMagickMatrix(double **matrix,
-% const size_t number_rows)
-%
-% A description of each parameter follows:
-%
-% o matrix: the matrix to relinquish
-%
-% o number_rows: the first dimension of the acquired matrix (number of
-% pointers)
-%
-*/
-MagickExport double **RelinquishMagickMatrix(double **matrix,
- const size_t number_rows)
-{
- register ssize_t
- i;
-
- if (matrix == (double **) NULL )
- return(matrix);
- for (i=0; i < (ssize_t) number_rows; i++)
- matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
- matrix=(double **) RelinquishMagickMemory(matrix);
- return(matrix);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% %
-% %
-% %
-% R e s e t M a g i c k M e m o r y %
-% %
-% %
-% %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% ResetMagickMemory() sets all elements of the matrix to zero.
+% NullMatrix() sets all elements of the matrix to zero.
%
% The format of the ResetMagickMemory method is:
%
-% MagickBoolenType *ResetMatrixInfo(MatrixInfo *matrix_info)
+% MagickBooleanType *NullMatrix(MatrixInfo *matrix_info)
%
% A description of each parameter follows:
%
% o matrix_info: the matrix.
%
*/
-MagickExport MagickBooleanType ResetMatrixInfo(MatrixInfo *matrix_info)
+MagickExport MagickBooleanType NullMatrix(MatrixInfo *matrix_info)
{
register ssize_t
x;
% %
% %
% %
+% R e l i n q u i s h M a g i c k M a t r i x %
+% %
+% %
+% %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% RelinquishMagickMatrix() frees the previously acquired matrix (array of
+% pointers to arrays of doubles).
+%
+% The format of the RelinquishMagickMatrix method is:
+%
+% double **RelinquishMagickMatrix(double **matrix,
+% const size_t number_rows)
+%
+% A description of each parameter follows:
+%
+% o matrix: the matrix to relinquish
+%
+% o number_rows: the first dimension of the acquired matrix (number of
+% pointers)
+%
+*/
+MagickExport double **RelinquishMagickMatrix(double **matrix,
+ const size_t number_rows)
+{
+ register ssize_t
+ i;
+
+ if (matrix == (double **) NULL )
+ return(matrix);
+ for (i=0; i < (ssize_t) number_rows; i++)
+ matrix[i]=(double *) RelinquishMagickMemory(matrix[i]);
+ matrix=(double **) RelinquishMagickMemory(matrix);
+ return(matrix);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% %
+% %
+% %
% S e t M a t r i x E l e m e n t %
% %
% %
%
*/
-static inline ssize_t WriteMatrixInfo(const MatrixInfo *matrix_info,
- const MagickOffsetType offset,const size_t length,const unsigned char *buffer)
-{
- register ssize_t
- i;
-
- ssize_t
- count;
-
- i=0;
-#if !defined(MAGICKCORE_HAVE_PWRITE)
-#if defined(MAGICKCORE_OPENMP_SUPPORT)
- #pragma omp critical (MagickCore_WriteMatrixInfo)
-#endif
- {
- if (lseek(matrix_info->file,offset,SEEK_SET) >= 0)
- {
-#endif
- count=0;
- for (i=0; i < (ssize_t) length; i+=count)
- {
-#if !defined(MAGICKCORE_HAVE_PWRITE)
- count=write(matrix_info->file,buffer+i,MagickMin(length-i,(size_t)
- SSIZE_MAX));
-#else
- count=pwrite(matrix_info->file,buffer+i,MagickMin(length-i,(size_t)
- SSIZE_MAX),offset+i);
-#endif
- if (count > 0)
- continue;
- count=0;
- if (errno != EINTR)
- {
- i=(-1);
- break;
- }
- }
-#if !defined(MAGICKCORE_HAVE_PWRITE)
- }
- }
-#endif
- return(i);
-}
-
MagickExport MagickBooleanType SetMatrixElement(const MatrixInfo *matrix_info,
const ssize_t x,const ssize_t y,const void *value)
{
MagickOffsetType
+ count,
i;
- ssize_t
- count;
-
assert(matrix_info != (const MatrixInfo *) NULL);
assert(matrix_info->signature == MagickSignature);
i=(MagickOffsetType) matrix_info->rows*x+y;
matrix_info->stride);
return(MagickTrue);
}
- count=WriteMatrixInfo(matrix_info,i*matrix_info->stride,matrix_info->stride,
- value);
- if (count != (ssize_t) matrix_info->stride)
+ count=WriteMatrixElements(matrix_info,i*matrix_info->stride,
+ matrix_info->stride,value);
+ if (count != (MagickOffsetType) matrix_info->stride)
return(MagickFalse);
return(MagickTrue);
}