1 /*
2  * Copyright (c) 2015-2017, 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 <stdlib.h>
34 #include <string.h>
35 
36 #include "allocator.h"
37 
38 #define default_malloc malloc
39 #define default_free free
40 
41 hs_alloc_t hs_database_alloc = default_malloc;
42 hs_alloc_t hs_misc_alloc = default_malloc;
43 hs_alloc_t hs_scratch_alloc = default_malloc;
44 hs_alloc_t hs_stream_alloc = default_malloc;
45 
46 hs_free_t hs_database_free = default_free;
47 hs_free_t hs_misc_free = default_free;
48 hs_free_t hs_scratch_free = default_free;
49 hs_free_t hs_stream_free = default_free;
50 
51 static
normalise_alloc(hs_alloc_t a)52 hs_alloc_t normalise_alloc(hs_alloc_t a) {
53     if (!a) {
54         return default_malloc;
55     } else {
56         return a;
57     }
58 }
59 
60 static
normalise_free(hs_free_t f)61 hs_free_t normalise_free(hs_free_t f) {
62     if (!f) {
63         return default_free;
64     } else {
65         return f;
66     }
67 }
68 
69 HS_PUBLIC_API
hs_set_allocator(hs_alloc_t allocfunc,hs_free_t freefunc)70 hs_error_t HS_CDECL hs_set_allocator(hs_alloc_t allocfunc, hs_free_t freefunc) {
71     hs_set_database_allocator(allocfunc, freefunc);
72     hs_set_misc_allocator(allocfunc, freefunc);
73     hs_set_stream_allocator(allocfunc, freefunc);
74     hs_set_scratch_allocator(allocfunc, freefunc);
75 
76     return HS_SUCCESS;
77 }
78 
79 HS_PUBLIC_API
hs_set_database_allocator(hs_alloc_t allocfunc,hs_free_t freefunc)80 hs_error_t HS_CDECL hs_set_database_allocator(hs_alloc_t allocfunc,
81                                               hs_free_t freefunc) {
82     hs_database_alloc = normalise_alloc(allocfunc);
83     hs_database_free = normalise_free(freefunc);
84 
85     return HS_SUCCESS;
86 }
87 
88 HS_PUBLIC_API
hs_set_misc_allocator(hs_alloc_t allocfunc,hs_free_t freefunc)89 hs_error_t HS_CDECL hs_set_misc_allocator(hs_alloc_t allocfunc,
90                                           hs_free_t freefunc) {
91     hs_misc_alloc = normalise_alloc(allocfunc);
92     hs_misc_free = normalise_free(freefunc);
93 
94     return HS_SUCCESS;
95 }
96 
97 HS_PUBLIC_API
hs_set_scratch_allocator(hs_alloc_t allocfunc,hs_free_t freefunc)98 hs_error_t HS_CDECL hs_set_scratch_allocator(hs_alloc_t allocfunc,
99                                              hs_free_t freefunc) {
100     hs_scratch_alloc = normalise_alloc(allocfunc);
101     hs_scratch_free = normalise_free(freefunc);
102 
103     return HS_SUCCESS;
104 }
105 
106 HS_PUBLIC_API
hs_set_stream_allocator(hs_alloc_t allocfunc,hs_free_t freefunc)107 hs_error_t HS_CDECL hs_set_stream_allocator(hs_alloc_t allocfunc,
108                                             hs_free_t freefunc) {
109     hs_stream_alloc = normalise_alloc(allocfunc);
110     hs_stream_free = normalise_free(freefunc);
111 
112     return HS_SUCCESS;
113 }
114