]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/allocator.c
Remove support for pre-C89 libc versions that lack memcpy,
[icculus/xz.git] / src / liblzma / common / allocator.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       allocator.c
4 /// \brief      Allocating and freeing memory
5 //
6 //  Copyright (C) 2007 Lasse Collin
7 //
8 //  This library is free software; you can redistribute it and/or
9 //  modify it under the terms of the GNU Lesser General Public
10 //  License as published by the Free Software Foundation; either
11 //  version 2.1 of the License, or (at your option) any later version.
12 //
13 //  This library is distributed in the hope that it will be useful,
14 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 //  Lesser General Public License for more details.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "common.h"
21
22 #undef lzma_free
23
24 extern void * lzma_attribute((malloc))
25 lzma_alloc(size_t size, lzma_allocator *allocator)
26 {
27         // Some malloc() variants return NULL if called with size == 0.
28         if (size == 0)
29                 size = 1;
30
31         void *ptr;
32
33         if (allocator != NULL && allocator->alloc != NULL)
34                 ptr = allocator->alloc(allocator->opaque, 1, size);
35         else
36                 ptr = malloc(size);
37
38 #ifndef NDEBUG
39         // This helps to catch some stupid mistakes, but also hides them from
40         // Valgrind. Uncomment when useful.
41 //      if (ptr != NULL)
42 //              memset(ptr, 0xFD, size);
43 #endif
44
45         return ptr;
46 }
47
48
49 extern void
50 lzma_free(void *ptr, lzma_allocator *allocator)
51 {
52         if (allocator != NULL && allocator->free != NULL)
53                 allocator->free(allocator->opaque, ptr);
54         else
55                 free(ptr);
56
57         return;
58 }