]>
git.proxmox.com Git - libgit2.git/blob - src/allocators/stdalloc.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 static void *stdalloc__malloc(size_t len
, const char *file
, int line
)
17 #ifdef GIT_DEBUG_STRICT_ALLOC
30 static void *stdalloc__calloc(size_t nelem
, size_t elsize
, const char *file
, int line
)
37 #ifdef GIT_DEBUG_STRICT_ALLOC
38 if (!elsize
|| !nelem
)
42 ptr
= calloc(nelem
, elsize
);
50 static char *stdalloc__strdup(const char *str
, const char *file
, int line
)
65 static char *stdalloc__strndup(const char *str
, size_t n
, const char *file
, int line
)
67 size_t length
= 0, alloclength
;
70 length
= p_strnlen(str
, n
);
72 if (GIT_ADD_SIZET_OVERFLOW(&alloclength
, length
, 1) ||
73 !(ptr
= stdalloc__malloc(alloclength
, file
, line
)))
77 memcpy(ptr
, str
, length
);
84 static char *stdalloc__substrdup(const char *start
, size_t n
, const char *file
, int line
)
89 if (GIT_ADD_SIZET_OVERFLOW(&alloclen
, n
, 1) ||
90 !(ptr
= stdalloc__malloc(alloclen
, file
, line
)))
93 memcpy(ptr
, start
, n
);
98 static void *stdalloc__realloc(void *ptr
, size_t size
, const char *file
, int line
)
105 #ifdef GIT_DEBUG_STRICT_ALLOC
110 new_ptr
= realloc(ptr
, size
);
118 static void *stdalloc__reallocarray(void *ptr
, size_t nelem
, size_t elsize
, const char *file
, int line
)
122 if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize
, nelem
, elsize
))
125 return stdalloc__realloc(ptr
, newsize
, file
, line
);
128 static void *stdalloc__mallocarray(size_t nelem
, size_t elsize
, const char *file
, int line
)
130 return stdalloc__reallocarray(NULL
, nelem
, elsize
, file
, line
);
133 static void stdalloc__free(void *ptr
)
138 int git_stdalloc_init_allocator(git_allocator
*allocator
)
140 allocator
->gmalloc
= stdalloc__malloc
;
141 allocator
->gcalloc
= stdalloc__calloc
;
142 allocator
->gstrdup
= stdalloc__strdup
;
143 allocator
->gstrndup
= stdalloc__strndup
;
144 allocator
->gsubstrdup
= stdalloc__substrdup
;
145 allocator
->grealloc
= stdalloc__realloc
;
146 allocator
->greallocarray
= stdalloc__reallocarray
;
147 allocator
->gmallocarray
= stdalloc__mallocarray
;
148 allocator
->gfree
= stdalloc__free
;