forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebugalloc.c
More file actions
73 lines (56 loc) · 1.53 KB
/
Copy pathdebugalloc.c
File metadata and controls
73 lines (56 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* 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.
*/
#include "debugalloc.h"
static void *debugalloc__malloc(size_t len, const char *file, int line)
{
unsigned char *ptr;
size_t total = len + sizeof(size_t);
GIT_UNUSED(file);
GIT_UNUSED(line);
if (!len || (ptr = malloc(total)) == NULL)
return NULL;
memcpy(ptr, &len, sizeof(size_t));
return ptr + sizeof(size_t);
}
static void *debugalloc__realloc(void *_ptr, size_t len, const char *file, int line)
{
unsigned char *ptr = _ptr, *newptr;
size_t original_len;
size_t total = len + sizeof(size_t);
GIT_UNUSED(file);
GIT_UNUSED(line);
if (!len && !ptr)
return NULL;
if (!len) {
free(ptr - sizeof(size_t));
return NULL;
}
if ((newptr = malloc(total)) == NULL)
return NULL;
if (ptr) {
memcpy(&original_len, ptr - sizeof(size_t), sizeof(size_t));
memcpy(newptr + sizeof(size_t), ptr, min(len, original_len));
memset(ptr - sizeof(size_t), 0xfd, original_len + sizeof(size_t));
free(ptr - sizeof(size_t));
}
memcpy(newptr, &len, sizeof(size_t));
return newptr + sizeof(size_t);
}
static void debugalloc__free(void *_ptr)
{
unsigned char *ptr = _ptr;
if (!ptr)
return;
free(ptr - sizeof(size_t));
}
int git_debugalloc_init_allocator(git_allocator *allocator)
{
allocator->gmalloc = debugalloc__malloc;
allocator->grealloc = debugalloc__realloc;
allocator->gfree = debugalloc__free;
return 0;
}