Lock-free memory allocator
[blender-staging.git] / intern / guardedalloc / intern / mallocn.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel
24  *                 Campbell Barton
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file guardedalloc/intern/mallocn.c
30  *  \ingroup MEM
31  *
32  * Guarded memory allocation, and boundary-write detection.
33  */
34
35 #include "MEM_guardedalloc.h"
36
37 /* to ensure strict conversions */
38 #include "../../source/blender/blenlib/BLI_strict_flags.h"
39
40 #include "mallocn_intern.h"
41
42 size_t (*MEM_allocN_len)(const void *vmemh) = MEM_lockfree_allocN_len;
43 void (*MEM_freeN)(void *vmemh) = MEM_lockfree_freeN;
44 void *(*MEM_dupallocN)(const void *vmemh) = MEM_lockfree_dupallocN;
45 void *(*MEM_reallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_reallocN_id;
46 void *(*MEM_recallocN_id)(void *vmemh, size_t len, const char *str) = MEM_lockfree_recallocN_id;;
47 void *(*MEM_callocN)(size_t len, const char *str) = MEM_lockfree_callocN;
48 void *(*MEM_mallocN)(size_t len, const char *str) = MEM_lockfree_mallocN;
49 void *(*MEM_mapallocN)(size_t len, const char *str) = MEM_lockfree_mapallocN;
50 void (*MEM_printmemlist_pydict)(void) = MEM_lockfree_printmemlist_pydict;
51 void (*MEM_printmemlist)(void) = MEM_lockfree_printmemlist;
52 void (*MEM_callbackmemlist)(void (*func)(void *)) = MEM_lockfree_callbackmemlist;
53 void (*MEM_printmemlist_stats)(void) = MEM_lockfree_printmemlist_stats;
54 void (*MEM_set_error_callback)(void (*func)(const char *)) = MEM_lockfree_set_error_callback;
55 bool (*MEM_check_memory_integrity)(void) = MEM_lockfree_check_memory_integrity;
56 void (*MEM_set_lock_callback)(void (*lock)(void), void (*unlock)(void)) = MEM_lockfree_set_lock_callback;
57 void (*MEM_set_memory_debug)(void) = MEM_lockfree_set_memory_debug;
58 uintptr_t (*MEM_get_memory_in_use)(void) = MEM_lockfree_get_memory_in_use;
59 uintptr_t (*MEM_get_mapped_memory_in_use)(void) = MEM_lockfree_get_mapped_memory_in_use;
60 unsigned int (*MEM_get_memory_blocks_in_use)(void) = MEM_lockfree_get_memory_blocks_in_use;
61 void (*MEM_reset_peak_memory)(void) = MEM_lockfree_reset_peak_memory;
62 uintptr_t (*MEM_get_peak_memory)(void) = MEM_lockfree_get_peak_memory;
63
64 #ifndef NDEBUG
65 const char *(*MEM_name_ptr)(void *vmemh) = MEM_lockfree_name_ptr;
66 #endif
67
68 void MEM_use_guarded_allocator(void)
69 {
70         MEM_allocN_len = MEM_guarded_allocN_len;
71         MEM_freeN = MEM_guarded_freeN;
72         MEM_dupallocN = MEM_guarded_dupallocN;
73         MEM_reallocN_id = MEM_guarded_reallocN_id;
74         MEM_recallocN_id = MEM_guarded_recallocN_id;;
75         MEM_callocN = MEM_guarded_callocN;
76         MEM_mallocN = MEM_guarded_mallocN;
77         MEM_mapallocN = MEM_guarded_mapallocN;
78         MEM_printmemlist_pydict = MEM_guarded_printmemlist_pydict;
79         MEM_printmemlist = MEM_guarded_printmemlist;
80         MEM_callbackmemlist = MEM_guarded_callbackmemlist;
81         MEM_printmemlist_stats = MEM_guarded_printmemlist_stats;
82         MEM_set_error_callback = MEM_guarded_set_error_callback;
83         MEM_check_memory_integrity = MEM_guarded_check_memory_integrity;
84         MEM_set_lock_callback = MEM_guarded_set_lock_callback;
85         MEM_set_memory_debug = MEM_guarded_set_memory_debug;
86         MEM_get_memory_in_use = MEM_guarded_get_memory_in_use;
87         MEM_get_mapped_memory_in_use = MEM_guarded_get_mapped_memory_in_use;
88         MEM_get_memory_blocks_in_use = MEM_guarded_get_memory_blocks_in_use;
89         MEM_reset_peak_memory = MEM_guarded_reset_peak_memory;
90         MEM_get_peak_memory = MEM_guarded_get_peak_memory;
91
92 #ifndef NDEBUG
93         MEM_name_ptr = MEM_guarded_name_ptr;
94 #endif
95 }