Commit 3286c408eccb18c525ca123383f3ebf5097441bc

Vicent Marti 2011-10-28T14:51:13

global: Properly use `git__` memory wrappers Ensure that all memory related functions (malloc, calloc, strdup, free, etc) are using their respective `git__` wrappers.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
diff --git a/examples/network/fetch.c b/examples/network/fetch.c
index dd732f2..35fc3ea 100644
--- a/examples/network/fetch.c
+++ b/examples/network/fetch.c
@@ -119,7 +119,7 @@ int fetch(git_repository *repo, int argc, char **argv)
   if (error < GIT_SUCCESS)
     return error;
 
-  free(packname);
+  git__free(packname);
   git_indexer_free(idx);
   git_remote_free(remote);
 
diff --git a/include/git2/oid.h b/include/git2/oid.h
index b9824b8..9cebda9 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -100,7 +100,7 @@ GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid);
  *
  * @param oid the oid structure to format
  * @return the c-string; NULL if memory is exhausted. Caller must
- *			deallocate the string with free().
+ *			deallocate the string with git__free().
  */
 GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *oid);
 
diff --git a/src/blob.c b/src/blob.c
index 42564ab..f13a5be 100644
--- a/src/blob.c
+++ b/src/blob.c
@@ -27,7 +27,7 @@ size_t git_blob_rawsize(git_blob *blob)
 void git_blob__free(git_blob *blob)
 {
 	git_odb_object_close(blob->odb_object);
-	free(blob);
+	git__free(blob);
 }
 
 int git_blob__parse(git_blob *blob, git_odb_object *odb_obj)
diff --git a/src/buffer.c b/src/buffer.c
index 0eeeecf..1fb848e 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -97,7 +97,7 @@ const char *git_buf_cstr(git_buf *buf)
 
 void git_buf_free(git_buf *buf)
 {
-	free(buf->ptr);
+	git__free(buf->ptr);
 }
 
 void git_buf_clear(git_buf *buf)
diff --git a/src/cache.c b/src/cache.c
index 79f3eae..6ba4d21 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -53,7 +53,7 @@ void git_cache_free(git_cache *cache)
 		git_mutex_free(&cache->nodes[i].lock);
 	}
 
-	free(cache->nodes);
+	git__free(cache->nodes);
 }
 
 void *git_cache_get(git_cache *cache, const git_oid *oid)
diff --git a/src/commit.c b/src/commit.c
index ced457e..b9eb365 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -32,7 +32,7 @@ static void clear_parents(git_commit *commit)
 
 	for (i = 0; i < commit->parent_oids.length; ++i) {
 		git_oid *parent = git_vector_get(&commit->parent_oids, i);
-		free(parent);
+		git__free(parent);
 	}
 
 	git_vector_clear(&commit->parent_oids);
@@ -46,9 +46,9 @@ void git_commit__free(git_commit *commit)
 	git_signature_free(commit->author);
 	git_signature_free(commit->committer);
 
-	free(commit->message);
-	free(commit->message_encoding);
-	free(commit);
+	git__free(commit->message);
+	git__free(commit->message_encoding);
+	git__free(commit);
 }
 
 const git_oid *git_commit_id(git_commit *c)
@@ -84,7 +84,7 @@ int git_commit_create_v(
 		message_encoding, message,
 		tree, parent_count, parents);
 
-	free((void *)parents);
+	git__free((void *)parents);
 
 	return error;
 }
diff --git a/src/config.c b/src/config.c
index ad6f885..4e48ff7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -35,11 +35,11 @@ void git_config_free(git_config *cfg)
 		internal = git_vector_get(&cfg->files, i);
 		file = internal->file;
 		file->free(file);
-		free(internal);
+		git__free(internal);
 	}
 
 	git_vector_free(&cfg->files);
-	free(cfg);
+	git__free(cfg);
 }
 
 static int config_backend_cmp(const void *a, const void *b)
@@ -61,7 +61,7 @@ int git_config_new(git_config **out)
 	memset(cfg, 0x0, sizeof(git_config));
 
 	if (git_vector_init(&cfg->files, 3, config_backend_cmp) < 0) {
-		free(cfg);
+		git__free(cfg);
 		return GIT_ENOMEM;
 	}
 
@@ -125,7 +125,7 @@ int git_config_add_file(git_config *cfg, git_config_file *file, int priority)
 	internal->priority = priority;
 
 	if (git_vector_insert(&cfg->files, internal) < 0) {
-		free(internal);
+		git__free(internal);
 		return GIT_ENOMEM;
 	}
 
@@ -366,20 +366,20 @@ static int win32_find_system(char *system_config_path)
 		return git__throw(GIT_ERROR, "Failed to expand environment strings");
 
 	if (_waccess(apphome_utf16, F_OK) < 0) {
-		free(apphome_utf16);
+		git__free(apphome_utf16);
 		return GIT_ENOTFOUND;
 	}
 
 	apphome_utf8 = gitwin_from_utf16(apphome_utf16);
-	free(apphome_utf16);
+	git__free(apphome_utf16);
 
 	if (strlen(apphome_utf8) >= GIT_PATH_MAX) {
-		free(apphome_utf8);
+		git__free(apphome_utf8);
 		return git__throw(GIT_ESHORTBUFFER, "Path is too long");
 	}
 
 	strcpy(system_config_path, apphome_utf8);
-	free(apphome_utf8);
+	git__free(apphome_utf8);
 	return GIT_SUCCESS;
 }
 #endif
diff --git a/src/config_file.c b/src/config_file.c
index 3540aae..3cf1bb2 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -90,10 +90,10 @@ static void cvar_free(cvar_t *var)
 	if (var == NULL)
 		return;
 
-	free(var->section);
-	free(var->name);
-	free(var->value);
-	free(var);
+	git__free(var->section);
+	git__free(var->name);
+	git__free(var->value);
+	git__free(var);
 }
 
 static void cvar_list_free(cvar_t_list *list)
@@ -188,7 +188,7 @@ static int cvar_normalize_name(cvar_t *var, char **output)
 	if (section_sp == NULL) {
 		ret = p_snprintf(name, len + 1, "%s.%s", var->section, var->name);
 		if (ret < 0) {
-			free(name);
+			git__free(name);
 			return git__throw(GIT_EOSERR, "Failed to normalize name. OS err: %s", strerror(errno));
 		}
 
@@ -281,10 +281,10 @@ static void backend_free(git_config_file *_backend)
 	if (backend == NULL)
 		return;
 
-	free(backend->file_path);
+	git__free(backend->file_path);
 	cvar_list_free(&backend->var_list);
 
-	free(backend);
+	git__free(backend);
 }
 
 static int file_foreach(git_config_file *backend, int (*fn)(const char *, const char *, void *), void *data)
@@ -301,7 +301,7 @@ static int file_foreach(git_config_file *backend, int (*fn)(const char *, const 
 			return ret;
 
 		ret = fn(normalized, var->value, data);
-		free(normalized);
+		git__free(normalized);
 		if (ret)
 			break;
 	}
@@ -326,7 +326,7 @@ static int config_set(git_config_file *cfg, const char *name, const char *value)
 		if (tmp == NULL && value != NULL)
 			return GIT_ENOMEM;
 
-		free(existing->value);
+		git__free(existing->value);
 		existing->value = tmp;
 
 		return config_write(b, existing);
@@ -411,7 +411,7 @@ int git_config_file__ondisk(git_config_file **out, const char *path)
 
 	backend->file_path = git__strdup(path);
 	if (backend->file_path == NULL) {
-		free(backend);
+		git__free(backend);
 		return GIT_ENOMEM;
 	}
 
@@ -653,13 +653,13 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out)
 	/* find the end of the variable's name */
 	name_end = strchr(line, ']');
 	if (name_end == NULL) {
-		free(line);
+		git__free(line);
 		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse header. Can't find header name end");
 	}
 
 	name = (char *)git__malloc((size_t)(name_end - line) + 1);
 	if (name == NULL) {
-		free(line);
+		git__free(line);
 		return GIT_ENOMEM;
 	}
 
@@ -679,8 +679,8 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out)
 		if (isspace(c)){
 			name[name_length] = '\0';
 			error = parse_section_header_ext(line, name, section_out);
-			free(line);
-			free(name);
+			git__free(line);
+			git__free(name);
 			return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse header");
 		}
 
@@ -699,14 +699,14 @@ static int parse_section_header(diskfile_backend *cfg, char **section_out)
 	}
 
 	name[name_length] = 0;
-	free(line);
+	git__free(line);
 	git__strtolower(name);
 	*section_out = name;
 	return GIT_SUCCESS;
 
 error:
-	free(line);
-	free(name);
+	git__free(line);
+	git__free(name);
 	return error;
 }
 
@@ -810,7 +810,7 @@ static int config_parse(diskfile_backend *cfg_file)
 			break;
 
 		case '[': /* section header, new section begins */
-			free(current_section);
+			git__free(current_section);
 			current_section = NULL;
 			error = parse_section_header(cfg_file, &current_section);
 			break;
@@ -826,7 +826,7 @@ static int config_parse(diskfile_backend *cfg_file)
 			if (error < GIT_SUCCESS)
 				break;
 
