Coverage Report

Created: 2026-02-14 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/LibRaw/libraw/libraw_alloc.h
Line
Count
Source
1
/* -*- C++ -*-
2
 * File: libraw_alloc.h
3
 * Copyright 2008-2025 LibRaw LLC (info@libraw.org)
4
 * Created: Sat Mar  22, 2008
5
 *
6
 * LibRaw C++ interface
7
 *
8
LibRaw is free software; you can redistribute it and/or modify
9
it under the terms of the one of two licenses as you choose:
10
11
1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
12
   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
13
14
2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
15
   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
16
17
 */
18
19
#ifndef __LIBRAW_ALLOC_H
20
#define __LIBRAW_ALLOC_H
21
22
#include <stdlib.h>
23
#include <string.h>
24
#include "libraw_const.h"
25
26
#ifdef __cplusplus
27
28
63.0M
#define LIBRAW_MSIZE 512
29
30
class DllDef libraw_memmgr
31
{
32
public:
33
31.8k
  libraw_memmgr(unsigned ee) : extra_bytes(ee)
34
31.8k
  {
35
31.8k
    size_t alloc_sz = LIBRAW_MSIZE * sizeof(void *);
36
31.8k
    mems = (void **)::malloc(alloc_sz);
37
31.8k
    memset(mems, 0, alloc_sz);
38
31.8k
  }
39
  ~libraw_memmgr()
40
31.8k
  {
41
31.8k
    cleanup();
42
31.8k
    ::free(mems);
43
31.8k
  }
44
  void *malloc(size_t sz)
45
5.56k
  {
46
#ifdef LIBRAW_USE_CALLOC_INSTEAD_OF_MALLOC
47
    void *ptr = ::calloc(sz + extra_bytes, 1);
48
#else
49
5.56k
    void *ptr = ::malloc(sz + extra_bytes);
50
5.56k
#endif
51
5.56k
    mem_ptr(ptr);
52
5.56k
    return ptr;
53
5.56k
  }
54
  void *calloc(size_t n, size_t sz)
55
330k
  {
56
330k
    void *ptr = ::calloc(n + (extra_bytes + sz - 1) / (sz ? sz : 1), sz);
57
330k
    mem_ptr(ptr);
58
330k
    return ptr;
59
330k
  }
60
  void *realloc(void *ptr, size_t newsz)
61
66
  {
62
66
    void *ret = ::realloc(ptr, newsz + extra_bytes);
63
66
    forget_ptr(ptr);
64
66
    mem_ptr(ret);
65
66
    return ret;
66
66
  }
67
  void free(void *ptr)
68
303k
  {
69
303k
    forget_ptr(ptr);
70
303k
    ::free(ptr);
71
303k
  }
72
  void cleanup(void)
73
110k
  {
74
56.6M
    for (int i = 0; i < LIBRAW_MSIZE; i++)
75
56.5M
      if (mems[i])
76
32.3k
      {
77
32.3k
        ::free(mems[i]);
78
32.3k
        mems[i] = NULL;
79
32.3k
      }
80
110k
  }
81
82
private:
83
  void **mems;
84
  unsigned extra_bytes;
85
  void mem_ptr(void *ptr)
86
336k
  {
87
#if defined(LIBRAW_USE_OPENMP)
88
      bool ok = false; /* do not return from critical section */
89
#endif
90
91
#if defined(LIBRAW_USE_OPENMP)
92
#pragma omp critical
93
      {
94
#endif
95
336k
          if (ptr)
96
336k
          {
97
4.14M
              for (int i = 0; i < LIBRAW_MSIZE - 1; i++)
98
4.14M
                  if (!mems[i])
99
336k
                  {
100
336k
                      mems[i] = ptr;
101
#if defined(LIBRAW_USE_OPENMP)
102
          ok = true;
103
          break;
104
#else
105
336k
                      return;
106
336k
#endif
107
336k
                  }
108
2
#ifdef LIBRAW_MEMPOOL_CHECK
109
2
#if !defined(LIBRAW_USE_OPENMP)
110
              /* remember ptr in last mems item to be free'ed at cleanup */
111
2
              if (!mems[LIBRAW_MSIZE - 1])
112
2
                  mems[LIBRAW_MSIZE - 1] = ptr;
113
2
              throw LIBRAW_EXCEPTION_MEMPOOL;
114
336k
#endif
115
336k
#endif
116
336k
          }
117
#if defined(LIBRAW_USE_OPENMP)
118
      }
119
      if(!ok)
120
      {
121
          if (!mems[LIBRAW_MSIZE - 1])
122
              mems[LIBRAW_MSIZE - 1] = ptr;
123
          throw LIBRAW_EXCEPTION_MEMPOOL;
124
      }
125
#endif
126
336k
  }
127
  void forget_ptr(void *ptr)
128
303k
  {
129
#if defined(LIBRAW_USE_OPENMP)
130
#pragma omp critical
131
    {
132
#endif
133
303k
     if (ptr)
134
2.22M
      for (int i = 0; i < LIBRAW_MSIZE; i++)
135
2.22M
        if (mems[i] == ptr)
136
303k
        {
137
303k
          mems[i] = NULL;
138
303k
          break;
139
303k
        }
140
#if defined(LIBRAW_USE_OPENMP)
141
    }
142
#endif
143
303k
  }
144
};
145
146
#endif /* C++ */
147
148
#endif