]>
git.proxmox.com Git - libgit2.git/blob - src/allocators/stdalloc.c
c4938e32b6bcb9318d7004ba7578a01a4d0a0f32
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
)
12 void *ptr
= malloc(len
);
17 if (!ptr
) git_error_set_oom();
21 static void *stdalloc__calloc(size_t nelem
, size_t elsize
, const char *file
, int line
)
23 void *ptr
= calloc(nelem
, elsize
);
28 if (!ptr
) git_error_set_oom();
32 static char *stdalloc__strdup(const char *str
, const char *file
, int line
)
34 char *ptr
= strdup(str
);
39 if (!ptr
) git_error_set_oom();
43 static char *stdalloc__strndup(const char *str
, size_t n
, const char *file
, int line
)
45 size_t length
= 0, alloclength
;
48 length
= p_strnlen(str
, n
);
50 if (GIT_ADD_SIZET_OVERFLOW(&alloclength
, length
, 1) ||
51 !(ptr
= stdalloc__malloc(alloclength
, file
, line
)))
55 memcpy(ptr
, str
, length
);
62 static char *stdalloc__substrdup(const char *start
, size_t n
, const char *file
, int line
)
67 if (GIT_ADD_SIZET_OVERFLOW(&alloclen
, n
, 1) ||
68 !(ptr
= stdalloc__malloc(alloclen
, file
, line
)))
71 memcpy(ptr
, start
, n
);
76 static void *stdalloc__realloc(void *ptr
, size_t size
, const char *file
, int line
)
78 void *new_ptr
= realloc(ptr
, size
);
83 if (!new_ptr
) git_error_set_oom();
87 static void *stdalloc__reallocarray(void *ptr
, size_t nelem
, size_t elsize
, const char *file
, int line
)
91 if (GIT_MULTIPLY_SIZET_OVERFLOW(&newsize
, nelem
, elsize
))
94 return stdalloc__realloc(ptr
, newsize
, file
, line
);
97 static void *stdalloc__mallocarray(size_t nelem
, size_t elsize
, const char *file
, int line
)
99 return stdalloc__reallocarray(NULL
, nelem
, elsize
, file
, line
);
102 static void stdalloc__free(void *ptr
)
107 int git_stdalloc_init_allocator(git_allocator
*allocator
)
109 allocator
->gmalloc
= stdalloc__malloc
;
110 allocator
->gcalloc
= stdalloc__calloc
;
111 allocator
->gstrdup
= stdalloc__strdup
;
112 allocator
->gstrndup
= stdalloc__strndup
;
113 allocator
->gsubstrdup
= stdalloc__substrdup
;
114 allocator
->grealloc
= stdalloc__realloc
;
115 allocator
->greallocarray
= stdalloc__reallocarray
;
116 allocator
->gmallocarray
= stdalloc__mallocarray
;
117 allocator
->gfree
= stdalloc__free
;