-			var = malloc(sizeof(cvar_t));
+			var = git__malloc(sizeof(cvar_t));
 			if (var == NULL) {
 				error = GIT_ENOMEM;
 				break;
@@ -837,7 +837,7 @@ static int config_parse(diskfile_backend *cfg_file)
 			var->section = git__strdup(current_section);
 			if (var->section == NULL) {
 				error = GIT_ENOMEM;
-				free(var);
+				git__free(var);
 				break;
 			}
 
@@ -851,7 +851,7 @@ static int config_parse(diskfile_backend *cfg_file)
 		}
 	}
 
-	free(current_section);
+	git__free(current_section);
 
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse config");
 }
@@ -915,7 +915,7 @@ static int config_write(diskfile_backend *cfg, cvar_t *var)
 			 */
 			pre_end = post_start = cfg->reader.read_ptr;
 			if (current_section)
-				free(current_section);
+				git__free(current_section);
 			error = parse_section_header(cfg, &current_section);
 			if (error < GIT_SUCCESS)
 				break;
@@ -953,8 +953,8 @@ static int config_write(diskfile_backend *cfg, cvar_t *var)
 				if ((error = parse_variable(cfg, &var_name, &var_value)) == GIT_SUCCESS)
 					cmp = strcasecmp(var->name, var_name);
 
-				free(var_name);
-				free(var_value);
+				git__free(var_name);
+				git__free(var_value);
 
 				if (cmp != 0)
 					break;
@@ -1029,7 +1029,7 @@ static int config_write(diskfile_backend *cfg, cvar_t *var)
 		git__rethrow(error, "Failed to write new section");
 
  cleanup:
-	free(current_section);
+	git__free(current_section);
 
 	if (error < GIT_SUCCESS)
 		git_filebuf_cleanup(&file);
@@ -1093,7 +1093,7 @@ static int parse_multiline_variable(diskfile_backend *cfg, const char *first, ch
 	ret = p_snprintf(buf, len, "%s %s", first, line);
 	if (ret < 0) {
 		error = git__throw(GIT_EOSERR, "Failed to parse multiline var. Failed to put together two lines. OS err: %s", strerror(errno));
-		free(buf);
+		git__free(buf);
 		goto out;
 	}
 
@@ -1105,14 +1105,14 @@ static int parse_multiline_variable(diskfile_backend *cfg, const char *first, ch
 	if (is_multiline_var(buf)) {
 		char *final_val;
 		error = parse_multiline_variable(cfg, buf, &final_val);
-		free(buf);
+		git__free(buf);
 		buf = final_val;
 	}
 
 	*out = buf;
 
  out:
-	free(line);
+	git__free(line);
 	return error;
 }
 
@@ -1168,14 +1168,14 @@ static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_val
 			if (error != GIT_SUCCESS)
 			{
 				*var_value = NULL;
-				free(*var_name);
+				git__free(*var_name);
 			}
 			goto out;
 		}
 
-		tmp = strdup(value_start);
+		tmp = git__strdup(value_start);
 		if (tmp == NULL) {
-			free(*var_name);
+			git__free(*var_name);
 			*var_value = NULL;
 			error = GIT_ENOMEM;
 			goto out;
@@ -1188,6 +1188,6 @@ static int parse_variable(diskfile_backend *cfg, char **var_name, char **var_val
 	}
 
  out:
-	free(line);
+	git__free(line);
 	return error;
 }
diff --git a/src/delta-apply.c b/src/delta-apply.c
index e1fb15b..3e40bf8 100644
--- a/src/delta-apply.c
+++ b/src/delta-apply.c
@@ -109,7 +109,7 @@ int git__delta_apply(
 	return GIT_SUCCESS;
 
 fail:
-	free(out->data);
+	git__free(out->data);
 	out->data = NULL;
 	return git__throw(GIT_ERROR, "Failed to apply delta");
 }
diff --git a/src/errors.c b/src/errors.c
index 60d7746..18afff3 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -70,9 +70,9 @@ void git___rethrow(const char *msg, ...)
 	vsnprintf(new_error, sizeof(new_error), msg, va);
 	va_end(va);
 
-	old_error = strdup(g_last_error);
+	old_error = git__strdup(g_last_error);
 	snprintf(g_last_error, sizeof(g_last_error), "%s \n	- %s", new_error, old_error);
-	free(old_error);
+	git__free(old_error);
 }
 
 void git___throw(const char *msg, ...)
diff --git a/src/filebuf.c b/src/filebuf.c
index 1a98e3f..3f57d29 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -63,13 +63,13 @@ void git_filebuf_cleanup(git_filebuf *file)
 	if (file->digest)
 		git_hash_free_ctx(file->digest);
 
-	free(file->buffer);
-	free(file->z_buf);
+	git__free(file->buffer);
+	git__free(file->z_buf);
 
 	deflateEnd(&file->zs);
 
-	free(file->path_original);
-	free(file->path_lock);
+	git__free(file->path_original);
+	git__free(file->path_lock);
 }
 
 GIT_INLINE(int) flush_buffer(git_filebuf *file)
@@ -248,7 +248,7 @@ int git_filebuf_hash(git_oid *oid, git_filebuf *file)
 
 int git_filebuf_commit_at(git_filebuf *file, const char *path)
 {
-	free(file->path_original);
+	git__free(file->path_original);
 	file->path_original = git__strdup(path);
 	if (file->path_original == NULL)
 		return GIT_ENOMEM;
@@ -368,12 +368,12 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...)
 	va_end(arglist);
 
 	if (len < 0) {
-		free(tmp_buffer);
+		git__free(tmp_buffer);
 		return git__throw(GIT_EOSERR, "Failed to format string");
 	}
 
 	error = git_filebuf_write(file, tmp_buffer, len);
-	free(tmp_buffer);
+	git__free(tmp_buffer);
 
 	return error;
 }
diff --git a/src/fileops.c b/src/fileops.c
index 203cce0..c8de8d8 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -181,7 +181,7 @@ int git_futils_readbuffer_updated(git_fbuffer *obj, const char *path, time_t *mt
 
 	if (p_read(fd, buff, len) < 0) {
 		p_close(fd);
-		free(buff);
+		git__free(buff);
 		return git__throw(GIT_ERROR, "Failed to read file `%s`", path);
 	}
 	buff[len] = '\0';
@@ -207,7 +207,7 @@ int git_futils_readbuffer(git_fbuffer *obj, const char *path)
 void git_futils_freebuffer(git_fbuffer *obj)
 {
 	assert(obj);
-	free(obj->data);
+	git__free(obj->data);
 	obj->data = NULL;
 }
 
@@ -326,7 +326,7 @@ int git_futils_mkdir_r(const char *path, int mode)
 			error = GIT_SUCCESS;
 	}
 
-	free(path_copy);
+	git__free(path_copy);
 
 	if (error < GIT_SUCCESS)
 		return git__throw(error, "Failed to recursively create `%s` tree structure", path);
diff --git a/src/hash.c b/src/hash.c
index ff85ca9..56063cc 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -32,7 +32,7 @@ git_hash_ctx *git_hash_new_ctx(void)
 
 void git_hash_free_ctx(git_hash_ctx *ctx)
 {
-	free(ctx);
+	git__free(ctx);
 }
 
 void git_hash_init(git_hash_ctx *ctx)
diff --git a/src/hashtable.c b/src/hashtable.c
index 1382eab..15d1739 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -39,17 +39,17 @@ static int resize_to(git_hashtable *self, size_t new_size)
 			self->is_resizing = 0;
 		else {
 			new_size *= 2;
-			free(self->nodes);
+			git__free(self->nodes);
 		}
 	} while(self->is_resizing);
 
