MagickCore  6.9.13-47
Convert, Edit, Or Compose Bitmap Images
memory_.h
1 /*
2  Copyright 1999 ImageMagick Studio LLC, a non-profit organization
3  dedicated to making software imaging solutions freely available.
4 
5  You may not use this file except in compliance with the License. You may
6  obtain a copy of the License at
7 
8  https://imagemagick.org/license/
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16  MagickCore memory methods.
17 */
18 #ifndef MAGICKCORE_MEMORY_H
19 #define MAGICKCORE_MEMORY_H
20 
21 #include <errno.h>
22 
23 #define MAGICK_INT_MAX (INT_MAX)
24 #define MAGICK_PTRDIFF_MAX (PTRDIFF_MAX)
25 #define MAGICK_PTRDIFF_MIN (-PTRDIFF_MAX-1)
26 #define MAGICK_SIZE_MAX (SIZE_MAX)
27 #define MAGICK_SSIZE_MAX (SSIZE_MAX)
28 #define MAGICK_SSIZE_MIN (-SSIZE_MAX-1)
29 #define MAGICK_UCHAR_MAX (UCHAR_MAX)
30 #define MAGICK_UINT_MAX (UINT_MAX)
31 #define MAGICK_ULONG_MAX (ULONG_MAX)
32 #define MAGICK_USHORT_MAX (USHRT_MAX)
33 
34 #if defined(__cplusplus) || defined(c_plusplus)
35 extern "C" {
36 #endif
37 
38 typedef struct _MemoryInfo
39  MemoryInfo;
40 
41 typedef void
42  *(*AcquireMemoryHandler)(size_t) magick_alloc_size(1),
43  (*DestroyMemoryHandler)(void *),
44  *(*ResizeMemoryHandler)(void *,size_t) magick_alloc_size(2),
45  *(*AcquireAlignedMemoryHandler)(const size_t,const size_t),
46  (*RelinquishAlignedMemoryHandler)(void *);
47 
48 extern MagickExport MemoryInfo
49  *AcquireVirtualMemory(const size_t,const size_t) magick_alloc_sizes(1,2),
50  *RelinquishVirtualMemory(MemoryInfo *);
51 
52 extern MagickExport size_t
53  GetMaxMemoryRequest(void);
54 
55 extern MagickExport void
56  *AcquireAlignedMemory(const size_t,const size_t)
57  magick_attribute((__malloc__)) magick_alloc_sizes(1,2),
58  *AcquireMagickMemory(const size_t) magick_attribute((__malloc__))
59  magick_alloc_size(1),
60  *AcquireCriticalMemory(const size_t),
61  *AcquireQuantumMemory(const size_t,const size_t)
62  magick_attribute((__malloc__)) magick_alloc_sizes(1,2),
63  *CopyMagickMemory(void *magick_restrict,const void *magick_restrict,
64  const size_t) magick_attribute((__nonnull__)),
65  DestroyMagickMemory(void),
66  GetMagickMemoryMethods(AcquireMemoryHandler *,ResizeMemoryHandler *,
67  DestroyMemoryHandler *),
68  *GetVirtualMemoryBlob(const MemoryInfo *),
69  *RelinquishAlignedMemory(void *),
70  *RelinquishMagickMemory(void *),
71  *ResetMagickMemory(void *,int,const size_t),
72  *ResizeMagickMemory(void *,const size_t)
73  magick_attribute((__malloc__)) magick_alloc_size(2),
74  *ResizeQuantumMemory(void *,const size_t,const size_t)
75  magick_attribute((__malloc__)) magick_alloc_sizes(2,3),
76  SetMagickAlignedMemoryMethods(AcquireAlignedMemoryHandler,
77  RelinquishAlignedMemoryHandler),
78  SetMagickMemoryMethods(AcquireMemoryHandler,ResizeMemoryHandler,
79  DestroyMemoryHandler);
80 
81 static inline MagickBooleanType HeapOverflowSanityCheck(
82  const size_t count,const size_t quantum)
83 {
84  if ((count == 0) || (quantum == 0))
85  return(MagickTrue);
86  if (count > (MAGICK_SIZE_MAX/quantum))
87  {
88  errno=ENOMEM;
89  return(MagickTrue);
90  }
91  return(MagickFalse);
92 }
93 
94 static inline MagickBooleanType HeapOverflowSanityCheckGetSize(
95  const size_t count,const size_t quantum,size_t *const extent)
96 {
97  if ((count == 0) || (quantum == 0))
98  return(MagickTrue);
99  if (count > (SIZE_MAX/quantum))
100  {
101  errno=ENOMEM;
102  return(MagickTrue);
103  }
104  if (extent != (size_t *) NULL)
105  *extent=count*quantum;
106  return(MagickFalse);
107 }
108 
109 #if defined(__cplusplus) || defined(c_plusplus)
110 }
111 #endif
112 
113 #endif