Coverage Report

Created: 2026-09-14 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/graphicsmagick/magick/omp_data_view.c
Line
Count
Source
1
/*
2
% Copyright (C) 2008 GraphicsMagick Group
3
% Copyright (C) 2002 ImageMagick Studio
4
%
5
% This program is covered by multiple licenses, which are described in
6
% Copyright.txt. You should have received a copy of Copyright.txt with this
7
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
8
%
9
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10
%                                                                             %
11
%                                                                             %
12
%                                                                             %
13
%                  GraphicsMagick OpenMP Data View Methods                    %
14
%                                                                             %
15
%                                                                             %
16
%                             Software Design                                 %
17
%                             Bob Friesenhahn                                 %
18
%                              September 2008                                 %
19
%                                                                             %
20
%                                                                             %
21
%                                                                             %
22
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23
%
24
%
25
%
26
*/
27

28
/*
29
  Include declarations.
30
*/
31
#include "magick/studio.h"
32
#include "magick/utility.h"
33
#include "magick/omp_data_view.h"
34
35

36
/*
37
  Declare OpenMP data view interfaces.
38
*/
39
#if defined(__cplusplus) || defined(c_plusplus)
40
extern "C" {
41
#endif
42
43
#if defined(__cplusplus) || defined(c_plusplus)
44
}
45
#endif
46
/*
47
  Destroy a thread view data set.
48
*/
49
MagickExport void
50
DestroyThreadViewDataSet(ThreadViewDataSet *data_set)
51
2.73M
{
52
2.73M
  unsigned int
53
2.73M
    i;
54
55
2.73M
  if (data_set != (ThreadViewDataSet *) NULL)
56
2.73M
    {
57
2.73M
      if (data_set->view_data != (void *) NULL)
58
2.73M
        {
59
2.73M
          if (data_set->destructor != (MagickFreeFunc) NULL)
60
2.73M
            {
61
5.47M
              for (i=0; i < data_set->nviews; i++)
62
2.73M
                {
63
2.73M
                  (data_set->destructor)(data_set->view_data[i]);
64
2.73M
                  data_set->view_data[i]=(void *) NULL;
65
2.73M
                }
66
2.73M
            }
67
2.73M
          MagickFreeMemory(data_set->view_data);
68
2.73M
        }
69
2.73M
      data_set->nviews=0;
70
2.73M
      MagickFreeMemory(data_set);
71
2.73M
    }
72
2.73M
}
73
74
/*
75
  Allocate an empty thread view data set.
76
*/
77
MagickExport ThreadViewDataSet *
78
AllocateThreadViewDataSet(const MagickFreeFunc destructor,
79
                          const Image *image,
80
                          ExceptionInfo *exception)
81
2.73M
{
82
2.73M
  ThreadViewDataSet
83
2.73M
    *data_set;
84
85
2.73M
  MagickPassFail
86
2.73M
    status=MagickPass;
87
88
2.73M
  data_set=MagickAllocateMemory(ThreadViewDataSet *,sizeof(ThreadViewDataSet));
89
2.73M
  if (data_set == (ThreadViewDataSet *) NULL)
90
0
    MagickFatalError3(ResourceLimitFatalError,MemoryAllocationFailed,
91
2.73M
                      UnableToAllocateCacheView);
92
2.73M
  data_set->destructor=destructor;
93
2.73M
  data_set->nviews=omp_get_max_threads();
94
2.73M
  data_set->view_data=MagickAllocateArray(void **,data_set->nviews,sizeof(void *));
95
2.73M
  if (data_set->view_data == (void *) NULL)
96
0
    {
97
0
      ThrowException(exception,ResourceLimitError,MemoryAllocationFailed,
98
0
                     image->filename);
99
0
      status=MagickFail;
100
0
    }
101
102
2.73M
  if (data_set->view_data != (void *) NULL)
103
2.73M
    (void) memset(data_set->view_data,0,data_set->nviews*sizeof(void *));
104
105
2.73M
  if (status == MagickFail)
106
0
    {
107
0
      DestroyThreadViewDataSet(data_set);
108
0
      data_set=(ThreadViewDataSet *) NULL;
109
0
    }
110
111
2.73M
  return data_set;
112
2.73M
}
113
114
/*
115
  Allocate a thread view data set containing data elements with
116
  allocation size dictated by 'count' and 'size'.
117
  The allocated data is initialized to zero.
118
*/
119
MagickExport ThreadViewDataSet *
120
AllocateThreadViewDataArray(const Image *image,
121
                            ExceptionInfo *exception,
122
                            size_t count,size_t size)