-	free(old_nodes);
+	git__free(old_nodes);
 	return GIT_SUCCESS;
 }
 
 static int set_size(git_hashtable *self, size_t new_size)
 {
-	self->nodes = realloc(self->nodes, new_size * sizeof(git_hashtable_node));
+	self->nodes = git__realloc(self->nodes, new_size * sizeof(git_hashtable_node));
 	if (self->nodes == NULL)
 		return GIT_ENOMEM;
 
@@ -156,8 +156,8 @@ void git_hashtable_free(git_hashtable *self)
 {
 	assert(self);
 
-	free(self->nodes);
-	free(self);
+	git__free(self->nodes);
+	git__free(self);
 }
 
 
diff --git a/src/index.c b/src/index.c
index 7bf5daf..9ace951 100644
--- a/src/index.c
+++ b/src/index.c
@@ -138,7 +138,7 @@ static int index_initialize(git_index **index_out, git_repository *owner, const 
 
 	index->index_file_path = git__strdup(index_path);
 	if (index->index_file_path == NULL) {
-		free(index);
+		git__free(index);
 		return GIT_ENOMEM;
 	}
 
@@ -179,8 +179,8 @@ void git_index_free(git_index *index)
 	git_vector_free(&index->entries);
 	git_vector_free(&index->unmerged);
 
-	free(index->index_file_path);
-	free(index);
+	git__free(index->index_file_path);
+	git__free(index);
 }
 
 void git_index_clear(git_index *index)
@@ -192,15 +192,15 @@ void git_index_clear(git_index *index)
 	for (i = 0; i < index->entries.length; ++i) {
 		git_index_entry *e;
 		e = git_vector_get(&index->entries, i);
-		free(e->path);
-		free(e);
+		git__free(e->path);
+		git__free(e);
 	}
 
 	for (i = 0; i < index->unmerged.length; ++i) {
 		git_index_entry_unmerged *e;
 		e = git_vector_get(&index->unmerged, i);
-		free(e->path);
-		free(e);
+		git__free(e->path);
+		git__free(e);
 	}
 
 	git_vector_clear(&index->entries);
@@ -334,7 +334,7 @@ static int index_entry_init(git_index_entry **entry_out, git_index *index, const
 	entry->flags |= (stage << GIT_IDXENTRY_STAGESHIFT);
 	entry->path = git__strdup(rel_path);
 	if (entry->path == NULL) {
-		free(entry);
+		git__free(entry);
 		return GIT_ENOMEM;
 	}
 
@@ -364,8 +364,8 @@ static void index_entry_free(git_index_entry *entry)
 {
 	if (!entry)
 		return;
-	free(entry->path);
-	free(entry);
+	git__free(entry->path);
+	git__free(entry);
 }
 
 static int index_insert(git_index *index, git_index_entry *entry, int replace)
@@ -416,8 +416,8 @@ static int index_insert(git_index *index, git_index_entry *entry, int replace)
 
 	/* exists, replace it */
 	entry_array = (git_index_entry **) index->entries.contents;
-	free(entry_array[position]->path);
-	free(entry_array[position]);
+	git__free(entry_array[position]->path);
+	git__free(entry_array[position]);
 	entry_array[position] = entry;
 
 	return GIT_SUCCESS;
diff --git a/src/indexer.c b/src/indexer.c
index d5f605f..a09353a 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -367,7 +367,7 @@ int git_indexer_run(git_indexer *idx, git_indexer_stats *stats)
 			idx->fanout[i]++;
 		}
 
-		free(obj.data);
+		git__free(obj.data);
 
 		stats->processed = ++processed;
 	}
@@ -390,12 +390,12 @@ void git_indexer_free(git_indexer *idx)
 
 	p_close(idx->pack->mwf.fd);
 	git_vector_foreach(&idx->objects, i, e)
-		free(e);
+		git__free(e);
 	git_vector_free(&idx->objects);
 	git_vector_foreach(&idx->pack->cache, i, pe)
-		free(pe);
+		git__free(pe);
 	git_vector_free(&idx->pack->cache);
-	free(idx->pack);
-	free(idx);
+	git__free(idx->pack);
+	git__free(idx);
 }
 
diff --git a/src/mwindow.c b/src/mwindow.c
index e53477e..126268f 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -69,7 +69,7 @@ void git_mwindow_free_all(git_mwindow_file *mwf)
 		git_futils_mmap_free(&w->window_map);
 
 		mwf->windows = w->next;
-		free(w);
+		git__free(w);
 	}
 }
 
@@ -139,7 +139,7 @@ static int git_mwindow_close_lru(git_mwindow_file *mwf)
 		else
 			*list = lru_w->next;
 
-		free(lru_w);
+		git__free(lru_w);
 		ctl.open_windows--;
 
 		return GIT_SUCCESS;
@@ -191,7 +191,7 @@ static git_mwindow *new_window(git_mwindow_file *mwf, git_file fd, git_off_t siz
 	return w;
 
 cleanup:
-	free(w);
+	git__free(w);
 	return NULL;
 }
 
diff --git a/src/netops.c b/src/netops.c
index dad296a..73375d7 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -190,7 +190,7 @@ int gitno_extract_host_and_port(char **host, char **port, const char *url, const
 	delim = colon == NULL ? slash : colon;
 	*host = git__strndup(url, delim - url);
 	if (*host == NULL) {
-		free(*port);
+		git__free(*port);
 		error = GIT_ENOMEM;
 	}
 
diff --git a/src/object.c b/src/object.c
index edc2d80..c84e94b 100644
--- a/src/object.c
+++ b/src/object.c
@@ -213,7 +213,7 @@ void git_object__free(void *_obj)
 		break;
 
 	default:
-		free(object);
+		git__free(object);
 		break;
 	}
 }
diff --git a/src/odb.c b/src/odb.c
index 60789cf..69fdba0 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -83,8 +83,8 @@ static void free_odb_object(void *o)
 	git_odb_object *object = (git_odb_object *)o;
 
 	if (object != NULL) {
-		free(object->raw.data);
-		free(object);
+		git__free(object->raw.data);
+		git__free(object);
 	}
 }
 
@@ -205,8 +205,8 @@ static void fake_wstream__free(git_odb_stream *_stream)
 {
 	fake_wstream *stream = (fake_wstream *)_stream;
 
-	free(stream->buffer);
-	free(stream);
+	git__free(stream->buffer);
+	git__free(stream);
 }
 
 static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend, size_t size, git_otype type)
@@ -221,7 +221,7 @@ static int init_fake_wstream(git_odb_stream **stream_p, git_odb_backend *backend
 	stream->type = type;
 	stream->buffer = git__malloc(size);
 	if (stream->buffer == NULL) {
-		free(stream);
+		git__free(stream);
 		return GIT_ENOMEM;
 	}
 
@@ -265,12 +265,12 @@ int git_odb_new(git_odb **out)
 
 	error = git_cache_init(&db->cache, GIT_DEFAULT_CACHE_SIZE, &free_odb_object);
 	if (error < GIT_SUCCESS) {
-		free(db);
+		git__free(db);
 		return git__rethrow(error, "Failed to create object database");
 	}
 
 	if ((error = git_vector_init(&db->backends, 4, backend_sort_cmp)) < GIT_SUCCESS) {
-		free(db);
+		git__free(db);
 		return git__rethrow(error, "Failed to create object database");
 	}
 
@@ -296,7 +296,7 @@ static int add_backend_internal(git_odb *odb, git_odb_backend *backend, int prio
 	internal->is_alternate = is_alternate;
 
 	if (git_vector_insert(&odb->backends, internal) < 0) {
-		free(internal);
+		git__free(internal);
 		return GIT_ENOMEM;
 	}
 
@@ -421,14 +421,14 @@ void git_odb_close(git_odb *db)
 		git_odb_backend *backend = internal->backend;
 
 		if (backend->free) backend->free(backend);
-		else free(backend);
+		else git__free(backend);
 
-		free(internal);
+		git__free(internal);
 	}
 
 	git_vector_free(&db->backends);
 	git_cache_free(&db->cache);
-	free(db);
+	git__free(db);
 }
 
 int git_odb_exists(git_odb *db, const git_oid *id)
diff --git a/src/odb_loose.c b/src/odb_loose.c
index dbfe18b..538fbc9 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -277,7 +277,7 @@ static void *inflate_tail(z_stream *s, void *hb, size_t used, obj_hdr *hdr)
 	else {
 		set_stream_output(s, buf + used, hdr->size - used);
 		if (finish_inflate(s)) {
-			free(buf);
+			git__free(buf);
 			return NULL;
 		}
 	}
@@ -317,7 +317,7 @@ static int inflate_packlike_loose_disk_obj(git_rawobj *out, git_fbuffer *obj)
 	in = ((unsigned char *)obj->data) + used;
 	len = obj->len - used;
 	if (inflate_buffer(in, len, buf, hdr.size)) {
-		free(buf);
+		git__free(buf);
 		return git__throw(GIT_ERROR, "Failed to inflate loose object. Could not inflate buffer");
 	}
 	buf[hdr.size] = '\0';
@@ -686,7 +686,7 @@ static void loose_backend__stream_free(git_odb_stream *_stream)
 	if (!stream->finished)
 		git_filebuf_cleanup(&stream->fbuf);
 
-	free(stream);
+	git__free(stream);
 }
 
 static int format_object_header(char *hdr, size_t n, size_t obj_len, git_otype obj_type)
@@ -739,14 +739,14 @@ static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_
 		(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT));
 
 	if (error < GIT_SUCCESS) {
-		free(stream);
+		git__free(stream);
 		return git__rethrow(error, "Failed to create loose backend stream");
 	}
 
 	error = stream->stream.write((git_odb_stream *)stream, hdr, hdrlen);
 	if (error < GIT_SUCCESS) {
 		git_filebuf_cleanup(&stream->fbuf);
-		free(stream);
+		git__free(stream);
 		return git__rethrow(error, "Failed to create loose backend stream");
 	}
 
@@ -803,8 +803,8 @@ static void loose_backend__free(git_odb_backend *_backend)
 	assert(_backend);
 	backend = (loose_backend *)_backend;
 
