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