]> mj.ucw.cz Git - libucw.git/blob - ucw/alloc-std.c
XTypes: CF_XTYPE requires '&' before xt_*, just like most other parameters in CF_...
[libucw.git] / ucw / alloc-std.c
1 /*
2  *      UCW Library -- Generic Allocator Using Malloc
3  *
4  *      (c) 2014 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/alloc.h>
12
13 #include <string.h>
14
15 /* Default allocator */
16
17 static void *ucw_std_alloc(struct ucw_allocator *a UNUSED, size_t size)
18 {
19   return xmalloc(size);
20 }
21
22 static void *ucw_std_realloc(struct ucw_allocator *a UNUSED, void *ptr, size_t old_size UNUSED, size_t new_size)
23 {
24   return xrealloc(ptr, new_size);
25 }
26
27 static void ucw_std_free(struct ucw_allocator *a UNUSED, void *ptr)
28 {
29   xfree(ptr);
30 }
31
32 struct ucw_allocator ucw_allocator_std = {
33   .alloc = ucw_std_alloc,
34   .realloc = ucw_std_realloc,
35   .free = ucw_std_free,
36 };
37
38 /* Zeroing allocator */
39
40 static void *ucw_zeroed_alloc(struct ucw_allocator *a UNUSED, size_t size)
41 {
42   return xmalloc_zero(size);
43 }
44
45 static void *ucw_zeroed_realloc(struct ucw_allocator *a UNUSED, void *ptr, size_t old_size, size_t new_size)
46 {
47   ptr = xrealloc(ptr, new_size);
48   if (old_size < new_size)
49     bzero((byte *) ptr + old_size, new_size - old_size);
50   return ptr;
51 }
52
53 struct ucw_allocator ucw_allocator_zeroed = {
54   .alloc = ucw_zeroed_alloc,
55   .realloc = ucw_zeroed_realloc,
56   .free = ucw_std_free,
57 };