-	free(backend->objects_dir);
-	free(backend);
+	git__free(backend->objects_dir);
+	git__free(backend);
 }
 
 int git_odb_backend_loose(
@@ -821,7 +821,7 @@ int git_odb_backend_loose(
 
 	backend->objects_dir = git__strdup(objects_dir);
 	if (backend->objects_dir == NULL) {
-		free(backend);
+		git__free(backend);
 		return GIT_ENOMEM;
 	}
 
diff --git a/src/odb_pack.c b/src/odb_pack.c
index a8f8542..800e7b0 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -231,7 +231,7 @@ static int packfile_load__cb(void *_data, char *path)
 		return git__rethrow(error, "Failed to load packfile");
 
 	if (git_vector_insert(&backend->packs, pack) < GIT_SUCCESS) {
-		free(pack);
+		git__free(pack);
 		return GIT_ENOMEM;
 	}
 
@@ -445,8 +445,8 @@ static void pack_backend__free(git_odb_backend *_backend)
 	}
 
 	git_vector_free(&backend->packs);
-	free(backend->pack_folder);
-	free(backend);
+	git__free(backend->pack_folder);
+	git__free(backend);
 }
 
 int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
@@ -459,7 +459,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
 		return GIT_ENOMEM;
 
 	if (git_vector_init(&backend->packs, 8, packfile_sort__cb) < GIT_SUCCESS) {
-		free(backend);
+		git__free(backend);
 		return GIT_ENOMEM;
 	}
 
@@ -469,7 +469,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
 		backend->pack_folder_mtime = 0;
 
 		if (backend->pack_folder == NULL) {
-			free(backend);
+			git__free(backend);
 			return GIT_ENOMEM;
 		}
 	}
diff --git a/src/oid.c b/src/oid.c
index bbf19ea..4b30804 100644
--- a/src/oid.c
+++ b/src/oid.c
@@ -223,7 +223,7 @@ struct git_oid_shorten {
 
 static int resize_trie(git_oid_shorten *self, size_t new_size)
 {
-	self->nodes = realloc(self->nodes, new_size * sizeof(trie_node));
+	self->nodes = git__realloc(self->nodes, new_size * sizeof(trie_node));
 	if (self->nodes == NULL)
 		return GIT_ENOMEM;
 
@@ -270,7 +270,7 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length)
 	memset(os, 0x0, sizeof(git_oid_shorten));
 
 	if (resize_trie(os, 16) < GIT_SUCCESS) {
-		free(os);
+		git__free(os);
 		return NULL;
 	}
 
@@ -282,8 +282,8 @@ git_oid_shorten *git_oid_shorten_new(size_t min_length)
 
 void git_oid_shorten_free(git_oid_shorten *os)
 {
-	free(os->nodes);
-	free(os);
+	git__free(os->nodes);
+	git__free(os);
 }
 
 
diff --git a/src/pack.c b/src/pack.c
index 2516bea..429bb5e 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -181,7 +181,7 @@ static int pack_index_open(struct git_pack_file *p)
 	strcpy(idx_name + strlen(idx_name) - strlen(".pack"), ".idx");
 
 	error = pack_index_check(idx_name, p);
-	free(idx_name);
+	git__free(idx_name);
 
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to open index");
 }
@@ -297,7 +297,7 @@ static int packfile_unpack_delta(
 
 	error = packfile_unpack_compressed(&delta, p, w_curs, curpos, delta_size, delta_type);
 	if (error < GIT_SUCCESS) {
-		free(base.data);
+		git__free(base.data);
 		return git__rethrow(error, "Corrupted delta");
 	}
 
@@ -306,8 +306,8 @@ static int packfile_unpack_delta(
 			base.data, base.len,
 			delta.data, delta.len);
 
-	free(base.data);
-	free(delta.data);
+	git__free(base.data);
+	git__free(delta.data);
 
 	/* TODO: we might want to cache this shit. eventually */
 	//add_delta_base_cache(p, base_offset, base, base_size, *type);
@@ -390,7 +390,7 @@ int packfile_unpack_compressed(
 
 	st = inflateInit(&stream);
 	if (st != Z_OK) {
-		free(buffer);
+		git__free(buffer);
 		return git__throw(GIT_EZLIB, "Error in zlib");
 	}
 
@@ -408,7 +408,7 @@ int packfile_unpack_compressed(
 	inflateEnd(&stream);
 
 	if ((st != Z_STREAM_END) || stream.total_out != size) {
-		free(buffer);
+		git__free(buffer);
 		return git__throw(GIT_EZLIB, "Error in zlib");
 	}
 
@@ -504,8 +504,8 @@ void packfile_free(struct git_pack_file *p)
 
 	pack_index_free(p);
 
-	free(p->bad_object_sha1);
-	free(p);
+	git__free(p->bad_object_sha1);
+	git__free(p);
 }
 
 static int packfile_open(struct git_pack_file *p)
@@ -598,7 +598,7 @@ int git_packfile_check(struct git_pack_file **pack_out, const char *path)
 	 */
 	path_len -= strlen(".idx");
 	if (path_len < 1) {
-		free(p);
+		git__free(p);
 		return git__throw(GIT_ENOTFOUND, "Failed to check packfile. Wrong path name");
 	}
 
@@ -610,7 +610,7 @@ int git_packfile_check(struct git_pack_file **pack_out, const char *path)
 
 	strcpy(p->pack_name + path_len, ".pack");
 	if (p_stat(p->pack_name, &st) < GIT_SUCCESS || !S_ISREG(st.st_mode)) {
-		free(p);
+		git__free(p);
 		return git__throw(GIT_ENOTFOUND, "Failed to check packfile. File not found");
 	}
 
diff --git a/src/path.c b/src/path.c
index 2c6b76d..a8851df 100644
--- a/src/path.c
+++ b/src/path.c
@@ -144,7 +144,7 @@ char *git_path_dirname(const char *path)
 		return NULL;
 
 	if (git_path_dirname_r(dname, len, path) < GIT_SUCCESS) {
-		free(dname);
+		git__free(dname);
 		return NULL;
 	}
 
@@ -162,7 +162,7 @@ char *git_path_basename(const char *path)
 		return NULL;
 
 	if (git_path_basename_r(bname, len, path) < GIT_SUCCESS) {
-		free(bname);
+		git__free(bname);
 		return NULL;
 	}
 
diff --git a/src/pkt.c b/src/pkt.c
index 9471df2..ff8c56e 100644
--- a/src/pkt.c
+++ b/src/pkt.c
@@ -149,7 +149,7 @@ static int ref_pkt(git_pkt **out, const char *line, size_t len)
 
 out:
 	if (error < GIT_SUCCESS)
-		free(pkt);
+		git__free(pkt);
 	else
 		*out = (git_pkt *)pkt;
 
@@ -260,10 +260,10 @@ void git_pkt_free(git_pkt *pkt)
 {
 	if(pkt->type == GIT_PKT_REF) {
 		git_pkt_ref *p = (git_pkt_ref *) pkt;
-		free(p->head.name);
+		git__free(p->head.name);
 	}
 
-	free(pkt);
+	git__free(pkt);
 }
 
 int git_pkt_buffer_flush(git_buf *buf)
diff --git a/src/pqueue.c b/src/pqueue.c
index b5ddab8..80713fb 100644
--- a/src/pqueue.c
+++ b/src/pqueue.c
@@ -17,7 +17,7 @@ int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri)
 	assert(q);
 
 	/* Need to allocate n+1 elements since element 0 isn't used. */
-	if ((q->d = malloc((n + 1) * sizeof(void *))) == NULL)
+	if ((q->d = git__malloc((n + 1) * sizeof(void *))) == NULL)
 		return GIT_ENOMEM;
 
 	q->size = 1;
@@ -30,7 +30,7 @@ int git_pqueue_init(git_pqueue *q, size_t n, git_pqueue_cmp cmppri)
 
 void git_pqueue_free(git_pqueue *q)
 {
-	free(q->d);
+	git__free(q->d);
 	q->d = NULL;
 }
 
@@ -102,7 +102,7 @@ int git_pqueue_insert(git_pqueue *q, void *d)
 	/* allocate more memory if necessary */
 	if (q->size >= q->avail) {
 		newsize = q->size + q->step;
-		if ((tmp = realloc(q->d, sizeof(void *) * newsize)) == NULL)
+		if ((tmp = git__realloc(q->d, sizeof(void *) * newsize)) == NULL)
 			return GIT_ENOMEM;
 
 		q->d = tmp;
diff --git a/src/reflog.c b/src/reflog.c
index 594963c..a7e1f92 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -25,8 +25,8 @@ static int reflog_init(git_reflog **reflog, git_reference *ref)
 	log->ref_name = git__strdup(ref->name);
 
 	if (git_vector_init(&log->entries, 0, NULL) < 0) {
-		free(log->ref_name);
-		free(log);
+		git__free(log->ref_name);
+		git__free(log);
 		return GIT_ENOMEM;
 	}
 
@@ -86,8 +86,8 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
 #define seek_forward(_increase) { \
 	if (_increase >= buf_size) { \
 		if (entry->committer) \
-			free(entry->committer); \
-		free(entry); \
+			git__free(entry->committer); \
+		git__free(entry); \
 		return git__throw(GIT_ERROR, "Failed to seek forward. Buffer size exceeded"); \
 	} \
 	buf += _increase; \
@@ -101,13 +101,13 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
 		entry->committer = NULL;
 
 		if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) {
-			free(entry);
+			git__free(entry);
 			return GIT_ERROR;
 		}
 		seek_forward(GIT_OID_HEXSZ + 1);
 
 		if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) {
-			free(entry);
+			git__free(entry);
 			return GIT_ERROR;
 		}
 		seek_forward(GIT_OID_HEXSZ + 1);
