]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/common/allocator.c
Imported to git.
[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 #if !defined(NDEBUG) && defined(HAVE_MEMSET)
39         // This helps to catch some stupid mistakes.
40         if (ptr != NULL)
41                 memset(ptr, 0xFD, size);
42 #endif
43
44         return ptr;
45 }
46
47
48 extern void
49 lzma_free(void *ptr, lzma_allocator *allocator)
50 {
51         if (allocator != NULL && allocator->free != NULL)
52                 allocator->free(allocator->opaque, ptr);
53         else
54                 free(ptr);
55
56         return;
57 }