xref: /aosp_15_r20/external/pdfium/third_party/libopenjpeg/0034-opj_malloc.patch (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1diff --git a/third_party/libopenjpeg/opj_malloc.cc b/third_party/libopenjpeg/opj_malloc.cc
2new file mode 100644
3--- /dev/null
4+++ b/third_party/libopenjpeg/opj_malloc.cc
5@@ -0,0 +1,42 @@
6+// Copyright 2020 The PDFium Authors
7+// Use of this source code is governed by a BSD-style license that can be
8+// found in the LICENSE file.
9+
10+// Deliberately not including opj_malloc.h, which has poisoned malloc and
11+// friends.
12+
13+#include "core/fxcrt/fx_memory.h"
14+#include "third_party/base/memory/aligned_memory.h"
15+
16+extern "C" {
17+
18+void* opj_malloc(size_t size) {
19+  return FXMEM_DefaultAlloc(size);
20+}
21+
22+void* opj_calloc(size_t numOfElements, size_t sizeOfElements) {
23+  return FXMEM_DefaultCalloc(numOfElements, sizeOfElements);
24+}
25+
26+void* opj_aligned_malloc(size_t size) {
27+  return size ? pdfium::base::AlignedAlloc(size, 16) : nullptr;
28+}
29+
30+void opj_aligned_free(void* ptr) {
31+  pdfium::base::AlignedFree(ptr);
32+}
33+
34+void* opj_aligned_32_malloc(size_t size) {
35+  return size ? pdfium::base::AlignedAlloc(size, 32) : nullptr;
36+}
37+
38+void* opj_realloc(void* m, size_t s) {
39+  return FXMEM_DefaultRealloc(m, s);
40+}
41+
42+void opj_free(void* m) {
43+  if (m)
44+    FXMEM_DefaultFree(m);
45+}
46+
47+}  // extern "C"
48diff --git a/third_party/libopenjpeg/opj_malloc.h b/third_party/libopenjpeg/opj_malloc.h
49--- a/third_party/libopenjpeg/opj_malloc.h
50+++ b/third_party/libopenjpeg/opj_malloc.h
51@@ -33,6 +33,11 @@
52 #define OPJ_MALLOC_H
53
54 #include <stddef.h>
55+
56+#ifdef __cplusplus
57+extern "C" {
58+#endif
59+
60 /**
61 @file opj_malloc.h
62 @brief Internal functions
63@@ -68,7 +73,6 @@ Allocate memory aligned to a 16 byte bou
64 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
65 */
66 void * opj_aligned_malloc(size_t size);
67-void * opj_aligned_realloc(void *ptr, size_t size);
68 void opj_aligned_free(void* ptr);
69
70 /**
71@@ -77,7 +81,6 @@ Allocate memory aligned to a 32 byte bou
72 @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
73 */
74 void * opj_aligned_32_malloc(size_t size);
75-void * opj_aligned_32_realloc(void *ptr, size_t size);
76
77 /**
78 Reallocate memory blocks.
79@@ -102,5 +105,8 @@ void opj_free(void * m);
80
81 /*@}*/
82
83-#endif /* OPJ_MALLOC_H */
84+#ifdef __cplusplus
85+}  // extern "C"
86+#endif
87
88+#endif /* OPJ_MALLOC_H */
89