Coverage Report

Created: 2026-07-25 07:06

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
3.26M
{
52
3.26M
  unsigned int
53
3.26M
    i;
54
55
3.26M
  if (data_set != (ThreadViewDataSet *) NULL)
56
3.26M
    {
57
3.26M
      if (data_set->view_data != (void *) NULL)
58
3.26M
        {
59
3.26M
          if (data_set->destructor != (MagickFreeFunc) NULL)
60
3.26M
            {
61
6.53M
              for (i=0; i < data_set->nviews; i++)
62
3.26M
                {
63
3.26M
                  (data_set->destructor)(data_set->view_data[i]);
64
3.26M
                  data_set->view_data[i]=(void *) NULL;
65
3.26M
                }
66
3.26M
            }
67
3.26M
          MagickFreeMemory(data_set->view_data);
68
3.26M
        }
69
3.26M
      data_set->nviews=0;
70
3.26M
      MagickFreeMemory(data_set);
71
3.26M
    }
72
3.26M
}
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
3.26M
{
82
3.26M
  ThreadViewDataSet
83
3.26M
    *data_set;
84
85
3.26M
  MagickPassFail
86
3.26M
    status=MagickPass;
87
88
3.26M
  data_set=MagickAllocateMemory(ThreadViewDataSet *,sizeof(ThreadViewDataSet));
89
3.26M
  if (data_set == (ThreadViewDataSet *) NULL)
90
0
    MagickFatalError3(ResourceLimitFatalError,MemoryAllocationFailed,
91
3.26M
                      UnableToAllocateCacheView);
92
3.26M
  data_set->destructor=destructor;
93
3.26M
  data_set->nviews=omp_get_max_threads();
94
3.26M
  data_set->view_data=MagickAllocateArray(void **,data_set->nviews,sizeof(void *));
95
3.26M
  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
3.26M
  if (data_set->view_data != (void *) NULL)
103
3.26M
    (void) memset(data_set->view_data,0,data_set->nviews*sizeof(void *));
104
105
3.26M
  if (status == MagickFail)
106
0
    {
107
0
      DestroyThreadViewDataSet(data_set);
108
0
      data_set=(ThreadViewDataSet *) NULL;
109
0
    }
110
111
3.26M
  return data_set;
112
3.26M
}
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
61.4k
{
124
  /*
125
    Allocate per-thread-view memory.
126
  */
127
61.4k
  ThreadViewDataSet
128
61.4k
    *data_set;
129
130
61.4k
  MagickPassFail
131
61.4k
    alloc_status=MagickFail;
132
133
61.4k
  data_set=AllocateThreadViewDataSet(MagickFree,image,exception);
134
61.4k
  if (data_set != (ThreadViewDataSet *) NULL)
135
61.4k
    {
136
61.4k
      unsigned int
137
61.4k
        allocated_views;
138
139
61.4k
      unsigned int
140
61.4k
        i;
141
142
61.4k
      alloc_status=MagickPass;
143
61.4k
      allocated_views=GetThreadViewDataSetAllocatedViews(data_set);
144
145
122k
      for (i=0; i < allocated_views; i++)
146
61.4k
        {
147
61.4k
          unsigned char
148
61.4k
            *data;
149
150
61.4k
          data=MagickAllocateArray(unsigned char *,count,size);
151
61.4k
          if (data == (unsigned char *) NULL)
152
534
            {
153
534
              ThrowException(exception,ResourceLimitError,MemoryAllocationFailed,
154
534
                             image->filename);
155
534
              alloc_status=MagickFail;
156
534
              break;
157
534
            }
158
60.9k
          (void) memset(data,0,count*size);
159
60.9k
          AssignThreadViewData(data_set,i,data);
160
60.9k
        }
161
61.4k
      if (alloc_status == MagickFail)
162
534
        {
163
534
          DestroyThreadViewDataSet(data_set);
164
534
          data_set=(ThreadViewDataSet *) NULL;
165
534
        }
166
61.4k
    }
167
168
61.4k
  return data_set;
169
61.4k
}
170
171
/*
172
  Access allocated thread data.
173
*/
174
MagickExport void *
175
AccessThreadViewData(ThreadViewDataSet *data_set)
176
21.2M
{
177
21.2M
  unsigned int
178
21.2M
    index=0;
179
180
21.2M
  index=omp_get_thread_num();
181
21.2M
  assert(index < data_set->nviews);
182
21.2M
  return data_set->view_data[index];
183
21.2M
}
184
185
MagickExport void *
186
AccessThreadViewDataById(ThreadViewDataSet *data_set,
187
                         unsigned int index)
188
3.20M
{
189
3.20M
  assert(index < data_set->nviews);
190
3.20M
  return data_set->view_data[index];
191
3.20M
}
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
3.26M
{
199
3.26M
  assert(index < data_set->nviews);
200
3.26M
  MagickFreeMemory(data_set->view_data[index]);
201
3.26M
  data_set->view_data[index]=data;
202
3.26M
}
203
204
/*
205
  Obtain the number of thread data views.
206
*/
207
MagickExport unsigned int
208
GetThreadViewDataSetAllocatedViews
209
(ThreadViewDataSet *data_set)
210
3.26M
{
211
3.26M
  return data_set->nviews;
212
3.26M
}