1 /*
2 * Copyright (c) 2018, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /** \file
30 * \brief Runtime functions for setting custom allocators.
31 */
32
33 #include "ch.h"
34 #include "ch_common.h"
35 #include "ch_internal.h"
36 #include "hs.h"
37 #include "ue2common.h"
38
39 #define default_malloc malloc
40 #define default_free free
41
42 ch_alloc_t ch_database_alloc = default_malloc;
43 ch_alloc_t ch_misc_alloc = default_malloc;
44 ch_alloc_t ch_scratch_alloc = default_malloc;
45
46 ch_free_t ch_database_free = default_free;
47 ch_free_t ch_misc_free = default_free;
48 ch_free_t ch_scratch_free = default_free;
49
50 static
normalise_alloc(ch_alloc_t a)51 ch_alloc_t normalise_alloc(ch_alloc_t a) {
52 if (!a) {
53 return default_malloc;
54 } else {
55 return a;
56 }
57 }
58
59 static
normalise_free(ch_free_t f)60 ch_free_t normalise_free(ch_free_t f) {
61 if (!f) {
62 return default_free;
63 } else {
64 return f;
65 }
66 }
67
68 HS_PUBLIC_API
ch_set_allocator(ch_alloc_t allocfunc,ch_free_t freefunc)69 ch_error_t HS_CDECL ch_set_allocator(ch_alloc_t allocfunc,
70 ch_free_t freefunc) {
71 ch_set_database_allocator(allocfunc, freefunc);
72 ch_set_misc_allocator(allocfunc, freefunc);
73 ch_set_scratch_allocator(allocfunc, freefunc);
74
75 // Set core Hyperscan alloc/free.
76 hs_error_t ret = hs_set_allocator(allocfunc, freefunc);
77
78 return ret;
79 }
80
81 HS_PUBLIC_API
ch_set_database_allocator(ch_alloc_t allocfunc,ch_free_t freefunc)82 ch_error_t HS_CDECL ch_set_database_allocator(ch_alloc_t allocfunc,
83 ch_free_t freefunc) {
84 ch_database_alloc = normalise_alloc(allocfunc);
85 ch_database_free = normalise_free(freefunc);
86
87 // Set Hyperscan database alloc/free.
88 return hs_set_database_allocator(allocfunc, freefunc);
89 }
90
91 HS_PUBLIC_API
ch_set_misc_allocator(ch_alloc_t allocfunc,ch_free_t freefunc)92 ch_error_t HS_CDECL ch_set_misc_allocator(ch_alloc_t allocfunc,
93 ch_free_t freefunc) {
94 ch_misc_alloc = normalise_alloc(allocfunc);
95 ch_misc_free = normalise_free(freefunc);
96
97 // Set Hyperscan misc alloc/free.
98 return hs_set_misc_allocator(allocfunc, freefunc);
99 }
100
101 HS_PUBLIC_API
ch_set_scratch_allocator(ch_alloc_t allocfunc,ch_free_t freefunc)102 ch_error_t HS_CDECL ch_set_scratch_allocator(ch_alloc_t allocfunc,
103 ch_free_t freefunc) {
104 ch_scratch_alloc = normalise_alloc(allocfunc);
105 ch_scratch_free = normalise_free(freefunc);
106
107 // Set Hyperscan scratch alloc/free.
108 return hs_set_scratch_allocator(allocfunc, freefunc);
109 }
110