Edit

kc3-lang/libgit2/src/util/alloc.h

Branch :

  • Show log

    Commit

  • Author : Edward Thomson
    Date : 2023-05-14 14:07:59
    Hash : 9dd1bfe8
    Message : alloc: simplify pluggable allocators Remove the number of functions that custom allocator users need to provide; nobody should need to implement `substrdup`. Keep it to the basics that are actually _needed_ for allocation (malloc, realloc, free) and reimplement the rest ourselves. In addition, move the failure check and error setting _out_ of the custom allocators and into a wrapper so that users don't need to deal with this. This also allows us to call our allocator (without the wrapper) early so that it does not try to set an error on failure, which may be important for bootstrapping.

  • src/util/alloc.h
  • /*
     * Copyright (C) the libgit2 contributors. All rights reserved.
     *
     * This file is part of libgit2, distributed under the GNU GPL v2 with
     * a Linking Exception. For full terms see the included COPYING file.
     */
    
    #ifndef INCLUDE_alloc_h__
    #define INCLUDE_alloc_h__
    
    #include "git2/sys/alloc.h"
    
    #include "git2_util.h"
    
    extern git_allocator git__allocator;
    
    GIT_INLINE(void *) git__malloc(size_t len)
    {
    	void *p = git__allocator.gmalloc(len, __FILE__, __LINE__);
    
    	if (!p)
    		git_error_set_oom();
    
    	return p;
    }
    
    GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
    {
    	void *p = git__allocator.grealloc(ptr, size, __FILE__, __LINE__);
    
    	if (!p)
    		git_error_set_oom();
    
    	return p;
    }
    
    GIT_INLINE(void) git__free(void *ptr)
    {
    	git__allocator.gfree(ptr);
    }
    
    extern void *git__calloc(size_t nelem, size_t elsize);
    extern void *git__mallocarray(size_t nelem, size_t elsize);
    extern void *git__reallocarray(void *ptr, size_t nelem, size_t elsize);
    
    extern char *git__strdup(const char *str);
    extern char *git__strndup(const char *str, size_t n);
    extern char *git__substrdup(const char *str, size_t n);
    
    /**
     * This function is being called by our global setup routines to
     * initialize the standard allocator.
     */
    int git_allocator_global_init(void);
    
    /**
     * Switch out libgit2's global memory allocator
     *
     * @param allocator The new allocator that should be used. All function pointers
     *                  of it need to be set correctly.
     * @return An error code or 0.
     */
    int git_allocator_setup(git_allocator *allocator);
    
    #endif