@@ -120,13 +120,13 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
 
 		entry->committer = git__malloc(sizeof(git_signature));
 		if (entry->committer == NULL) {
-			free(entry);
+			git__free(entry);
 			return GIT_ENOMEM;
 		}
 
 		if ((error = git_signature__parse(entry->committer, &ptr, buf + 1, NULL, *buf)) < GIT_SUCCESS) {
-			free(entry->committer);
-			free(entry);
+			git__free(entry->committer);
+			git__free(entry);
 			return git__rethrow(error, "Failed to parse reflog. Could not parse signature");
 		}
 
@@ -164,13 +164,13 @@ void git_reflog_free(git_reflog *reflog)
 
 		git_signature_free(entry->committer);
 
-		free(entry->msg);
-		free(entry);
+		git__free(entry->msg);
+		git__free(entry);
 	}
 
 	git_vector_free(&reflog->entries);
-	free(reflog->ref_name);
-	free(reflog);
+	git__free(reflog->ref_name);
+	git__free(reflog);
 }
 
 int git_reflog_read(git_reflog **reflog, git_reference *ref)
diff --git a/src/refs.c b/src/refs.c
index fcf771b..f21ca69 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -77,12 +77,12 @@ static void reference_free(git_reference *reference)
 		return;
 
 	if (reference->name)
-		free(reference->name);
+		git__free(reference->name);
 
 	if (reference->type == GIT_REF_SYMBOLIC)
-		free(((reference_symbolic *)reference)->target);
+		git__free(((reference_symbolic *)reference)->target);
 
-	free(reference);
+	git__free(reference);
 }
 
 static int reference_create(
@@ -212,7 +212,7 @@ static int loose_parse_symbolic(git_reference *ref, git_fbuffer *file_content)
 
 	refname_start += header_len;
 
-	free(ref_sym->target);
+	git__free(ref_sym->target);
 	ref_sym->target = git__strdup(refname_start);
 	if (ref_sym->target == NULL)
 		return GIT_ENOMEM;
@@ -1203,7 +1203,7 @@ int git_reference_set_oid(git_reference *ref, const git_oid *id)
 
 		ref_old->ref.name = git__strdup(ref->name);
 		if (ref_old->ref.name == NULL) {
-			free(ref_old);
+			git__free(ref_old);
 			return GIT_ENOMEM;
 		}
 	}
@@ -1252,7 +1252,7 @@ int git_reference_set_target(git_reference *ref, const char *target)
 
 	ref_sym = (reference_symbolic *)ref;
 
-	free(ref_sym->target);
+	git__free(ref_sym->target);
 	ref_sym->target = git__strdup(target);
 	if (ref_sym->target == NULL)
 		return GIT_ENOMEM;
@@ -1381,7 +1381,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 			goto rollback;
 	}
 
-	free(ref->name);
+	git__free(ref->name);
 	ref->name = new_ref->name;
 
 	/*
@@ -1408,7 +1408,7 @@ int git_reference_rename(git_reference *ref, const char *new_name, int force)
 			goto rollback;
 
 cleanup:
-	free(old_name);
+	git__free(old_name);
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to rename reference");
 
 rollback:
diff --git a/src/refspec.c b/src/refspec.c
index ed4b5e6..e60e8f5 100644
--- a/src/refspec.c
+++ b/src/refspec.c
@@ -32,7 +32,7 @@ int git_refspec_parse(git_refspec *refspec, const char *str)
 
 	refspec->dst = git__strdup(delim + 1);
 	if (refspec->dst == NULL) {
-		free(refspec->src);
+		git__free(refspec->src);
 		refspec->src = NULL;
 		return GIT_ENOMEM;
 	}
diff --git a/src/remote.c b/src/remote.c
index a557a49..3ff08a2 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -36,7 +36,7 @@ static int refspec_parse(git_refspec *refspec, const char *str)
 
 	refspec->dst = git__strdup(delim + 1);
 	if (refspec->dst == NULL) {
-		free(refspec->src);
+		git__free(refspec->src);
 		refspec->src = NULL;
 		return GIT_ENOMEM;
 	}
@@ -68,7 +68,7 @@ int git_remote_new(git_remote **out, git_repository *repo, const char *url)
 	remote->repo = repo;
 	remote->url = git__strdup(url);
 	if (remote->url == NULL) {
-		free(remote);
+		git__free(remote);
 		return GIT_ENOMEM;
 	}
 
@@ -150,7 +150,7 @@ int git_remote_get(git_remote **out, git_config *cfg, const char *name)
 	*out = remote;
 
 cleanup:
-	free(buf);
+	git__free(buf);
 	if (error < GIT_SUCCESS)
 		git_remote_free(remote);
 
@@ -260,17 +260,17 @@ void git_remote_free(git_remote *remote)
 	if (remote == NULL)
 		return;
 
-	free(remote->fetch.src);
-	free(remote->fetch.dst);
-	free(remote->push.src);
-	free(remote->push.dst);
-	free(remote->url);
-	free(remote->name);
+	git__free(remote->fetch.src);
+	git__free(remote->fetch.dst);
+	git__free(remote->push.src);
+	git__free(remote->push.dst);
+	git__free(remote->url);
+	git__free(remote->name);
 	if (remote->transport != NULL) {
 		if (remote->transport->connected)
 			remote->transport->close(remote->transport);
 
 		remote->transport->free(remote->transport);
 	}
-	free(remote);
+	git__free(remote);
 }
diff --git a/src/repository.c b/src/repository.c
index 328bc0d..33a3f27 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -168,12 +168,12 @@ static git_repository *repository_alloc(void)
 
 	error = git_cache_init(&repo->objects, GIT_DEFAULT_CACHE_SIZE, &git_object__free);
 	if (error < GIT_SUCCESS) {
-		free(repo);
+		git__free(repo);
 		return NULL;
 	}
 
 	if (git_repository__refcache_init(&repo->references) < GIT_SUCCESS) {
-		free(repo);
+		git__free(repo);
 		return NULL;
 	}
 
@@ -467,13 +467,13 @@ static int read_gitfile(char *path_out, const char *file_path, const char *base_
 
 static void git_repository__free_dirs(git_repository *repo)
 {
-	free(repo->path_workdir);
+	git__free(repo->path_workdir);
 	repo->path_workdir = NULL;
-	free(repo->path_index);
+	git__free(repo->path_index);
 	repo->path_index = NULL;
-	free(repo->path_repository);
+	git__free(repo->path_repository);
 	repo->path_repository = NULL;
-	free(repo->path_odb);
+	git__free(repo->path_odb);
 	repo->path_odb = NULL;
 }
 
@@ -489,7 +489,7 @@ void git_repository_free(git_repository *repo)
 	if (repo->db != NULL)
 		git_odb_close(repo->db);
 
-	free(repo);
+	git__free(repo);
 }
 
 int git_repository_discover(char *repository_path, size_t size, const char *start_path, int across_fs, const char *ceiling_dirs)
diff --git a/src/revwalk.c b/src/revwalk.c
index 2d70d40..7e31650 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -68,7 +68,7 @@ static void commit_list_free(commit_list **list_p)
 	while (list) {
 		commit_list *temp = list;
 		list = temp->next;
-		free(temp);
+		git__free(temp);
 	}
 
 	*list_p = NULL;
@@ -81,7 +81,7 @@ static commit_object *commit_list_pop(commit_list **stack)
 
 	if (top) {
 		*stack = top->next;
-		free(top);
+		git__free(top);
 	}
 	return item;
 }
@@ -156,7 +156,7 @@ static commit_object *commit_lookup(git_revwalk *walk, const git_oid *oid)
 	git_oid_cpy(&commit->oid, oid);
 
 	if (git_hashtable_insert(walk->commits, &commit->oid, commit) < GIT_SUCCESS) {
-		free(commit);
+		git__free(commit);
 		return NULL;
 	}
 
@@ -442,7 +442,7 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
 			(git_hash_keyeq_ptr)git_oid_cmp);
 
 	if (walk->commits == NULL) {
-		free(walk);
+		git__free(walk);
 		return GIT_ENOMEM;
 	}
 
@@ -475,17 +475,17 @@ void git_revwalk_free(git_revwalk *walk)
 	 * make sure it's being free'd */
 	GIT_HASHTABLE_FOREACH(walk->commits, _unused, commit, {
 		if (commit->out_degree > PARENTS_PER_COMMIT)
-			free(commit->parents);
+			git__free(commit->parents);
 	});
 
 	git_hashtable_free(walk->commits);
 	git_pqueue_free(&walk->iterator_time);
 
 	for (i = 0; i < walk->memory_alloc.length; ++i)
-		free(git_vector_get(&walk->memory_alloc, i));
+		git__free(git_vector_get(&walk->memory_alloc, i));
 
 	git_vector_free(&walk->memory_alloc);
-	free(walk);
+	git__free(walk);
 }
 
 git_repository *git_revwalk_repository(git_revwalk *walk)
