/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 | 0 | #define LIBRAW_MSIZE 512 |
29 | | |
30 | | class DllDef libraw_memmgr |
31 | | { |
32 | | public: |
33 | 0 | libraw_memmgr(unsigned ee) : extra_bytes(ee) |
34 | 0 | { |
35 | 0 | size_t alloc_sz = LIBRAW_MSIZE * sizeof(void *); |
36 | 0 | mems = (void **)::malloc(alloc_sz); |
37 | 0 | if(mems) |
38 | 0 | memset(mems, 0, alloc_sz); |
39 | 0 | } |
40 | | ~libraw_memmgr() |
41 | 0 | { |
42 | 0 | cleanup(); |
43 | 0 | if(mems) |
44 | 0 | ::free(mems); |
45 | 0 | } |
46 | | void *malloc(size_t sz) |
47 | 0 | { |
48 | | #ifdef LIBRAW_USE_CALLOC_INSTEAD_OF_MALLOC |
49 | | void *ptr = ::calloc(sz + extra_bytes, 1); |
50 | | #else |
51 | 0 | void *ptr = ::malloc(sz + extra_bytes); |
52 | 0 | #endif |
53 | 0 | mem_ptr(ptr); |
54 | 0 | return ptr; |
55 | 0 | } |
56 | | void *calloc(size_t n, size_t sz) |
57 | 0 | { |
58 | 0 | void *ptr = ::calloc(n + (extra_bytes + sz - 1) / (sz ? sz : 1), sz); |
59 | 0 | mem_ptr(ptr); |
60 | 0 | return ptr; |
61 | 0 | } |
62 | | void *realloc(void *ptr, size_t newsz) |
63 | 0 | { |
64 | 0 | void *ret = ::realloc(ptr, newsz + extra_bytes); |
65 | 0 | forget_ptr(ptr); |
66 | 0 | mem_ptr(ret); |
67 | 0 | return ret; |
68 | 0 | } |
69 | | void free(void *ptr) |
70 | 0 | { |
71 | 0 | forget_ptr(ptr); |
72 | 0 | ::free(ptr); |
73 | 0 | } |
74 | | void cleanup(void) |
75 | 0 | { |
76 | 0 | if (!mems) return; |
77 | 0 | for (int i = 0; i < LIBRAW_MSIZE; i++) |
78 | 0 | if (mems[i]) |
79 | 0 | { |
80 | 0 | ::free(mems[i]); |
81 | 0 | mems[i] = NULL; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | | private: |
86 | | void **mems; |
87 | | unsigned extra_bytes; |
88 | | void mem_ptr(void *ptr) |
89 | 0 | { |
90 | 0 | if (!mems) return; |
91 | | #if defined(LIBRAW_USE_OPENMP) |
92 | | bool ok = false; /* do not return from critical section */ |
93 | | #endif |
94 | | |
95 | | #if defined(LIBRAW_USE_OPENMP) |
96 | | #pragma omp critical |
97 | | { |
98 | | #endif |
99 | 0 | if (ptr) |
100 | 0 | { |
101 | 0 | for (int i = 0; i < LIBRAW_MSIZE - 1; i++) |
102 | 0 | if (!mems[i]) |
103 | 0 | { |
104 | 0 | mems[i] = ptr; |
105 | | #if defined(LIBRAW_USE_OPENMP) |
106 | | ok = true; |
107 | | break; |
108 | | #else |
109 | 0 | return; |
110 | 0 | #endif |
111 | 0 | } |
112 | 0 | #ifdef LIBRAW_MEMPOOL_CHECK |
113 | 0 | #if !defined(LIBRAW_USE_OPENMP) |
114 | | /* remember ptr in last mems item to be free'ed at cleanup */ |
115 | 0 | if (!mems[LIBRAW_MSIZE - 1]) |
116 | 0 | mems[LIBRAW_MSIZE - 1] = ptr; |
117 | 0 | throw LIBRAW_EXCEPTION_MEMPOOL; |
118 | 0 | #endif |
119 | 0 | #endif |
120 | 0 | } |
121 | | #if defined(LIBRAW_USE_OPENMP) |
122 | | } |
123 | | if(!ok) |
124 | | { |
125 | | if (!mems[LIBRAW_MSIZE - 1]) |
126 | | mems[LIBRAW_MSIZE - 1] = ptr; |
127 | | throw LIBRAW_EXCEPTION_MEMPOOL; |
128 | | } |
129 | | #endif |
130 | 0 | } |
131 | | void forget_ptr(void *ptr) |
132 | 0 | { |
133 | 0 | if (!mems) return; |
134 | | #if defined(LIBRAW_USE_OPENMP) |
135 | | #pragma omp critical |
136 | | { |
137 | | #endif |
138 | 0 | if (ptr) |
139 | 0 | for (int i = 0; i < LIBRAW_MSIZE; i++) |
140 | 0 | if (mems[i] == ptr) |
141 | 0 | { |
142 | 0 | mems[i] = NULL; |
143 | 0 | break; |
144 | 0 | } |
145 | | #if defined(LIBRAW_USE_OPENMP) |
146 | | } |
147 | | #endif |
148 | 0 | } |
149 | | }; |
150 | | |
151 | | #endif /* C++ */ |
152 | | |
153 | | #endif |