1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 module libgit2_d.sys.alloc;
8 
9 
10 extern (C):
11 nothrow @nogc:
12 package(libgit2_d):
13 
14 /**
15  * An instance for a custom memory allocator
16  *
17  * Setting the pointers of this structure allows the developer to implement
18  * custom memory allocators. The global memory allocator can be set by using
19  * "git_libgit2_opt_t.GIT_OPT_SET_ALLOCATOR" with the `git_libgit2_opts` function. Keep in mind
20  * that all fields need to be set to a proper function.
21  */
22 struct git_allocator
23 {
24 	/**
25 	 * Allocate `n` bytes of memory
26 	 */
27 	void* function(size_t n, const (char)* file, int line) gmalloc;
28 
29 	/**
30 	 * Allocate memory for an array of `nelem` elements, where each element
31 	 * has a size of `elsize`. Returned memory shall be initialized to
32 	 * all-zeroes
33 	 */
34 	void* function(size_t nelem, size_t elsize, const (char)* file, int line) gcalloc;
35 
36 	/**
37 	 * Allocate memory for the string `str` and duplicate its contents.
38 	 */
39 	char* function(const (char)* str, const (char)* file, int line) gstrdup;
40 
41 	/**
42 	 * Equivalent to the `gstrdup` function, but only duplicating at most
43 	 * `n + 1` bytes
44 	 */
45 	char* function(const (char)* str, size_t n, const (char)* file, int line) gstrndup;
46 
47 	/**
48 	 * Equivalent to `gstrndup`, but will always duplicate exactly `n` bytes
49 	 * of `str`. Thus, out of bounds reads at `str` may happen.
50 	 */
51 	char* function(const (char)* str, size_t n, const (char)* file, int line) gsubstrdup;
52 
53 	/**
54 	 * This function shall deallocate the old object `ptr_` and return a
55 	 * pointer to a new object that has the size specified by `size`. In
56 	 * case `ptr_` is `NULL`, a new array shall be allocated.
57 	 */
58 	void* function(void* ptr_, size_t size, const (char)* file, int line) grealloc;
59 
60 	/**
61 	 * This function shall be equivalent to `grealloc`, but allocating
62 	 * `neleme * elsize` bytes.
63 	 */
64 	void* function(void* ptr_, size_t nelem, size_t elsize, const (char)* file, int line) greallocarray;
65 
66 	/**
67 	 * This function shall allocate a new array of `nelem` elements, where
68 	 * each element has a size of `elsize` bytes.
69 	 */
70 	void* function(size_t nelem, size_t elsize, const (char)* file, int line) gmallocarray;
71 
72 	/**
73 	 * This function shall free the memory pointed to by `ptr_`. In case
74 	 * `ptr_` is `NULL`, this shall be a no-op.
75 	 */
76 	void function(void* ptr_) gfree;
77 }
78 
79 /**
80  * Initialize the allocator structure to use the `stdalloc` pointer.
81  *
82  * Set up the structure so that all of its members are using the standard
83  * "stdalloc" allocator functions. The structure can then be used with
84  * `git_allocator_setup`.
85  *
86  * Params:
87  *      allocator = The allocator that is to be initialized.
88  *
89  * Returns: An error code or 0.
90  */
91 int git_stdalloc_init_allocator(.git_allocator* allocator);
92 
93 /**
94  * Initialize the allocator structure to use the `crtdbg` pointer.
95  *
96  * Set up the structure so that all of its members are using the "crtdbg"
97  * allocator functions. Note that this allocator is only available on Windows
98  * platforms and only if libgit2 is being compiled with "-DMSVC_CRTDBG".
99  *
100  * Params:
101  *      allocator = The allocator that is to be initialized.
102  *
103  * Returns: An error code or 0.
104  */
105 int git_win32_crtdbg_init_allocator(.git_allocator* allocator);