diff --git a/src/signature.c b/src/signature.c
index 388e8d9..832d643 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -15,9 +15,9 @@ void git_signature_free(git_signature *sig)
 	if (sig == NULL)
 		return;
 
-	free(sig->name);
-	free(sig->email);
-	free(sig);
+	git__free(sig->name);
+	git__free(sig->email);
+	git__free(sig);
 }
 
 static const char *skip_leading_spaces(const char *buffer, const char *buffer_end)
diff --git a/src/status.c b/src/status.c
index c53c4ac..1fc3794 100644
--- a/src/status.c
+++ b/src/status.c
@@ -229,7 +229,7 @@ static int store_if_changed(struct status_st *st, struct status_entry *e)
 			return git__throw(error, "Failed to process the file '%s'. It doesn't exist in the workdir, in the HEAD nor in the index", e->path);
 
 	if (e->status_flags == GIT_STATUS_CURRENT) {
-		free(e);
+		git__free(e);
 		return GIT_SUCCESS;
 	}
 
@@ -446,7 +446,7 @@ int git_status_foreach(git_repository *repo, int (*callback)(const char *, unsig
 				error = git__rethrow(error, "Failed to determine statuses. User callback failed");
 		}
 
-		free(e);
+		git__free(e);
 	}
 
 exit:
@@ -550,7 +550,7 @@ int git_status_file(unsigned int *status_flags, git_repository *repo, const char
 
 exit:
 	git_tree_close(tree);
-	free(e);
+	git__free(e);
 	return error;
 }
 
@@ -606,7 +606,7 @@ static int alphasorted_dirent_cb(void *state, char *full_path)
 		return GIT_ENOMEM;
 
 	if (git_vector_insert(entry_names, entry) < GIT_SUCCESS) {
-		free(entry);
+		git__free(entry);
 		return GIT_ENOMEM;
 	}
 
@@ -639,7 +639,7 @@ static int alphasorted_futils_direach(
 			error = fn(arg, entry->path);
 		}
 
-		free(entry);
+		git__free(entry);
 	}
 
 	git_vector_free(&entry_names);
diff --git a/src/tag.c b/src/tag.c
index ba75104..0bdca93 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -16,9 +16,9 @@
 void git_tag__free(git_tag *tag)
 {
 	git_signature_free(tag->tagger);
-	free(tag->message);
-	free(tag->tag_name);
-	free(tag);
+	git__free(tag->message);
+	git__free(tag->tag_name);
+	git__free(tag);
 }
 
 const git_oid *git_tag_id(git_tag *c)
@@ -341,8 +341,8 @@ int git_tag_create_frombuffer(git_oid *oid, git_repository *repo, const char *bu
 		error = git_reference_set_oid(new_ref, oid);
 
 	git_signature_free(tag.tagger);
-	free(tag.tag_name);
-	free(tag.message);
+	git__free(tag.tag_name);
+	git__free(tag.message);
 
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to create tag");
 }
diff --git a/src/transports/git.c b/src/transports/git.c
index 4898078..c201452 100644
--- a/src/transports/git.c
+++ b/src/transports/git.c
@@ -109,8 +109,8 @@ static int do_connect(transport_git *t, const char *url)
 	error = send_request(s, NULL, url);
 	t->socket = s;
 
-	free(host);
-	free(port);
+	git__free(host);
+	git__free(port);
 
 	if (error < GIT_SUCCESS && s > 0)
 		close(s);
@@ -357,11 +357,11 @@ static int git_negotiate_fetch(git_transport *transport, git_repository *repo, g
 				gitno_consume(buf, line_end);
 
 				if (pkt->type == GIT_PKT_ACK) {
-					free(pkt);
+					git__free(pkt);
 					error = GIT_SUCCESS;
 					goto done;
 				} else if (pkt->type == GIT_PKT_NAK) {
-					free(pkt);
+					git__free(pkt);
 					break;
 				} else {
 					error = git__throw(GIT_ERROR, "Got unexpected pkt type");
@@ -424,12 +424,12 @@ static int git_download_pack(char **out, git_transport *transport, git_repositor
 				return error;
 
 			if (pkt->type == GIT_PKT_PACK) {
-				free(pkt);
+				git__free(pkt);
 				return git_fetch__download_pack(out, buf->data, buf->offset, t->socket, repo);
 			}
 
 			/* For now we don't care about anything */
-			free(pkt);
+			git__free(pkt);
 			gitno_consume(buf, line_end);
 		}
 
@@ -475,9 +475,9 @@ static void git_free(git_transport *transport)
 	}
 
 	git_vector_free(refs);
-	free(t->heads);
-	free(t->parent.url);
-	free(t);
+	git__free(t->heads);
+	git__free(t->parent.url);
+	git__free(t);
 }
 
 int git_transport_git(git_transport **out)
diff --git a/src/transports/http.c b/src/transports/http.c
index 680354b..a1a73cb 100644
--- a/src/transports/http.c
+++ b/src/transports/http.c
@@ -389,18 +389,18 @@ static int on_body_parse_response(http_parser *parser, const char *str, size_t l
 		git_buf_consume(buf, line_end);
 
 		if (pkt->type == GIT_PKT_PACK) {
-			free(pkt);
+			git__free(pkt);
 			t->pack_ready = 1;
 			return 0;
 		}
 
 		if (pkt->type == GIT_PKT_NAK) {
-			free(pkt);
+			git__free(pkt);
 			return 0;
 		}
 
 		if (pkt->type != GIT_PKT_ACK) {
-			free(pkt);
+			git__free(pkt);
 			continue;
 		}
 
@@ -749,13 +749,13 @@ static void http_free(git_transport *transport)
 	}
 	git_vector_free(common);
 	git_buf_free(&t->buf);
-	free(t->heads);
-	free(t->content_type);
-	free(t->host);
-	free(t->port);
-	free(t->service);
-	free(t->parent.url);
-	free(t);
+	git__free(t->heads);
+	git__free(t->content_type);
+	git__free(t->host);
+	git__free(t->port);
+	git__free(t->service);
+	git__free(t->parent.url);
+	git__free(t);
 }
 
 int git_transport_http(git_transport **out)
diff --git a/src/transports/local.c b/src/transports/local.c
index 3f47e9b..e096804 100644
--- a/src/transports/local.c
+++ b/src/transports/local.c
@@ -113,8 +113,8 @@ static int add_ref(const char *name, git_repository *repo, git_vector *vec)
  out:
 	git_object_close(obj);
 	if (error < GIT_SUCCESS) {
-		free(head->name);
-		free(head);
+		git__free(head->name);
+		git__free(head);
 	}
 	return error;
 }
@@ -190,16 +190,16 @@ static void local_free(git_transport *transport)
 
 	if (t->refs != NULL) {
 		git_vector_foreach (vec, i, h) {
-			free(h->name);
-			free(h);
+			git__free(h->name);
+			git__free(h);
 		}
 		git_vector_free(vec);
-		free(vec);
+		git__free(vec);
 	}
 
 	git_repository_free(t->repo);