123
63.3k
{
124
  /*
125
    Allocate per-thread-view memory.
126
  */
127
63.3k
  ThreadViewDataSet
128
63.3k
    *data_set;
129
130
63.3k
  MagickPassFail
131
63.3k
    alloc_status=MagickFail;
132
133
63.3k
  data_set=AllocateThreadViewDataSet(MagickFree,image,exception);
134
63.3k
  if (data_set != (ThreadViewDataSet *) NULL)
135
63.3k
    {
136
63.3k
      unsigned int
137
63.3k
        allocated_views;
138
139
63.3k
      unsigned int
140
63.3k
        i;
141
142
63.3k
      alloc_status=MagickPass;
143
63.3k
      allocated_views=GetThreadViewDataSetAllocatedViews(data_set);
144
145
126k
      for (i=0; i < allocated_views; i++)
146
63.3k
        {
147
63.3k
          unsigned char
148
63.3k
            *data;
149
150
63.3k
          data=MagickAllocateArray(unsigned char *,count,size);
151
63.3k
          if (data == (unsigned char *) NULL)
152
513
            {
153
513
              ThrowException(exception,ResourceLimitError,MemoryAllocationFailed,
154
513
                             image->filename);
155
513
              alloc_status=MagickFail;
156
513
              break;
157
513
            }
158
62.8k
          (void) memset(data,0,count*size);
159
62.8k
          AssignThreadViewData(data_set,i,data);
160
62.8k
        }
161
63.3k
      if (alloc_status == MagickFail)
162
513
        {
163
513
          DestroyThreadViewDataSet(data_set);
164
513
          data_set=(ThreadViewDataSet *) NULL;
165
513
        }
166
63.3k
    }
167
168
63.3k
  return data_set;
169
63.3k
}
170
171
/*
172
  Access allocated thread data.
173
*/
174
MagickExport void *
175
AccessThreadViewData(ThreadViewDataSet *data_set)
176
19.4M
{
177
19.4M
  unsigned int
178
19.4M
    index=0;
179
180
19.4M
  index=omp_get_thread_num();
181
19.4M
  assert(index < data_set->nviews);
182
19.4M
  return data_set->view_data[index];
183
19.4M
}
184
185
MagickExport void *
186
AccessThreadViewDataById(ThreadViewDataSet *data_set,
187
                         unsigned int index)
188
2.67M
{
189
2.67M
  assert(index < data_set->nviews);
190
2.67M
  return data_set->view_data[index];
191
2.67M
}
192
193
/*
194
  Associate data with a thread data view.
195
*/
196
MagickExport void AssignThreadViewData
197
(ThreadViewDataSet *data_set, unsigned int index, void *data)
198
2.73M
{
199
2.73M
  assert(index < data_set->nviews);
200
2.73M
  MagickFreeMemory(data_set->view_data[index]);
201
2.73M
  data_set->view_data[index]=data;
202
2.73M
}
203
204
/*
205
  Obtain the number of thread data views.
206
*/
207
MagickExport unsigned int
208
GetThreadViewDataSetAllocatedViews
209
(ThreadViewDataSet *data_set)
210
2.73M
{
211
2.73M
  return data_set->nviews;
212
2.73M
}