-	free(t->parent.url);
-	free(t);
+	git__free(t->parent.url);
+	git__free(t);
 }
 
 /**************
diff --git a/src/tree-cache.c b/src/tree-cache.c
index 5a32575..ea8b7bf 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -196,6 +196,6 @@ void git_tree_cache_free(git_tree_cache *tree)
 	for (i = 0; i < tree->children_count; ++i)
 		git_tree_cache_free(tree->children[i]);
 
-	free(tree->children);
-	free(tree);
+	git__free(tree->children);
+	git__free(tree);
 }
diff --git a/src/tree.c b/src/tree.c
index 77034fa..6efedcf 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -130,12 +130,12 @@ void git_tree__free(git_tree *tree)
 		git_tree_entry *e;
 		e = git_vector_get(&tree->entries, i);
 
-		free(e->filename);
-		free(e);
+		git__free(e->filename);
+		git__free(e);
 	}
 
 	git_vector_free(&tree->entries);
-	free(tree);
+	git__free(tree);
 }
 
 const git_oid *git_tree_id(git_tree *c)
@@ -378,7 +378,7 @@ static int write_tree(git_oid *oid, git_index *index, const char *dirname, unsig
 				last_comp = subdir;
 			}
 			error = append_entry(bld, last_comp, &sub_oid, S_IFDIR);
-			free(subdir);
+			git__free(subdir);
 			if (error < GIT_SUCCESS) {
 				error = git__rethrow(error, "Failed to insert dir");
 				goto cleanup;
@@ -441,7 +441,7 @@ int git_treebuilder_create(git_treebuilder **builder_p, const git_tree *source)
 		source_entries = source->entries.length;
 
 	if (git_vector_init(&bld->entries, source_entries, entry_sort_cmp) < GIT_SUCCESS) {
-		free(bld);
+		git__free(bld);
 		return GIT_ENOMEM;
 	}
 
@@ -596,8 +596,8 @@ void git_treebuilder_clear(git_treebuilder *bld)
 
 	for (i = 0; i < bld->entries.length; ++i) {
 		git_tree_entry *e = bld->entries.contents[i];
-		free(e->filename);
-		free(e);
+		git__free(e->filename);
+		git__free(e);
 	}
 
 	git_vector_clear(&bld->entries);
@@ -607,7 +607,7 @@ void git_treebuilder_free(git_treebuilder *bld)
 {
 	git_treebuilder_clear(bld);
 	git_vector_free(&bld->entries);
-	free(bld);
+	git__free(bld);
 }
 
 static int tree_frompath(git_tree **parent_out, git_tree *root, const char *treeentry_path, int offset)
diff --git a/src/tsort.c b/src/tsort.c
index 5dd99cc..df230b5 100644
--- a/src/tsort.c
+++ b/src/tsort.c
@@ -178,7 +178,7 @@ static int check_invariant(struct tsort_run *stack, int stack_curr)
 static int resize(struct tsort_store *store, size_t new_size)
 {
 	if (store->alloc < new_size) {
-		void **tempstore = realloc(store->storage, new_size * sizeof(void *));
+		void **tempstore = git__realloc(store->storage, new_size * sizeof(void *));
 
 		/**
 		 * Do not propagate on OOM; this will abort the sort and
@@ -319,7 +319,7 @@ static ssize_t collapse(void **dst, struct tsort_run *stack, ssize_t stack_curr,
 			stack_curr--; \
 		} \
 		if (store->storage != NULL) {\
-			free(store->storage);\
+			git__free(store->storage);\
 			store->storage = NULL;\
 		}\
 		return;\
diff --git a/src/util.c b/src/util.c
index c81ed2d..b3af7ff 100644
--- a/src/util.c
+++ b/src/util.c
@@ -26,9 +26,9 @@ void git_strarray_free(git_strarray *array)
 {
 	size_t i;
 	for (i = 0; i < array->count; ++i)
-		free(array->strings[i]);
+		git__free(array->strings[i]);
 
-	free(array->strings);
+	git__free(array->strings);
 }
 
 int git__fnmatch(const char *pattern, const char *name, int flags)
diff --git a/src/util.h b/src/util.h
index 4de91b4..fbf9012 100644
--- a/src/util.h
+++ b/src/util.h
@@ -72,6 +72,8 @@ GIT_INLINE(void *) git__realloc(void *ptr, size_t size)
 	return new_ptr;
 }
 
+#define git__free(ptr) free(ptr)
+
 extern int git__prefixcmp(const char *str, const char *prefix);
 extern int git__suffixcmp(const char *str, const char *suffix);
 
diff --git a/src/vector.c b/src/vector.c
index 8b20bb8..123aae8 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -18,7 +18,7 @@ static int resize_vector(git_vector *v)
 	if (v->_alloc_size < minimum_size)
 		v->_alloc_size = minimum_size;
 
-	v->contents = realloc(v->contents, v->_alloc_size * sizeof(void *));
+	v->contents = git__realloc(v->contents, v->_alloc_size * sizeof(void *));
 	if (v->contents == NULL)
 		return GIT_ENOMEM;
 
@@ -29,7 +29,7 @@ static int resize_vector(git_vector *v)
 void git_vector_free(git_vector *v)
 {
 	assert(v);
-	free(v->contents);
+	git__free(v->contents);
 }
 
 int git_vector_init(git_vector *v, unsigned int initial_size, git_vector_cmp cmp)
diff --git a/src/win32/dir.c b/src/win32/dir.c
index fea74b4..01aaaaa 100644
--- a/src/win32/dir.c
+++ b/src/win32/dir.c
@@ -39,18 +39,18 @@ git__DIR *git__opendir(const char *dir)
 
 	new->dir = git__malloc(strlen(dir)+1);
 	if (!new->dir) {
-		free(new);
+		git__free(new);
 		return NULL;
 	}
 	strcpy(new->dir, dir);
 
 	filter_w = gitwin_to_utf16(filter);
 	new->h = FindFirstFileW(filter_w, &new->f);
-	free(filter_w);
+	git__free(filter_w);
 
 	if (new->h == INVALID_HANDLE_VALUE) {
-		free(new->dir);
-		free(new);
+		git__free(new->dir);
+		git__free(new);
 		return NULL;
 	}
 	new->first = 1;
@@ -93,7 +93,7 @@ void git__rewinddir(git__DIR *d)
 		if (init_filter(filter, sizeof(filter), d->dir)) {
 			filter_w = gitwin_to_utf16(filter);
 			d->h = FindFirstFileW(filter_w, &d->f);
-			free(filter_w);
+			git__free(filter_w);
 
 			if (d->h != INVALID_HANDLE_VALUE)
 				d->first = 1;
@@ -107,8 +107,8 @@ int git__closedir(git__DIR *d)
 		if (d->h != INVALID_HANDLE_VALUE)
 			FindClose(d->h);
 		if (d->dir)
-			free(d->dir);
-		free(d);
+			git__free(d->dir);
+		git__free(d);
 	}
 	return 0;
 }
diff --git a/src/win32/posix.h b/src/win32/posix.h
index 3774e78..5dcdb5f 100644
--- a/src/win32/posix.h
+++ b/src/win32/posix.h
@@ -26,7 +26,7 @@ GIT_INLINE(int) p_mkdir(const char *path, int GIT_UNUSED(mode))
 
 	GIT_UNUSED_ARG(mode)
 
-	free(buf);
+	git__free(buf);
 	return ret;
 }
 
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index a05aafc..ed7450a 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -20,7 +20,7 @@ int p_unlink(const char *path)
 	buf = gitwin_to_utf16(path);
 	_wchmod(buf, 0666);
 	ret = _wunlink(buf);
-	free(buf);
+	git__free(buf);
 
 	return ret;
 }
@@ -86,11 +86,11 @@ static int do_lstat(const char *file_name, struct stat *buf)
 		buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
 		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
 
-		free(fbuf);
+		git__free(fbuf);
 		return GIT_SUCCESS;
 	}
 
-	free(fbuf);
+	git__free(fbuf);
 
 	switch (GetLastError()) {
 		case ERROR_ACCESS_DENIED:
@@ -171,7 +171,7 @@ int p_readlink(const char *link, char *target, size_t target_len)
 			FILE_FLAG_BACKUP_SEMANTICS, // normal file
 			NULL);					// no attr. template
 
-	free(link_w);
+	git__free(link_w);
 
 	if (hFile == INVALID_HANDLE_VALUE)
 		return GIT_EOSERR;
@@ -184,17 +184,17 @@ int p_readlink(const char *link, char *target, size_t target_len)
 
 	dwRet = pGetFinalPath(hFile, target_w, target_len, 0x0);
 	if (dwRet >= target_len) {
-		free(target_w);
+		git__free(target_w);
 		CloseHandle(hFile);
 		return GIT_ENOMEM;
 	}
 
 	if (!WideCharToMultiByte(CP_UTF8, 0, target_w, -1, target, target_len * sizeof(char), NULL, NULL)) {
-		free(target_w);
+		git__free(target_w);
 		return GIT_EOSERR;
 	}
 
-	free(target_w);
+	git__free(target_w);
 	CloseHandle(hFile);
 
 	if (dwRet > 4) {
@@ -226,7 +226,7 @@ int p_open(const char *path, int flags)
 	wchar_t* buf = gitwin_to_utf16(path);
 	fd = _wopen(buf, flags | _O_BINARY);
 
-	free(buf);
+	git__free(buf);
 	return fd;
 }
 
@@ -236,7 +236,7 @@ int p_creat(const char *path, int mode)
 	wchar_t* buf = gitwin_to_utf16(path);
 	fd = _wopen(buf, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, mode);
 
-	free(buf);
+	git__free(buf);
 	return fd;
 }
 
@@ -246,11 +246,11 @@ int p_getcwd(char *buffer_out, size_t size)
 	_wgetcwd(buf, (int)size);
 
 	if (!WideCharToMultiByte(CP_UTF8, 0, buf, -1, buffer_out, size, NULL, NULL)) {
-		free(buf);
+		git__free(buf);
 		return GIT_EOSERR;
 	}
 
-	free(buf);
+	git__free(buf);
 	return GIT_SUCCESS;
 }
 
@@ -264,7 +264,7 @@ int p_chdir(const char* path)
 	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wchdir(buf);
 
-	free(buf);
+	git__free(buf);
 	return ret;
 }
 
@@ -273,7 +273,7 @@ int p_chmod(const char* path, int mode)
 	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wchmod(buf, mode);
 
-	free(buf);
+	git__free(buf);
 	return ret;
 }
 
@@ -282,7 +282,7 @@ int p_rmdir(const char* path)
 	wchar_t* buf = gitwin_to_utf16(path);
 	int ret = _wrmdir(buf);
 
-	free(buf);
+	git__free(buf);
 	return ret;
 }
 
@@ -294,7 +294,7 @@ int p_hide_directory__w32(const char *path)
 	error = SetFileAttributesW(buf, FILE_ATTRIBUTE_HIDDEN) != 0 ?
 		GIT_SUCCESS : GIT_ERROR; /* MSDN states a "non zero" value indicates a success */
 
-	free(buf);
+	git__free(buf);
 
 	if (error < GIT_SUCCESS)
 		error = git__throw(GIT_EOSERR, "Failed to hide directory '%s'", path);
@@ -314,21 +314,21 @@ char *p_realpath(const char *orig_path, char *buffer)
 	}
 
 	ret = GetFullPathNameW(orig_path_w, GIT_PATH_MAX, buffer_w, NULL);
-	free(orig_path_w);
+	git__free(orig_path_w);
 
 	if (!ret || ret > GIT_PATH_MAX) {
-		free(buffer_w);
-		if (alloc) free(buffer);
+		git__free(buffer_w);
+		if (alloc) git__free(buffer);
 
 		return NULL;
 	}
 
 	if (!WideCharToMultiByte(CP_UTF8, 0, buffer_w, -1, buffer, GIT_PATH_MAX, NULL, NULL)) {
-		free(buffer_w);
-		if (alloc) free(buffer);
+		git__free(buffer_w);
+		if (alloc) git__free(buffer);
 	}
 	
-	free(buffer_w);
+	git__free(buffer_w);
 	git_path_mkposix(buffer);
 	return buffer;
 }
@@ -384,7 +384,7 @@ int p_access(const char* path, int mode)
 	int ret;
 
 	ret = _waccess(buf, mode);
-	free(buf);
+	git__free(buf);
 
 	return ret;
 }
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index cb60783..b41c78f 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -50,7 +50,7 @@ wchar_t* gitwin_to_utf16(const char* str)
 	ret = (wchar_t*)git__malloc(cb);
 
 	if (MultiByteToWideChar(_active_codepage, 0, str, -1, ret, cb) == 0) {
-		free(ret);
+		git__free(ret);
 		ret = NULL;
 	}
 
@@ -79,7 +79,7 @@ char* gitwin_from_utf16(const wchar_t* str)
 	ret = (char*)git__malloc(cb);
 
 	if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, cb, NULL, NULL) == 0) {
-		free(ret);
+		git__free(ret);
 		ret = NULL;
 	}
 
diff --git a/tests-clay/core/path.c b/tests-clay/core/path.c
index db8f33d..c394c72 100644
--- a/tests-clay/core/path.c
+++ b/tests-clay/core/path.c
@@ -11,7 +11,7 @@ check_dirname(const char *A, const char *B)
 	cl_assert((dir2 = git_path_dirname(A)) != NULL);
 	cl_assert(strcmp(dir2, B) == 0);
 
-	free(dir2);
+	git__free(dir2);
 }
 
 static void
@@ -24,7 +24,7 @@ check_basename(const char *A, const char *B)
 	cl_assert((base2 = git_path_basename(A)) != NULL);
 	cl_assert(strcmp(base2, B) == 0);
 
-	free(base2);
+	git__free(base2);
 }
 
 static void
diff --git a/tests-clay/core/vector.c b/tests-clay/core/vector.c
index 44e6d87..b8a853c 100644
--- a/tests-clay/core/vector.c
+++ b/tests-clay/core/vector.c
@@ -59,8 +59,8 @@ void test_core_vector__2(void)
 
 	git_vector_free(&x);
 
-	free(ptrs[0]);
-	free(ptrs[1]);
+	git__free(ptrs[0]);
+	git__free(ptrs[1]);
 }
 
 
diff --git a/tests-clay/object/raw/compare.c b/tests-clay/object/raw/compare.c
index 45e5331..94b1969 100644
--- a/tests-clay/object/raw/compare.c
+++ b/tests-clay/object/raw/compare.c
@@ -101,7 +101,7 @@ void test_object_raw_compare__compare_allocfmt_oids(void)
 	out = git_oid_allocfmt(&in);
 	cl_assert(out);
 	cl_assert(strcmp(exp, out) == 0);
-	free(out);
+	git__free(out);
 }
 
 void test_object_raw_compare__compare_pathfmt_oids(void)
diff --git a/tests-clay/object/raw/short.c b/tests-clay/object/raw/short.c
index 46e4fba..996f3f7 100644
--- a/tests-clay/object/raw/short.c
+++ b/tests-clay/object/raw/short.c
@@ -86,7 +86,7 @@ void test_object_raw_short__oid_shortener_stresstest_git_oid_shorten(void)
 
 	/* cleanup */
 	for (i = 0; i < MAX_OIDS; ++i)
-		free(oids[i]);
+		git__free(oids[i]);
 
 	git_oid_shorten_free(os);
 
diff --git a/tests/t00-core.c b/tests/t00-core.c
index 703504b..f93444d 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -99,8 +99,8 @@ BEGIN_TEST(vector2, "remove duplicates")
 	must_be_true(x.length == 2);
 	git_vector_free(&x);
 
-	free(ptrs[0]);
-	free(ptrs[1]);
+	git__free(ptrs[0]);
+	git__free(ptrs[1]);
 END_TEST
 
 
@@ -112,7 +112,7 @@ BEGIN_TEST(path0, "get the dirname of a path")
 	must_be_true(strcmp(dir, B) == 0);				\
 	must_be_true((dir2 = git_path_dirname(A)) != NULL);	\
 	must_be_true(strcmp(dir2, B) == 0);				\
-	free(dir2);										\
+	git__free(dir2);										\
 }
 
 	DIRNAME_TEST(NULL, ".");
@@ -141,7 +141,7 @@ BEGIN_TEST(path1, "get the base name of a path")
 	must_be_true(strcmp(base, B) == 0);					\
 	must_be_true((base2 = git_path_basename(A)) != NULL);	\
 	must_be_true(strcmp(base2, B) == 0);				\
-	free(base2);										\
+	git__free(base2);										\
 }
 
 	BASENAME_TEST(NULL, ".");
diff --git a/tests/t01-rawobj.c b/tests/t01-rawobj.c
index 2552085..8b05f33 100644
--- a/tests/t01-rawobj.c
+++ b/tests/t01-rawobj.c
@@ -217,7 +217,7 @@ BEGIN_TEST(oid12, "compare oids (allocate + format)")
 	out = git_oid_allocfmt(&in);
 	must_be_true(out);
 	must_be_true(strcmp(exp, out) == 0);
-	free(out);
+	git__free(out);
 END_TEST
 
 BEGIN_TEST(oid13, "compare oids (path format)")
@@ -390,7 +390,7 @@ BEGIN_TEST(oid17, "stress test for the git_oid_shorten object")
 
 	/* cleanup */
 	for (i = 0; i < MAX_OIDS; ++i)
-		free(oids[i]);
+		git__free(oids[i]);
 
 	git_oid_shorten_free(os);
 
diff --git a/tests/t04-commit.c b/tests/t04-commit.c
index 3fb4d37..303b8f7 100644
--- a/tests/t04-commit.c
+++ b/tests/t04-commit.c
@@ -162,7 +162,7 @@ BEGIN_TEST(parse1, "parse the signature line in a commit")
 	must_be_true(strcmp(_email, person.email) == 0);\
 	must_be_true(_time == person.when.time);\
 	must_be_true(_offset == person.when.offset);\
-	free(person.name); free(person.email);\
+	git__free(person.name); git__free(person.email);\
 }
 
 #define TEST_SIGNATURE_FAIL(_string, _header) { \
@@ -170,7 +170,7 @@ BEGIN_TEST(parse1, "parse the signature line in a commit")
 	size_t len = strlen(_string);\
 	git_signature person = {NULL, NULL, {0, 0}}; \
 	must_fail(git_signature__parse(&person, &ptr, ptr + len, _header, '\n'));\
-	free(person.name); free(person.email);\
+	git__free(person.name); git__free(person.email);\
 }
 
 	TEST_SIGNATURE_PASS(
@@ -759,7 +759,7 @@ BEGIN_TEST(root0, "create a root commit")
 	must_pass(git_reference_set_target(head, head_old));
 	must_pass(git_reference_delete(branch));
 	must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit));
-	free(head_old);
+	git__free(head_old);
 	git_commit_close(commit);
 	git_repository_free(repo);
 END_TEST
diff --git a/tests/t07-hashtable.c b/tests/t07-hashtable.c
index c0e8522..41d52af 100644
--- a/tests/t07-hashtable.c
+++ b/tests/t07-hashtable.c
@@ -103,7 +103,7 @@ BEGIN_TEST(table1, "fill the hashtable with random entries")
 	}
 
 	git_hashtable_free(table);
-	free(objects);
+	git__free(objects);
 
 END_TEST
 
@@ -145,7 +145,7 @@ BEGIN_TEST(table2, "make sure the table resizes automatically")
 	}
 
 	git_hashtable_free(table);
-	free(objects);
+	git__free(objects);
 
 END_TEST
 
@@ -179,7 +179,7 @@ BEGIN_TEST(tableit0, "iterate through all the contents of the table")
 		must_be_true(objects[i].visited);
 
 	git_hashtable_free(table);
-	free(objects);
+	git__free(objects);
 END_TEST
 
 
diff --git a/tests/test_helpers.c b/tests/test_helpers.c
index cb95607..d1d7c9e 100644
--- a/tests/test_helpers.c
+++ b/tests/test_helpers.c
@@ -116,7 +116,7 @@ int remove_loose_object(const char *repository_folder, git_object *object)
 		return -1;
 	}
 
-	free(full_path);
+	git__free(full_path);
 
 	return GIT_SUCCESS;
 }